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