Skip to content

Commit 27a13e1

Browse files
committed
move spring, servlet, filter etc init to WebApplicationInitializer
1 parent 346b38d commit 27a13e1

File tree

2 files changed

+95
-45
lines changed

2 files changed

+95
-45
lines changed

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

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@
1818
*/
1919
package de.rwth.idsg.steve;
2020

21-
import org.apache.cxf.transport.servlet.CXFServlet;
2221
import org.apache.tomcat.InstanceManager;
2322
import org.apache.tomcat.SimpleInstanceManager;
2423
import org.eclipse.jetty.ee10.apache.jsp.JettyJasperInitializer;
25-
import org.eclipse.jetty.ee10.servlet.FilterHolder;
2624
import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
27-
import org.eclipse.jetty.ee10.servlet.ServletHolder;
2825
import org.eclipse.jetty.ee10.webapp.WebAppContext;
2926
import org.eclipse.jetty.rewrite.handler.RedirectPatternRule;
3027
import org.eclipse.jetty.rewrite.handler.RewriteHandler;
@@ -34,16 +31,8 @@
3431
import org.eclipse.jetty.server.handler.gzip.GzipHandler;
3532
import org.eclipse.jetty.util.component.AbstractLifeCycle;
3633
import org.springframework.core.io.ClassPathResource;
37-
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
38-
import org.springframework.web.context.ContextLoaderListener;
39-
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
40-
import org.springframework.web.filter.DelegatingFilterProxy;
41-
import org.springframework.web.servlet.DispatcherServlet;
42-
43-
import jakarta.servlet.DispatcherType;
4434

4535
import java.io.IOException;
46-
import java.util.EnumSet;
4736
import java.util.HashSet;
4837

4938
import static de.rwth.idsg.steve.SteveConfiguration.CONFIG;
@@ -54,13 +43,33 @@
5443
*/
5544
public class SteveAppContext {
5645

57-
private final AnnotationConfigWebApplicationContext springContext;
46+
// scan all jars in the classpath for ServletContainerInitializers
47+
// (e.g. Spring's WebApplicationInitializer)
48+
private static final String SCAN_PATTERN = ".*\\.jar$|.*/classes/.*";
49+
5850
private final WebAppContext webAppContext;
5951

6052
public SteveAppContext() {
61-
springContext = new AnnotationConfigWebApplicationContext();
62-
springContext.scan("de.rwth.idsg.steve.config");
63-
webAppContext = initWebApp();
53+
webAppContext = new WebAppContext();
54+
webAppContext.setContextPath(CONFIG.getContextPath());
55+
webAppContext.setBaseResourceAsString(getWebAppURIAsString());
56+
57+
// if during startup an exception happens, do not swallow it, throw it
58+
webAppContext.setThrowUnavailableOnStartupException(true);
59+
60+
// Disable directory listings if no index.html is found.
61+
webAppContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
62+
63+
// Crucial for Spring's WebApplicationInitializer to be discovered
64+
// and for the DispatcherServlet to be initialized.
65+
// It tells Jetty to scan for classes implementing WebApplicationInitializer.
66+
// The pattern ensures that Jetty finds the Spring classes in the classpath.
67+
//
68+
// https://jetty.org/docs/jetty/12.1/programming-guide/maven-jetty/jetty-maven-plugin.html
69+
// https://jetty.org/docs/jetty/12.1/operations-guide/annotations/index.html#og-container-include-jar-pattern
70+
webAppContext.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", SCAN_PATTERN);
71+
72+
initJSP(webAppContext);
6473
}
6574

6675
public ContextHandlerCollection getHandlers() {
@@ -80,36 +89,6 @@ private Handler getWebApp() {
8089
return new GzipHandler(webAppContext);
8190
}
8291

83-
private WebAppContext initWebApp() {
84-
WebAppContext ctx = new WebAppContext();
85-
ctx.setContextPath(CONFIG.getContextPath());
86-
ctx.setBaseResourceAsString(getWebAppURIAsString());
87-
88-
// if during startup an exception happens, do not swallow it, throw it
89-
ctx.setThrowUnavailableOnStartupException(true);
90-
91-
// Disable directory listings if no index.html is found.
92-
ctx.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
93-
94-
ServletHolder web = new ServletHolder("spring-dispatcher", new DispatcherServlet(springContext));
95-
ServletHolder cxf = new ServletHolder("cxf", new CXFServlet());
96-
97-
ctx.addEventListener(new ContextLoaderListener(springContext));
98-
ctx.addServlet(web, CONFIG.getSpringMapping());
99-
ctx.addServlet(cxf, CONFIG.getCxfMapping() + "/*");
100-
101-
// add spring security
102-
ctx.addFilter(
103-
// The bean name is not arbitrary, but is as expected by Spring
104-
new FilterHolder(new DelegatingFilterProxy(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME)),
105-
CONFIG.getSpringMapping() + "*",
106-
EnumSet.allOf(DispatcherType.class)
107-
);
108-
109-
initJSP(ctx);
110-
return ctx;
111-
}
112-
11392
private Handler getRedirectHandler() {
11493
RewriteHandler rewrite = new RewriteHandler();
11594
for (String redirect : getRedirectSet()) {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* SteVe - SteckdosenVerwaltung - https://github.com/steve-community/steve
3+
* Copyright (C) 2013-2025 SteVe Community Team
4+
* All Rights Reserved.
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
package de.rwth.idsg.steve.config;
20+
21+
import lombok.extern.slf4j.Slf4j;
22+
import org.apache.cxf.transport.servlet.CXFServlet;
23+
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
24+
import org.springframework.web.WebApplicationInitializer;
25+
import org.springframework.web.context.ContextLoaderListener;
26+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
27+
import org.springframework.web.servlet.DispatcherServlet;
28+
29+
import jakarta.servlet.DispatcherType;
30+
import jakarta.servlet.ServletContext;
31+
import jakarta.servlet.ServletException;
32+
33+
import java.util.EnumSet;
34+
35+
import static de.rwth.idsg.steve.SteveConfiguration.CONFIG;
36+
37+
/**
38+
* Jetty will automatically detect this class because of {@link de.rwth.idsg.steve.SteveAppContext#SCAN_PATTERN} and
39+
* use it to initialize the Spring and servlets and filters.
40+
*
41+
* @author Sevket Goekay <[email protected]>
42+
* @since 17.09.2025
43+
*/
44+
@Slf4j
45+
public class WebConfig implements WebApplicationInitializer {
46+
47+
@Override
48+
public void onStartup(ServletContext servletContext) throws ServletException {
49+
log.info("Initializing");
50+
51+
// Spring root context
52+
AnnotationConfigWebApplicationContext springContext = new AnnotationConfigWebApplicationContext();
53+
springContext.scan("de.rwth.idsg.steve.config");
54+
servletContext.addListener(new ContextLoaderListener(springContext));
55+
56+
// Spring MVC
57+
var dispatcher = servletContext.addServlet("spring-dispatcher", new DispatcherServlet(springContext));
58+
dispatcher.setLoadOnStartup(1);
59+
dispatcher.addMapping(CONFIG.getSpringMapping());
60+
61+
// Apache CXF
62+
var cxf = servletContext.addServlet("cxf", new CXFServlet());
63+
cxf.setLoadOnStartup(1);
64+
cxf.addMapping(CONFIG.getCxfMapping() + "/*");
65+
66+
// add spring security
67+
servletContext
68+
.addFilter(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME, "org.springframework.web.filter.DelegatingFilterProxy")
69+
.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, CONFIG.getSpringMapping() + "*");
70+
}
71+
}

0 commit comments

Comments
 (0)