Persoenliche Mediazentrale
ulrich
2021-04-10 005d7a23659592b32d2dce25ae0b08850f29a241
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;
86bbf7 22 import de.uhilger.mediaz.api.ListFileHandler;
e60cff 23 import de.uhilger.mediaz.api.ListHandler;
b56bb3 24 import de.uhilger.mediaz.api.MediaSteuerung;
2b6134 25 import de.uhilger.mediaz.api.StopServerHandler;
081606 26 import de.uhilger.mediaz.api.StorageHandler;
U 27 import de.uhilger.mediaz.store.FileStorage;
b379f5 28 import de.uhilger.mediaz.entity.Ablageort;
cfa858 29 import java.io.File;
c3b1d1 30 import java.io.IOException;
U 31 import java.util.logging.Logger;
32 import java.net.InetSocketAddress;
33 import java.util.concurrent.Executors;
2b6134 34 import java.util.logging.Level;
081606 35 import de.uhilger.mediaz.entity.Entity;
U 36 import java.util.Iterator;
37 import java.util.List;
c3b1d1 38
U 39 /**
b379f5 40  * Die Klasse Server stellt Methoden zur Ausf&uuml;hrung eines HTTP-Servers
U 41  * bereit
42  *
c3b1d1 43  * @author Ulrich Hilger
U 44  * @version 0.1, 25.03.2021
45  */
46 public class Server {
b379f5 47
c3b1d1 48   private static final Logger logger = Logger.getLogger(Server.class.getName());
b379f5 49
2b6134 50   public static final String RB_SERVER_START_MSG = "msgServerStart";
U 51   public static final String RB_WEBROOT = "webroot";
b379f5 52   public static final String RB_STORE = "store";
b56bb3 53   public static final String RB_STRG = "strg";
e60cff 54   public static final String RB_ALIST= "alist";
14baa3 55   //public static final String RB_UI_ROOT = "uiroot";
2b6134 56   public static final String RB_STOP_SERVER = "stopServer";
2597cd 57   //public static final String RB_ABLAGE_TEST = "testAblage";
U 58   //public static final String RB_STORE_TEST = "testStore";
0e9cd3 59   public static final String SLASH = "/";
b379f5 60
c3b1d1 61   private int port;
b379f5 62
c3b1d1 63   private String ctx;
b379f5 64
c3b1d1 65   /**
U 66    * Ein neues Objekt der Kalsse Server erzeugen
b379f5 67    *
U 68    * @param port der Port, &uuml;ber den dieser Server erreichbar sein soll
c3b1d1 69    */
U 70   public Server(int port) {
71     this.port = port;
72   }
b379f5 73
c3b1d1 74   /**
U 75    * Den Port angeben, unter dem der Server erreichbar sein soll
b379f5 76    *
c3b1d1 77    * @param port der Port, unter dem der Server erreichbar sein soll
U 78    */
79   public void setPort(int port) {
80     this.port = port;
81   }
b379f5 82
c3b1d1 83   /**
b379f5 84    * Den Namen des Kontexts angeben, &uuml;ber den dieser Server erreichbar sein
U 85    * soll
86    *
c3b1d1 87    * @param ctxName Name des Kontexts, unter dem der Server aufrufbar sein soll
U 88    */
89   public void setContextName(String ctxName) {
0e9cd3 90     if (!ctxName.startsWith(SLASH)) {
U 91       this.ctx = SLASH + ctxName;
c3b1d1 92     } else {
U 93       this.ctx = ctxName;
94     }
95   }
b379f5 96
c3b1d1 97   /**
2597cd 98    * Die Endpunkte einrichten, unter denen die Dienste dieses
b379f5 99    * Servers erreichbar sein sollen und den Server starten
U 100    *
101    * @throws IOException wenn etwas schief geht, finden sich Angaben in diesem
102    * Objekt
103    * @throws java.lang.ClassNotFoundException
c3b1d1 104    */
b379f5 105   public void start() throws IOException, ClassNotFoundException {
2b6134 106     logger.log(Level.INFO, App.getRs(RB_SERVER_START_MSG), Integer.toString(port));
b379f5 107
14baa3 108     String wwwData = App.getInitParameter(App.getRs(App.RB_AP_WWW_DATA));
U 109     File wwwDir = new File(wwwData);
c3b1d1 110
U 111     HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
dfb7d3 112     server.createContext(ctx + App.getRs(RB_WEBROOT), new FileHandler(wwwDir.getAbsolutePath()));
b379f5 113     ablageorteEinklinken(server);
081606 114     server.createContext(ctx + App.getRs(RB_STORE), new StorageHandler());
b56bb3 115     server.createContext(ctx + App.getRs(RB_STRG), new MediaSteuerung());
e60cff 116     server.createContext(ctx + App.getRs(RB_ALIST), new ListHandler());
2b6134 117     server.createContext(ctx + App.getRs(RB_STOP_SERVER), new StopServerHandler());
c3b1d1 118     server.setExecutor(Executors.newFixedThreadPool(20));
U 119     server.start();
120   }
121
dfb7d3 122   private void ablageorteEinklinken(HttpServer server) 
U 123               throws ClassNotFoundException, IOException {
081606 124     String typ = Ablageort.class.getSimpleName();
U 125     FileStorage store = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF)));
126     List<String> orte = store.list(typ);
127     Iterator<String> i = orte.iterator();
128     while(i.hasNext()) {
129       String ortName = i.next();
130       Entity e = store.read(typ, ortName);
131       if(e instanceof Ablageort) {
dfb7d3 132         Ablageort ablageort = (Ablageort) e;
U 133         logger.log(Level.FINE, "{0}{1}", new Object[]{ctx, ablageort.getUrl()});
134         logger.fine(ablageort.getOrt());
135         server.createContext(ctx + ablageort.getUrl(),  
136           new ListFileHandler(new File(ablageort.getOrt()).getAbsolutePath()));
081606 137       }
U 138     }
b379f5 139   }
c3b1d1 140 }