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 |
|
095119
|
20 |
import com.sun.net.httpserver.Headers; |
8d7d35
|
21 |
import com.sun.net.httpserver.HttpExchange; |
U |
22 |
import com.sun.net.httpserver.HttpHandler; |
|
23 |
import java.io.BufferedReader; |
|
24 |
import java.io.IOException; |
|
25 |
import java.io.InputStream; |
|
26 |
import java.io.InputStreamReader; |
|
27 |
import java.io.OutputStream; |
|
28 |
import java.util.logging.Logger; |
|
29 |
|
86bbf7
|
30 |
|
U |
31 |
/** |
|
32 |
* |
b56bb3
|
33 |
* @author Ulrich Hilger |
U |
34 |
* @version 1, 8.4.2021 |
86bbf7
|
35 |
*/ |
8d7d35
|
36 |
public abstract class AbstractHandler extends JsonHelper implements HttpHandler { |
86bbf7
|
37 |
|
8d7d35
|
38 |
private static final Logger logger = Logger.getLogger(AbstractHandler.class.getName()); |
U |
39 |
|
|
40 |
/** Name der HTTP Methode GET */ |
|
41 |
public static final String HTTP_GET = "GET"; |
|
42 |
|
|
43 |
/** Name der HTTP Methode PUT */ |
|
44 |
public static final String HTTP_PUT = "PUT"; |
|
45 |
|
|
46 |
/** Name der HTTP Methode POST */ |
|
47 |
public static final String HTTP_POST = "POST"; |
|
48 |
|
|
49 |
/** Name der HTTP Methode DELETE */ |
|
50 |
public static final String HTTP_DELETE = "DELETE"; |
|
51 |
|
0e9cd3
|
52 |
public static final int RTC_OK = 200; |
U |
53 |
public static final int RTC_NOT_FOUND = 404; |
|
54 |
|
|
55 |
protected int returnCode; |
|
56 |
|
|
57 |
public AbstractHandler() { |
|
58 |
this.returnCode = RTC_OK; |
|
59 |
} |
|
60 |
|
8d7d35
|
61 |
@Override |
U |
62 |
public void handle(HttpExchange e) throws IOException { |
|
63 |
String method = e.getRequestMethod(); |
|
64 |
String response = ""; |
|
65 |
switch(method) { |
|
66 |
case HTTP_GET: |
|
67 |
String json = get(e); |
|
68 |
if(json != null) { |
|
69 |
response = json; |
|
70 |
} else { |
|
71 |
response = "nicht gefunden"; |
0e9cd3
|
72 |
returnCode = RTC_NOT_FOUND; |
8d7d35
|
73 |
} |
U |
74 |
break; |
|
75 |
|
|
76 |
case HTTP_PUT: |
|
77 |
response = put(e); |
|
78 |
break; |
|
79 |
|
|
80 |
case HTTP_POST: |
|
81 |
response = post(e); |
|
82 |
break; |
|
83 |
|
|
84 |
case HTTP_DELETE: |
|
85 |
boolean geloescht = delete(e); |
|
86 |
if(geloescht) { |
|
87 |
response = "geloescht"; |
|
88 |
} else { |
|
89 |
response = "nicht geloescht"; |
|
90 |
} |
|
91 |
break; |
|
92 |
} |
78d707
|
93 |
logger.fine(response); |
095119
|
94 |
Headers headers = e.getResponseHeaders(); |
U |
95 |
headers.add("Content-Type", "application/json"); |
0e9cd3
|
96 |
e.sendResponseHeaders(getReturnCode(), response.length()); |
8d7d35
|
97 |
OutputStream os = e.getResponseBody(); |
U |
98 |
os.write(response.getBytes()); |
|
99 |
os.close(); |
0e9cd3
|
100 |
} |
U |
101 |
|
|
102 |
protected void setReturnCode(int code) { |
|
103 |
this.returnCode = code; |
|
104 |
} |
|
105 |
|
|
106 |
protected int getReturnCode() { |
|
107 |
return returnCode; |
86bbf7
|
108 |
} |
U |
109 |
|
8d7d35
|
110 |
protected String bodyLesen(HttpExchange e) throws IOException { |
U |
111 |
InputStream is = e.getRequestBody(); |
|
112 |
BufferedReader r = new BufferedReader(new InputStreamReader(is)); |
86bbf7
|
113 |
StringBuilder sb = new StringBuilder(); |
8d7d35
|
114 |
String line = r.readLine(); |
U |
115 |
while(line != null) { |
|
116 |
sb.append(line); |
|
117 |
line = r.readLine(); |
|
118 |
} |
|
119 |
r.close(); |
|
120 |
String json = sb.toString(); |
|
121 |
return json; |
86bbf7
|
122 |
} |
8d7d35
|
123 |
|
0e9cd3
|
124 |
protected String put(HttpExchange e) throws IOException { |
U |
125 |
setReturnCode(RTC_NOT_FOUND); |
|
126 |
return "nicht unterstuetzt"; |
|
127 |
} |
|
128 |
|
|
129 |
protected String post(HttpExchange e) { |
|
130 |
setReturnCode(RTC_NOT_FOUND); |
|
131 |
return "nicht unterstuetzt"; |
|
132 |
} |
|
133 |
|
|
134 |
protected boolean delete(HttpExchange e) { |
|
135 |
setReturnCode(RTC_NOT_FOUND); |
|
136 |
return false; |
|
137 |
} |
|
138 |
|
8d7d35
|
139 |
|
U |
140 |
protected abstract String get(HttpExchange e); |
0e9cd3
|
141 |
/* |
8d7d35
|
142 |
protected abstract String put(HttpExchange e) throws IOException; |
U |
143 |
protected abstract String post(HttpExchange e); |
|
144 |
protected abstract boolean delete(HttpExchange e); |
0e9cd3
|
145 |
*/ |
86bbf7
|
146 |
|
U |
147 |
} |