de.pksoftware.springstrap.core.config.WebMvcConfigBase.java Source code

Java tutorial

Introduction

Here is the source code for de.pksoftware.springstrap.core.config.WebMvcConfigBase.java

Source

/*
 * 
 * Springstrap
 *
 * @author Jan Philipp Knller <info@pksoftware.de>
 * 
 * Homepage: http://ui5strap.com/springstrap
 *
 * Copyright (c) 2013-2014 Jan Philipp Knller <info@pksoftware.de>
 * 
 * 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.
 * Released under Apache2 license: http://www.apache.org/licenses/LICENSE-2.0.txt
 * 
 */

package de.pksoftware.springstrap.core.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
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.LocaleChangeInterceptor;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import de.pksoftware.springstrap.core.util.ConfigUtils;
import de.pksoftware.springstrap.fs.service.IResourceProvider;
import de.pksoftware.springstrap.fs.service.IFileSystemService;

@Configuration
public abstract class WebMvcConfigBase extends WebMvcConfigurerAdapter {

    protected static final Logger logger = LoggerFactory.getLogger(WebMvcConfigBase.class);

    @Autowired(required = false)
    @Qualifier("mainFileSystemServiceBean")
    protected IFileSystemService mainFileSystemService;

    /**
     * Enable default servlet handling
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    /*
     * Resources
     */

    /**
     * Add the Resource Handlers.
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        addUi5StrapResourceHandlers(registry);
        addSpringstrapResourceHandlers(registry);
    }

    /**
     * Add Resource Handlers for Ui5Strap.
     * 
     * @param registry
     */
    protected void addUi5StrapResourceHandlers(ResourceHandlerRegistry registry) {
        //Register ui5strap library
        ConfigUtils.addLibraryResourceHandler(registry, SpringstrapConfiguration.ARTIFACT_UI5STRAP,
                SpringstrapConfiguration.UI5LIB_UI5STRAP);

        //Register sap library
        ConfigUtils.addLibraryResourceHandler(registry, SpringstrapConfiguration.ARTIFACT_UI5STRAP,
                SpringstrapConfiguration.UI5LIB_SAP);

        //Register Main Scripts
        ConfigUtils.addMainScriptsResourceHandler(registry, SpringstrapConfiguration.ARTIFACT_UI5STRAP);
    }

    /**
     * Add Resource Handlers for Springstrap.
     * 
     * @param registry
     */
    protected void addSpringstrapResourceHandlers(ResourceHandlerRegistry registry) {
        ConfigUtils.addLibraryResourceHandler(registry, SpringstrapConfiguration.ARTIFACT_SPRINGSTRAP,
                SpringstrapConfiguration.UI5LIB_SPRINGSTRAP);

        if (null != mainFileSystemService && mainFileSystemService instanceof IResourceProvider) {
            logger.info("Added resource handlers for main file system service.");
            ((IResourceProvider) mainFileSystemService).addResourceHandlers(registry);
        }
    }

    /*
     * View Resolver
     */

    /**
     * Adds the Interceptors
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }

    /**
     * Interceptor Bean for changing language.
     * 
     * @return
     */
    protected LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
        localeChangeInterceptor.setParamName("lang");
        return localeChangeInterceptor;
    }

    /**
     * Expose InternalResourceViewResolver as Bean.
     * 
     * @return
     */
    @Bean
    public InternalResourceViewResolver internalResourceViewResolverBean() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

}