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 = "/"; |
03b95f
|
65 |
public static final String NEWLINE = "\n"; |
b379f5
|
66 |
|
c3b1d1
|
67 |
private int port; |
b379f5
|
68 |
|
c3b1d1
|
69 |
private String ctx; |
0c14c0
|
70 |
|
f70acb
|
71 |
//private HttpServer server; |
b379f5
|
72 |
|
c3b1d1
|
73 |
/** |
U |
74 |
* Ein neues Objekt der Kalsse Server erzeugen |
b379f5
|
75 |
* |
U |
76 |
* @param port der Port, über den dieser Server erreichbar sein soll |
c3b1d1
|
77 |
*/ |
U |
78 |
public Server(int port) { |
|
79 |
this.port = port; |
f70acb
|
80 |
} |
b379f5
|
81 |
|
c3b1d1
|
82 |
/** |
U |
83 |
* Den Port angeben, unter dem der Server erreichbar sein soll |
b379f5
|
84 |
* |
c3b1d1
|
85 |
* @param port der Port, unter dem der Server erreichbar sein soll |
U |
86 |
*/ |
|
87 |
public void setPort(int port) { |
|
88 |
this.port = port; |
|
89 |
} |
b379f5
|
90 |
|
c3b1d1
|
91 |
/** |
b379f5
|
92 |
* Den Namen des Kontexts angeben, über den dieser Server erreichbar sein |
U |
93 |
* soll |
|
94 |
* |
c3b1d1
|
95 |
* @param ctxName Name des Kontexts, unter dem der Server aufrufbar sein soll |
U |
96 |
*/ |
|
97 |
public void setContextName(String ctxName) { |
0e9cd3
|
98 |
if (!ctxName.startsWith(SLASH)) { |
U |
99 |
this.ctx = SLASH + ctxName; |
c3b1d1
|
100 |
} else { |
U |
101 |
this.ctx = ctxName; |
|
102 |
} |
|
103 |
} |
b379f5
|
104 |
|
c3b1d1
|
105 |
/** |
2597cd
|
106 |
* Die Endpunkte einrichten, unter denen die Dienste dieses |
b379f5
|
107 |
* Servers erreichbar sein sollen und den Server starten |
U |
108 |
* |
|
109 |
* @throws IOException wenn etwas schief geht, finden sich Angaben in diesem |
|
110 |
* Objekt |
|
111 |
* @throws java.lang.ClassNotFoundException |
c3b1d1
|
112 |
*/ |
f70acb
|
113 |
public void start(String wwwData, String conf) throws IOException, ClassNotFoundException { |
U |
114 |
ResourceBundle rb = ResourceBundle.getBundle(App.RB_NAME); |
|
115 |
logger.log(Level.INFO, rb.getString(RB_SERVER_START_MSG), Integer.toString(port)); |
b379f5
|
116 |
|
f70acb
|
117 |
//String wwwData = App.getInitParameter(rb.getString(App.RB_AP_WWW_DATA)); |
14baa3
|
118 |
File wwwDir = new File(wwwData); |
c3b1d1
|
119 |
|
f70acb
|
120 |
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); |
U |
121 |
server.createContext(ctx + rb.getString(RB_WEBROOT), new FileHandler(wwwDir.getAbsolutePath())); |
|
122 |
ablageorteEinklinken(server, rb, conf); |
|
123 |
server.createContext(ctx + rb.getString(RB_STORE), new StorageHandler(conf)); |
7f4414
|
124 |
MediaSteuerung ms = new MediaSteuerung(conf); |
U |
125 |
server.createContext(ctx + rb.getString(RB_STRG), ms); |
f70acb
|
126 |
server.createContext(ctx + rb.getString(RB_GSTRG), new GeraetSteuerung(conf)); |
7f4414
|
127 |
ListHandler lh = new ListHandler(conf); |
U |
128 |
lh.addPlaylistListener(ms); |
|
129 |
server.createContext(ctx + rb.getString(RB_ALIST), lh); |
5bf530
|
130 |
server.createContext(ctx + rb.getString(RB_STRM), new StreamHandler(conf)); |
f70acb
|
131 |
server.createContext(ctx + rb.getString(RB_STOP_SERVER), new StopServerHandler()); |
aeb43e
|
132 |
//server.setExecutor(Executors.newFixedThreadPool(20)); |
828703
|
133 |
server.setExecutor(Executors.newFixedThreadPool(5)); |
c3b1d1
|
134 |
server.start(); |
U |
135 |
} |
0c14c0
|
136 |
|
f70acb
|
137 |
/* |
0c14c0
|
138 |
public void ablageortEntfernen(String url) { |
U |
139 |
server.removeContext(ctx + url); |
|
140 |
} |
c3b1d1
|
141 |
|
f70acb
|
142 |
public void ablageortHinzufuegen(Ablageort ort, String conf) { |
0c14c0
|
143 |
server.createContext(ctx + ort.getUrl(), |
f70acb
|
144 |
new ListFileHandler(new File(ort.getOrt()).getAbsolutePath(), conf)); |
0c14c0
|
145 |
} |
f70acb
|
146 |
*/ |
0c14c0
|
147 |
|
f70acb
|
148 |
private void ablageorteEinklinken(HttpServer server, ResourceBundle rb, String conf) |
dfb7d3
|
149 |
throws ClassNotFoundException, IOException { |
081606
|
150 |
String typ = Ablageort.class.getSimpleName(); |
f70acb
|
151 |
FileStorage store = new FileStorage(conf); |
081606
|
152 |
List<String> orte = store.list(typ); |
U |
153 |
Iterator<String> i = orte.iterator(); |
|
154 |
while(i.hasNext()) { |
|
155 |
String ortName = i.next(); |
|
156 |
Entity e = store.read(typ, ortName); |
|
157 |
if(e instanceof Ablageort) { |
dfb7d3
|
158 |
Ablageort ablageort = (Ablageort) e; |
U |
159 |
logger.log(Level.FINE, "{0}{1}", new Object[]{ctx, ablageort.getUrl()}); |
|
160 |
logger.fine(ablageort.getOrt()); |
|
161 |
server.createContext(ctx + ablageort.getUrl(), |
f70acb
|
162 |
new ListFileHandler(new File(ablageort.getOrt()).getAbsolutePath(), conf)); |
081606
|
163 |
} |
U |
164 |
} |
b379f5
|
165 |
} |
c3b1d1
|
166 |
} |