com.alehuo.wepas2016projekti.configuration.DevelopmentConfiguration.java Source code

Java tutorial

Introduction

Here is the source code for com.alehuo.wepas2016projekti.configuration.DevelopmentConfiguration.java

Source

/*
 * Copyright (C) 2016 alehuo
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.alehuo.wepas2016projekti.configuration;

import java.util.Locale;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
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.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

/**
 * Kehitysympristn konfiguraatio
 * 
 * @author alehuo
 */
@Configuration
@Profile({ "default", "development" })
@EnableWebSecurity
@EnableCaching
public class DevelopmentConfiguration extends WebMvcConfigurerAdapter {

    /**
     * Resurssien "mappaus"
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
        super.addResourceHandlers(registry);
    }

    /**
     * Lokalisaatiotiedostojen lataaminen
     * @return 
     */
    @Bean
    public ReloadableResourceBundleMessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:lang");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    /**
     * Lokalisaatioevsteen asetus
     * 
     * Oletusarvoinen lokalisaatio: fi_FI
     * 
     * @return 
     */
    @Bean
    public CookieLocaleResolver localeResolver() {
        CookieLocaleResolver localeResolver = new CookieLocaleResolver();
        Locale finnishLocale = new Locale.Builder().setLanguage("fi").setRegion("FI").build();
        localeResolver.setDefaultLocale(finnishLocale);
        localeResolver.setCookieName("locale");
        //Evste vanhenee tunnissa
        localeResolver.setCookieMaxAge(3600);
        return localeResolver;
    }

    /**
     * Tmn avulla voidaan asettaa sivuston kieli lang GET -parametrill.
     * Esim. http://localhost:8080?lang=fi
     * 
     * @return 
     */
    @Bean
    public LocaleChangeInterceptor localeInterceptor() {
        LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
        interceptor.setParamName("lang");
        return interceptor;
    }

    /**
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeInterceptor());
    }

}