com.mjeanroy.backbone_isomorphic.configuration.AppConfiguration.java Source code

Java tutorial

Introduction

Here is the source code for com.mjeanroy.backbone_isomorphic.configuration.AppConfiguration.java

Source

/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 <mickael.jeanroy@gmail.com>
 *
 * 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 com.mjeanroy.backbone_isomorphic.configuration;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.mjeanroy.springmvc.view.mustache.configuration.EnableMustache;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.resource.GzipResourceResolver;
import org.springframework.web.servlet.resource.PathResourceResolver;
import org.springframework.web.util.UrlPathHelper;

import java.util.List;

@Configuration
@EnableWebMvc
@EnableMustache
@ComponentScan(basePackages = "com.mjeanroy.backbone_isomorphic")
public class AppConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        for (HttpMessageConverter<?> httpMessageConverter : converters) {
            if (httpMessageConverter instanceof MappingJackson2HttpMessageConverter) {
                ((MappingJackson2HttpMessageConverter) httpMessageConverter).setObjectMapper(jsonObjectMapper());
            }
        }
    }

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        urlPathHelper.setAlwaysUseFullPath(true);

        configurer.setUseSuffixPatternMatch(false).setUrlPathHelper(urlPathHelper);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**/*.js").addResourceLocations("/").setCachePeriod(3600).resourceChain(true)
                .addResolver(new GzipResourceResolver()).addResolver(new PathResourceResolver());

        registry.addResourceHandler("/**/*.css").addResourceLocations("/").setCachePeriod(3600).resourceChain(true)
                .addResolver(new GzipResourceResolver()).addResolver(new PathResourceResolver());

        registry.addResourceHandler("/**/*.map").addResourceLocations("/").setCachePeriod(3600).resourceChain(true)
                .addResolver(new GzipResourceResolver()).addResolver(new PathResourceResolver());

        registry.addResourceHandler("/**/*.html").addResourceLocations("/").resourceChain(true)
                .addResolver(new GzipResourceResolver()).addResolver(new PathResourceResolver());
    }

    @Bean
    public ObjectMapper jsonObjectMapper() {
        return Jackson2ObjectMapperBuilder.json().failOnEmptyBeans(false).failOnUnknownProperties(false).build();
    }
}