Skip to content

Commit

Permalink
#3611 - Upgrade to Spring 6, Boot 3, Wicket 10, etc.
Browse files Browse the repository at this point in the history
- Fix filters and listeners
  • Loading branch information
reckart committed Feb 26, 2023
1 parent 351f6d4 commit 9fb6d41
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,7 +29,6 @@
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;

@Component
public class OpenCasStorageSessionForRequestFilter
implements Filter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@
*/
package de.tudarmstadt.ukp.inception.annotation.storage.config;

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 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;

Expand All @@ -27,6 +33,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;
Expand Down Expand Up @@ -63,4 +70,16 @@ public CasStorageServiceSharedAccessCacheAdapter CasStorageServiceSharedAccessCa
return new CasStorageServiceSharedAccessCacheAdapter(aCasStorageService,
aCasStorageProperties);
}

@Bean
public FilterRegistrationBean<OpenCasStorageSessionForRequestFilter> openCasStorageSessionForRequestFilter()
{
var registration = new FilterRegistrationBean<>(
new OpenCasStorageSessionForRequestFilter());
registration.setName("openCasStorageSessionForRequestFilter");
registration.setDispatcherTypes(REQUEST, FORWARD, ASYNC);
registration.setUrlPatterns(asList("/*"));
registration.setOrder(0);
return registration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<EventListener> springSessionLookup()
{
configureCoep(aServletContext);
configureLogging(aServletContext);
configurePerRequestJpaSession(aServletContext);
configureSpringSessionLookup(aServletContext);
var registration = new ServletListenerRegistrationBean<EventListener>();
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<OpenEntityManagerInViewFilter> 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> 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<OncePerRequestFilter> coepFilter()
{
FilterRegistration coepFilter = aServletContext.addFilter("coep", new OncePerRequestFilter()
OncePerRequestFilter filter = new OncePerRequestFilter()
{
@Override
protected void doFilterInternal(HttpServletRequest aRequest,
Expand All @@ -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;
}
}

0 comments on commit 9fb6d41

Please sign in to comment.