commit | author | age
|
933ccd
|
1 |
/* |
U |
2 |
Calypso - Media Player Remote Control via HTTP for Raspberry Pi |
|
3 |
Copyright (C) 2021-2023 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/>. |
|
17 |
*/ |
|
18 |
|
929228
|
19 |
package de.uhilger.calypso.neu.http; |
U |
20 |
|
|
21 |
import com.sun.net.httpserver.HttpExchange; |
|
22 |
import com.sun.net.httpserver.HttpHandler; |
|
23 |
import java.io.IOException; |
|
24 |
import java.io.OutputStream; |
|
25 |
import java.util.HashMap; |
|
26 |
import java.util.Map; |
|
27 |
import java.util.Set; |
|
28 |
import java.util.logging.Level; |
|
29 |
import java.util.logging.Logger; |
|
30 |
|
|
31 |
/** |
|
32 |
* Die Klasse HttpApi verwendet die von der Klasse HttpExchange |
|
33 |
* gelieferten Elemente einer HTTP-Anfrage und leitet sie an |
|
34 |
* die abstrakte Methode process, wo Subklassen das jeweilige |
|
35 |
* Kommando aus der HTTP-Anfrage ausfuehren können. |
|
36 |
* |
|
37 |
* @author Ulrich Hilger |
|
38 |
*/ |
|
39 |
public abstract class HttpApi implements HttpHandler { |
|
40 |
|
|
41 |
public static final String AMP = "&"; |
|
42 |
public static final String SLASH = "/"; |
|
43 |
|
|
44 |
@Override |
|
45 |
public void handle(HttpExchange exchange) throws IOException { |
|
46 |
String path = exchange.getRequestURI().getPath(); |
|
47 |
String[] elems = path.split(SLASH); |
|
48 |
Map params = getQueryMap(exchange); |
|
49 |
String antwort = process(elems, params, exchange); |
|
50 |
antwortSenden(exchange, params, path, antwort); |
|
51 |
} |
|
52 |
|
|
53 |
/** |
|
54 |
* Eine HTTP-Anfrage ausführen |
|
55 |
* |
|
56 |
* @param elems die Elemente des URI-Pfads |
|
57 |
* @param parameter die Parameter des Query-Teils der URI |
|
58 |
* @return die Antwort, die gesendet werden soll |
|
59 |
*/ |
|
60 |
protected abstract String process(String[] elems, Map parameter, HttpExchange exchange); |
|
61 |
|
|
62 |
/* |
|
63 |
Den Query-Teil einer URL in die Parameter zerlegen |
|
64 |
|
|
65 |
Die Zerlegung erfolgt mit String.split nach |
|
66 |
& und dann nach = |
|
67 |
*/ |
|
68 |
protected Map getQueryMap(HttpExchange t) { |
|
69 |
HashMap map = new HashMap(); |
|
70 |
String query = t.getRequestURI().getQuery(); |
|
71 |
if(query != null && query.length() > 0) { |
|
72 |
String qParts[] = query.split("&"); |
|
73 |
for(String qPart : qParts) { |
|
74 |
Logger logger = Logger.getLogger(de.uhilger.calypso.neu.http.HttpApi.class.getName()); |
|
75 |
logger.log(Level.FINER, "qPart: {0}", qPart); |
|
76 |
String pParts[] = qPart.split("="); |
|
77 |
map.put(pParts[0], pParts[1]); |
|
78 |
logger.log(Level.FINER, "pParts[0]: {0} pParts[1]: {1}", new Object[]{pParts[0], pParts[1]}); |
|
79 |
} |
|
80 |
} |
|
81 |
return map; |
|
82 |
} |
|
83 |
|
|
84 |
protected void antwortSenden(HttpExchange exchange, Map params, String cmd, String antwort) throws IOException { |
|
85 |
String httpResponseStr = getResponseString(params, cmd, antwort); |
|
86 |
sendResponse(exchange, httpResponseStr); |
|
87 |
} |
|
88 |
|
|
89 |
protected String getResponseString(Map map, String cmd, String antwort) { |
|
90 |
Set keys = map.keySet(); |
|
91 |
StringBuilder buf = new StringBuilder(); |
|
92 |
buf.append(cmd); |
|
93 |
buf.append(System.lineSeparator()); |
|
94 |
keys.forEach((Object key) -> { |
|
95 |
buf.append("key: "); |
|
96 |
buf.append(key); |
|
97 |
buf.append(System.lineSeparator()); |
|
98 |
buf.append("value: "); |
|
99 |
buf.append(map.get(key)); |
|
100 |
buf.append(System.lineSeparator()); |
|
101 |
//logger.log(Level.FINE, "key {0} value {1}", new Object[]{key, map.get(key)}); |
|
102 |
}); |
|
103 |
buf.append(antwort); |
|
104 |
return buf.toString(); |
|
105 |
} |
|
106 |
|
|
107 |
protected void sendResponse(HttpExchange t, String response) throws IOException { |
|
108 |
t.sendResponseHeaders(200, response.length()); |
|
109 |
OutputStream os = t.getResponseBody(); |
|
110 |
os.write(response.getBytes()); |
|
111 |
os.close(); |
|
112 |
} |
|
113 |
|
|
114 |
|
|
115 |
} |