@Before public void setup() throws Exception { userService = _userService; CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); characterEncodingFilter.setEncoding( "UTF-8" ); characterEncodingFilter.setForceEncoding( true ); mvc = MockMvcBuilders.webAppContextSetup( webApplicationContext ) .addFilters( characterEncodingFilter, new ShallowEtagHeaderFilter(), filterChainProxy ) .apply( documentationConfiguration( this.restDocumentation ) ) .build(); executeStartupRoutines(); setUpTest(); }
@Before public void setup() throws Exception { objectMapper = new ObjectMapperFactoryBean().getObject(); CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); characterEncodingFilter.setEncoding( "UTF-8" ); characterEncodingFilter.setForceEncoding( true ); mvc = MockMvcBuilders.webAppContextSetup( wac ) .addFilters( characterEncodingFilter, new ShallowEtagHeaderFilter(), filterChainProxy ) .build(); executeStartupRoutines(); setUpTest(); }
@Bean public FilterRegistrationBean shallowEtagHeaderFilter() { FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter(new ShallowEtagHeaderFilter()); registration.setDispatcherTypes(EnumSet.allOf(DispatcherType.class)); return registration; }
@Bean public FilterRegistrationBean shallowEtagHeaderFilter() { ShallowEtagHeaderFilter filter = new ShallowEtagHeaderFilter(); filter.setWriteWeakETag(true); FilterRegistrationBean bean = new FilterRegistrationBean(filter); bean.addUrlPatterns("/api/*", "/admin/*"); return bean; }
@Override public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { if (returnValue == null) { mavContainer.setRequestHandled(true); return; } HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class); ServerHttpResponse outputMessage = new ServletServerHttpResponse(response); if (ResponseEntity.class.isAssignableFrom(returnValue.getClass())) { ResponseEntity<?> responseEntity = (ResponseEntity<?>) returnValue; outputMessage.setStatusCode(responseEntity.getStatusCode()); outputMessage.getHeaders().putAll(responseEntity.getHeaders()); returnValue = responseEntity.getBody(); if (returnValue == null) { mavContainer.setRequestHandled(true); return; } } ServletRequest request = webRequest.getNativeRequest(ServletRequest.class); ShallowEtagHeaderFilter.disableContentCaching(request); Assert.isInstanceOf(StreamingResponseBody.class, returnValue); StreamingResponseBody streamingBody = (StreamingResponseBody) returnValue; Callable<Void> callable = new StreamingResponseBodyTask(outputMessage.getBody(), streamingBody); WebAsyncUtils.getAsyncManager(webRequest).startCallableProcessing(callable, mavContainer); }
/** * Define filter to enable etag. * * @return filter */ @Bean public FilterRegistrationBean shallowEtagBean() { FilterRegistrationBean frb = new FilterRegistrationBean(); frb.setFilter(new ShallowEtagHeaderFilter()); frb.addUrlPatterns("/", "/admin", "/admin/*"); frb.setOrder(2); return frb; }
@Override public void onStartup(ServletContext servletContext) throws ServletException { super.onStartup(servletContext); // initialize cookie if(StringUtils.isNotEmpty(System.getProperty(CookieNames.PROPERTY_NAME))) { CookieNames.updatePrefix(System.getProperty(CookieNames.PROPERTY_NAME)); } // //definition order = execution order, the first executed filter is HSTSFilter addFilter(servletContext, "HSTSFilter", HSTSFilter.class, "/*"); addFilter(servletContext, "CSFRFilter", CSFRFilter.class, "/*"); addFilter(servletContext, "RememberMeFilter", RememberMeFilter.class, "/*"); addFilter(servletContext, "AnonymousUserFilter", AnonymousUserFilter.class, "/*"); addFilter(servletContext, "SecurityFilter", SecurityFilter.class, "/*"); addFilter(servletContext, "ETagFilter", ShallowEtagHeaderFilter.class, "*.js", "*.css",// "/", "/project/*", "/admin/*", "/me/",// "*.html", "*.woff", "*.eot", "*.svg", "*.ttf"); addFilter(servletContext, "GzipFilter", GzipFilter.class, "*.js", "*.css",// "/", "/project/*", "/admin/*", "/me/",// "/api/self", "/api/board/*", "/api/project/*"); servletContext.setSessionTrackingModes(Collections.singleton(SessionTrackingMode.COOKIE)); servletContext.getSessionCookieConfig().setHttpOnly(true); servletContext.getSessionCookieConfig().setName(CookieNames.getSessionCookieName()); }
protected void createFilters(ServletContext ctx) { CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); characterEncodingFilter.setEncoding("UTF-8"); SimpleCORSFilter corsFilter = new SimpleCORSFilter(); corsFilter.setCorsAllowCredentials("true"); corsFilter.setCorsAllowMethods("GET, POST, PUT, PATCH, DELETE, OPTIONS"); corsFilter.setCorsAllowHeaders("content-type, x-requested-with, origin, accept, authorization, username, password, x-app-type, x-app-version, x-auth-token, soapaction"); corsFilter.setCorsExposeHeaders("content-type, cookie, x-requested-with, origin, accept, username, password, x-app-type, x-app-version, x-auth-token, soapaction"); corsFilter.setCorsMaxAge("3600"); ctx.addFilter("springSessionRepositoryFilter", DelegatingFilterProxy.class).addMappingForUrlPatterns( EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, FILTER_MAPPING); ctx.addFilter("simpleCORSFilter", DelegatingFilterProxy.class).addMappingForUrlPatterns( EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, FILTER_MAPPING); // ctx.addFilter("SimpleCorsFilter", corsFilter).addMappingForUrlPatterns( // EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, FILTER_MAPPING); ctx.addFilter("JsonHttpRequestFilter", new JsonHttpRequestFilter()).addMappingForUrlPatterns( EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, "/api/authenticate"); ctx.addFilter("springSecurityFilterChain", DelegatingFilterProxy.class).addMappingForUrlPatterns( EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, FILTER_MAPPING); ctx.addFilter("CharacterEncodingFilter", characterEncodingFilter).addMappingForUrlPatterns( EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, FILTER_MAPPING); ctx.addFilter("HiddenHttpMethodFilter", new HiddenHttpMethodFilter()).addMappingForUrlPatterns( EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, FILTER_MAPPING); ctx.addFilter("HttpPutFormContentFilter", new HttpPutFormContentFilter()).addMappingForUrlPatterns( EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, FILTER_MAPPING); ctx.addFilter("ShallowEtagHeaderFilter", new ShallowEtagHeaderFilter()).addMappingForUrlPatterns( EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, FILTER_MAPPING); }
@Override public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { if (returnValue == null) { mavContainer.setRequestHandled(true); return; } HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class); ServerHttpResponse outputMessage = new ServletServerHttpResponse(response); if (ResponseEntity.class.isAssignableFrom(returnValue.getClass())) { ResponseEntity<?> responseEntity = (ResponseEntity<?>) returnValue; outputMessage.setStatusCode(responseEntity.getStatusCode()); outputMessage.getHeaders().putAll(responseEntity.getHeaders()); returnValue = responseEntity.getBody(); if (returnValue == null) { mavContainer.setRequestHandled(true); return; } } ServletRequest request = webRequest.getNativeRequest(ServletRequest.class); ShallowEtagHeaderFilter.disableContentCaching(request); Assert.isInstanceOf(ResponseBodyEmitter.class, returnValue); ResponseBodyEmitter emitter = (ResponseBodyEmitter) returnValue; emitter.extendResponse(outputMessage); // Commit the response and wrap to ignore further header changes outputMessage.getBody(); outputMessage.flush(); outputMessage = new StreamingServletServerHttpResponse(outputMessage); DeferredResult<?> deferredResult = new DeferredResult<Object>(emitter.getTimeout()); WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(deferredResult, mavContainer); HttpMessageConvertingHandler handler = new HttpMessageConvertingHandler(outputMessage, deferredResult); emitter.initialize(handler); }
private void disableShallowEtagHeaderFilter(ServerHttpRequest request) { if (request instanceof ServletServerHttpRequest) { ServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest(); ShallowEtagHeaderFilter.disableContentCaching(servletRequest); } }
@Bean public Filter shallowEtagHeaderFilter() { return new ShallowEtagHeaderFilter(); }
@Bean public ShallowEtagHeaderFilter etagFilter() { return new ShallowEtagHeaderFilter(); }
/** * ETag support */ @Bean public ShallowEtagHeaderFilter shallowEtagHeaderFilter() { return new ShallowEtagHeaderFilter(); }
/** * 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); }
/** * Etag filter filter. * * @return the filter */ @Bean(name = "etagFilter") public Filter etagFilter() { return new ShallowEtagHeaderFilter(); }