/*
|
AV-Direktor - Control OMXPlayer on Raspberry Pi via HTTP
|
Copyright (C) 2021 Ulrich Hilger
|
|
This program is free software: you can redistribute it and/or modify
|
it under the terms of the GNU Affero General Public License as
|
published by the Free Software Foundation, either version 3 of the
|
License, or (at your option) any later version.
|
|
This program is distributed in the hope that it will be useful,
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
GNU Affero General Public License for more details.
|
|
You should have received a copy of the GNU Affero General Public License
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
*/
|
|
package de.uhilger.avdirektor.handler;
|
|
import com.sun.net.httpserver.HttpExchange;
|
import com.sun.net.httpserver.HttpHandler;
|
import java.io.IOException;
|
import java.io.OutputStream;
|
import java.util.HashMap;
|
import java.util.Map;
|
import java.util.Set;
|
import java.util.logging.Level;
|
import java.util.logging.Logger;
|
|
/**
|
*
|
* @author ulrich
|
*/
|
public abstract class AbstractHandler extends OMXPlayer implements HttpHandler {
|
|
private static final Logger logger = Logger.getLogger(AbstractHandler.class.getName());
|
|
protected String cmd;
|
protected Map map;
|
|
@Override
|
public void handle(HttpExchange t) throws IOException {
|
logger.log(Level.FINE, "RequestURI: {0}", t.getRequestURI().toString());
|
StringBuilder params = buildParams(t);
|
String antwort = process(t, params.toString());
|
sendResponse(t, cmd, antwort);
|
}
|
|
protected abstract String process(HttpExchange t, String params);
|
|
protected StringBuilder buildParams(HttpExchange t) {
|
map = getQueryMap(t);
|
StringBuilder params = new StringBuilder();
|
return params;
|
}
|
|
protected void sendResponse(HttpExchange t, String cmd, String antwort) throws IOException {
|
String response = getResponseString(map, cmd, antwort);
|
t.sendResponseHeaders(200, response.length());
|
OutputStream os = t.getResponseBody();
|
os.write(response.getBytes());
|
os.close();
|
}
|
|
public void setCmd(String cmd) {
|
this.cmd = cmd;
|
}
|
|
public String getCmd(String cmd) {
|
return this.cmd;
|
}
|
|
/* --- --- */
|
|
protected String getParam(Map map, String key) {
|
Object o = map.get(key);
|
if(o != null) {
|
return o.toString();
|
} else {
|
return null;
|
}
|
}
|
|
/*
|
Den Query-Teil einer URL in die Parameter zerlegen
|
|
Die Zerlegung erfolgt mit String.split nach
|
& und dann nach =
|
*/
|
protected Map getQueryMap(HttpExchange t) {
|
HashMap map = new HashMap();
|
String query = t.getRequestURI().getQuery();
|
if(query != null && query.length() > 0) {
|
String qParts[] = query.split("&");
|
for(String qPart : qParts) {
|
logger.finer("qPart: " + qPart);
|
String pParts[] = qPart.split("=");
|
map.put(pParts[0], pParts[1]);
|
logger.finer("pParts[0]: " + pParts[0] + ", pParts[1]: " + pParts[1]);
|
}
|
}
|
return map;
|
}
|
|
protected String getResponseString(Map map, String cmd, String antwort) {
|
Set keys = map.keySet();
|
StringBuilder buf = new StringBuilder();
|
buf.append(cmd);
|
buf.append(System.lineSeparator());
|
keys.forEach((Object key) -> {
|
buf.append("key: ");
|
buf.append(key);
|
buf.append(System.lineSeparator());
|
buf.append("value: ");
|
buf.append(map.get(key));
|
buf.append(System.lineSeparator());
|
//logger.log(Level.FINE, "key {0} value {1}", new Object[]{key, map.get(key)});
|
});
|
buf.append(antwort);
|
return buf.toString();
|
}
|
|
|
}
|