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