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 |
|
0e9cd3
|
51 |
public static final int RTC_OK = 200; |
U |
52 |
public static final int RTC_NOT_FOUND = 404; |
|
53 |
|
|
54 |
protected int returnCode; |
|
55 |
|
|
56 |
public AbstractHandler() { |
|
57 |
this.returnCode = RTC_OK; |
|
58 |
} |
|
59 |
|
8d7d35
|
60 |
@Override |
U |
61 |
public void handle(HttpExchange e) throws IOException { |
|
62 |
String method = e.getRequestMethod(); |
|
63 |
String response = ""; |
|
64 |
switch(method) { |
|
65 |
case HTTP_GET: |
|
66 |
String json = get(e); |
|
67 |
if(json != null) { |
|
68 |
response = json; |
|
69 |
} else { |
|
70 |
response = "nicht gefunden"; |
0e9cd3
|
71 |
returnCode = RTC_NOT_FOUND; |
8d7d35
|
72 |
} |
U |
73 |
break; |
|
74 |
|
|
75 |
case HTTP_PUT: |
|
76 |
response = put(e); |
|
77 |
break; |
|
78 |
|
|
79 |
case HTTP_POST: |
|
80 |
response = post(e); |
|
81 |
break; |
|
82 |
|
|
83 |
case HTTP_DELETE: |
|
84 |
boolean geloescht = delete(e); |
|
85 |
if(geloescht) { |
|
86 |
response = "geloescht"; |
|
87 |
} else { |
|
88 |
response = "nicht geloescht"; |
|
89 |
} |
|
90 |
break; |
|
91 |
} |
a1b669
|
92 |
logger.info(response); |
0e9cd3
|
93 |
e.sendResponseHeaders(getReturnCode(), response.length()); |
8d7d35
|
94 |
OutputStream os = e.getResponseBody(); |
U |
95 |
os.write(response.getBytes()); |
|
96 |
os.close(); |
0e9cd3
|
97 |
} |
U |
98 |
|
|
99 |
protected void setReturnCode(int code) { |
|
100 |
this.returnCode = code; |
|
101 |
} |
|
102 |
|
|
103 |
protected int getReturnCode() { |
|
104 |
return returnCode; |
86bbf7
|
105 |
} |
U |
106 |
|
8d7d35
|
107 |
protected String bodyLesen(HttpExchange e) throws IOException { |
U |
108 |
InputStream is = e.getRequestBody(); |
|
109 |
BufferedReader r = new BufferedReader(new InputStreamReader(is)); |
86bbf7
|
110 |
StringBuilder sb = new StringBuilder(); |
8d7d35
|
111 |
String line = r.readLine(); |
U |
112 |
while(line != null) { |
|
113 |
sb.append(line); |
|
114 |
line = r.readLine(); |
|
115 |
} |
|
116 |
r.close(); |
|
117 |
String json = sb.toString(); |
|
118 |
return json; |
86bbf7
|
119 |
} |
8d7d35
|
120 |
|
0e9cd3
|
121 |
protected String put(HttpExchange e) throws IOException { |
U |
122 |
setReturnCode(RTC_NOT_FOUND); |
|
123 |
return "nicht unterstuetzt"; |
|
124 |
} |
|
125 |
|
|
126 |
protected String post(HttpExchange e) { |
|
127 |
setReturnCode(RTC_NOT_FOUND); |
|
128 |
return "nicht unterstuetzt"; |
|
129 |
} |
|
130 |
|
|
131 |
protected boolean delete(HttpExchange e) { |
|
132 |
setReturnCode(RTC_NOT_FOUND); |
|
133 |
return false; |
|
134 |
} |
|
135 |
|
8d7d35
|
136 |
|
U |
137 |
protected abstract String get(HttpExchange e); |
0e9cd3
|
138 |
/* |
8d7d35
|
139 |
protected abstract String put(HttpExchange e) throws IOException; |
U |
140 |
protected abstract String post(HttpExchange e); |
|
141 |
protected abstract boolean delete(HttpExchange e); |
0e9cd3
|
142 |
*/ |
86bbf7
|
143 |
|
U |
144 |
} |