/*
|
Calypso - Media Player Remote Control via HTTP for Raspberry Pi
|
Copyright (C) 2021-2023 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.calypso.http;
|
|
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;
|
|
/**
|
* Die Klasse HttpApi verwendet die von der Klasse HttpExchange
|
* gelieferten Elemente einer HTTP-Anfrage und leitet sie an
|
* die abstrakte Methode process, wo Subklassen das jeweilige
|
* Kommando aus der HTTP-Anfrage ausfuehren können.
|
*
|
* @author Ulrich Hilger
|
*/
|
public abstract class HttpApi implements HttpHandler {
|
|
public static final String AMP = "&";
|
public static final String SLASH = "/";
|
|
@Override
|
public void handle(HttpExchange exchange) throws IOException {
|
String path = exchange.getRequestURI().getPath();
|
String[] elems = path.split(SLASH);
|
Map params = getQueryMap(exchange);
|
String antwort = process(elems, params, exchange);
|
antwortSenden(exchange, params, path, antwort);
|
}
|
|
/**
|
* Eine HTTP-Anfrage ausführen
|
*
|
* @param elems die Elemente des URI-Pfads
|
* @param parameter die Parameter des Query-Teils der URI
|
* @return die Antwort, die gesendet werden soll
|
*/
|
protected abstract String process(String[] elems, Map parameter, HttpExchange exchange);
|
|
/*
|
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 logger = Logger.getLogger(de.uhilger.calypso.http.HttpApi.class.getName());
|
logger.log(Level.FINER, "qPart: {0}", qPart);
|
String pParts[] = qPart.split("=");
|
map.put(pParts[0], pParts[1]);
|
logger.log(Level.FINER, "pParts[0]: {0} pParts[1]: {1}", new Object[]{pParts[0], pParts[1]});
|
}
|
}
|
return map;
|
}
|
|
protected void antwortSenden(HttpExchange exchange, Map params, String cmd, String antwort) throws IOException {
|
String httpResponseStr = getResponseString(params, cmd, antwort);
|
sendResponse(exchange, httpResponseStr);
|
}
|
|
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();
|
}
|
|
protected void sendResponse(HttpExchange t, String response) throws IOException {
|
t.sendResponseHeaders(200, response.length());
|
OutputStream os = t.getResponseBody();
|
os.write(response.getBytes());
|
os.close();
|
}
|
|
|
}
|