From d6ba14926f59c832021a0df454e8b97ee19f17f4 Mon Sep 17 00:00:00 2001 From: Richard Eckart de Castilho Date: Mon, 27 Feb 2023 00:13:36 +0100 Subject: [PATCH] #3611 - Upgrade to Spring 6, Boot 3, Wicket 10, etc. - Fix filters and listeners --- ...OpenCasStorageSessionForRequestFilter.java | 2 - .../CasStorageServiceAutoConfiguration.java | 15 +++++ .../InceptionServletContextInitializer.java | 65 +++++++++++-------- 3 files changed, 52 insertions(+), 30 deletions(-) diff --git a/inception/inception-annotation-storage/src/main/java/de/tudarmstadt/ukp/inception/annotation/storage/OpenCasStorageSessionForRequestFilter.java b/inception/inception-annotation-storage/src/main/java/de/tudarmstadt/ukp/inception/annotation/storage/OpenCasStorageSessionForRequestFilter.java index 15fef1815a2..b226cfd3b2a 100644 --- a/inception/inception-annotation-storage/src/main/java/de/tudarmstadt/ukp/inception/annotation/storage/OpenCasStorageSessionForRequestFilter.java +++ b/inception/inception-annotation-storage/src/main/java/de/tudarmstadt/ukp/inception/annotation/storage/OpenCasStorageSessionForRequestFilter.java @@ -21,7 +21,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; import jakarta.servlet.Filter; import jakarta.servlet.FilterChain; @@ -30,7 +29,6 @@ import jakarta.servlet.ServletRequest; import jakarta.servlet.ServletResponse; -@Component public class OpenCasStorageSessionForRequestFilter implements Filter { diff --git a/inception/inception-annotation-storage/src/main/java/de/tudarmstadt/ukp/inception/annotation/storage/config/CasStorageServiceAutoConfiguration.java b/inception/inception-annotation-storage/src/main/java/de/tudarmstadt/ukp/inception/annotation/storage/config/CasStorageServiceAutoConfiguration.java index 6c9ae8f8fe7..29a979f7bb3 100644 --- a/inception/inception-annotation-storage/src/main/java/de/tudarmstadt/ukp/inception/annotation/storage/config/CasStorageServiceAutoConfiguration.java +++ b/inception/inception-annotation-storage/src/main/java/de/tudarmstadt/ukp/inception/annotation/storage/config/CasStorageServiceAutoConfiguration.java @@ -17,8 +17,11 @@ */ package de.tudarmstadt.ukp.inception.annotation.storage.config; +import static java.util.Arrays.asList; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -27,6 +30,7 @@ import de.tudarmstadt.ukp.clarin.webanno.diag.CasDoctor; import de.tudarmstadt.ukp.inception.annotation.storage.CasStorageServiceImpl; import de.tudarmstadt.ukp.inception.annotation.storage.CasStorageServiceSharedAccessCacheAdapter; +import de.tudarmstadt.ukp.inception.annotation.storage.OpenCasStorageSessionForRequestFilter; import de.tudarmstadt.ukp.inception.annotation.storage.driver.CasStorageDriver; import de.tudarmstadt.ukp.inception.annotation.storage.driver.filesystem.FileSystemCasStorageDriver; import de.tudarmstadt.ukp.inception.schema.AnnotationSchemaService; @@ -63,4 +67,15 @@ public CasStorageServiceSharedAccessCacheAdapter CasStorageServiceSharedAccessCa return new CasStorageServiceSharedAccessCacheAdapter(aCasStorageService, aCasStorageProperties); } + + @Bean + public FilterRegistrationBean openCasStorageSessionForRequestFilter() + { + var registration = new FilterRegistrationBean<>( + new OpenCasStorageSessionForRequestFilter()); + registration.setName("openCasStorageSessionForRequestFilter"); + registration.setUrlPatterns(asList("/*")); + registration.setOrder(0); + return registration; + } } diff --git a/inception/inception-app-webapp/src/main/java/de/tudarmstadt/ukp/inception/app/config/InceptionServletContextInitializer.java b/inception/inception-app-webapp/src/main/java/de/tudarmstadt/ukp/inception/app/config/InceptionServletContextInitializer.java index ece11eeb471..ba52920b20b 100644 --- a/inception/inception-app-webapp/src/main/java/de/tudarmstadt/ukp/inception/app/config/InceptionServletContextInitializer.java +++ b/inception/inception-app-webapp/src/main/java/de/tudarmstadt/ukp/inception/app/config/InceptionServletContextInitializer.java @@ -20,12 +20,15 @@ import static jakarta.servlet.DispatcherType.ASYNC; import static jakarta.servlet.DispatcherType.FORWARD; import static jakarta.servlet.DispatcherType.REQUEST; +import static java.util.Arrays.asList; import java.io.IOException; -import java.util.EnumSet; +import java.util.EventListener; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.web.servlet.ServletContextInitializer; +import org.springframework.boot.web.servlet.FilterRegistrationBean; +import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter; import org.springframework.security.web.session.HttpSessionEventPublisher; @@ -34,53 +37,54 @@ import de.tudarmstadt.ukp.clarin.webanno.api.config.RepositoryProperties; import de.tudarmstadt.ukp.clarin.webanno.support.logging.LoggingFilter; import jakarta.servlet.FilterChain; -import jakarta.servlet.FilterRegistration; -import jakarta.servlet.ServletContext; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @Configuration public class InceptionServletContextInitializer - implements ServletContextInitializer { private @Autowired RepositoryProperties repoProperties; - @Override - public void onStartup(ServletContext aServletContext) throws ServletException + @Bean + public ServletListenerRegistrationBean springSessionLookup() { - configureCoep(aServletContext); - configureLogging(aServletContext); - configurePerRequestJpaSession(aServletContext); - configureSpringSessionLookup(aServletContext); + var registration = new ServletListenerRegistrationBean(); + registration.setListener(new HttpSessionEventPublisher()); + registration.setOrder(0); + return registration; } - private void configureSpringSessionLookup(ServletContext aServletContext) - { - // Provide Spring with access to the HTTP sessions - aServletContext.addListener(HttpSessionEventPublisher.class); - } - - private void configurePerRequestJpaSession(ServletContext aServletContext) + @Bean + public FilterRegistrationBean perRequestJpaSession() { // Make sure we have one JPA session/transaction per request. Closes session at the // end, without this, changed data may not be automatically saved to the DB. - FilterRegistration openSessionInViewFilter = aServletContext.addFilter("opensessioninview", - OpenEntityManagerInViewFilter.class); - openSessionInViewFilter.addMappingForUrlPatterns(EnumSet.of(REQUEST), false, "/*"); + var registration = new FilterRegistrationBean<>(new OpenEntityManagerInViewFilter()); + registration.setName("opensessioninview"); + registration.setDispatcherTypes(REQUEST); + registration.setUrlPatterns(asList("/*")); + registration.setOrder(0); + return registration; } - private void configureLogging(ServletContext aServletContext) + @Bean + public FilterRegistrationBean loggingFilter() { // Make username / repository accessible to logging framework - FilterRegistration loggingFilter = aServletContext.addFilter("logging", + var registration = new FilterRegistrationBean<>( new LoggingFilter(repoProperties.getPath().getAbsolutePath().toString())); - loggingFilter.addMappingForUrlPatterns(EnumSet.of(REQUEST, FORWARD, ASYNC), false, "/*"); + registration.setName("logging"); + registration.setDispatcherTypes(REQUEST, FORWARD, ASYNC); + registration.setUrlPatterns(asList("/*")); + registration.setOrder(0); + return registration; } - private void configureCoep(ServletContext aServletContext) + @Bean + public FilterRegistrationBean coepFilter() { - FilterRegistration coepFilter = aServletContext.addFilter("coep", new OncePerRequestFilter() + OncePerRequestFilter filter = new OncePerRequestFilter() { @Override protected void doFilterInternal(HttpServletRequest aRequest, @@ -94,7 +98,12 @@ protected void doFilterInternal(HttpServletRequest aRequest, response.setHeader("Cross-Origin-Opener-Policy", "same-origin"); aFilterChain.doFilter(aRequest, aResponse); } - }); - coepFilter.addMappingForUrlPatterns(EnumSet.of(REQUEST, FORWARD, ASYNC), false, "/*"); + }; + var registration = new FilterRegistrationBean<>(filter); + registration.setName("coep"); + registration.setDispatcherTypes(REQUEST, FORWARD, ASYNC); + registration.setUrlPatterns(asList("/*")); + registration.setOrder(0); + return registration; } }