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(); |
U |
50 |
String[] elems = path.split(App.getRs(Server.RB_SLASH)); |
|
51 |
String plname = elems[elems.length - 1]; |
|
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(); |
U |
60 |
String[] elems = path.split(App.getRs(Server.RB_SLASH)); |
|
61 |
String response = "ListHandler.put: ungueltiger URL"; |
|
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 |
|
|
98 |
@Override |
|
99 |
protected boolean delete(HttpExchange e) { |
|
100 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
|
101 |
} |
|
102 |
|
|
103 |
} |