Persoenliche Mediazentrale
ulrich
2021-04-10 a1b6691e14dc551ec6ff9007f5d4f8f5f8a90b43
commit | author | age
8d7d35 1 /*
U 2   Mediazentrale - Personal Media Center
3   Copyright (C) 2021  Ulrich Hilger
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  */
18 package de.uhilger.mediaz.api;
19
e60cff 20 import com.google.gson.Gson;
8d7d35 21 import com.sun.net.httpserver.HttpExchange;
e60cff 22 import de.uhilger.mediaz.App;
U 23 import de.uhilger.mediaz.Server;
24 import de.uhilger.mediaz.entity.Abspielliste;
25 import de.uhilger.mediaz.entity.Entity;
26 import de.uhilger.mediaz.entity.Titel;
27 import de.uhilger.mediaz.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  
38  *
39  * @author Ulrich Hilger
40  * @version 1, 8.4.2021
41  */
42 public class ListHandler extends AbstractHandler {
e60cff 43   
U 44   private static final Logger logger = Logger.getLogger(ListHandler.class.getName());
45
8d7d35 46
U 47   @Override
48   protected String get(HttpExchange e) {
e60cff 49     String path = e.getRequestURI().toString();
0e9cd3 50     String[] elems = path.split(Server.SLASH);
e60cff 51     String plname = elems[elems.length - 1];
U 52     FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF)));
53     String json = fs.readJson(FileStorage.ST_ABSPIELLISTE, plname);
fcc734 54     return embedInCustomType(json, FileStorage.ST_ABSPIELLISTE);
8d7d35 55   }
U 56
57   @Override
58   protected String put(HttpExchange e) throws IOException {
e60cff 59     String path = e.getRequestURI().toString();
0e9cd3 60     String[] elems = path.split(Server.SLASH);
e60cff 61     String response = "ListHandler.put: ungueltiger URL";
U 62     switch(elems.length) {
8e2578 63       case 5: // ohne nr am Ende
e60cff 64         response = addTitel(e, elems[4]);
U 65         break;
66         
67       case 6:
68         response = "Einfuegen noch nicht fertig.";
69         break;
70     }
71     return response;
72   }
73   
74   private String addTitel(HttpExchange e, String plname) throws IOException {
75     FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF)));
76     Entity entity = fs.read(FileStorage.ST_ABSPIELLISTE, plname);
77     String response = "Titel konnte nicht hinzugefuegt werden.";
78     if(entity instanceof Abspielliste) {
79       Abspielliste aliste = (Abspielliste) entity;
80       String titelJson = bodyLesen(e);
81       Gson gson = new Gson();
82       Object o = gson.fromJson(titelJson, fs.typeFromName(Titel.class.getSimpleName()).getType());
83       if(o instanceof Titel) {
84         Titel titel = (Titel) o;
85         aliste.addTitel(titel);
86         fs.write(aliste, true);
87         response = "Titel " + titel.getName() + " der Liste " + aliste.getName() + " hinzugefuegt.";
88       }
89     }
90     return response;
8d7d35 91   }
U 92
93   @Override
94   protected String post(HttpExchange e) {
95     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
96   }
97
172013 98   // DELETE /mz/api/alist/[pl-name]/[nr]  den Titel an der Position [nr] aus der Liste [pl-name] entfernen  
8d7d35 99   @Override
U 100   protected boolean delete(HttpExchange e) {
172013 101     String path = e.getRequestURI().toString();
U 102     String[] elems = path.split(Server.SLASH);
5f7e0b 103     String listName = elems[elems.length - 2];
172013 104     
U 105     FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF)));
106     Entity entity = fs.read(Abspielliste.class.getSimpleName(), listName);
107     if(entity instanceof Abspielliste) {
108       Abspielliste liste = (Abspielliste) entity;
109       liste.getTitel().remove(Integer.parseInt(elems[elems.length-1]));
110       fs.write(liste, true);
111       return true;
112     } else {
113       return false;
114     }
8d7d35 115   }
U 116   
117 }