-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyServer.java
133 lines (107 loc) · 4.53 KB
/
MyServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.example.jetty;
import java.util.ResourceBundle;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.server.handler.ShutdownHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.glassfish.jersey.servlet.ServletContainer;
import static org.eclipse.jetty.servlet.ServletContextHandler.NO_SESSIONS;
import org.glassfish.jersey.server.ResourceConfig;
/**
* Jetty Server.
*
* @author atsushihondoh
*/
@Slf4j
@Data
public class MyServer implements Runnable {
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
private Server server;
private int port;
private String shutdownToken;
private final ResourceBundle RB = ResourceBundle.getBundle("app");
@Override
public void run() {
// -- Server
ServletContextHandler servletContextHandler = new ServletContextHandler(NO_SESSIONS);
servletContextHandler.setContextPath("/");
// -- The handler handls dynamic(REST) contents.
// Set Jersey ServletContainer
ResourceConfig resourceConfig = new MyResourceConfig();
ServletHolder servletHolder = new ServletHolder(new ServletContainer(resourceConfig));
servletHolder.setInitOrder(0);
servletContextHandler.addServlet(servletHolder, "/api/*");
// Max Upload Size
servletContextHandler.setMaxFormContentSize(1024 * 1024 * 1024);
// -- The handler handls static(HTML,CSS,JS) contents.
final ResourceHandler resourceHandler = new ResourceHandler();
// Where is the static contents?
// The following setting is "[JAR]/htdocs". You can specify a directory on the file system, like "/opt/htpdcs".
String webroot = RB.getString("webroot");
if (webroot.indexOf("${classpath}") > 0) {
String classpathRelative = webroot.replaceAll("${classpath}", "").replaceAll("\\/", "");
webroot = MyServer.class.getClassLoader().getResource(classpathRelative).toExternalForm();
}
log.info("Static contetns = {}", webroot);
resourceHandler.setResourceBase(webroot);
// Don't list files.
resourceHandler.setDirectoriesListed(false);
// Welcome file
resourceHandler.setWelcomeFiles(new String[]{"index.html"});
// Don't use cache
resourceHandler.setCacheControl("no-store,no-cache,must-revalidate");
// -- make handlers list
// You must add the hander which handls static contents FIRST.
HandlerList handlerList = new HandlerList();
// shutdown from a valid POST Request
// ex. curl -X POST http://localhost:8080/shutdown?token=imadmin
handlerList.addHandler(new ShutdownHandler(shutdownToken, false, true));
// static contents (HTML,CSS,JS)
handlerList.addHandler(resourceHandler);
// dynamic contents (REST)
handlerList.addHandler(servletContextHandler);
// -- Server
server = new Server(port);
// Bind handlers to the server.
server.setHandler(handlerList);
// Settings about HTTP
final HttpConfiguration httpConfig = new HttpConfiguration();
// Don't show server version on the HTTP-Header
httpConfig.setSendServerVersion(false);
final HttpConnectionFactory httpConnFactory = new HttpConnectionFactory(httpConfig);
final ServerConnector httpConnector = new ServerConnector(server, httpConnFactory);
httpConnector.setPort(port);
server.setConnectors(new Connector[]{httpConnector});
try {
server.start();
log.info("Server Start : Success");
server.join();
log.info("Server Stop : Success");
} catch (Exception ex) {
log.info("Server Start : Fail", ex);
} finally {
server.destroy();
log.info("Server Destroyed");
}
}
public void stop() {
try {
server.stop();
log.info("Request Server Stop : Success");
} catch (Exception ex) {
log.error("Request Server Stop : Fail", ex);
}
}
}