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