public static ServletAdapter initRestTestServletAdapter(Class<?> applicationConfigClass, int testServerPort, String testContextPath, String testServletPath, boolean enableJPA) { final ServletAdapter adapter = new ServletAdapter(); adapter.addInitParameter(ServletContainer.APPLICATION_CONFIG_CLASS, applicationConfigClass.getName()); adapter.addInitParameter("com.sun.jersey.spi.container.ContainerRequestFilters", LoggingFilter.class.getName()); adapter.addInitParameter("com.sun.jersey.spi.container.ContainerResponseFilters", LoggingFilter.class.getName()); adapter.addServletListener(ContextLoaderListener.class.getName()); if (enableJPA) { adapter.addFilter(new OpenEntityManagerInViewFilter(), "openEntityManagerInViewFilter", null); } adapter.setServletInstance(new SpringServlet()); adapter.setContextPath(testContextPath); adapter.setServletPath(testServletPath); return adapter; }
@Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext webAppContext = new AnnotationConfigWebApplicationContext(); webAppContext.setConfigLocation(getClass().getPackage().getName()); ServletRegistration.Dynamic dispatcher = servletContext.addServlet(DISPATCHER_SERVLET_NAME, new DispatcherServlet(webAppContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping(DISPATCHER_SERVLET_MAPPING); OpenEntityManagerInViewFilter openEntityManagerInViewFilter = new OpenEntityManagerInViewFilter(); openEntityManagerInViewFilter.setEntityManagerFactoryBeanName("entityManagerFactory"); Dynamic dynamic = servletContext.addFilter("openEntityManagerInViewFilter", openEntityManagerInViewFilter); dynamic.addMappingForUrlPatterns(getDispatcherTypes(), false, DISPATCHER_SERVLET_MAPPING); servletContext.addListener(new ContextLoaderListener(webAppContext)); }
@Override public void onStartup(ServletContext servletContext) throws ServletException { CharacterEncodingFilter filter = new CharacterEncodingFilter(); filter.setEncoding("UTF-8"); filter.setForceEncoding(true); servletContext.addFilter("characterEncodingFilter", filter).addMappingForUrlPatterns(null, false, "/*"); servletContext.addFilter("openEntityManagerInViewFilter", new OpenEntityManagerInViewFilter()).addMappingForUrlPatterns(null, false, "/*"); DelegatingFilterProxy delegatingFilterProxy = new DelegatingFilterProxy(); delegatingFilterProxy.setTargetFilterLifecycle(true); servletContext.addFilter("shiroFilter", delegatingFilterProxy).addMappingForUrlPatterns(null, false, "/*"); servletContext.addServlet("kaptcha", new KaptchaServlet()).addMapping("/kaptcha"); super.onStartup(servletContext); }
@Override protected Filter[] getServletFilters() { return new Filter[]{ new OpenEntityManagerInViewFilter(), new RecaptchaResponseFilter(), new ErrorHandlerFilter() }; }
protected void configureContext(WebappContext context) { context.setInitParameter(ContextLoader.CONTEXT_CLASS_PARAM, AnnotationConfigWebApplicationContext.class.getName()); context.setInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, ExtendedApplicationContextInitializer.class.getName()); if (javaConfigClass != null) { context.setInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, javaConfigClass.getName()); } context.addListener(SLF4JLoggingListener.class); context.addListener(ContextLoaderListener.class); context.addFilter("openEntityManagerInViewFilter", new OpenEntityManagerInViewFilter()) .addMappingForUrlPatterns(null, "/*"); }
@Override public void onStartup(ServletContext aServletContext) throws ServletException { // 2) Make username accessible to logging framework FilterRegistration loggingFilter = aServletContext.addFilter("logging", LoggingFilter.class); loggingFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*"); // 5) 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(DispatcherType.REQUEST), false, "/*"); }
@Bean public FilterRegistrationBean openEntityManagerInViewFilter() { FilterRegistrationBean reg = new FilterRegistrationBean(); reg.setName("OpenEntityManagerInViewFilter"); reg.setFilter(new OpenEntityManagerInViewFilter()); return reg; }
private void initializeOpenEMInViewFilter(ServletContext container) { OpenEntityManagerInViewFilter sessionFilter = new OpenEntityManagerInViewFilter(); FilterRegistration.Dynamic filterReg = container.addFilter("Open EM In View Filter", sessionFilter); filterReg.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, URL_PATTERN_WEB); filterReg.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, URL_PATTERN_REST); }
@Override protected Filter[] getServletFilters() { return new Filter[]{ new MDCInsertingServletFilter(), new HiddenHttpMethodFilter(), new OpenEntityManagerInViewFilter(), new RequestContextFilter(), new SecurityContextPersistenceFilter() }; }
@Bean @ConditionalOnMissingBean public OpenEntityManagerInViewFilter openEntityManagerInViewFilter() { return new OpenEntityManagerInViewFilter(); }
@Test public void testFilterEnabled() { OpenEntityManagerInViewFilter filter = this.applicationContext.getBean(OpenEntityManagerInViewFilter.class); assertThat(filter).isNotNull(); }
@Test(expected = NoSuchBeanDefinitionException.class) public void testFilterDisabled() { this.applicationContext.getBean(OpenEntityManagerInViewFilter.class); }
@Bean public OpenEntityManagerInViewFilter openEntityManagerInViewFilter() { return new OpenEntityManagerInViewFilter(); }
private OpenEntityManagerInViewFilter buildOpenEntityManagerFilter() { OpenEntityManagerInViewFilter openEntityManagerInViewFilter = new OpenEntityManagerInViewFilter(); openEntityManagerInViewFilter.setEntityManagerFactoryBeanName("entityManagerFactory"); return openEntityManagerInViewFilter; }
/** * Open entity manager in view filter. * * @return the open entity manager in view filter */ @Bean public OpenEntityManagerInViewFilter openEntityManagerInViewFilter() { return new OpenEntityManagerInViewFilter(); }