Persoenliche Mediazentrale
ulrich
2021-04-21 5c621434b008d1671accfc4b9c9c9016a256fd9f
commit | author | age
c3b1d1 1 /*
043915 2   Mediazentrale - Personal Media Center
c3b1d1 3   Copyright (C) 2021  Ulrich Hilger
U 4
5   This program is free software: you can redistribute it and/or modify
6   it under the terms of the GNU Affero General Public License as
7   published by the Free Software Foundation, either version 3 of the
8   License, or (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU Affero General Public License for more details.
14
15   You should have received a copy of the GNU Affero General Public License
16   along with this program.  If not, see <https://www.gnu.org/licenses/>.
043915 17  */
c3b1d1 18 package de.uhilger.mediaz;
U 19
20 import com.sun.net.httpserver.HttpServer;
2b6134 21 import de.uhilger.mediaz.api.FileHandler;
849ee2 22 import de.uhilger.mediaz.api.GeraetSteuerung;
86bbf7 23 import de.uhilger.mediaz.api.ListFileHandler;
e60cff 24 import de.uhilger.mediaz.api.ListHandler;
b56bb3 25 import de.uhilger.mediaz.api.MediaSteuerung;
2b6134 26 import de.uhilger.mediaz.api.StopServerHandler;
081606 27 import de.uhilger.mediaz.api.StorageHandler;
U 28 import de.uhilger.mediaz.store.FileStorage;
b379f5 29 import de.uhilger.mediaz.entity.Ablageort;
cfa858 30 import java.io.File;
c3b1d1 31 import java.io.IOException;
U 32 import java.util.logging.Logger;
33 import java.net.InetSocketAddress;
34 import java.util.concurrent.Executors;
2b6134 35 import java.util.logging.Level;
081606 36 import de.uhilger.mediaz.entity.Entity;
U 37 import java.util.Iterator;
38 import java.util.List;
c3b1d1 39
U 40 /**
b379f5 41  * Die Klasse Server stellt Methoden zur Ausf&uuml;hrung eines HTTP-Servers
U 42  * bereit
43  *
c3b1d1 44  * @author Ulrich Hilger
U 45  * @version 0.1, 25.03.2021
46  */
47 public class Server {
b379f5 48
c3b1d1 49   private static final Logger logger = Logger.getLogger(Server.class.getName());
b379f5 50
2b6134 51   public static final String RB_SERVER_START_MSG = "msgServerStart";
U 52   public static final String RB_WEBROOT = "webroot";
b379f5 53   public static final String RB_STORE = "store";
b56bb3 54   public static final String RB_STRG = "strg";
849ee2 55   public static final String RB_GSTRG = "gstrg";
e60cff 56   public static final String RB_ALIST= "alist";
14baa3 57   //public static final String RB_UI_ROOT = "uiroot";
2b6134 58   public static final String RB_STOP_SERVER = "stopServer";
2597cd 59   //public static final String RB_ABLAGE_TEST = "testAblage";
U 60   //public static final String RB_STORE_TEST = "testStore";
0e9cd3 61   public static final String SLASH = "/";
b379f5 62
c3b1d1 63   private int port;
b379f5 64
c3b1d1 65   private String ctx;
b379f5 66
c3b1d1 67   /**
U 68    * Ein neues Objekt der Kalsse Server erzeugen
b379f5 69    *
U 70    * @param port der Port, &uuml;ber den dieser Server erreichbar sein soll
c3b1d1 71    */
U 72   public Server(int port) {
73     this.port = port;
74   }
b379f5 75
c3b1d1 76   /**
U 77    * Den Port angeben, unter dem der Server erreichbar sein soll
b379f5 78    *
c3b1d1 79    * @param port der Port, unter dem der Server erreichbar sein soll
U 80    */
81   public void setPort(int port) {
82     this.port = port;
83   }
b379f5 84
c3b1d1 85   /**
b379f5 86    * Den Namen des Kontexts angeben, &uuml;ber den dieser Server erreichbar sein
U 87    * soll
88    *
c3b1d1 89    * @param ctxName Name des Kontexts, unter dem der Server aufrufbar sein soll
U 90    */
91   public void setContextName(String ctxName) {
0e9cd3 92     if (!ctxName.startsWith(SLASH)) {
U 93       this.ctx = SLASH + ctxName;
c3b1d1 94     } else {
U 95       this.ctx = ctxName;
96     }
97   }
b379f5 98
c3b1d1 99   /**
2597cd 100    * Die Endpunkte einrichten, unter denen die Dienste dieses
b379f5 101    * Servers erreichbar sein sollen und den Server starten
U 102    *
103    * @throws IOException wenn etwas schief geht, finden sich Angaben in diesem
104    * Objekt
105    * @throws java.lang.ClassNotFoundException
c3b1d1 106    */
b379f5 107   public void start() throws IOException, ClassNotFoundException {
2b6134 108     logger.log(Level.INFO, App.getRs(RB_SERVER_START_MSG), Integer.toString(port));
b379f5 109
14baa3 110     String wwwData = App.getInitParameter(App.getRs(App.RB_AP_WWW_DATA));
U 111     File wwwDir = new File(wwwData);
c3b1d1 112
U 113     HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
dfb7d3 114     server.createContext(ctx + App.getRs(RB_WEBROOT), new FileHandler(wwwDir.getAbsolutePath()));
b379f5 115     ablageorteEinklinken(server);
081606 116     server.createContext(ctx + App.getRs(RB_STORE), new StorageHandler());
b56bb3 117     server.createContext(ctx + App.getRs(RB_STRG), new MediaSteuerung());
849ee2 118     server.createContext(ctx + App.getRs(RB_GSTRG), new GeraetSteuerung());
e60cff 119     server.createContext(ctx + App.getRs(RB_ALIST), new ListHandler());
2b6134 120     server.createContext(ctx + App.getRs(RB_STOP_SERVER), new StopServerHandler());
c3b1d1 121     server.setExecutor(Executors.newFixedThreadPool(20));
U 122     server.start();
123   }
124
dfb7d3 125   private void ablageorteEinklinken(HttpServer server) 
U 126               throws ClassNotFoundException, IOException {
081606 127     String typ = Ablageort.class.getSimpleName();
U 128     FileStorage store = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF)));
129     List<String> orte = store.list(typ);
130     Iterator<String> i = orte.iterator();
131     while(i.hasNext()) {
132       String ortName = i.next();
133       Entity e = store.read(typ, ortName);
134       if(e instanceof Ablageort) {
dfb7d3 135         Ablageort ablageort = (Ablageort) e;
U 136         logger.log(Level.FINE, "{0}{1}", new Object[]{ctx, ablageort.getUrl()});
137         logger.fine(ablageort.getOrt());
138         server.createContext(ctx + ablageort.getUrl(),  
139           new ListFileHandler(new File(ablageort.getOrt()).getAbsolutePath()));
081606 140       }
U 141     }
b379f5 142   }
c3b1d1 143 }