package eu.ttbox.androgister.config.web; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.CharEncoding; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.context.support.ReloadableResourceBundleMessageSource; import org.springframework.core.env.Environment; import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.View; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.i18n.SessionLocaleResolver; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import org.springframework.web.servlet.view.ContentNegotiatingViewResolver; import org.springframework.web.servlet.view.JstlView; import org.springframework.web.servlet.view.UrlBasedViewResolver; import org.springframework.web.servlet.view.json.MappingJackson2JsonView; import eu.ttbox.androgister.config.ApplicationConfiguration; @Configuration @ComponentScan("eu.ttbox.androgister.web") @EnableWebMvc @PropertySource({ "classpath:/androgister.properties", "classpath:/customization.properties" }) // @ImportResource("classpath:META-INF/spring/applicationContext-metrics.xml") public class DispatcherServletConfig extends WebMvcConfigurerAdapter { private final Logger log = LoggerFactory.getLogger(ApplicationConfiguration.class); @Autowired private Environment env; @Bean public ViewResolver ContentNegotiatingViewResolver() { ContentNegotiatingViewResolver viewResolver = new ContentNegotiatingViewResolver(); List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>(); UrlBasedViewResolver urlBasedViewResolver = new UrlBasedViewResolver(); urlBasedViewResolver.setViewClass(JstlView.class); urlBasedViewResolver.setPrefix("/WEB-INF/jsp/"); urlBasedViewResolver.setSuffix(".jsp"); viewResolvers.add(urlBasedViewResolver); viewResolver.setViewResolvers(viewResolvers); List<View> defaultViews = new ArrayList<View>(); defaultViews.add(new MappingJackson2JsonView()); viewResolver.setDefaultViews(defaultViews); return viewResolver; } @Bean public SessionLocaleResolver localeResolver() { SessionLocaleResolver resolver = new SessionLocaleResolver(); return resolver; } @Bean public LocaleChangeInterceptor localeChangeInterceptor() { LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor(); localeChangeInterceptor.setParamName("language"); return localeChangeInterceptor; } @Bean public MessageSource messageSource() { ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); messageSource.setBasename("/WEB-INF/messages/messages"); messageSource.setDefaultEncoding(CharEncoding.UTF_8); if ("true".equals(env.getProperty("app.message.reloading.enabled"))) { messageSource.setCacheSeconds(1); } return messageSource; } @Bean public MultipartResolver multipartResolver() { CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(); multipartResolver.setMaxUploadSize(10000000); // 10 Mo max file size return multipartResolver; } @Bean public RequestMappingHandlerMapping requestMappingHandlerMapping() { RequestMappingHandlerMapping requestMappingHandlerMapping = new RequestMappingHandlerMapping(); requestMappingHandlerMapping.setUseSuffixPatternMatch(false); Object[] interceptors = { localeChangeInterceptor() }; requestMappingHandlerMapping.setInterceptors(interceptors); return requestMappingHandlerMapping; } // @Override // public void addResourceHandlers(ResourceHandlerRegistry registry) { // registry.addResourceHandler("/static-wro4j/" + env.getProperty("app.version") + "/**") // // .addResourceLocations("/WEB-INF/generated-wro4j/") // // .setCachePeriod(60 * 60 * 24 * 30); // // registry.addResourceHandler("/static/" + env.getProperty("app.version") + "/**")// // .addResourceLocations("/js/") // // .setCachePeriod(60 * 60 * 24 * 30); // } // @Override // public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) { // exceptionResolvers.add(new HandlerExceptionResolver() { // // @Override // public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { // try { // response.sendError(HttpServletResponse.SC_FORBIDDEN); // return new ModelAndView(); // } catch (Exception handlerException) { // log.warn("Handling of [" + ex.getClass().getName() + "] resulted in Exception", handlerException); // } // return null; // } // }); // } }