Skip to content

Commit dccc0a0

Browse files
committed
move contents of SteveAppContext into JettyServer
1 parent 8ba8c1c commit dccc0a0

File tree

3 files changed

+41
-89
lines changed

3 files changed

+41
-89
lines changed

src/main/java/de/rwth/idsg/steve/JettyServer.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,24 @@
1919
package de.rwth.idsg.steve;
2020

2121
import lombok.extern.slf4j.Slf4j;
22+
import org.eclipse.jetty.ee10.webapp.WebAppContext;
2223
import org.eclipse.jetty.http.HttpScheme;
2324
import org.eclipse.jetty.http.HttpVersion;
2425
import org.eclipse.jetty.server.ForwardedRequestCustomizer;
26+
import org.eclipse.jetty.server.Handler;
2527
import org.eclipse.jetty.server.HttpConfiguration;
2628
import org.eclipse.jetty.server.HttpConnectionFactory;
2729
import org.eclipse.jetty.server.SecureRequestCustomizer;
2830
import org.eclipse.jetty.server.Server;
2931
import org.eclipse.jetty.server.ServerConnector;
3032
import org.eclipse.jetty.server.SslConnectionFactory;
33+
import org.eclipse.jetty.server.handler.gzip.GzipHandler;
3134
import org.eclipse.jetty.util.ssl.SslContextFactory;
3235
import org.eclipse.jetty.util.thread.QueuedThreadPool;
3336
import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler;
37+
import org.springframework.core.io.ClassPathResource;
3438

39+
import java.io.IOException;
3540
import java.util.concurrent.TimeUnit;
3641

3742
import static de.rwth.idsg.steve.SteveConfiguration.CONFIG;
@@ -44,18 +49,21 @@
4449
public class JettyServer {
4550

4651
private Server server;
47-
private SteveAppContext steveAppContext;
4852

4953
private static final int MIN_THREADS = 4;
5054
private static final int MAX_THREADS = 50;
5155

5256
private static final long STOP_TIMEOUT = TimeUnit.SECONDS.toMillis(5);
5357
private static final long IDLE_TIMEOUT = TimeUnit.MINUTES.toMillis(1);
5458

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+
5563
/**
5664
* A fully configured Jetty Server instance
5765
*/
58-
private void prepare() {
66+
private void prepare() throws IOException {
5967

6068
// === jetty.xml ===
6169
// Setup Threadpool
@@ -98,8 +106,7 @@ private void prepare() {
98106
server.addConnector(httpsConnector(httpConfig));
99107
}
100108

101-
steveAppContext = new SteveAppContext();
102-
server.setHandler(steveAppContext.getHandler());
109+
server.setHandler(getWebApp());
103110
}
104111

105112
private ServerConnector httpConnector(HttpConfiguration httpConfig) {
@@ -133,6 +140,35 @@ private ServerConnector httpsConnector(HttpConfiguration httpConfig) {
133140
return https;
134141
}
135142

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+
136172
/**
137173
* Starts the Jetty Server instance
138174
*/

src/main/java/de/rwth/idsg/steve/SteveAppContext.java

Lines changed: 0 additions & 84 deletions
This file was deleted.

src/main/java/de/rwth/idsg/steve/config/WebConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import static de.rwth.idsg.steve.SteveConfiguration.CONFIG;
3636

3737
/**
38-
* Jetty will automatically detect this class because of {@link de.rwth.idsg.steve.SteveAppContext#SCAN_PATTERN} and
38+
* Jetty will automatically detect this class because of {@link de.rwth.idsg.steve.JettyServer#SCAN_PATTERN} and
3939
* use it to initialize the Spring and servlets and filters.
4040
*
4141
* @author Sevket Goekay <[email protected]>

0 commit comments

Comments
 (0)