commit | author | age
|
c3b1d1
|
1 |
/* |
U |
2 |
mini-server - Ein minimalistischer HTTP-Server |
|
3 |
Copyright (C) 2021 Ulrich Hilger |
|
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/>. |
|
17 |
*/ |
|
18 |
|
|
19 |
package de.uhilger.mediaz; |
|
20 |
|
|
21 |
import com.sun.net.httpserver.HttpServer; |
|
22 |
import de.uhilger.mediaz.handler.FileHandler; |
|
23 |
import de.uhilger.mediaz.handler.StopServerHandler; |
|
24 |
import java.io.IOException; |
|
25 |
import java.util.logging.Logger; |
|
26 |
import java.net.InetSocketAddress; |
|
27 |
import java.util.concurrent.Executors; |
|
28 |
|
|
29 |
/** |
|
30 |
* Die Klasse Server stellt Methoden zur Ausführung eines |
|
31 |
* HTTP-Servers bereit |
|
32 |
* |
|
33 |
* @author Ulrich Hilger |
|
34 |
* @version 0.1, 25.03.2021 |
|
35 |
*/ |
|
36 |
public class Server { |
|
37 |
|
|
38 |
private static final Logger logger = Logger.getLogger(Server.class.getName()); |
|
39 |
|
|
40 |
public static final String STR_SLASH = "/"; |
|
41 |
public static final String CMD_SERVER_STOP = "/server/stop"; |
|
42 |
|
|
43 |
private int port; |
|
44 |
|
|
45 |
private String ctx; |
|
46 |
|
|
47 |
/** |
|
48 |
* Ein neues Objekt der Kalsse Server erzeugen |
|
49 |
* @param port der Port, über den dieser Server erreichbar sein soll |
|
50 |
*/ |
|
51 |
public Server(int port) { |
|
52 |
this.port = port; |
|
53 |
} |
|
54 |
|
|
55 |
/** |
|
56 |
* Den Port angeben, unter dem der Server erreichbar sein soll |
|
57 |
* |
|
58 |
* @param port der Port, unter dem der Server erreichbar sein soll |
|
59 |
*/ |
|
60 |
public void setPort(int port) { |
|
61 |
this.port = port; |
|
62 |
} |
|
63 |
|
|
64 |
/** |
|
65 |
* Den Namen des Kontexts angeben, über den dieser Server |
|
66 |
* erreichbar sein soll |
|
67 |
* @param ctxName Name des Kontexts, unter dem der Server aufrufbar sein soll |
|
68 |
*/ |
|
69 |
public void setContextName(String ctxName) { |
|
70 |
if(!ctxName.startsWith(STR_SLASH)) { |
|
71 |
this.ctx = STR_SLASH + ctxName; |
|
72 |
} else { |
|
73 |
this.ctx = ctxName; |
|
74 |
} |
|
75 |
} |
|
76 |
|
|
77 |
/** |
|
78 |
* Die Endpunkte ('Context'e) einrichten, unter denen die Dienste |
|
79 |
* dieses Servers erreichbar sein sollen und den Server starten |
|
80 |
* |
|
81 |
* @throws IOException wenn etwas schief geht, finden sich Angaben |
|
82 |
* in diesem Objekt |
|
83 |
*/ |
|
84 |
public void start() throws IOException { |
|
85 |
logger.info("Server starting on port " + port); |
|
86 |
|
|
87 |
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); |
|
88 |
server.createContext(ctx + STR_SLASH, new FileHandler(App.getInitParameter(App.IP_WWW_DATA))); |
|
89 |
server.createContext(ctx + CMD_SERVER_STOP, new StopServerHandler()); |
|
90 |
server.setExecutor(Executors.newFixedThreadPool(20)); |
|
91 |
server.start(); |
|
92 |
} |
|
93 |
|
|
94 |
} |