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