|
19 | 19 | package de.rwth.idsg.steve; |
20 | 20 |
|
21 | 21 | import lombok.extern.slf4j.Slf4j; |
| 22 | +import org.eclipse.jetty.ee10.webapp.WebAppContext; |
22 | 23 | import org.eclipse.jetty.http.HttpScheme; |
23 | 24 | import org.eclipse.jetty.http.HttpVersion; |
24 | 25 | import org.eclipse.jetty.server.ForwardedRequestCustomizer; |
| 26 | +import org.eclipse.jetty.server.Handler; |
25 | 27 | import org.eclipse.jetty.server.HttpConfiguration; |
26 | 28 | import org.eclipse.jetty.server.HttpConnectionFactory; |
27 | 29 | import org.eclipse.jetty.server.SecureRequestCustomizer; |
28 | 30 | import org.eclipse.jetty.server.Server; |
29 | 31 | import org.eclipse.jetty.server.ServerConnector; |
30 | 32 | import org.eclipse.jetty.server.SslConnectionFactory; |
| 33 | +import org.eclipse.jetty.server.handler.gzip.GzipHandler; |
31 | 34 | import org.eclipse.jetty.util.ssl.SslContextFactory; |
32 | 35 | import org.eclipse.jetty.util.thread.QueuedThreadPool; |
33 | 36 | import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler; |
| 37 | +import org.springframework.core.io.ClassPathResource; |
34 | 38 |
|
| 39 | +import java.io.IOException; |
35 | 40 | import java.util.concurrent.TimeUnit; |
36 | 41 |
|
37 | 42 | import static de.rwth.idsg.steve.SteveConfiguration.CONFIG; |
|
44 | 49 | public class JettyServer { |
45 | 50 |
|
46 | 51 | private Server server; |
47 | | - private SteveAppContext steveAppContext; |
48 | 52 |
|
49 | 53 | private static final int MIN_THREADS = 4; |
50 | 54 | private static final int MAX_THREADS = 50; |
51 | 55 |
|
52 | 56 | private static final long STOP_TIMEOUT = TimeUnit.SECONDS.toMillis(5); |
53 | 57 | private static final long IDLE_TIMEOUT = TimeUnit.MINUTES.toMillis(1); |
54 | 58 |
|
| 59 | + // scan all jars in the classpath for ServletContainerInitializers |
| 60 | + // (e.g. Spring's WebApplicationInitializer) |
| 61 | + private static final String SCAN_PATTERN = ".*\\.jar$|.*/classes/.*"; |
| 62 | + |
55 | 63 | /** |
56 | 64 | * A fully configured Jetty Server instance |
57 | 65 | */ |
58 | | - private void prepare() { |
| 66 | + private void prepare() throws IOException { |
59 | 67 |
|
60 | 68 | // === jetty.xml === |
61 | 69 | // Setup Threadpool |
@@ -98,8 +106,7 @@ private void prepare() { |
98 | 106 | server.addConnector(httpsConnector(httpConfig)); |
99 | 107 | } |
100 | 108 |
|
101 | | - steveAppContext = new SteveAppContext(); |
102 | | - server.setHandler(steveAppContext.getHandler()); |
| 109 | + server.setHandler(getWebApp()); |
103 | 110 | } |
104 | 111 |
|
105 | 112 | private ServerConnector httpConnector(HttpConfiguration httpConfig) { |
@@ -133,6 +140,35 @@ private ServerConnector httpsConnector(HttpConfiguration httpConfig) { |
133 | 140 | return https; |
134 | 141 | } |
135 | 142 |
|
| 143 | + private Handler getWebApp() throws IOException { |
| 144 | + var webAppContext = new WebAppContext(); |
| 145 | + webAppContext.setContextPath(CONFIG.getContextPath()); |
| 146 | + webAppContext.setBaseResourceAsString(new ClassPathResource("webapp").getURI().toString()); |
| 147 | + |
| 148 | + // if during startup an exception happens, do not swallow it, throw it |
| 149 | + webAppContext.setThrowUnavailableOnStartupException(true); |
| 150 | + |
| 151 | + // Disable directory listings if no index.html is found. |
| 152 | + webAppContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false"); |
| 153 | + |
| 154 | + // Crucial for Spring's WebApplicationInitializer to be discovered |
| 155 | + // and for the DispatcherServlet to be initialized. |
| 156 | + // It tells Jetty to scan for classes implementing WebApplicationInitializer. |
| 157 | + // The pattern ensures that Jetty finds the Spring classes in the classpath. |
| 158 | + // |
| 159 | + // https://jetty.org/docs/jetty/12.1/programming-guide/maven-jetty/jetty-maven-plugin.html |
| 160 | + // https://jetty.org/docs/jetty/12.1/operations-guide/annotations/index.html#og-container-include-jar-pattern |
| 161 | + webAppContext.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", SCAN_PATTERN); |
| 162 | + |
| 163 | + if (CONFIG.getJetty().isGzipEnabled()) { |
| 164 | + // Wraps the whole web app in a gzip handler to make Jetty return compressed content |
| 165 | + // http://www.eclipse.org/jetty/documentation/current/gzip-filter.html |
| 166 | + return new GzipHandler(webAppContext); |
| 167 | + } else { |
| 168 | + return webAppContext; |
| 169 | + } |
| 170 | + } |
| 171 | + |
136 | 172 | /** |
137 | 173 | * Starts the Jetty Server instance |
138 | 174 | */ |
|
0 commit comments