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