/*
|
* To change this license header, choose License Headers in Project Properties.
|
* To change this template file, choose Tools | Templates
|
* and open the template in the editor.
|
*/
|
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();
|
}
|
|
|
}
|