Persoenliche Mediazentrale
ulrich
2021-04-24 94b1c2f60bde70d681b4bf8a5cd04b86e9ccf4ed
commit | author | age
8d7d35 1 /*
94b1c2 2   Tango - Personal Media Center
8d7d35 3   Copyright (C) 2021  Ulrich Hilger
U 4
5   This program is free software: you can redistribute it and/or modify
6   it under the terms of the GNU Affero General Public License as
7   published by the Free Software Foundation, either version 3 of the
8   License, or (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU Affero General Public License for more details.
14
15   You should have received a copy of the GNU Affero General Public License
16   along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  */
94b1c2 18 package de.uhilger.tango.api;
8d7d35 19
e60cff 20 import com.google.gson.Gson;
8d7d35 21 import com.sun.net.httpserver.HttpExchange;
94b1c2 22 import de.uhilger.tango.App;
U 23 import de.uhilger.tango.Server;
24 import de.uhilger.tango.entity.Abspielliste;
25 import de.uhilger.tango.entity.Entity;
26 import de.uhilger.tango.entity.Titel;
27 import de.uhilger.tango.store.FileStorage;
8d7d35 28 import java.io.IOException;
e60cff 29 import java.util.logging.Logger;
8d7d35 30
U 31 /**
32  * Der ListHandler bearbeitet HTTP-Anfragen zu Abspiellisten
33  * 
34  * GET /mz/api/alist/[pl-name]          die Titel-Objekte der Liste [pl-name] liefern
35  * PUT /mz/api/alist/[pl-name]          den Titel im Body anfuegen an die Liste [pl-name]
36  * PUT /mz/api/alist/[pl-name]/[nr]     an der Position nr der Liste [pl-name] den Titel im Body einfuegen
37  * DELETE /mz/api/alist/[pl-name]/[nr]  den Titel an der Position [nr] aus der Liste [pl-name] entfernen  
095119 38  * DELETE /mz/api/alist/[pl-name]/alle  alle Titel aus der Liste [pl-name] entfernen  
8d7d35 39  *
U 40  * @author Ulrich Hilger
41  * @version 1, 8.4.2021
42  */
43 public class ListHandler extends AbstractHandler {
e60cff 44   
U 45   private static final Logger logger = Logger.getLogger(ListHandler.class.getName());
095119 46   
U 47   public static final String ALLE_TITEL = "alle";
e60cff 48
8d7d35 49
U 50   @Override
51   protected String get(HttpExchange e) {
e60cff 52     String path = e.getRequestURI().toString();
0e9cd3 53     String[] elems = path.split(Server.SLASH);
e60cff 54     String plname = elems[elems.length - 1];
U 55     FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF)));
56     String json = fs.readJson(FileStorage.ST_ABSPIELLISTE, plname);
fcc734 57     return embedInCustomType(json, FileStorage.ST_ABSPIELLISTE);
8d7d35 58   }
U 59
60   @Override
61   protected String put(HttpExchange e) throws IOException {
e60cff 62     String path = e.getRequestURI().toString();
0e9cd3 63     String[] elems = path.split(Server.SLASH);
e60cff 64     String response = "ListHandler.put: ungueltiger URL";
U 65     switch(elems.length) {
8e2578 66       case 5: // ohne nr am Ende
e60cff 67         response = addTitel(e, elems[4]);
U 68         break;
69         
70       case 6:
71         response = "Einfuegen noch nicht fertig.";
72         break;
73     }
74     return response;
75   }
76   
77   private String addTitel(HttpExchange e, String plname) throws IOException {
78     FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF)));
79     Entity entity = fs.read(FileStorage.ST_ABSPIELLISTE, plname);
80     String response = "Titel konnte nicht hinzugefuegt werden.";
81     if(entity instanceof Abspielliste) {
82       Abspielliste aliste = (Abspielliste) entity;
83       String titelJson = bodyLesen(e);
84       Gson gson = new Gson();
85       Object o = gson.fromJson(titelJson, fs.typeFromName(Titel.class.getSimpleName()).getType());
86       if(o instanceof Titel) {
87         Titel titel = (Titel) o;
88         aliste.addTitel(titel);
89         fs.write(aliste, true);
90         response = "Titel " + titel.getName() + " der Liste " + aliste.getName() + " hinzugefuegt.";
91       }
92     }
93     return response;
8d7d35 94   }
U 95
96   @Override
97   protected String post(HttpExchange e) {
98     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
99   }
100
172013 101   // DELETE /mz/api/alist/[pl-name]/[nr]  den Titel an der Position [nr] aus der Liste [pl-name] entfernen  
8d7d35 102   @Override
U 103   protected boolean delete(HttpExchange e) {
172013 104     String path = e.getRequestURI().toString();
U 105     String[] elems = path.split(Server.SLASH);
5f7e0b 106     String listName = elems[elems.length - 2];
172013 107     FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF)));
U 108     Entity entity = fs.read(Abspielliste.class.getSimpleName(), listName);
109     if(entity instanceof Abspielliste) {
110       Abspielliste liste = (Abspielliste) entity;
095119 111       String titelStr = elems[elems.length-1];
U 112       if(titelStr.equalsIgnoreCase(ALLE_TITEL)) {
113         liste.getTitel().clear();
114       } else {
115         liste.getTitel().remove(Integer.parseInt(elems[elems.length-1]));
116       }
172013 117       fs.write(liste, true);
U 118       return true;
119     } else {
120       return false;
121     }
8d7d35 122   }
U 123   
124 }