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.Server; |
U |
23 |
import de.uhilger.tango.entity.Abspielliste; |
|
24 |
import de.uhilger.tango.entity.Entity; |
|
25 |
import de.uhilger.tango.entity.Titel; |
|
26 |
import de.uhilger.tango.store.FileStorage; |
8d7d35
|
27 |
import java.io.IOException; |
1bf4f6
|
28 |
import java.util.List; |
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 |
1bf4f6
|
37 |
* PUT /mz/api/alist/[pl-name]/[nrVon]/[nrNach] den Titel von seiner aktuellen Position an eine |
U |
38 |
* andere Position der Liste [pl-name] verschieben |
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 |
* - Liste ab Titel spielen |
|
44 |
* - Ganzes Album der Liste hinzufuegen |
8d7d35
|
45 |
* |
U |
46 |
* @author Ulrich Hilger |
|
47 |
* @version 1, 8.4.2021 |
|
48 |
*/ |
|
49 |
public class ListHandler extends AbstractHandler { |
e60cff
|
50 |
|
U |
51 |
private static final Logger logger = Logger.getLogger(ListHandler.class.getName()); |
095119
|
52 |
|
U |
53 |
public static final String ALLE_TITEL = "alle"; |
f70acb
|
54 |
|
U |
55 |
private String conf; |
|
56 |
|
|
57 |
public ListHandler(String conf) { |
|
58 |
this.conf = conf; |
|
59 |
} |
8d7d35
|
60 |
|
U |
61 |
@Override |
|
62 |
protected String get(HttpExchange e) { |
e60cff
|
63 |
String path = e.getRequestURI().toString(); |
0e9cd3
|
64 |
String[] elems = path.split(Server.SLASH); |
e60cff
|
65 |
String plname = elems[elems.length - 1]; |
f70acb
|
66 |
FileStorage fs = new FileStorage(conf); |
e60cff
|
67 |
String json = fs.readJson(FileStorage.ST_ABSPIELLISTE, plname); |
fcc734
|
68 |
return embedInCustomType(json, FileStorage.ST_ABSPIELLISTE); |
8d7d35
|
69 |
} |
U |
70 |
|
|
71 |
@Override |
|
72 |
protected String put(HttpExchange e) throws IOException { |
e60cff
|
73 |
String path = e.getRequestURI().toString(); |
0e9cd3
|
74 |
String[] elems = path.split(Server.SLASH); |
e60cff
|
75 |
String response = "ListHandler.put: ungueltiger URL"; |
U |
76 |
switch(elems.length) { |
8e2578
|
77 |
case 5: // ohne nr am Ende |
e60cff
|
78 |
response = addTitel(e, elems[4]); |
U |
79 |
break; |
|
80 |
|
|
81 |
case 6: |
1bf4f6
|
82 |
response = insertTitel(e, elems[4], Integer.parseInt(elems[5])); |
e60cff
|
83 |
break; |
1bf4f6
|
84 |
|
U |
85 |
case 7: |
|
86 |
response = moveTitel(e, elems[4], Integer.parseInt(elems[5]), Integer.parseInt(elems[6])); |
|
87 |
break; |
|
88 |
} |
|
89 |
return response; |
|
90 |
} |
|
91 |
|
|
92 |
/** |
|
93 |
* Den Titel im Body von seiner aktuellen Position an die angegebene |
|
94 |
* Position setzen. Der Titel an der angegebenen Position rueckt nach |
|
95 |
* unten. |
|
96 |
* |
|
97 |
* Annahme: Die Abspielliste enthaelt keine Titel mehrfach. |
|
98 |
* |
|
99 |
* @param e |
|
100 |
* @param plname |
|
101 |
* @param zielPos |
|
102 |
* @return |
|
103 |
* @throws IOException |
|
104 |
*/ |
|
105 |
private String moveTitel(HttpExchange e, String plname, int pos, int zielPos) throws IOException { |
|
106 |
FileStorage fs = new FileStorage(conf); |
|
107 |
Entity entity = fs.read(FileStorage.ST_ABSPIELLISTE, plname); |
|
108 |
String response = "Titel konnte nicht verschoben werden."; |
|
109 |
if(entity instanceof Abspielliste) { |
|
110 |
if(pos < zielPos) { |
|
111 |
--zielPos; |
|
112 |
} |
|
113 |
Abspielliste aliste = (Abspielliste) entity; |
|
114 |
List<Titel> liste = aliste.getTitel(); |
|
115 |
Titel t = liste.get(pos); |
|
116 |
liste.remove(pos); |
|
117 |
liste.add(zielPos, t); |
|
118 |
fs.write(aliste, true); |
|
119 |
response = "Titel " + t.getName() + " der Liste " + aliste.getName() + |
|
120 |
" an Position " + zielPos + " verschoben."; |
|
121 |
} |
|
122 |
return response; |
|
123 |
} |
|
124 |
|
|
125 |
/** |
|
126 |
* Den Titel im Body an Position anPos einfuegen. Der Titel an anPos |
|
127 |
* rueckt nach unten. |
|
128 |
* |
|
129 |
* @param e |
|
130 |
* @param plname |
|
131 |
* @param anPos |
|
132 |
* @return |
|
133 |
* @throws IOException |
|
134 |
*/ |
|
135 |
private String insertTitel(HttpExchange e, String plname, int anPos) throws IOException { |
|
136 |
FileStorage fs = new FileStorage(conf); |
|
137 |
Entity entity = fs.read(FileStorage.ST_ABSPIELLISTE, plname); |
|
138 |
String response = "Titel konnte nicht hinzugefuegt werden."; |
|
139 |
if(entity instanceof Abspielliste) { |
|
140 |
Abspielliste aliste = (Abspielliste) entity; |
|
141 |
String titelJson = bodyLesen(e); |
|
142 |
Gson gson = new Gson(); |
|
143 |
Object o = gson.fromJson(titelJson, fs.typeFromName(Titel.class.getSimpleName()).getType()); |
|
144 |
if(o instanceof Titel) { |
|
145 |
Titel titel = (Titel) o; |
|
146 |
aliste.putTitel(titel, anPos); |
|
147 |
fs.write(aliste, true); |
|
148 |
response = "Titel " + titel.getName() + " der Liste " + aliste.getName() + |
|
149 |
" an Position " + anPos + " hinzugefuegt."; |
|
150 |
} |
e60cff
|
151 |
} |
U |
152 |
return response; |
|
153 |
} |
|
154 |
|
|
155 |
private String addTitel(HttpExchange e, String plname) throws IOException { |
f70acb
|
156 |
FileStorage fs = new FileStorage(conf); |
e60cff
|
157 |
Entity entity = fs.read(FileStorage.ST_ABSPIELLISTE, plname); |
U |
158 |
String response = "Titel konnte nicht hinzugefuegt werden."; |
|
159 |
if(entity instanceof Abspielliste) { |
|
160 |
Abspielliste aliste = (Abspielliste) entity; |
|
161 |
String titelJson = bodyLesen(e); |
|
162 |
Gson gson = new Gson(); |
|
163 |
Object o = gson.fromJson(titelJson, fs.typeFromName(Titel.class.getSimpleName()).getType()); |
|
164 |
if(o instanceof Titel) { |
|
165 |
Titel titel = (Titel) o; |
|
166 |
aliste.addTitel(titel); |
|
167 |
fs.write(aliste, true); |
|
168 |
response = "Titel " + titel.getName() + " der Liste " + aliste.getName() + " hinzugefuegt."; |
|
169 |
} |
|
170 |
} |
|
171 |
return response; |
8d7d35
|
172 |
} |
U |
173 |
|
172013
|
174 |
// DELETE /mz/api/alist/[pl-name]/[nr] den Titel an der Position [nr] aus der Liste [pl-name] entfernen |
8d7d35
|
175 |
@Override |
U |
176 |
protected boolean delete(HttpExchange e) { |
172013
|
177 |
String path = e.getRequestURI().toString(); |
U |
178 |
String[] elems = path.split(Server.SLASH); |
5f7e0b
|
179 |
String listName = elems[elems.length - 2]; |
f70acb
|
180 |
FileStorage fs = new FileStorage(conf); |
172013
|
181 |
Entity entity = fs.read(Abspielliste.class.getSimpleName(), listName); |
U |
182 |
if(entity instanceof Abspielliste) { |
|
183 |
Abspielliste liste = (Abspielliste) entity; |
095119
|
184 |
String titelStr = elems[elems.length-1]; |
U |
185 |
if(titelStr.equalsIgnoreCase(ALLE_TITEL)) { |
|
186 |
liste.getTitel().clear(); |
|
187 |
} else { |
|
188 |
liste.getTitel().remove(Integer.parseInt(elems[elems.length-1])); |
|
189 |
} |
172013
|
190 |
fs.write(liste, true); |
U |
191 |
return true; |
|
192 |
} else { |
|
193 |
return false; |
|
194 |
} |
8d7d35
|
195 |
} |
U |
196 |
|
|
197 |
} |