commit | author | age
|
b379f5
|
1 |
/* |
U |
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package de.uhilger.mediaz.api; |
|
7 |
|
|
8 |
import com.google.gson.Gson; |
|
9 |
import com.sun.net.httpserver.HttpExchange; |
|
10 |
import com.sun.net.httpserver.HttpHandler; |
|
11 |
import de.uhilger.mediaz.App; |
|
12 |
import de.uhilger.mediaz.Server; |
081606
|
13 |
import de.uhilger.mediaz.store.FileStorage; |
b379f5
|
14 |
import de.uhilger.mediaz.entity.Ablageort; |
U |
15 |
import java.io.BufferedReader; |
081606
|
16 |
import java.io.File; |
b379f5
|
17 |
import java.io.IOException; |
U |
18 |
import java.io.InputStream; |
|
19 |
import java.io.InputStreamReader; |
|
20 |
import java.io.OutputStream; |
081606
|
21 |
import java.util.logging.Level; |
b379f5
|
22 |
import java.util.logging.Logger; |
U |
23 |
|
|
24 |
/** |
|
25 |
* |
|
26 |
* @author ulrich |
|
27 |
*/ |
081606
|
28 |
public class StorageHandler implements HttpHandler { |
b379f5
|
29 |
|
081606
|
30 |
private static final Logger logger = Logger.getLogger(StorageHandler.class.getName()); |
b379f5
|
31 |
|
U |
32 |
|
|
33 |
/* |
|
34 |
|
|
35 |
HTTP GET: lies einen Ablageort und schreibe JSON |
|
36 |
HTTP PUT: schreibe einen neuen Ablageort auf die Platte |
|
37 |
HTTP POST: schreibe Aenderungen auf die Platte |
|
38 |
HTTP DELETE: loesche den Ablageort |
|
39 |
|
|
40 |
Beispiele: |
|
41 |
|
|
42 |
HTTP GET an /mz/api/store/Ablageort/Katalog |
|
43 |
liest den Ablageort namens "Katalog" |
|
44 |
|
|
45 |
HTTP POST an /mz/api/store/Ablageort |
081606
|
46 |
schreibt den neuen Ablageort im Body der Anfrage (Neu) |
b379f5
|
47 |
|
U |
48 |
HTTP PUT an /mz/api/store/Ablageort |
|
49 |
sucht den Ablageort mit dem Namen laut Body der Anfrage |
081606
|
50 |
und schreibt den Inhalt aus der Anfrage in die Datei (Aenderung) |
b379f5
|
51 |
|
U |
52 |
HTTP DELETE an /mz/api/store/Ablageort/Katalog |
|
53 |
löscht den Ablageort namens "Katalog" |
|
54 |
|
|
55 |
*/ |
|
56 |
|
|
57 |
public static final String HTTP_GET = "GET"; |
|
58 |
public static final String HTTP_PUT = "PUT"; |
|
59 |
public static final String HTTP_POST = "POST"; |
|
60 |
public static final String HTTP_DELETE = "DELETE"; |
|
61 |
|
|
62 |
@Override |
|
63 |
public void handle(HttpExchange e) throws IOException { |
|
64 |
String method = e.getRequestMethod(); |
|
65 |
String path = e.getRequestURI().toString(); |
|
66 |
String[] elems = path.split(App.getRs(Server.RB_SLASH)); |
|
67 |
String type = ""; |
|
68 |
String elemName = ""; |
081606
|
69 |
FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); |
b379f5
|
70 |
switch(method) { |
U |
71 |
case HTTP_GET: |
|
72 |
type = elems[elems.length - 2]; |
|
73 |
elemName = elems[elems.length - 1]; |
|
74 |
//this.readFromFile(file); |
081606
|
75 |
fs.read(type, elemName); |
b379f5
|
76 |
break; |
U |
77 |
|
|
78 |
case HTTP_PUT: |
|
79 |
type = elems[elems.length - 1]; |
|
80 |
elemName = "noch bauen: lesen aus Body"; |
|
81 |
break; |
|
82 |
|
|
83 |
case HTTP_POST: |
|
84 |
type = elems[elems.length - 1]; |
|
85 |
elemName = bodyLesen(e); |
|
86 |
if(type.equalsIgnoreCase("Ablageort")) { |
|
87 |
Gson gson = new Gson(); |
|
88 |
Ablageort ort = gson.fromJson(elemName, Ablageort.class); |
|
89 |
elemName = ort.getName(); |
081606
|
90 |
Object o = fs.write(ort); |
U |
91 |
if(o instanceof File) { |
|
92 |
File file = (File) o; |
|
93 |
logger.log(Level.INFO, "Datei {0} geschrieben.", file.getAbsolutePath()); |
|
94 |
} |
b379f5
|
95 |
} |
U |
96 |
break; |
|
97 |
|
|
98 |
case HTTP_DELETE: |
|
99 |
type = elems[elems.length - 2]; |
|
100 |
elemName = elems[elems.length - 1]; |
|
101 |
break; |
|
102 |
} |
|
103 |
|
|
104 |
|
|
105 |
String response = "Method: " + method + ", Path: " + path + |
|
106 |
", Type: " + type + ", elemName: " + elemName; |
|
107 |
logger.info(response); |
|
108 |
e.sendResponseHeaders(200, response.length()); |
|
109 |
OutputStream os = e.getResponseBody(); |
|
110 |
os.write(response.getBytes()); |
|
111 |
os.close(); |
|
112 |
} |
|
113 |
|
|
114 |
|
|
115 |
private String bodyLesen(HttpExchange e) throws IOException { |
|
116 |
InputStream is = e.getRequestBody(); |
|
117 |
BufferedReader r = new BufferedReader(new InputStreamReader(is)); |
|
118 |
StringBuilder sb = new StringBuilder(); |
|
119 |
String line = r.readLine(); |
|
120 |
while(line != null) { |
|
121 |
sb.append(line); |
|
122 |
line = r.readLine(); |
|
123 |
} |
|
124 |
r.close(); |
|
125 |
// {"Ablageort":{"name":"test1","ort":"test2","url":"test3"}} |
|
126 |
//String data = sb.toString(); |
|
127 |
//data = data.substring(1, data.length() - 1); |
|
128 |
//String json = data.substring(data.indexOf("{")); |
|
129 |
// {"name":"test1","ort":"test2","url":"test3"} |
|
130 |
String json = sb.toString(); |
|
131 |
logger.info("json: " + json); |
|
132 |
return json; |
|
133 |
} |
|
134 |
} |