Persoenliche Mediazentrale
ulrich
2021-04-23 0c14c00bab23e52508e0d30ef5fdf82399096612
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;
0c14c0 66   
U 67   private HttpServer server;
b379f5 68
c3b1d1 69   /**
U 70    * Ein neues Objekt der Kalsse Server erzeugen
b379f5 71    *
U 72    * @param port der Port, &uuml;ber den dieser Server erreichbar sein soll
c3b1d1 73    */
U 74   public Server(int port) {
75     this.port = port;
76   }
b379f5 77
c3b1d1 78   /**
U 79    * Den Port angeben, unter dem der Server erreichbar sein soll
b379f5 80    *
c3b1d1 81    * @param port der Port, unter dem der Server erreichbar sein soll
U 82    */
83   public void setPort(int port) {
84     this.port = port;
85   }
b379f5 86
c3b1d1 87   /**
b379f5 88    * Den Namen des Kontexts angeben, &uuml;ber den dieser Server erreichbar sein
U 89    * soll
90    *
c3b1d1 91    * @param ctxName Name des Kontexts, unter dem der Server aufrufbar sein soll
U 92    */
93   public void setContextName(String ctxName) {
0e9cd3 94     if (!ctxName.startsWith(SLASH)) {
U 95       this.ctx = SLASH + ctxName;
c3b1d1 96     } else {
U 97       this.ctx = ctxName;
98     }
99   }
b379f5 100
c3b1d1 101   /**
2597cd 102    * Die Endpunkte einrichten, unter denen die Dienste dieses
b379f5 103    * Servers erreichbar sein sollen und den Server starten
U 104    *
105    * @throws IOException wenn etwas schief geht, finden sich Angaben in diesem
106    * Objekt
107    * @throws java.lang.ClassNotFoundException
c3b1d1 108    */
b379f5 109   public void start() throws IOException, ClassNotFoundException {
2b6134 110     logger.log(Level.INFO, App.getRs(RB_SERVER_START_MSG), Integer.toString(port));
b379f5 111
14baa3 112     String wwwData = App.getInitParameter(App.getRs(App.RB_AP_WWW_DATA));
U 113     File wwwDir = new File(wwwData);
c3b1d1 114
0c14c0 115     server = HttpServer.create(new InetSocketAddress(port), 0);
dfb7d3 116     server.createContext(ctx + App.getRs(RB_WEBROOT), new FileHandler(wwwDir.getAbsolutePath()));
b379f5 117     ablageorteEinklinken(server);
081606 118     server.createContext(ctx + App.getRs(RB_STORE), new StorageHandler());
b56bb3 119     server.createContext(ctx + App.getRs(RB_STRG), new MediaSteuerung());
849ee2 120     server.createContext(ctx + App.getRs(RB_GSTRG), new GeraetSteuerung());
e60cff 121     server.createContext(ctx + App.getRs(RB_ALIST), new ListHandler());
2b6134 122     server.createContext(ctx + App.getRs(RB_STOP_SERVER), new StopServerHandler());
c3b1d1 123     server.setExecutor(Executors.newFixedThreadPool(20));
U 124     server.start();
125   }
0c14c0 126   
U 127   public void ablageortEntfernen(String url) {
128     server.removeContext(ctx + url);
129   }
c3b1d1 130
0c14c0 131   public void ablageortHinzufuegen(Ablageort ort) {
U 132     server.createContext(ctx + ort.getUrl(),  
133           new ListFileHandler(new File(ort.getOrt()).getAbsolutePath()));
134   }
135   
dfb7d3 136   private void ablageorteEinklinken(HttpServer server) 
U 137               throws ClassNotFoundException, IOException {
081606 138     String typ = Ablageort.class.getSimpleName();
U 139     FileStorage store = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF)));
140     List<String> orte = store.list(typ);
141     Iterator<String> i = orte.iterator();
142     while(i.hasNext()) {
143       String ortName = i.next();
144       Entity e = store.read(typ, ortName);
145       if(e instanceof Ablageort) {
dfb7d3 146         Ablageort ablageort = (Ablageort) e;
U 147         logger.log(Level.FINE, "{0}{1}", new Object[]{ctx, ablageort.getUrl()});
148         logger.fine(ablageort.getOrt());
149         server.createContext(ctx + ablageort.getUrl(),  
150           new ListFileHandler(new File(ablageort.getOrt()).getAbsolutePath()));
081606 151       }
U 152     }
b379f5 153   }
c3b1d1 154 }