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