Java tutorial
/* * Copyright 2013-2015 JIWHIZ Consulting Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.jiwhiz.web.config; import java.util.Arrays; import java.util.EnumSet; import javax.inject.Inject; import javax.servlet.DispatcherType; import javax.servlet.FilterRegistration; import javax.servlet.ServletContext; import javax.servlet.ServletException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; import org.springframework.boot.context.embedded.MimeMappings; import org.springframework.boot.context.embedded.ServletContextInitializer; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import com.jiwhiz.web.filter.CachingHttpHeadersFilter; import com.jiwhiz.web.filter.StaticResourcesProductionFilter; /** * Configuration of web application with Servlet 3.0 APIs. */ @Configuration public class WebConfigurer implements ServletContextInitializer, EmbeddedServletContainerCustomizer { private final Logger log = LoggerFactory.getLogger(WebConfigurer.class); @Inject private Environment env; @Override public void onStartup(ServletContext servletContext) throws ServletException { log.info("Web application configuration, using profiles: {}", Arrays.toString(env.getActiveProfiles())); EnumSet<DispatcherType> disps = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.ASYNC); if (env.acceptsProfiles("prod")) { initCachingHttpHeadersFilter(servletContext, disps); initStaticResourcesProductionFilter(servletContext, disps); } log.info("Web application fully configured"); } /** * Set up Mime types. */ @Override public void customize(ConfigurableEmbeddedServletContainer container) { MimeMappings mappings = new MimeMappings(MimeMappings.DEFAULT); // IE issue, see https://github.com/jhipster/generator-jhipster/pull/711 mappings.add("html", "text/html;charset=utf-8"); // CloudFoundry issue, see https://github.com/cloudfoundry/gorouter/issues/64 mappings.add("json", "text/html;charset=utf-8"); container.setMimeMappings(mappings); } /** * Initializes the static resources production Filter. */ private void initStaticResourcesProductionFilter(ServletContext servletContext, EnumSet<DispatcherType> disps) { log.debug("Registering static resources production Filter"); FilterRegistration.Dynamic staticResourcesProductionFilter = servletContext .addFilter("staticResourcesProductionFilter", new StaticResourcesProductionFilter()); staticResourcesProductionFilter.addMappingForUrlPatterns(disps, true, "/"); staticResourcesProductionFilter.addMappingForUrlPatterns(disps, true, "/index.html"); staticResourcesProductionFilter.addMappingForUrlPatterns(disps, true, "/images/*"); staticResourcesProductionFilter.addMappingForUrlPatterns(disps, true, "/fonts/*"); staticResourcesProductionFilter.addMappingForUrlPatterns(disps, true, "/scripts/*"); staticResourcesProductionFilter.addMappingForUrlPatterns(disps, true, "/styles/*"); staticResourcesProductionFilter.addMappingForUrlPatterns(disps, true, "/views/*"); staticResourcesProductionFilter.setAsyncSupported(true); } /** * Initializes the cachig HTTP Headers Filter. */ private void initCachingHttpHeadersFilter(ServletContext servletContext, EnumSet<DispatcherType> disps) { log.debug("Registering Caching HTTP Headers Filter"); FilterRegistration.Dynamic cachingHttpHeadersFilter = servletContext.addFilter("cachingHttpHeadersFilter", new CachingHttpHeadersFilter()); cachingHttpHeadersFilter.addMappingForUrlPatterns(disps, true, "/images/*"); cachingHttpHeadersFilter.addMappingForUrlPatterns(disps, true, "/fonts/*"); cachingHttpHeadersFilter.addMappingForUrlPatterns(disps, true, "/scripts/*"); cachingHttpHeadersFilter.addMappingForUrlPatterns(disps, true, "/styles/*"); cachingHttpHeadersFilter.setAsyncSupported(true); } }