commit | author | age
|
b5cc80
|
1 |
/* |
60719c
|
2 |
AV-Direktor - Control OMXPlayer on Raspberry Pi via HTTP |
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/>. |
|
17 |
*/ |
|
18 |
|
b5cc80
|
19 |
package de.uhilger.avdirektor.handler; |
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 |
* |
|
33 |
* @author ulrich |
|
34 |
*/ |
|
35 |
public abstract class AbstractHandler extends OMXPlayer implements HttpHandler { |
|
36 |
|
|
37 |
private static final Logger logger = Logger.getLogger(AbstractHandler.class.getName()); |
|
38 |
|
|
39 |
protected String cmd; |
|
40 |
protected Map map; |
|
41 |
|
|
42 |
@Override |
|
43 |
public void handle(HttpExchange t) throws IOException { |
|
44 |
logger.log(Level.FINE, "RequestURI: {0}", t.getRequestURI().toString()); |
|
45 |
StringBuilder params = buildParams(t); |
|
46 |
String antwort = process(t, params.toString()); |
|
47 |
sendResponse(t, cmd, antwort); |
|
48 |
} |
|
49 |
|
|
50 |
protected abstract String process(HttpExchange t, String params); |
|
51 |
|
|
52 |
protected StringBuilder buildParams(HttpExchange t) { |
|
53 |
map = getQueryMap(t); |
|
54 |
StringBuilder params = new StringBuilder(); |
|
55 |
return params; |
|
56 |
} |
|
57 |
|
|
58 |
protected void sendResponse(HttpExchange t, String cmd, String antwort) throws IOException { |
|
59 |
String response = getResponseString(map, cmd, antwort); |
|
60 |
t.sendResponseHeaders(200, response.length()); |
|
61 |
OutputStream os = t.getResponseBody(); |
|
62 |
os.write(response.getBytes()); |
|
63 |
os.close(); |
|
64 |
} |
|
65 |
|
|
66 |
public void setCmd(String cmd) { |
|
67 |
this.cmd = cmd; |
|
68 |
} |
|
69 |
|
|
70 |
public String getCmd(String cmd) { |
|
71 |
return this.cmd; |
|
72 |
} |
|
73 |
|
|
74 |
/* --- --- */ |
|
75 |
|
|
76 |
protected String getParam(Map map, String key) { |
|
77 |
Object o = map.get(key); |
|
78 |
if(o != null) { |
|
79 |
return o.toString(); |
|
80 |
} else { |
|
81 |
return null; |
|
82 |
} |
|
83 |
} |
|
84 |
|
|
85 |
/* |
|
86 |
Den Query-Teil einer URL in die Parameter zerlegen |
|
87 |
|
|
88 |
Die Zerlegung erfolgt mit String.split nach |
|
89 |
& und dann nach = |
|
90 |
*/ |
|
91 |
protected Map getQueryMap(HttpExchange t) { |
|
92 |
HashMap map = new HashMap(); |
|
93 |
String query = t.getRequestURI().getQuery(); |
|
94 |
if(query != null && query.length() > 0) { |
|
95 |
String qParts[] = query.split("&"); |
|
96 |
for(String qPart : qParts) { |
|
97 |
logger.finer("qPart: " + qPart); |
|
98 |
String pParts[] = qPart.split("="); |
|
99 |
map.put(pParts[0], pParts[1]); |
|
100 |
logger.finer("pParts[0]: " + pParts[0] + ", pParts[1]: " + pParts[1]); |
|
101 |
} |
|
102 |
} |
|
103 |
return map; |
|
104 |
} |
|
105 |
|
|
106 |
protected String getResponseString(Map map, String cmd, String antwort) { |
|
107 |
Set keys = map.keySet(); |
|
108 |
StringBuilder buf = new StringBuilder(); |
|
109 |
buf.append(cmd); |
|
110 |
buf.append(System.lineSeparator()); |
|
111 |
keys.forEach((Object key) -> { |
|
112 |
buf.append("key: "); |
|
113 |
buf.append(key); |
|
114 |
buf.append(System.lineSeparator()); |
|
115 |
buf.append("value: "); |
|
116 |
buf.append(map.get(key)); |
|
117 |
buf.append(System.lineSeparator()); |
|
118 |
//logger.log(Level.FINE, "key {0} value {1}", new Object[]{key, map.get(key)}); |
|
119 |
}); |
|
120 |
buf.append(antwort); |
|
121 |
return buf.toString(); |
|
122 |
} |
|
123 |
|
|
124 |
|
|
125 |
} |