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; |
|
13 |
import de.uhilger.mediaz.conf.Store; |
|
14 |
import de.uhilger.mediaz.entity.Ablageort; |
|
15 |
import java.io.BufferedReader; |
|
16 |
import java.io.IOException; |
|
17 |
import java.io.InputStream; |
|
18 |
import java.io.InputStreamReader; |
|
19 |
import java.io.OutputStream; |
|
20 |
import java.util.logging.Logger; |
|
21 |
|
|
22 |
/** |
|
23 |
* |
|
24 |
* @author ulrich |
|
25 |
*/ |
|
26 |
public class StoreHandler extends Store implements HttpHandler { |
|
27 |
|
|
28 |
private static final Logger logger = Logger.getLogger(StoreHandler.class.getName()); |
|
29 |
|
|
30 |
|
|
31 |
/* |
|
32 |
|
|
33 |
HTTP GET: lies einen Ablageort und schreibe JSON |
|
34 |
HTTP PUT: schreibe einen neuen Ablageort auf die Platte |
|
35 |
HTTP POST: schreibe Aenderungen auf die Platte |
|
36 |
HTTP DELETE: loesche den Ablageort |
|
37 |
|
|
38 |
Beispiele: |
|
39 |
|
|
40 |
HTTP GET an /mz/api/store/Ablageort/Katalog |
|
41 |
liest den Ablageort namens "Katalog" |
|
42 |
|
|
43 |
HTTP POST an /mz/api/store/Ablageort |
|
44 |
schreibt den neuen Ablageort im Body der Anfrage |
|
45 |
|
|
46 |
HTTP PUT an /mz/api/store/Ablageort |
|
47 |
sucht den Ablageort mit dem Namen laut Body der Anfrage |
|
48 |
und schreibt den Inhalt aus der Anfrage in die Datei |
|
49 |
|
|
50 |
HTTP DELETE an /mz/api/store/Ablageort/Katalog |
|
51 |
löscht den Ablageort namens "Katalog" |
|
52 |
|
|
53 |
*/ |
|
54 |
|
|
55 |
public static final String HTTP_GET = "GET"; |
|
56 |
public static final String HTTP_PUT = "PUT"; |
|
57 |
public static final String HTTP_POST = "POST"; |
|
58 |
public static final String HTTP_DELETE = "DELETE"; |
|
59 |
|
|
60 |
@Override |
|
61 |
public void handle(HttpExchange e) throws IOException { |
|
62 |
String method = e.getRequestMethod(); |
|
63 |
String path = e.getRequestURI().toString(); |
|
64 |
String[] elems = path.split(App.getRs(Server.RB_SLASH)); |
|
65 |
String type = ""; |
|
66 |
String elemName = ""; |
|
67 |
switch(method) { |
|
68 |
case HTTP_GET: |
|
69 |
type = elems[elems.length - 2]; |
|
70 |
elemName = elems[elems.length - 1]; |
|
71 |
//this.readFromFile(file); |
|
72 |
break; |
|
73 |
|
|
74 |
case HTTP_PUT: |
|
75 |
type = elems[elems.length - 1]; |
|
76 |
elemName = "noch bauen: lesen aus Body"; |
|
77 |
break; |
|
78 |
|
|
79 |
case HTTP_POST: |
|
80 |
type = elems[elems.length - 1]; |
|
81 |
elemName = bodyLesen(e); |
|
82 |
if(type.equalsIgnoreCase("Ablageort")) { |
|
83 |
Gson gson = new Gson(); |
|
84 |
Ablageort ort = gson.fromJson(elemName, Ablageort.class); |
|
85 |
elemName = ort.getName(); |
|
86 |
} |
|
87 |
break; |
|
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(); |
|
103 |
} |
|
104 |
|
|
105 |
|
|
106 |
private String bodyLesen(HttpExchange e) throws IOException { |
|
107 |
InputStream is = e.getRequestBody(); |
|
108 |
BufferedReader r = new BufferedReader(new InputStreamReader(is)); |
|
109 |
StringBuilder sb = new StringBuilder(); |
|
110 |
String line = r.readLine(); |
|
111 |
while(line != null) { |
|
112 |
sb.append(line); |
|
113 |
line = r.readLine(); |
|
114 |
} |
|
115 |
r.close(); |
|
116 |
// {"Ablageort":{"name":"test1","ort":"test2","url":"test3"}} |
|
117 |
//String data = sb.toString(); |
|
118 |
//data = data.substring(1, data.length() - 1); |
|
119 |
//String json = data.substring(data.indexOf("{")); |
|
120 |
// {"name":"test1","ort":"test2","url":"test3"} |
|
121 |
String json = sb.toString(); |
|
122 |
logger.info("json: " + json); |
|
123 |
return json; |
|
124 |
} |
|
125 |
} |