Persoenliche Mediazentrale
ulrich
2021-04-04 cfa85894465dbf2d286e083d962babdf14641582
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;
d86ba2 21 import de.uhilger.mediaz.api.AblageTestHandler;
2b6134 22 import de.uhilger.mediaz.api.FileHandler;
U 23 import de.uhilger.mediaz.api.StopServerHandler;
d86ba2 24 import de.uhilger.mediaz.api.StoreTestHandler;
cfa858 25 import java.io.File;
c3b1d1 26 import java.io.IOException;
U 27 import java.util.logging.Logger;
28 import java.net.InetSocketAddress;
29 import java.util.concurrent.Executors;
2b6134 30 import java.util.logging.Level;
c3b1d1 31
U 32 /**
33  * Die Klasse Server stellt Methoden zur Ausf&uuml;hrung eines 
34  * HTTP-Servers bereit
35  * 
36  * @author Ulrich Hilger
37  * @version 0.1, 25.03.2021
38  */
39 public class Server {
40   
41   private static final Logger logger = Logger.getLogger(Server.class.getName());
42   
2b6134 43   public static final String RB_SERVER_START_MSG = "msgServerStart";
U 44   public static final String RB_WEBROOT = "webroot";
cfa858 45   public static final String RB_UI_ROOT = "uiroot";
2b6134 46   public static final String RB_STOP_SERVER = "stopServer";
d86ba2 47   public static final String RB_ABLAGE_TEST = "testAblage";
U 48   public static final String RB_STORE_TEST = "testStore";
2b6134 49   public static final String RB_SLASH = "slash";
c3b1d1 50   
U 51   private int port;
52   
53   private String ctx;
54   
55   /**
56    * Ein neues Objekt der Kalsse Server erzeugen
57    * @param port  der Port, &uuml;ber den dieser Server erreichbar sein soll
58    */
59   public Server(int port) {
60     this.port = port;
61   }
62   
63   /**
64    * Den Port angeben, unter dem der Server erreichbar sein soll
65    * 
66    * @param port der Port, unter dem der Server erreichbar sein soll
67    */
68   public void setPort(int port) {
69     this.port = port;
70   }
71   
72   /**
73    * Den Namen des Kontexts angeben, &uuml;ber den dieser Server 
74    * erreichbar sein soll
75    * @param ctxName Name des Kontexts, unter dem der Server aufrufbar sein soll
76    */
77   public void setContextName(String ctxName) {
2b6134 78     String slash = App.getRs(RB_SLASH);
U 79     if(!ctxName.startsWith(slash)) {
80       this.ctx = slash + ctxName;
c3b1d1 81     } else {
U 82       this.ctx = ctxName;
83     }
84   }
85   
86   /**
87    * Die Endpunkte ('Context'e) einrichten, unter denen die Dienste 
88    * dieses Servers erreichbar sein sollen und den Server starten
89    * 
90    * @throws IOException wenn etwas schief geht, finden sich Angaben 
91    * in diesem Objekt 
92    */
93   public void start() throws IOException {
2b6134 94     logger.log(Level.INFO, App.getRs(RB_SERVER_START_MSG), Integer.toString(port));
cfa858 95     
U 96     String ui = App.getInitParameter(App.getRs(App.RB_AP_UI));
97     
98     File uiDir = new File(ui);
c3b1d1 99
U 100     HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
2b6134 101     server.createContext(ctx + App.getRs(RB_WEBROOT), new FileHandler(App.getInitParameter(App.getRs(App.RB_AP_WWW_DATA))));
cfa858 102     server.createContext(ctx + App.getRs(RB_UI_ROOT), new FileHandler(uiDir.getAbsolutePath()));    
2b6134 103     server.createContext(ctx + App.getRs(RB_STOP_SERVER), new StopServerHandler());
d86ba2 104     server.createContext(ctx + App.getRs(RB_ABLAGE_TEST), new AblageTestHandler());
U 105     server.createContext(ctx + App.getRs(RB_STORE_TEST), new StoreTestHandler());
c3b1d1 106     server.setExecutor(Executors.newFixedThreadPool(20));
U 107     server.start();
108   }
109
110 }