de.pksoftware.springstrap.core.util.ConfigUtils.java Source code

Java tutorial

Introduction

Here is the source code for de.pksoftware.springstrap.core.util.ConfigUtils.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.util;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import de.pksoftware.springstrap.core.config.SpringstrapConfiguration;

public class ConfigUtils {
    /**
     * Creates the filter chain.
     * @param sContext
     */
    public static void createFilterChain(ServletContext sContext) {
        // EnumSet<DispatcherType> dispatcherTypes =
        // EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD);

        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);

        FilterRegistration.Dynamic characterEncoding = sContext.addFilter("characterEncoding",
                characterEncodingFilter);
        characterEncoding.addMappingForUrlPatterns(null, true, "/*");

        DelegatingFilterProxy filter = new DelegatingFilterProxy("springSecurityFilterChain");
        //TODO Why is this needed?
        filter.setContextAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher");

        sContext.addFilter("springSecurityFilterChain", filter).addMappingForUrlPatterns(null, false, "/*");

        // FilterRegistration.Dynamic security =
        // sContext.addFilter("springSecurityFilterChain", new
        // DelegatingFilterProxy());
        // security.addMappingForUrlPatterns(null, true, "/*");
    }

    public static String getWebjarClasspathLocation(String artifactId, String path) {
        if (!path.startsWith("/")) {
            path = "/" + path;
        }

        if (!path.endsWith("/")) {
            path = path + "/";
        }

        return "classpath:/webjar/" + artifactId.replace(".", "/") + path;
    }

    public static void addMainScriptsResourceHandler(ResourceHandlerRegistry registry, String artifactId) {
        // TODO Find out why we need the wildcard in the filename.
        // SAPUI5 Core
        registry.addResourceHandler(SpringstrapConfiguration.URL_BASE_WWW_LIB + "sap-ui-cor*.js")
                .addResourceLocations(ConfigUtils.getWebjarClasspathLocation(artifactId,
                        SpringstrapConfiguration.CLASSPATH_WWW_LIB));
    }

    public static void addLibraryResourceHandler(ResourceHandlerRegistry registry, String artifactId,
            String libraryName) {
        //Register Library
        registry.addResourceHandler(SpringstrapConfiguration.URL_BASE_WWW_LIB + libraryName + "/**")
                .addResourceLocations(ConfigUtils.getWebjarClasspathLocation(artifactId,
                        SpringstrapConfiguration.CLASSPATH_WWW_LIB + libraryName + "/"));
    }
}