use of javax.servlet.FilterRegistration.Dynamic in project tutorials by eugenp.
the class MainWebAppInitializer method onStartup.
//
/**
* Register and configure all Servlet container components necessary to power the web application.
*/
@Override
public void onStartup(final ServletContext sc) throws ServletException {
System.out.println("MyWebAppInitializer.onStartup()");
// Create the 'root' Spring application context
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
root.scan("org.baeldung.config.parent");
// root.getEnvironment().setDefaultProfiles("embedded");
// Manages the lifecycle of the root application context
sc.addListener(new ContextLoaderListener(root));
// Handles requests into the application
final AnnotationConfigWebApplicationContext childWebApplicationContext = new AnnotationConfigWebApplicationContext();
childWebApplicationContext.scan("org.baeldung.config.child");
final ServletRegistration.Dynamic appServlet = sc.addServlet("api", new DispatcherServlet(childWebApplicationContext));
appServlet.setLoadOnStartup(1);
final Set<String> mappingConflicts = appServlet.addMapping("/");
if (!mappingConflicts.isEmpty()) {
throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278");
}
// spring security filter
final DelegatingFilterProxy springSecurityFilterChain = new DelegatingFilterProxy("springSecurityFilterChain");
final Dynamic addedFilter = sc.addFilter("springSecurityFilterChain", springSecurityFilterChain);
addedFilter.addMappingForUrlPatterns(null, false, "/*");
}
use of javax.servlet.FilterRegistration.Dynamic in project wildfly-swarm by wildfly-swarm.
the class OpenTracingInitializer method contextInitialized.
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
ServletContext servletContext = servletContextEvent.getServletContext();
String skipPatternAttribute = servletContext.getInitParameter(TracingFilter.SKIP_PATTERN);
if (null != skipPatternAttribute && !skipPatternAttribute.isEmpty()) {
servletContext.setAttribute(TracingFilter.SKIP_PATTERN, Pattern.compile(skipPatternAttribute));
}
logger.info("Registering Tracing Filter");
Dynamic filterRegistration = servletContext.addFilter("tracingFilter", new TracingFilter());
filterRegistration.setAsyncSupported(true);
filterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "*");
String skipParameter = servletContext.getInitParameter("skipOpenTracingResolver");
if (skipParameter != null && Boolean.parseBoolean(skipParameter)) {
logger.info("OpenTracing automatic resolution is being explicitly skipped.");
return;
}
if (GlobalTracer.isRegistered()) {
logger.info("A Tracer is already registered at the GlobalTracer. Skipping resolution via TraceResolver.");
return;
}
Tracer tracer = TracerResolver.resolveTracer();
if (null == tracer) {
logger.info("Could not get a valid OpenTracing Tracer from the classpath. Skipping.");
return;
}
logger.info(String.format("Registering %s as the OpenTracing Tracer", tracer.getClass().getName()));
GlobalTracer.register(tracer);
}
use of javax.servlet.FilterRegistration.Dynamic in project molgenis by molgenis.
the class MolgenisWebAppInitializer method onStartup.
/**
* A Molgenis common web application initializer
*/
protected void onStartup(ServletContext servletContext, Class<?> appConfig, int maxFileSize) {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(appConfig);
// Manage the lifecycle of the root application context
servletContext.addListener(new ContextLoaderListener(rootContext));
// Register and map the dispatcher servlet
DispatcherServlet dispatcherServlet = new DispatcherServlet(rootContext);
dispatcherServlet.setDispatchOptionsRequest(true);
// instead of throwing a 404 when a handler is not found allow for custom handling
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
ServletRegistration.Dynamic dispatcherServletRegistration = servletContext.addServlet("dispatcher", dispatcherServlet);
if (dispatcherServletRegistration == null) {
LOG.warn("ServletContext already contains a complete ServletRegistration for servlet 'dispatcher'");
} else {
final long maxSize = (long) maxFileSize * MB;
dispatcherServletRegistration.addMapping("/");
dispatcherServletRegistration.setMultipartConfig(new MultipartConfigElement(null, maxSize, maxSize, FILE_SIZE_THRESHOLD));
}
// add filters
Dynamic browserDetectionFiler = servletContext.addFilter("browserDetectionFilter", BrowserDetectionFilter.class);
browserDetectionFiler.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "*");
Dynamic etagFilter = servletContext.addFilter("etagFilter", ShallowEtagHeaderFilter.class);
etagFilter.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST), true, "dispatcher");
Dynamic corsFilter = servletContext.addFilter("corsFilter", CorsFilter.class);
corsFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/api/*");
corsFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/fdp/*");
// enable use of request scoped beans in FrontController
servletContext.addListener(new RequestContextListener());
servletContext.addListener(HttpSessionEventPublisher.class);
}
use of javax.servlet.FilterRegistration.Dynamic in project brave by openzipkin.
the class ITSpanCustomizingAsyncHandlerInterceptor method init.
@Override
public void init(ServletContextHandler handler) {
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext() {
// overriding this allows us to register dependencies of TracingHandlerInterceptor
// without passing static state to a configuration class.
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) {
beanFactory.registerSingleton("httpTracing", httpTracing);
super.loadBeanDefinitions(beanFactory);
}
};
// the test resource
appContext.register(Servlet3TestController.class);
// generic tracing setup
appContext.register(TracingConfig.class);
DispatcherServlet servlet = new DispatcherServlet(appContext);
servlet.setDispatchOptionsRequest(true);
ServletHolder servletHolder = new ServletHolder(servlet);
servletHolder.setAsyncSupported(true);
handler.addServlet(servletHolder, "/*");
handler.addEventListener(new ContextLoaderListener(appContext));
// add the trace filter, which lazy initializes a real tracing filter from the spring context
Dynamic filterRegistration = handler.getServletContext().addFilter("tracingFilter", DelegatingTracingFilter.class);
filterRegistration.setAsyncSupported(true);
filterRegistration.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
}
use of javax.servlet.FilterRegistration.Dynamic in project brave by openzipkin.
the class ITTracingFilter method init.
@Override
public void init(ServletContextHandler handler) {
handler.getServletContext().addFilter("tracingFilter", TracingFilter.create(httpTracing)).addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
Dynamic sparkFilter = handler.getServletContext().addFilter("sparkFilter", new SparkFilter());
sparkFilter.setInitParameter("applicationClass", TestApplication.class.getName());
sparkFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
}
Aggregations