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; |
U |
22 |
import de.uhilger.mediaz.api.StopServerHandler; |
081606
|
23 |
import de.uhilger.mediaz.api.StorageHandler; |
U |
24 |
import de.uhilger.mediaz.store.FileStorage; |
b379f5
|
25 |
import de.uhilger.mediaz.entity.Ablageort; |
cfa858
|
26 |
import java.io.File; |
c3b1d1
|
27 |
import java.io.IOException; |
U |
28 |
import java.util.logging.Logger; |
|
29 |
import java.net.InetSocketAddress; |
|
30 |
import java.util.concurrent.Executors; |
2b6134
|
31 |
import java.util.logging.Level; |
081606
|
32 |
import de.uhilger.mediaz.entity.Entity; |
U |
33 |
import java.util.Iterator; |
|
34 |
import java.util.List; |
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"; |
2597cd
|
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 |
/** |
2597cd
|
94 |
* Die Endpunkte einrichten, unter denen die Dienste dieses |
b379f5
|
95 |
* Servers erreichbar sein sollen und den Server starten |
U |
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); |
081606
|
113 |
server.createContext(ctx + App.getRs(RB_STORE), new StorageHandler()); |
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()); |
2597cd
|
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 { |
081606
|
123 |
String typ = Ablageort.class.getSimpleName(); |
U |
124 |
FileStorage store = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); |
|
125 |
List<String> orte = store.list(typ); |
|
126 |
Iterator<String> i = orte.iterator(); |
|
127 |
while(i.hasNext()) { |
|
128 |
String ortName = i.next(); |
|
129 |
Entity e = store.read(typ, ortName); |
|
130 |
if(e instanceof Ablageort) { |
|
131 |
Ablageort ort = (Ablageort) e; |
|
132 |
Ablageort ablageort = (Ablageort) e; |
|
133 |
server.createContext(ctx + ablageort.getUrl(), |
|
134 |
new FileHandler(new File(ablageort.getOrt()).getAbsolutePath())); |
|
135 |
} |
|
136 |
} |
b379f5
|
137 |
} |
c3b1d1
|
138 |
} |