net.przemkovv.sphinx.config.WebMvcConfig.java Source code

Java tutorial

Introduction

Here is the source code for net.przemkovv.sphinx.config.WebMvcConfig.java

Source

/*
 * The MIT License
 *
 * Copyright 2013 Przemyslaw Walkowiak <przemyslaw.walkowiak@put.poznan.pl>.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package net.przemkovv.sphinx.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import java.util.Locale;
import net.przemkovv.sphinx.web.cache.IFNoneMatchHandlerInterceptor;
import net.przemkovv.sphinx.web.util.ReloadableMessageSource;
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.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.aspectj.EnableSpringConfigured;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;

/**
 *
 * @author Przemyslaw Walkowiak <przemyslaw.walkowiak@put.poznan.pl>
 */
@Configuration
@EnableWebMvc
@EnableSpringConfigured
@ComponentScan(basePackages = { "net.przemkovv.sphinx.web" }, basePackageClasses = {
        net.przemkovv.sphinx.web.cache.IFNoneMatchHandlerInterceptor.class }, useDefaultFilters = false, includeFilters = {
                @Filter(value = { Controller.class }) })
@ImportResource("/WEB-INF/spring-servlet.xml")
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    private static final Logger logger = LoggerFactory.getLogger(WebMvcConfig.class);

    @Bean
    public ViewResolver viewResolver() {
        logger.debug("Bean initialization: ViewResolver");
        TilesViewResolver resolver = new TilesViewResolver();
        resolver.setOrder(0);
        resolver.setViewClass(org.springframework.web.servlet.view.tiles3.TilesView.class);
        return resolver;
    }

    @Bean
    public TilesConfigurer tilesConfigurer() {
        logger.debug("Bean initialization: TilesConfigurer");
        TilesConfigurer configurer = new TilesConfigurer();
        configurer.setDefinitions(new String[] { "/WEB-INF/tiles-defs.xml" });
        return configurer;
    }

    @Bean
    public ObjectMapper objectMapper() {
        logger.debug("Bean initialization: ObjectMapper");
        return new ObjectMapper();
    }

    @Bean
    public MultipartResolver multipartResolver() {
        logger.debug("Bean initialization: MultipartResolver");
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        resolver.setMaxUploadSize(10000000);
        return resolver;
    }

    @Bean
    public LocaleResolver localeResolver() {
        logger.debug("Bean initialization: LocaleResolver");
        SessionLocaleResolver resolver = new SessionLocaleResolver();
        resolver.setDefaultLocale(new Locale("pl", "pl"));
        return resolver;
    }

    @Autowired
    ApplicationConfig applicationConfig;

    @Bean
    public MessageSource messageSource() {
        logger.debug("Bean initialization: MessageSource");
        ReloadableMessageSource source = new ReloadableMessageSource();
        source.setCacheSeconds(5);
        source.setDefaultEncoding("UTF-8");
        source.setFallbackToSystemLocale(false);
        source.setUseCodeAsDefaultMessage(true);
        source.setParentMessageSource(applicationConfig.applicationMessageSource());
        source.setBasenames("classpath:errors", "classpath:messages");
        return source;

    }

    @Bean
    public IFNoneMatchHandlerInterceptor ifNoneMatchHandlerInterceptor() {
        IFNoneMatchHandlerInterceptor interceptor = new IFNoneMatchHandlerInterceptor();
        interceptor.setLocaleResolver(localeResolver());
        interceptor.setMessageSource(messageSource());
        return interceptor;
    }

    @Override
    public void addInterceptors(final InterceptorRegistry registry) {
        logger.debug("MVC Initialization: adding interceptors");

        registry.addInterceptor(ifNoneMatchHandlerInterceptor());
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        logger.debug("MVC Initialization: default servlet handling");
        configurer.enable();
    }

    //        Serve static content from / folder via /resources endpoint
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        logger.debug("MVC Initialization: adding resource handlers");
        registry.addResourceHandler("/resources/**").addResourceLocations("/");
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        logger.debug("MVC Initialization: configuring message converters");
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(objectMapper());
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        logger.debug("MVC Initialization: adding argument resolvers");
        argumentResolvers.add(new org.springframework.mobile.device.DeviceHandlerMethodArgumentResolver());
    }

}