/*
|
Tango - Personal Media Center
|
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.tango;
|
|
import com.sun.net.httpserver.HttpServer;
|
import de.uhilger.tango.api.FileHandler;
|
import de.uhilger.tango.api.GeraetSteuerung;
|
import de.uhilger.tango.api.ListFileHandler;
|
import de.uhilger.tango.api.ListHandler;
|
import de.uhilger.tango.api.MediaSteuerung;
|
import de.uhilger.tango.api.StopServerHandler;
|
import de.uhilger.tango.api.StorageHandler;
|
import de.uhilger.tango.store.FileStorage;
|
import de.uhilger.tango.entity.Ablageort;
|
import java.io.File;
|
import java.io.IOException;
|
import java.util.logging.Logger;
|
import java.net.InetSocketAddress;
|
import java.util.concurrent.Executors;
|
import java.util.logging.Level;
|
import de.uhilger.tango.entity.Entity;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.ResourceBundle;
|
|
/**
|
* Die Klasse Server stellt Methoden zur Ausführung eines HTTP-Servers
|
* bereit
|
*
|
* @author Ulrich Hilger
|
* @version 0.1, 25.03.2021
|
*/
|
public class Server {
|
|
private static final Logger logger = Logger.getLogger(Server.class.getName());
|
|
public static final String RB_SERVER_START_MSG = "msgServerStart";
|
public static final String RB_WEBROOT = "webroot";
|
public static final String RB_STORE = "store";
|
public static final String RB_STRG = "strg";
|
public static final String RB_GSTRG = "gstrg";
|
public static final String RB_ALIST= "alist";
|
//public static final String RB_UI_ROOT = "uiroot";
|
public static final String RB_STOP_SERVER = "stopServer";
|
//public static final String RB_ABLAGE_TEST = "testAblage";
|
//public static final String RB_STORE_TEST = "testStore";
|
public static final String SLASH = "/";
|
|
private int port;
|
|
private String ctx;
|
|
//private HttpServer server;
|
|
/**
|
* Ein neues Objekt der Kalsse Server erzeugen
|
*
|
* @param port der Port, über den dieser Server erreichbar sein soll
|
*/
|
public Server(int port) {
|
this.port = port;
|
}
|
|
/**
|
* Den Port angeben, unter dem der Server erreichbar sein soll
|
*
|
* @param port der Port, unter dem der Server erreichbar sein soll
|
*/
|
public void setPort(int port) {
|
this.port = port;
|
}
|
|
/**
|
* Den Namen des Kontexts angeben, über den dieser Server erreichbar sein
|
* soll
|
*
|
* @param ctxName Name des Kontexts, unter dem der Server aufrufbar sein soll
|
*/
|
public void setContextName(String ctxName) {
|
if (!ctxName.startsWith(SLASH)) {
|
this.ctx = SLASH + ctxName;
|
} else {
|
this.ctx = ctxName;
|
}
|
}
|
|
/**
|
* Die Endpunkte einrichten, unter denen die Dienste dieses
|
* Servers erreichbar sein sollen und den Server starten
|
*
|
* @throws IOException wenn etwas schief geht, finden sich Angaben in diesem
|
* Objekt
|
* @throws java.lang.ClassNotFoundException
|
*/
|
public void start(String wwwData, String conf) throws IOException, ClassNotFoundException {
|
ResourceBundle rb = ResourceBundle.getBundle(App.RB_NAME);
|
logger.log(Level.INFO, rb.getString(RB_SERVER_START_MSG), Integer.toString(port));
|
|
//String wwwData = App.getInitParameter(rb.getString(App.RB_AP_WWW_DATA));
|
File wwwDir = new File(wwwData);
|
|
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
|
server.createContext(ctx + rb.getString(RB_WEBROOT), new FileHandler(wwwDir.getAbsolutePath()));
|
ablageorteEinklinken(server, rb, conf);
|
server.createContext(ctx + rb.getString(RB_STORE), new StorageHandler(conf));
|
server.createContext(ctx + rb.getString(RB_STRG), new MediaSteuerung(conf));
|
server.createContext(ctx + rb.getString(RB_GSTRG), new GeraetSteuerung(conf));
|
server.createContext(ctx + rb.getString(RB_ALIST), new ListHandler(conf));
|
server.createContext(ctx + rb.getString(RB_STOP_SERVER), new StopServerHandler());
|
//server.setExecutor(Executors.newFixedThreadPool(20));
|
server.setExecutor(Executors.newCachedThreadPool());
|
server.start();
|
}
|
|
/*
|
public void ablageortEntfernen(String url) {
|
server.removeContext(ctx + url);
|
}
|
|
public void ablageortHinzufuegen(Ablageort ort, String conf) {
|
server.createContext(ctx + ort.getUrl(),
|
new ListFileHandler(new File(ort.getOrt()).getAbsolutePath(), conf));
|
}
|
*/
|
|
private void ablageorteEinklinken(HttpServer server, ResourceBundle rb, String conf)
|
throws ClassNotFoundException, IOException {
|
String typ = Ablageort.class.getSimpleName();
|
FileStorage store = new FileStorage(conf);
|
List<String> orte = store.list(typ);
|
Iterator<String> i = orte.iterator();
|
while(i.hasNext()) {
|
String ortName = i.next();
|
Entity e = store.read(typ, ortName);
|
if(e instanceof Ablageort) {
|
Ablageort ablageort = (Ablageort) e;
|
logger.log(Level.FINE, "{0}{1}", new Object[]{ctx, ablageort.getUrl()});
|
logger.fine(ablageort.getOrt());
|
server.createContext(ctx + ablageort.getUrl(),
|
new ListFileHandler(new File(ablageort.getOrt()).getAbsolutePath(), conf));
|
}
|
}
|
}
|
}
|