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; |
2b5c60
|
25 |
import static de.uhilger.mediaz.Server.RB_SLASH; |
081606
|
26 |
import de.uhilger.mediaz.store.FileStorage; |
b1bf96
|
27 |
import de.uhilger.mediaz.entity.Entity; |
b379f5
|
28 |
import java.io.IOException; |
2b5c60
|
29 |
import java.util.List; |
081606
|
30 |
import java.util.logging.Level; |
b379f5
|
31 |
import java.util.logging.Logger; |
U |
32 |
|
|
33 |
/** |
2597cd
|
34 |
* HttpHandler fuer die Verwaltung von Entitaeten der Mediazentrale |
5f70da
|
35 |
* |
U |
36 |
* @author Ulrich Hilger |
|
37 |
* @version 1, 5.4.2021 |
b379f5
|
38 |
*/ |
8d7d35
|
39 |
public class StorageHandler extends AbstractHandler { |
b379f5
|
40 |
|
081606
|
41 |
private static final Logger logger = Logger.getLogger(StorageHandler.class.getName()); |
b379f5
|
42 |
|
U |
43 |
|
0a58e2
|
44 |
/* |
U |
45 |
Das REST-Muster fuer die Mediazentrale (Beispiel Ablageort): |
2b5c60
|
46 |
|
0a58e2
|
47 |
1. HTTP GET Ablageort/[Name] - Liefere den Ablageort namens [Name] |
dfb7d3
|
48 |
2. HTTP GET Ablageort/liste/ - Liefere eine Liste mit Namen aller Ablageorte |
0a58e2
|
49 |
Es duerfen keine Elemente namens "liste" angelegt werden |
dfb7d3
|
50 |
3. HTTP GET Ablageort/ - Liefere eine Liste aller Ablageort-Objekte als JSON |
0a58e2
|
51 |
4. HTTP PUT Ablageort/[Name] - Der Ablageort wird angelegt. Wenn er bereits existiert, |
U |
52 |
wird er geändert. (im Falle einer Aenderung des Namens |
|
53 |
muss der alte Name im URL stehen und im Body die Aenderung) |
dfb7d3
|
54 |
5. HTTP DELETE - loesche den Ablageort |
b379f5
|
55 |
|
U |
56 |
Beispiele: |
2b5c60
|
57 |
|
0a58e2
|
58 |
HTTP GET /mz/api/store/Ablageort/ |
f45e20
|
59 |
liefert alle Ablageort-Objekte |
U |
60 |
|
0a58e2
|
61 |
HTTP GET /mz/api/store/Ablageort/liste/ |
2b5c60
|
62 |
liefert eine Liste der Namen vorhandener Ablageorte |
b379f5
|
63 |
|
0a58e2
|
64 |
HTTP GET /mz/api/store/Ablageort/[Name] |
dfb7d3
|
65 |
liest den Ablageort namens [Name] |
b379f5
|
66 |
|
0a58e2
|
67 |
HTTP PUT /mz/api/store/Ablageort/[Name] |
dfb7d3
|
68 |
wenn [Name] existiert: Aenderung, sonst neu anlegen |
b379f5
|
69 |
|
0a58e2
|
70 |
HTTP DELETE /mz/api/store/Ablageort/[Name] |
dfb7d3
|
71 |
löscht den Ablageort namens [Name] |
b379f5
|
72 |
|
U |
73 |
*/ |
|
74 |
|
2b5c60
|
75 |
/** Name der HTTP Methode GET */ |
8d7d35
|
76 |
//public static final String HTTP_GET = "GET"; |
2b5c60
|
77 |
|
U |
78 |
/** Name der HTTP Methode PUT */ |
8d7d35
|
79 |
//public static final String HTTP_PUT = "PUT"; |
2b5c60
|
80 |
|
U |
81 |
/** Name der HTTP Methode POST */ |
8d7d35
|
82 |
//public static final String HTTP_POST = "POST"; |
2b5c60
|
83 |
|
U |
84 |
/** Name der HTTP Methode DELETE */ |
8d7d35
|
85 |
//public static final String HTTP_DELETE = "DELETE"; |
f45e20
|
86 |
|
8d7d35
|
87 |
//public static final String EP_LISTE = "liste/"; |
b379f5
|
88 |
|
8d7d35
|
89 |
/* |
b379f5
|
90 |
@Override |
U |
91 |
public void handle(HttpExchange e) throws IOException { |
|
92 |
String method = e.getRequestMethod(); |
5f70da
|
93 |
String response = ""; |
U |
94 |
int code = 200; |
b379f5
|
95 |
switch(method) { |
U |
96 |
case HTTP_GET: |
5f70da
|
97 |
String json = lesen(e); |
U |
98 |
if(json != null) { |
|
99 |
response = json; |
|
100 |
} else { |
|
101 |
response = "nicht gefunden"; |
|
102 |
code = 404; |
|
103 |
} |
b379f5
|
104 |
break; |
U |
105 |
|
|
106 |
case HTTP_PUT: |
dfb7d3
|
107 |
response = put(e); |
b379f5
|
108 |
break; |
U |
109 |
|
|
110 |
case HTTP_POST: |
dfb7d3
|
111 |
response = "nicht unterstuertzt."; |
U |
112 |
code = 404; |
b379f5
|
113 |
break; |
U |
114 |
|
|
115 |
case HTTP_DELETE: |
2b5c60
|
116 |
boolean geloescht = loeschen(e); |
U |
117 |
if(geloescht) { |
|
118 |
response = "geloescht"; |
|
119 |
} else { |
|
120 |
response = "nicht geloescht"; |
|
121 |
} |
b379f5
|
122 |
break; |
U |
123 |
} |
b29119
|
124 |
logger.fine(response); |
5f70da
|
125 |
e.sendResponseHeaders(code, response.length()); |
b379f5
|
126 |
OutputStream os = e.getResponseBody(); |
U |
127 |
os.write(response.getBytes()); |
|
128 |
os.close(); |
b1bf96
|
129 |
} |
8d7d35
|
130 |
*/ |
b1bf96
|
131 |
|
8d7d35
|
132 |
@Override |
U |
133 |
protected String put(HttpExchange e) throws IOException { |
dfb7d3
|
134 |
String path = e.getRequestURI().toString(); |
U |
135 |
String[] elems = path.split(App.getRs(Server.RB_SLASH)); |
|
136 |
String type = elems[elems.length - 2]; |
|
137 |
String elemName = elems[elems.length - 1]; // alter Name, wenn Aenderung |
|
138 |
if(!elemName.equalsIgnoreCase(App.getRs(RB_EP_LISTE))) { |
|
139 |
FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); |
|
140 |
Gson gson = new Gson(); |
|
141 |
logger.log(Level.FINE, "type: {0}, token: {1}", new Object[]{type, fs.typeFromName(type).getType().getTypeName()}); |
|
142 |
Object o = gson.fromJson(bodyLesen(e), fs.typeFromName(type).getType()); |
|
143 |
if(o instanceof Entity) { |
|
144 |
Entity entity = (Entity) o; |
|
145 |
if(fs.exists(type, elemName)) { |
|
146 |
fs.delete(type, elemName); |
|
147 |
fs.write(entity, true); |
|
148 |
} else { |
|
149 |
fs.write(entity, false); |
|
150 |
} |
|
151 |
return type + App.getRs(Server.RB_SLASH) + entity.getName(); |
|
152 |
} else { |
|
153 |
return "Ungueltiges Objekt im Body."; |
|
154 |
} |
|
155 |
} else { |
|
156 |
return "Ungueltiger Elementname: " + App.getRs(RB_EP_LISTE); |
|
157 |
} |
|
158 |
} |
8d7d35
|
159 |
|
2b5c60
|
160 |
private boolean loeschen(HttpExchange e) { |
5f70da
|
161 |
String path = e.getRequestURI().toString(); |
U |
162 |
String[] elems = path.split(App.getRs(Server.RB_SLASH)); |
|
163 |
String type = elems[elems.length - 2]; |
|
164 |
String elemName = elems[elems.length - 1]; |
|
165 |
FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); |
2b5c60
|
166 |
return fs.delete(type, elemName); |
U |
167 |
} |
|
168 |
|
|
169 |
private String lesen(HttpExchange e) { |
|
170 |
String path = e.getRequestURI().toString(); |
|
171 |
String[] elems = path.split(App.getRs(Server.RB_SLASH)); |
|
172 |
FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); |
|
173 |
if(path.endsWith(App.getRs(RB_SLASH))) { |
f45e20
|
174 |
List list = null; |
dfb7d3
|
175 |
if(elems[elems.length - 1].equalsIgnoreCase(App.getRs(RB_EP_LISTE))) { |
f45e20
|
176 |
String type = elems[elems.length - 2]; |
U |
177 |
logger.fine(type); |
|
178 |
list = fs.list(type); |
|
179 |
} else { |
|
180 |
String type = elems[elems.length - 1]; |
|
181 |
logger.fine(type); |
|
182 |
list = fs.listObjects(type); |
|
183 |
} |
a43e1a
|
184 |
return jsonWithEnclosingType(list); |
2b5c60
|
185 |
} else { |
U |
186 |
String type = elems[elems.length - 2]; |
|
187 |
String elemName = elems[elems.length - 1]; |
|
188 |
return fs.readJson(type, elemName); |
|
189 |
} |
a43e1a
|
190 |
} |
b379f5
|
191 |
|
8d7d35
|
192 |
@Override |
U |
193 |
public String get(HttpExchange e) { |
|
194 |
return lesen(e); |
|
195 |
} |
|
196 |
|
|
197 |
@Override |
|
198 |
public String post(HttpExchange e) { |
|
199 |
return "nicht unterstuetzt"; |
|
200 |
} |
|
201 |
|
|
202 |
@Override |
|
203 |
public boolean delete(HttpExchange e) { |
|
204 |
return loeschen(e); |
b379f5
|
205 |
} |
U |
206 |
} |