commit | author | age
|
86bbf7
|
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; |
8d7d35
|
9 |
import com.sun.net.httpserver.HttpExchange; |
U |
10 |
import com.sun.net.httpserver.HttpHandler; |
|
11 |
import java.io.BufferedReader; |
|
12 |
import java.io.IOException; |
|
13 |
import java.io.InputStream; |
|
14 |
import java.io.InputStreamReader; |
|
15 |
import java.io.OutputStream; |
|
16 |
import java.util.logging.Level; |
|
17 |
import java.util.logging.Logger; |
|
18 |
|
86bbf7
|
19 |
|
U |
20 |
/** |
|
21 |
* |
|
22 |
* @author ulrich |
|
23 |
*/ |
8d7d35
|
24 |
public abstract class AbstractHandler extends JsonHelper implements HttpHandler { |
86bbf7
|
25 |
|
8d7d35
|
26 |
private static final Logger logger = Logger.getLogger(AbstractHandler.class.getName()); |
U |
27 |
|
|
28 |
/** Name der HTTP Methode GET */ |
|
29 |
public static final String HTTP_GET = "GET"; |
|
30 |
|
|
31 |
/** Name der HTTP Methode PUT */ |
|
32 |
public static final String HTTP_PUT = "PUT"; |
|
33 |
|
|
34 |
/** Name der HTTP Methode POST */ |
|
35 |
public static final String HTTP_POST = "POST"; |
|
36 |
|
|
37 |
/** Name der HTTP Methode DELETE */ |
|
38 |
public static final String HTTP_DELETE = "DELETE"; |
|
39 |
|
|
40 |
@Override |
|
41 |
public void handle(HttpExchange e) throws IOException { |
|
42 |
String method = e.getRequestMethod(); |
|
43 |
String response = ""; |
|
44 |
int code = 200; |
|
45 |
switch(method) { |
|
46 |
case HTTP_GET: |
|
47 |
String json = get(e); |
|
48 |
if(json != null) { |
|
49 |
response = json; |
|
50 |
} else { |
|
51 |
response = "nicht gefunden"; |
|
52 |
code = 404; |
|
53 |
} |
|
54 |
break; |
|
55 |
|
|
56 |
case HTTP_PUT: |
|
57 |
response = put(e); |
|
58 |
break; |
|
59 |
|
|
60 |
case HTTP_POST: |
|
61 |
response = post(e); |
|
62 |
code = 404; |
|
63 |
break; |
|
64 |
|
|
65 |
case HTTP_DELETE: |
|
66 |
boolean geloescht = delete(e); |
|
67 |
if(geloescht) { |
|
68 |
response = "geloescht"; |
|
69 |
} else { |
|
70 |
response = "nicht geloescht"; |
|
71 |
} |
|
72 |
break; |
|
73 |
} |
|
74 |
logger.fine(response); |
|
75 |
e.sendResponseHeaders(code, response.length()); |
|
76 |
OutputStream os = e.getResponseBody(); |
|
77 |
os.write(response.getBytes()); |
|
78 |
os.close(); |
86bbf7
|
79 |
} |
U |
80 |
|
8d7d35
|
81 |
protected String bodyLesen(HttpExchange e) throws IOException { |
U |
82 |
InputStream is = e.getRequestBody(); |
|
83 |
BufferedReader r = new BufferedReader(new InputStreamReader(is)); |
86bbf7
|
84 |
StringBuilder sb = new StringBuilder(); |
8d7d35
|
85 |
String line = r.readLine(); |
U |
86 |
while(line != null) { |
|
87 |
sb.append(line); |
|
88 |
line = r.readLine(); |
|
89 |
} |
|
90 |
r.close(); |
|
91 |
String json = sb.toString(); |
|
92 |
logger.log(Level.FINE, "json: {0}", json); |
|
93 |
return json; |
86bbf7
|
94 |
} |
8d7d35
|
95 |
|
U |
96 |
|
|
97 |
protected abstract String get(HttpExchange e); |
|
98 |
protected abstract String put(HttpExchange e) throws IOException; |
|
99 |
protected abstract String post(HttpExchange e); |
|
100 |
protected abstract boolean delete(HttpExchange e); |
86bbf7
|
101 |
|
U |
102 |
} |