commit | author | age
|
86bbf7
|
1 |
/* |
94b1c2
|
2 |
Tango - Personal Media Center |
b56bb3
|
3 |
Copyright (C) 2021 Ulrich Hilger |
U |
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 |
*/ |
94b1c2
|
18 |
package de.uhilger.tango.api; |
86bbf7
|
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; |
f70acb
|
23 |
import de.uhilger.tango.App; |
03b95f
|
24 |
import de.uhilger.tango.entity.Einstellung; |
U |
25 |
import de.uhilger.tango.entity.Entity; |
|
26 |
import de.uhilger.tango.store.Storage; |
8d7d35
|
27 |
import java.io.BufferedReader; |
U |
28 |
import java.io.IOException; |
|
29 |
import java.io.InputStream; |
|
30 |
import java.io.InputStreamReader; |
|
31 |
import java.io.OutputStream; |
f70acb
|
32 |
import java.util.ResourceBundle; |
8d7d35
|
33 |
import java.util.logging.Logger; |
U |
34 |
|
86bbf7
|
35 |
|
U |
36 |
/** |
|
37 |
* |
b56bb3
|
38 |
* @author Ulrich Hilger |
U |
39 |
* @version 1, 8.4.2021 |
86bbf7
|
40 |
*/ |
8d7d35
|
41 |
public abstract class AbstractHandler extends JsonHelper implements HttpHandler { |
86bbf7
|
42 |
|
8d7d35
|
43 |
private static final Logger logger = Logger.getLogger(AbstractHandler.class.getName()); |
U |
44 |
|
|
45 |
/** Name der HTTP Methode GET */ |
|
46 |
public static final String HTTP_GET = "GET"; |
|
47 |
|
|
48 |
/** Name der HTTP Methode PUT */ |
|
49 |
public static final String HTTP_PUT = "PUT"; |
|
50 |
|
|
51 |
/** Name der HTTP Methode POST */ |
|
52 |
public static final String HTTP_POST = "POST"; |
|
53 |
|
|
54 |
/** Name der HTTP Methode DELETE */ |
|
55 |
public static final String HTTP_DELETE = "DELETE"; |
|
56 |
|
0e9cd3
|
57 |
public static final int RTC_OK = 200; |
U |
58 |
public static final int RTC_NOT_FOUND = 404; |
|
59 |
|
|
60 |
protected int returnCode; |
f70acb
|
61 |
|
U |
62 |
private ResourceBundle rb; |
0e9cd3
|
63 |
|
U |
64 |
public AbstractHandler() { |
|
65 |
this.returnCode = RTC_OK; |
|
66 |
} |
|
67 |
|
8d7d35
|
68 |
@Override |
U |
69 |
public void handle(HttpExchange e) throws IOException { |
|
70 |
String method = e.getRequestMethod(); |
|
71 |
String response = ""; |
|
72 |
switch(method) { |
|
73 |
case HTTP_GET: |
|
74 |
String json = get(e); |
|
75 |
if(json != null) { |
|
76 |
response = json; |
|
77 |
} else { |
|
78 |
response = "nicht gefunden"; |
d027b5
|
79 |
//returnCode = RTC_NOT_FOUND; |
8d7d35
|
80 |
} |
U |
81 |
break; |
|
82 |
|
|
83 |
case HTTP_PUT: |
|
84 |
response = put(e); |
|
85 |
break; |
|
86 |
|
|
87 |
case HTTP_POST: |
|
88 |
response = post(e); |
|
89 |
break; |
|
90 |
|
|
91 |
case HTTP_DELETE: |
|
92 |
boolean geloescht = delete(e); |
|
93 |
if(geloescht) { |
|
94 |
response = "geloescht"; |
|
95 |
} else { |
|
96 |
response = "nicht geloescht"; |
|
97 |
} |
|
98 |
break; |
|
99 |
} |
78d707
|
100 |
logger.fine(response); |
095119
|
101 |
Headers headers = e.getResponseHeaders(); |
U |
102 |
headers.add("Content-Type", "application/json"); |
0e9cd3
|
103 |
e.sendResponseHeaders(getReturnCode(), response.length()); |
8d7d35
|
104 |
OutputStream os = e.getResponseBody(); |
U |
105 |
os.write(response.getBytes()); |
|
106 |
os.close(); |
0e9cd3
|
107 |
} |
U |
108 |
|
|
109 |
protected void setReturnCode(int code) { |
|
110 |
this.returnCode = code; |
|
111 |
} |
|
112 |
|
|
113 |
protected int getReturnCode() { |
|
114 |
return returnCode; |
86bbf7
|
115 |
} |
U |
116 |
|
8d7d35
|
117 |
protected String bodyLesen(HttpExchange e) throws IOException { |
U |
118 |
InputStream is = e.getRequestBody(); |
|
119 |
BufferedReader r = new BufferedReader(new InputStreamReader(is)); |
86bbf7
|
120 |
StringBuilder sb = new StringBuilder(); |
8d7d35
|
121 |
String line = r.readLine(); |
U |
122 |
while(line != null) { |
|
123 |
sb.append(line); |
|
124 |
line = r.readLine(); |
|
125 |
} |
|
126 |
r.close(); |
|
127 |
String json = sb.toString(); |
|
128 |
return json; |
86bbf7
|
129 |
} |
8d7d35
|
130 |
|
0e9cd3
|
131 |
protected String put(HttpExchange e) throws IOException { |
U |
132 |
setReturnCode(RTC_NOT_FOUND); |
|
133 |
return "nicht unterstuetzt"; |
|
134 |
} |
|
135 |
|
1bf4f6
|
136 |
protected String post(HttpExchange e) throws IOException { |
0e9cd3
|
137 |
setReturnCode(RTC_NOT_FOUND); |
U |
138 |
return "nicht unterstuetzt"; |
|
139 |
} |
|
140 |
|
|
141 |
protected boolean delete(HttpExchange e) { |
|
142 |
setReturnCode(RTC_NOT_FOUND); |
|
143 |
return false; |
|
144 |
} |
|
145 |
|
8d7d35
|
146 |
|
U |
147 |
protected abstract String get(HttpExchange e); |
0e9cd3
|
148 |
/* |
8d7d35
|
149 |
protected abstract String put(HttpExchange e) throws IOException; |
U |
150 |
protected abstract String post(HttpExchange e); |
|
151 |
protected abstract boolean delete(HttpExchange e); |
0e9cd3
|
152 |
*/ |
86bbf7
|
153 |
|
f70acb
|
154 |
protected String getResString(String key) { |
U |
155 |
if(rb == null) { |
|
156 |
rb = ResourceBundle.getBundle(App.RB_NAME); |
|
157 |
} |
|
158 |
return rb.getString(key); |
|
159 |
} |
|
160 |
|
03b95f
|
161 |
protected String getEinstellung(Storage s, String key, String standardWert) { |
U |
162 |
Entity entity = s.read(Einstellung.class.getSimpleName(), key); |
|
163 |
if (entity instanceof Einstellung) { |
|
164 |
Einstellung einstellung = (Einstellung) entity; |
|
165 |
Object o = einstellung.getValue(); |
|
166 |
if(o instanceof String) { |
|
167 |
return o.toString(); |
|
168 |
} else { |
|
169 |
return standardWert; |
|
170 |
} |
|
171 |
} else { |
|
172 |
return standardWert; |
|
173 |
} |
|
174 |
} |
|
175 |
|
|
176 |
|
86bbf7
|
177 |
} |