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; |
b379f5
|
24 |
import de.uhilger.mediaz.Server; |
081606
|
25 |
import de.uhilger.mediaz.store.FileStorage; |
b1bf96
|
26 |
import de.uhilger.mediaz.entity.Entity; |
d769f3
|
27 |
import de.uhilger.mediaz.entity.Geraet; |
d027b5
|
28 |
import static de.uhilger.mediaz.store.FileStorage.ST_ABLAGEORT; |
d769f3
|
29 |
import static de.uhilger.mediaz.store.FileStorage.ST_GERAET; |
U |
30 |
import java.io.BufferedReader; |
b379f5
|
31 |
import java.io.IOException; |
d769f3
|
32 |
import java.io.InputStream; |
U |
33 |
import java.io.InputStreamReader; |
|
34 |
import java.net.Authenticator; |
|
35 |
import java.net.HttpURLConnection; |
|
36 |
import java.net.InetSocketAddress; |
|
37 |
import java.net.ProxySelector; |
|
38 |
import java.net.URI; |
|
39 |
import java.net.URL; |
|
40 |
import java.util.Iterator; |
2b5c60
|
41 |
import java.util.List; |
081606
|
42 |
import java.util.logging.Level; |
b379f5
|
43 |
import java.util.logging.Logger; |
d769f3
|
44 |
|
U |
45 |
import java.net.http.HttpClient; |
|
46 |
import java.net.http.HttpClient.Version; |
|
47 |
import java.net.http.HttpClient.Redirect; |
|
48 |
import java.net.http.HttpClient.Builder; |
|
49 |
import java.net.http.HttpRequest; |
|
50 |
import java.net.http.HttpResponse; |
|
51 |
import java.net.http.HttpResponse.BodyHandlers; |
|
52 |
import java.time.Duration; |
|
53 |
import java.util.ArrayList; |
|
54 |
|
b379f5
|
55 |
|
U |
56 |
/** |
2597cd
|
57 |
* HttpHandler fuer die Verwaltung von Entitaeten der Mediazentrale |
5f70da
|
58 |
* |
U |
59 |
* @author Ulrich Hilger |
|
60 |
* @version 1, 5.4.2021 |
b379f5
|
61 |
*/ |
8d7d35
|
62 |
public class StorageHandler extends AbstractHandler { |
b379f5
|
63 |
|
081606
|
64 |
private static final Logger logger = Logger.getLogger(StorageHandler.class.getName()); |
b379f5
|
65 |
|
8d7d35
|
66 |
@Override |
U |
67 |
protected String put(HttpExchange e) throws IOException { |
dfb7d3
|
68 |
String path = e.getRequestURI().toString(); |
0e9cd3
|
69 |
String[] elems = path.split(Server.SLASH); |
dfb7d3
|
70 |
String type = elems[elems.length - 2]; |
U |
71 |
String elemName = elems[elems.length - 1]; // alter Name, wenn Aenderung |
|
72 |
if(!elemName.equalsIgnoreCase(App.getRs(RB_EP_LISTE))) { |
|
73 |
FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); |
|
74 |
Gson gson = new Gson(); |
|
75 |
logger.log(Level.FINE, "type: {0}, token: {1}", new Object[]{type, fs.typeFromName(type).getType().getTypeName()}); |
|
76 |
Object o = gson.fromJson(bodyLesen(e), fs.typeFromName(type).getType()); |
|
77 |
if(o instanceof Entity) { |
|
78 |
Entity entity = (Entity) o; |
|
79 |
if(fs.exists(type, elemName)) { |
|
80 |
fs.delete(type, elemName); |
|
81 |
fs.write(entity, true); |
|
82 |
} else { |
|
83 |
fs.write(entity, false); |
|
84 |
} |
0e9cd3
|
85 |
return type + Server.SLASH + entity.getName(); |
dfb7d3
|
86 |
} else { |
U |
87 |
return "Ungueltiges Objekt im Body."; |
|
88 |
} |
|
89 |
} else { |
|
90 |
return "Ungueltiger Elementname: " + App.getRs(RB_EP_LISTE); |
|
91 |
} |
|
92 |
} |
8d7d35
|
93 |
|
2b5c60
|
94 |
private boolean loeschen(HttpExchange e) { |
5f70da
|
95 |
String path = e.getRequestURI().toString(); |
0e9cd3
|
96 |
String[] elems = path.split(Server.SLASH); |
5f70da
|
97 |
String type = elems[elems.length - 2]; |
U |
98 |
String elemName = elems[elems.length - 1]; |
|
99 |
FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); |
2b5c60
|
100 |
return fs.delete(type, elemName); |
U |
101 |
} |
|
102 |
|
d769f3
|
103 |
private String lesen(HttpExchange e) throws IOException, InterruptedException { |
2b5c60
|
104 |
String path = e.getRequestURI().toString(); |
0e9cd3
|
105 |
String[] elems = path.split(Server.SLASH); |
2b5c60
|
106 |
FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); |
0e9cd3
|
107 |
if(path.endsWith(Server.SLASH)) { |
f45e20
|
108 |
List list = null; |
dfb7d3
|
109 |
if(elems[elems.length - 1].equalsIgnoreCase(App.getRs(RB_EP_LISTE))) { |
f45e20
|
110 |
String type = elems[elems.length - 2]; |
U |
111 |
logger.fine(type); |
d769f3
|
112 |
if(type.equalsIgnoreCase(ST_GERAET)) { |
U |
113 |
list = fs.listObjects(type); |
|
114 |
List<Geraet> newList = new ArrayList(); |
|
115 |
Iterator<Entity> i = list.iterator(); |
|
116 |
while(i.hasNext()) { |
|
117 |
Entity entity = i.next(); |
|
118 |
if(entity instanceof Geraet) { |
|
119 |
Geraet g = (Geraet) entity; |
|
120 |
String statusurl = g.getStatusUrl(); |
|
121 |
logger.info(statusurl); |
|
122 |
|
|
123 |
HttpRequest request = HttpRequest.newBuilder() |
|
124 |
.uri(URI.create(statusurl)) |
|
125 |
.build(); |
|
126 |
HttpClient client = HttpClient.newBuilder() |
|
127 |
.version(Version.HTTP_1_1) |
|
128 |
.followRedirects(Redirect.NORMAL) |
|
129 |
.connectTimeout(Duration.ofSeconds(20)) |
|
130 |
//.proxy(ProxySelector.of(new InetSocketAddress("proxy.example.com", 80))) |
|
131 |
//.authenticator(Authenticator.getDefault()) |
|
132 |
.build(); |
|
133 |
HttpResponse<String> response = client.send(request, BodyHandlers.ofString()); |
|
134 |
logger.finer(Integer.toString(response.statusCode())); |
|
135 |
logger.finer(response.body()); |
|
136 |
// {"ison":false,"has_timer":false,"overpower":false} |
|
137 |
String[] parts = response.body().split(",")[0].split(":"); |
|
138 |
logger.finer("ison: " + parts[1]); |
|
139 |
g.setStatus(parts[1]); |
|
140 |
newList.add(g); |
|
141 |
} |
|
142 |
} |
29be41
|
143 |
//list = newList; |
U |
144 |
Gson gson = new Gson(); |
|
145 |
Object o = gson.fromJson(bodyLesen(e), fs.typeFromName(type).getType()); |
|
146 |
return gson.toJson(newList); |
d769f3
|
147 |
} else { |
U |
148 |
list = fs.list(type); |
|
149 |
} |
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 |
} |
U |
185 |
} |