Persoenliche Mediazentrale
ulrich
2021-05-06 f70acbb491c6421623cca57292a75f1820efad4d
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";
f70acb 48   
U 49   private String conf;
50   
51   public ListHandler(String conf) {
52     this.conf = conf;
53   }
8d7d35 54
U 55   @Override
56   protected String get(HttpExchange e) {
e60cff 57     String path = e.getRequestURI().toString();
0e9cd3 58     String[] elems = path.split(Server.SLASH);
e60cff 59     String plname = elems[elems.length - 1];
f70acb 60     FileStorage fs = new FileStorage(conf);
e60cff 61     String json = fs.readJson(FileStorage.ST_ABSPIELLISTE, plname);
fcc734 62     return embedInCustomType(json, FileStorage.ST_ABSPIELLISTE);
8d7d35 63   }
U 64
65   @Override
66   protected String put(HttpExchange e) throws IOException {
e60cff 67     String path = e.getRequestURI().toString();
0e9cd3 68     String[] elems = path.split(Server.SLASH);
e60cff 69     String response = "ListHandler.put: ungueltiger URL";
U 70     switch(elems.length) {
8e2578 71       case 5: // ohne nr am Ende
e60cff 72         response = addTitel(e, elems[4]);
U 73         break;
74         
75       case 6:
76         response = "Einfuegen noch nicht fertig.";
77         break;
78     }
79     return response;
80   }
81   
82   private String addTitel(HttpExchange e, String plname) throws IOException {
f70acb 83     FileStorage fs = new FileStorage(conf);
e60cff 84     Entity entity = fs.read(FileStorage.ST_ABSPIELLISTE, plname);
U 85     String response = "Titel konnte nicht hinzugefuegt werden.";
86     if(entity instanceof Abspielliste) {
87       Abspielliste aliste = (Abspielliste) entity;
88       String titelJson = bodyLesen(e);
89       Gson gson = new Gson();
90       Object o = gson.fromJson(titelJson, fs.typeFromName(Titel.class.getSimpleName()).getType());
91       if(o instanceof Titel) {
92         Titel titel = (Titel) o;
93         aliste.addTitel(titel);
94         fs.write(aliste, true);
95         response = "Titel " + titel.getName() + " der Liste " + aliste.getName() + " hinzugefuegt.";
96       }
97     }
98     return response;
8d7d35 99   }
U 100
101   @Override
102   protected String post(HttpExchange e) {
103     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
104   }
105
172013 106   // DELETE /mz/api/alist/[pl-name]/[nr]  den Titel an der Position [nr] aus der Liste [pl-name] entfernen  
8d7d35 107   @Override
U 108   protected boolean delete(HttpExchange e) {
172013 109     String path = e.getRequestURI().toString();
U 110     String[] elems = path.split(Server.SLASH);
5f7e0b 111     String listName = elems[elems.length - 2];
f70acb 112     FileStorage fs = new FileStorage(conf);
172013 113     Entity entity = fs.read(Abspielliste.class.getSimpleName(), listName);
U 114     if(entity instanceof Abspielliste) {
115       Abspielliste liste = (Abspielliste) entity;
095119 116       String titelStr = elems[elems.length-1];
U 117       if(titelStr.equalsIgnoreCase(ALLE_TITEL)) {
118         liste.getTitel().clear();
119       } else {
120         liste.getTitel().remove(Integer.parseInt(elems[elems.length-1]));
121       }
172013 122       fs.write(liste, true);
U 123       return true;
124     } else {
125       return false;
126     }
8d7d35 127   }
U 128   
129 }