commit | author | age
|
b379f5
|
1 |
/* |
94b1c2
|
2 |
Tango - Personal Media Center |
5f70da
|
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/>. |
b379f5
|
17 |
*/ |
94b1c2
|
18 |
package de.uhilger.tango.api; |
b379f5
|
19 |
|
U |
20 |
import com.google.gson.Gson; |
|
21 |
import com.sun.net.httpserver.HttpExchange; |
94b1c2
|
22 |
import de.uhilger.tango.App; |
U |
23 |
import static de.uhilger.tango.App.RB_EP_LISTE; |
|
24 |
import static de.uhilger.tango.App.RB_EP_LISTE_ALLES; |
|
25 |
import de.uhilger.tango.Server; |
|
26 |
import de.uhilger.tango.entity.Ablageort; |
|
27 |
import de.uhilger.tango.store.FileStorage; |
|
28 |
import de.uhilger.tango.entity.Entity; |
|
29 |
import de.uhilger.tango.entity.Geraet; |
|
30 |
import static de.uhilger.tango.store.FileStorage.ST_ABLAGEORT; |
|
31 |
import static de.uhilger.tango.store.FileStorage.ST_GERAET; |
|
32 |
import de.uhilger.tango.store.Storage; |
b379f5
|
33 |
import java.io.IOException; |
d769f3
|
34 |
import java.net.URI; |
U |
35 |
import java.util.Iterator; |
2b5c60
|
36 |
import java.util.List; |
081606
|
37 |
import java.util.logging.Level; |
b379f5
|
38 |
import java.util.logging.Logger; |
d769f3
|
39 |
|
U |
40 |
import java.net.http.HttpClient; |
|
41 |
import java.net.http.HttpClient.Version; |
|
42 |
import java.net.http.HttpClient.Redirect; |
|
43 |
import java.net.http.HttpRequest; |
|
44 |
import java.net.http.HttpResponse; |
|
45 |
import java.net.http.HttpResponse.BodyHandlers; |
|
46 |
import java.time.Duration; |
|
47 |
import java.util.ArrayList; |
|
48 |
|
b379f5
|
49 |
|
U |
50 |
/** |
2597cd
|
51 |
* HttpHandler fuer die Verwaltung von Entitaeten der Mediazentrale |
a29f5c
|
52 |
* |
fe0cf7
|
53 |
* GET /mz/api/store/[typname]/[name] |
U |
54 |
* GET /mz/api/store/[typname]/liste |
|
55 |
* GET /mz/api/store/[typname]/listealles (nur Typ Geraet) |
5f70da
|
56 |
* |
U |
57 |
* @author Ulrich Hilger |
|
58 |
* @version 1, 5.4.2021 |
b379f5
|
59 |
*/ |
8d7d35
|
60 |
public class StorageHandler extends AbstractHandler { |
b379f5
|
61 |
|
081606
|
62 |
private static final Logger logger = Logger.getLogger(StorageHandler.class.getName()); |
b379f5
|
63 |
|
8d7d35
|
64 |
@Override |
U |
65 |
protected String put(HttpExchange e) throws IOException { |
dfb7d3
|
66 |
String path = e.getRequestURI().toString(); |
0e9cd3
|
67 |
String[] elems = path.split(Server.SLASH); |
dfb7d3
|
68 |
String type = elems[elems.length - 2]; |
U |
69 |
String elemName = elems[elems.length - 1]; // alter Name, wenn Aenderung |
|
70 |
if(!elemName.equalsIgnoreCase(App.getRs(RB_EP_LISTE))) { |
|
71 |
FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); |
|
72 |
Gson gson = new Gson(); |
|
73 |
logger.log(Level.FINE, "type: {0}, token: {1}", new Object[]{type, fs.typeFromName(type).getType().getTypeName()}); |
|
74 |
Object o = gson.fromJson(bodyLesen(e), fs.typeFromName(type).getType()); |
|
75 |
if(o instanceof Entity) { |
|
76 |
Entity entity = (Entity) o; |
0c14c0
|
77 |
if(fs.exists(type, elemName)) { // Aenderung |
dfb7d3
|
78 |
fs.delete(type, elemName); |
0c14c0
|
79 |
// wenn Ablageort, hier noch alten Kontext entfernen |
U |
80 |
if(type.equalsIgnoreCase(FileStorage.ST_ABLAGEORT)) { |
|
81 |
Entity aoe = fs.read(type, elemName); |
|
82 |
if(aoe instanceof Ablageort) { |
|
83 |
Ablageort ablageort = (Ablageort) aoe; |
|
84 |
App.getServer().ablageortEntfernen(ablageort.getUrl()); |
|
85 |
} |
|
86 |
} |
dfb7d3
|
87 |
fs.write(entity, true); |
0c14c0
|
88 |
// wenn Ablageort, hier noch neuen Kontext anlegen |
U |
89 |
if(type.equalsIgnoreCase(FileStorage.ST_ABLAGEORT)) { |
|
90 |
if(entity instanceof Ablageort) { |
|
91 |
Ablageort ablageort = (Ablageort) entity; |
|
92 |
App.getServer().ablageortHinzufuegen(ablageort); |
|
93 |
} |
|
94 |
} |
|
95 |
} else { // Neu |
dfb7d3
|
96 |
fs.write(entity, false); |
0c14c0
|
97 |
// wenn Ablageort, hier noch neuen Kontext anlegen |
U |
98 |
if(type.equalsIgnoreCase(FileStorage.ST_ABLAGEORT)) { |
|
99 |
if(entity instanceof Ablageort) { |
|
100 |
Ablageort ablageort = (Ablageort) entity; |
|
101 |
App.getServer().ablageortHinzufuegen(ablageort); |
|
102 |
} |
|
103 |
} |
dfb7d3
|
104 |
} |
0e9cd3
|
105 |
return type + Server.SLASH + entity.getName(); |
dfb7d3
|
106 |
} else { |
U |
107 |
return "Ungueltiges Objekt im Body."; |
|
108 |
} |
|
109 |
} else { |
|
110 |
return "Ungueltiger Elementname: " + App.getRs(RB_EP_LISTE); |
|
111 |
} |
|
112 |
} |
8d7d35
|
113 |
|
2b5c60
|
114 |
private boolean loeschen(HttpExchange e) { |
5f70da
|
115 |
String path = e.getRequestURI().toString(); |
0e9cd3
|
116 |
String[] elems = path.split(Server.SLASH); |
5f70da
|
117 |
String type = elems[elems.length - 2]; |
U |
118 |
String elemName = elems[elems.length - 1]; |
|
119 |
FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); |
0c14c0
|
120 |
if(type.equalsIgnoreCase(FileStorage.ST_ABLAGEORT)) { |
U |
121 |
// im laufenden Server den Context entfernen |
|
122 |
Entity entity = fs.read(type, elemName); |
|
123 |
if(entity instanceof Ablageort) { |
|
124 |
Ablageort ablageort = (Ablageort) entity; |
|
125 |
App.getServer().ablageortEntfernen(ablageort.getUrl()); |
|
126 |
} |
|
127 |
} |
2b5c60
|
128 |
return fs.delete(type, elemName); |
U |
129 |
} |
|
130 |
|
d769f3
|
131 |
private String lesen(HttpExchange e) throws IOException, InterruptedException { |
2b5c60
|
132 |
String path = e.getRequestURI().toString(); |
0e9cd3
|
133 |
String[] elems = path.split(Server.SLASH); |
2b5c60
|
134 |
FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); |
0e9cd3
|
135 |
if(path.endsWith(Server.SLASH)) { |
f45e20
|
136 |
List list = null; |
a29f5c
|
137 |
if(elems[elems.length - 1].equalsIgnoreCase(App.getRs(RB_EP_LISTE_ALLES))) { |
f45e20
|
138 |
String type = elems[elems.length - 2]; |
U |
139 |
logger.fine(type); |
d769f3
|
140 |
if(type.equalsIgnoreCase(ST_GERAET)) { |
757ace
|
141 |
list = collectDeviceStatus(fs, type); |
29be41
|
142 |
Gson gson = new Gson(); |
U |
143 |
Object o = gson.fromJson(bodyLesen(e), fs.typeFromName(type).getType()); |
757ace
|
144 |
return gson.toJson(list); |
d769f3
|
145 |
} |
a29f5c
|
146 |
} else if(elems[elems.length - 1].equalsIgnoreCase(App.getRs(RB_EP_LISTE))) { |
U |
147 |
String type = elems[elems.length - 2]; |
|
148 |
logger.fine(type); |
|
149 |
list = fs.list(type); |
d027b5
|
150 |
if(type.equalsIgnoreCase(ST_ABLAGEORT)) { |
U |
151 |
list.add("Livestreams"); |
|
152 |
} |
f45e20
|
153 |
} else { |
U |
154 |
String type = elems[elems.length - 1]; |
|
155 |
logger.fine(type); |
|
156 |
list = fs.listObjects(type); |
|
157 |
} |
a43e1a
|
158 |
return jsonWithEnclosingType(list); |
2b5c60
|
159 |
} else { |
U |
160 |
String type = elems[elems.length - 2]; |
|
161 |
String elemName = elems[elems.length - 1]; |
|
162 |
return fs.readJson(type, elemName); |
|
163 |
} |
a43e1a
|
164 |
} |
b379f5
|
165 |
|
8d7d35
|
166 |
@Override |
U |
167 |
public String get(HttpExchange e) { |
d769f3
|
168 |
try { |
U |
169 |
return lesen(e); |
|
170 |
} catch (IOException | InterruptedException ex) { |
|
171 |
Logger.getLogger(StorageHandler.class.getName()).log(Level.SEVERE, null, ex); |
|
172 |
return ex.getLocalizedMessage(); |
|
173 |
} |
8d7d35
|
174 |
} |
U |
175 |
|
|
176 |
@Override |
|
177 |
public String post(HttpExchange e) { |
|
178 |
return "nicht unterstuetzt"; |
|
179 |
} |
|
180 |
|
|
181 |
@Override |
|
182 |
public boolean delete(HttpExchange e) { |
|
183 |
return loeschen(e); |
b379f5
|
184 |
} |
757ace
|
185 |
|
U |
186 |
private List collectDeviceStatus(Storage fs, String type) throws IOException, InterruptedException { |
|
187 |
List list = fs.listObjects(type); |
|
188 |
List<Geraet> newList = new ArrayList(); |
|
189 |
Iterator<Entity> i = list.iterator(); |
|
190 |
while (i.hasNext()) { |
|
191 |
Entity entity = i.next(); |
|
192 |
if (entity instanceof Geraet) { |
|
193 |
Geraet g = (Geraet) entity; |
|
194 |
String statusurl = g.getStatusUrl(); |
|
195 |
logger.info(statusurl); |
|
196 |
|
|
197 |
HttpRequest request = HttpRequest.newBuilder() |
|
198 |
.uri(URI.create(statusurl)) |
|
199 |
.build(); |
|
200 |
HttpClient client = HttpClient.newBuilder() |
|
201 |
.version(Version.HTTP_1_1) |
|
202 |
.followRedirects(Redirect.NORMAL) |
|
203 |
.connectTimeout(Duration.ofSeconds(20)) |
|
204 |
//.proxy(ProxySelector.of(new InetSocketAddress("proxy.example.com", 80))) |
|
205 |
//.authenticator(Authenticator.getDefault()) |
|
206 |
.build(); |
|
207 |
HttpResponse<String> response = client.send(request, BodyHandlers.ofString()); |
|
208 |
logger.finer(Integer.toString(response.statusCode())); |
|
209 |
logger.finer(response.body()); |
|
210 |
// {"ison":false,"has_timer":false,"overpower":false} |
|
211 |
String[] parts = response.body().split(",")[0].split(":"); |
|
212 |
logger.finer("ison: " + parts[1]); |
5c6214
|
213 |
g.setStatus(Boolean.parseBoolean(parts[1])); |
757ace
|
214 |
newList.add(g); |
U |
215 |
} |
|
216 |
} |
|
217 |
return newList; |
|
218 |
} |
b379f5
|
219 |
} |