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