com.github.ukase.config.WebConfig.java Source code

Java tutorial

Introduction

Here is the source code for com.github.ukase.config.WebConfig.java

Source

/*
 * Copyright (c) 2015 Konstantin Lepa <konstantin+ukase@lepabox.net>
 *
 * This file is part of Ukase.
 *
 *  Ukase is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package com.github.ukase.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.web.servlet.config.annotation.ResourceChainRegistration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.resource.AppCacheManifestTransformer;
import org.springframework.web.servlet.resource.PathResourceResolver;
import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
    @Autowired
    private Environment env;
    @Autowired
    private UkaseSettings settings;
    private boolean devMode;

    @PostConstruct
    private void init() {
        devMode = env.acceptsProfiles("development");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/index.html");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String location = "classpath:static/";

        if (devMode) {
            Path path = Paths.get(getProjectRootRequired(), "ui", "target", "dist");
            location = path.toUri().toString();
        }

        newResourceHandler(registry, "/bundle.*.js", location).addTransformer(new AppCacheManifestTransformer());

        newResourceHandler(registry, "/cache*.appcache", location);

        newResourceHandler(registry, "/**", location + "index.html").addResolver(new PathResourceResolver() {
            @Override
            protected Resource getResource(String resourcePath, Resource location) throws IOException {
                Resource resource = location.createRelative(resourcePath);
                return resource.exists() && resource.isReadable() ? resource : null;
            }
        });
    }

    private ResourceChainRegistration newResourceHandler(ResourceHandlerRegistry registry, String pathPattern,
            String location) {
        Integer cachePeriod = devMode ? 0 : null;
        boolean useResourceCache = !devMode;

        return registry.addResourceHandler(pathPattern).addResourceLocations(location).setCachePeriod(cachePeriod)
                .resourceChain(useResourceCache);
    }

    private String getProjectRootRequired() {
        Assert.state(settings.getProjectRoot() != null, "Please set \"ukase.project.root\" in application.yml");
        return settings.getProjectRoot();
    }

    @Bean
    public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
        return new ResourceUrlEncodingFilter();
    }
}