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