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