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 com.sun.net.httpserver.HttpHandler; |
|
23 |
import de.uhilger.mediaz.App; |
|
24 |
import de.uhilger.mediaz.Server; |
081606
|
25 |
import de.uhilger.mediaz.store.FileStorage; |
b1bf96
|
26 |
import de.uhilger.mediaz.entity.Entity; |
b379f5
|
27 |
import java.io.BufferedReader; |
081606
|
28 |
import java.io.File; |
b379f5
|
29 |
import java.io.IOException; |
U |
30 |
import java.io.InputStream; |
|
31 |
import java.io.InputStreamReader; |
|
32 |
import java.io.OutputStream; |
081606
|
33 |
import java.util.logging.Level; |
b379f5
|
34 |
import java.util.logging.Logger; |
U |
35 |
|
|
36 |
/** |
5f70da
|
37 |
* HttpHandler fuer die Ablage von Entitaeten der Mediazentrale |
U |
38 |
* |
|
39 |
* @author Ulrich Hilger |
|
40 |
* @version 1, 5.4.2021 |
b379f5
|
41 |
*/ |
081606
|
42 |
public class StorageHandler implements HttpHandler { |
b379f5
|
43 |
|
081606
|
44 |
private static final Logger logger = Logger.getLogger(StorageHandler.class.getName()); |
b379f5
|
45 |
|
U |
46 |
|
|
47 |
/* |
|
48 |
|
|
49 |
HTTP GET: lies einen Ablageort und schreibe JSON |
|
50 |
HTTP PUT: schreibe einen neuen Ablageort auf die Platte |
|
51 |
HTTP POST: schreibe Aenderungen auf die Platte |
|
52 |
HTTP DELETE: loesche den Ablageort |
|
53 |
|
|
54 |
Beispiele: |
|
55 |
|
|
56 |
HTTP GET an /mz/api/store/Ablageort/Katalog |
|
57 |
liest den Ablageort namens "Katalog" |
|
58 |
|
|
59 |
HTTP POST an /mz/api/store/Ablageort |
081606
|
60 |
schreibt den neuen Ablageort im Body der Anfrage (Neu) |
b379f5
|
61 |
|
U |
62 |
HTTP PUT an /mz/api/store/Ablageort |
|
63 |
sucht den Ablageort mit dem Namen laut Body der Anfrage |
081606
|
64 |
und schreibt den Inhalt aus der Anfrage in die Datei (Aenderung) |
b379f5
|
65 |
|
U |
66 |
HTTP DELETE an /mz/api/store/Ablageort/Katalog |
|
67 |
löscht den Ablageort namens "Katalog" |
|
68 |
|
|
69 |
*/ |
|
70 |
|
|
71 |
public static final String HTTP_GET = "GET"; |
|
72 |
public static final String HTTP_PUT = "PUT"; |
|
73 |
public static final String HTTP_POST = "POST"; |
|
74 |
public static final String HTTP_DELETE = "DELETE"; |
|
75 |
|
|
76 |
@Override |
|
77 |
public void handle(HttpExchange e) throws IOException { |
|
78 |
String method = e.getRequestMethod(); |
5f70da
|
79 |
String response = ""; |
U |
80 |
int code = 200; |
b379f5
|
81 |
switch(method) { |
U |
82 |
case HTTP_GET: |
5f70da
|
83 |
String json = lesen(e); |
U |
84 |
if(json != null) { |
|
85 |
response = json; |
|
86 |
} else { |
|
87 |
response = "nicht gefunden"; |
|
88 |
code = 404; |
|
89 |
} |
b379f5
|
90 |
break; |
U |
91 |
|
|
92 |
case HTTP_PUT: |
5f70da
|
93 |
response = "PUT noch bauen."; |
b379f5
|
94 |
break; |
U |
95 |
|
|
96 |
case HTTP_POST: |
5f70da
|
97 |
response = neu(e); |
b379f5
|
98 |
break; |
U |
99 |
|
|
100 |
case HTTP_DELETE: |
5f70da
|
101 |
response = "DELETE noch bauen."; |
b379f5
|
102 |
break; |
U |
103 |
} |
|
104 |
logger.info(response); |
5f70da
|
105 |
e.sendResponseHeaders(code, response.length()); |
b379f5
|
106 |
OutputStream os = e.getResponseBody(); |
U |
107 |
os.write(response.getBytes()); |
|
108 |
os.close(); |
b1bf96
|
109 |
} |
U |
110 |
|
5f70da
|
111 |
private String neu(HttpExchange e) throws IOException { |
b1bf96
|
112 |
String path = e.getRequestURI().toString(); |
U |
113 |
String[] elems = path.split(App.getRs(Server.RB_SLASH)); |
|
114 |
String type = elems[elems.length - 1]; |
|
115 |
String body = bodyLesen(e); |
5f70da
|
116 |
String filename = ""; |
b1bf96
|
117 |
FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); |
U |
118 |
Gson gson = new Gson(); |
|
119 |
logger.log(Level.INFO, "type: {0}", type); |
|
120 |
Object o = gson.fromJson(body, fs.typeFromName(type).getType()); |
|
121 |
if(o instanceof Entity) { |
|
122 |
Object antwortObjekt = fs.write((Entity) o); |
|
123 |
if(antwortObjekt instanceof File) { |
|
124 |
File file = (File) antwortObjekt; |
|
125 |
logger.log(Level.INFO, "Datei {0} geschrieben.", file.getAbsolutePath()); |
5f70da
|
126 |
filename = file.getName(); |
b1bf96
|
127 |
} |
U |
128 |
} |
5f70da
|
129 |
return type + FileHandler.STR_BLANK + filename; |
b1bf96
|
130 |
} |
U |
131 |
|
|
132 |
private void aendern() { |
|
133 |
|
|
134 |
} |
|
135 |
|
|
136 |
private void loeschen() { |
|
137 |
|
|
138 |
} |
|
139 |
|
5f70da
|
140 |
private String lesen(HttpExchange e) { |
U |
141 |
String path = e.getRequestURI().toString(); |
|
142 |
String[] elems = path.split(App.getRs(Server.RB_SLASH)); |
|
143 |
String type = elems[elems.length - 2]; |
|
144 |
String elemName = elems[elems.length - 1]; |
|
145 |
FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); |
|
146 |
//Entity entity = fs.read(type, elemName); |
|
147 |
//return entity; |
|
148 |
return fs.readJson(type, elemName); |
b379f5
|
149 |
} |
U |
150 |
|
|
151 |
|
|
152 |
private String bodyLesen(HttpExchange e) throws IOException { |
|
153 |
InputStream is = e.getRequestBody(); |
|
154 |
BufferedReader r = new BufferedReader(new InputStreamReader(is)); |
|
155 |
StringBuilder sb = new StringBuilder(); |
|
156 |
String line = r.readLine(); |
|
157 |
while(line != null) { |
|
158 |
sb.append(line); |
|
159 |
line = r.readLine(); |
|
160 |
} |
|
161 |
r.close(); |
|
162 |
String json = sb.toString(); |
b1bf96
|
163 |
logger.log(Level.INFO, "json: {0}", json); |
b379f5
|
164 |
return json; |
U |
165 |
} |
|
166 |
} |