commit | author | age
|
9c7249
|
1 |
/* |
678b07
|
2 |
mini-server - Ein minimalistischer HTTP-Server |
U |
3 |
Copyright (C) 2021 Ulrich Hilger |
9c7249
|
4 |
|
678b07
|
5 |
This program is free software: you can redistribute it and/or modify |
U |
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. |
9c7249
|
9 |
|
678b07
|
10 |
This program is distributed in the hope that it will be useful, |
U |
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. |
9c7249
|
14 |
|
678b07
|
15 |
You should have received a copy of the GNU Affero General Public License |
U |
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
9c7249
|
17 |
*/ |
U |
18 |
|
|
19 |
package de.uhilger.minsrv; |
|
20 |
|
|
21 |
import com.sun.net.httpserver.HttpServer; |
|
22 |
import de.uhilger.minsrv.handler.StopServerHandler; |
|
23 |
import de.uhilger.minsrv.handler.FileHandler; |
|
24 |
import java.io.IOException; |
|
25 |
import java.util.logging.Logger; |
|
26 |
import java.net.InetSocketAddress; |
|
27 |
import java.util.concurrent.Executors; |
|
28 |
|
|
29 |
/** |
678b07
|
30 |
* Die Klasse Server stellt Methoden zur Ausführung eines |
U |
31 |
* HTTP-Servers bereit |
|
32 |
* |
8abbcf
|
33 |
* @author Ulrich Hilger |
678b07
|
34 |
* @version 0.1, 25.03.2021 |
9c7249
|
35 |
*/ |
U |
36 |
public class Server { |
|
37 |
|
|
38 |
private static final Logger logger = Logger.getLogger(Server.class.getName()); |
|
39 |
|
d0bb21
|
40 |
public static final String STR_SLASH = "/"; |
U |
41 |
|
|
42 |
|
9c7249
|
43 |
private int port; |
U |
44 |
|
d0bb21
|
45 |
private String ctx; |
678b07
|
46 |
|
d0bb21
|
47 |
/** |
U |
48 |
* Ein neues Objekt der Kalsse Server erzeugen |
|
49 |
* @param port der Port, über den dieser Server erreichbar sein soll |
|
50 |
*/ |
9c7249
|
51 |
public Server(int port) { |
U |
52 |
this.port = port; |
|
53 |
} |
|
54 |
|
678b07
|
55 |
/** |
U |
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 |
*/ |
9c7249
|
60 |
public void setPort(int port) { |
U |
61 |
this.port = port; |
|
62 |
} |
|
63 |
|
d0bb21
|
64 |
/** |
U |
65 |
* Den Namen des Kontexts angeben, über den dieser Server |
|
66 |
* erreichbar sein soll |
|
67 |
* @param ctxName |
|
68 |
*/ |
678b07
|
69 |
public void setContextName(String ctxName) { |
d0bb21
|
70 |
if(!ctxName.startsWith(STR_SLASH)) { |
U |
71 |
this.ctx = STR_SLASH + ctxName; |
|
72 |
} else { |
|
73 |
this.ctx = ctxName; |
|
74 |
} |
678b07
|
75 |
} |
U |
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 |
*/ |
9c7249
|
84 |
public void start() throws IOException { |
U |
85 |
logger.info("Server starting on port " + port); |
|
86 |
|
|
87 |
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); |
d0bb21
|
88 |
server.createContext(ctx + "/av", new FileHandler(App.getInitParameter(App.IP_WWW_DATA))); |
U |
89 |
server.createContext(ctx + "/server/stop", new StopServerHandler()); |
9c7249
|
90 |
server.setExecutor(Executors.newFixedThreadPool(20)); |
U |
91 |
server.start(); |
|
92 |
} |
|
93 |
|
|
94 |
} |