/*
|
Mediazentrale - 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.mediaz;
|
|
import com.sun.net.httpserver.HttpServer;
|
import de.uhilger.mediaz.api.FileHandler;
|
import de.uhilger.mediaz.api.ListFileHandler;
|
import de.uhilger.mediaz.api.ListHandler;
|
import de.uhilger.mediaz.api.MediaSteuerung;
|
import de.uhilger.mediaz.api.StopServerHandler;
|
import de.uhilger.mediaz.api.StorageHandler;
|
import de.uhilger.mediaz.store.FileStorage;
|
import de.uhilger.mediaz.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.mediaz.entity.Entity;
|
import java.util.Iterator;
|
import java.util.List;
|
|
/**
|
* 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_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;
|
|
/**
|
* 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() throws IOException, ClassNotFoundException {
|
logger.log(Level.INFO, App.getRs(RB_SERVER_START_MSG), Integer.toString(port));
|
|
String wwwData = App.getInitParameter(App.getRs(App.RB_AP_WWW_DATA));
|
File wwwDir = new File(wwwData);
|
|
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
|
server.createContext(ctx + App.getRs(RB_WEBROOT), new FileHandler(wwwDir.getAbsolutePath()));
|
ablageorteEinklinken(server);
|
server.createContext(ctx + App.getRs(RB_STORE), new StorageHandler());
|
server.createContext(ctx + App.getRs(RB_STRG), new MediaSteuerung());
|
server.createContext(ctx + App.getRs(RB_ALIST), new ListHandler());
|
server.createContext(ctx + App.getRs(RB_STOP_SERVER), new StopServerHandler());
|
server.setExecutor(Executors.newFixedThreadPool(20));
|
server.start();
|
}
|
|
private void ablageorteEinklinken(HttpServer server)
|
throws ClassNotFoundException, IOException {
|
String typ = Ablageort.class.getSimpleName();
|
FileStorage store = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_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()));
|
}
|
}
|
}
|
}
|