| | |
| | | /* |
| | | mini-server - Ein minimalistischer HTTP-Server |
| | | Mediazentrale - Personal Media Center |
| | | Copyright (C) 2021 Ulrich Hilger |
| | | |
| | | This program is free software: you can redistribute it and/or modify |
| | |
| | | |
| | | 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.handler.FileHandler; |
| | | import de.uhilger.mediaz.handler.StopServerHandler; |
| | | import de.uhilger.mediaz.api.FileHandler; |
| | | import de.uhilger.mediaz.api.GeraetSteuerung; |
| | | 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 |
| | | * |
| | | * 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 STR_SLASH = "/"; |
| | | public static final String CMD_SERVER_STOP = "/server/stop"; |
| | | |
| | | |
| | | 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 |
| | | * |
| | | * @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 |
| | | * 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(STR_SLASH)) { |
| | | this.ctx = STR_SLASH + ctxName; |
| | | if (!ctxName.startsWith(SLASH)) { |
| | | this.ctx = SLASH + ctxName; |
| | | } else { |
| | | this.ctx = ctxName; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Die Endpunkte ('Context'e) 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 |
| | | */ |
| | | public void start() throws IOException { |
| | | logger.info("Server starting on port " + port); |
| | | |
| | | HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); |
| | | server.createContext(ctx + STR_SLASH, new FileHandler(App.getInitParameter(App.IP_WWW_DATA))); |
| | | server.createContext(ctx + CMD_SERVER_STOP, new StopServerHandler()); |
| | | /** |
| | | * 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); |
| | | |
| | | 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_GSTRG), new GeraetSteuerung()); |
| | | 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(); |
| | | } |
| | | |
| | | public void ablageortEntfernen(String url) { |
| | | server.removeContext(ctx + url); |
| | | } |
| | | |
| | | public void ablageortHinzufuegen(Ablageort ort) { |
| | | server.createContext(ctx + ort.getUrl(), |
| | | new ListFileHandler(new File(ort.getOrt()).getAbsolutePath())); |
| | | } |
| | | |
| | | 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())); |
| | | } |
| | | } |
| | | } |
| | | } |