All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.cattleframework.webmvc.CattleWebMvcConfigurer Maven / Gradle / Ivy

There is a newer version: 1.0.1-M3
Show newest version
/*
 * Copyright (C) 2018 the original author or authors.
 *
 * 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.
 */
package org.cattleframework.webmvc;

import java.time.Duration;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.cattleframework.aop.utils.ReflectionsFactory;
import org.cattleframework.aop.utils.SimpleReflectUtils;
import org.cattleframework.exception.CommonException;
import org.cattleframework.exception.ExceptionWrapUtils;
import org.cattleframework.web.WebProperties;
import org.cattleframework.web.WebProperties.StaticResource;
import org.cattleframework.web.annotation.MethodArgumentResolver;
import org.springframework.http.CacheControl;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Web Mvc配置
 * 
 * @author orange
 *
 */
class CattleWebMvcConfigurer implements WebMvcConfigurer {

    private final WebProperties webProperties;

    public CattleWebMvcConfigurer(WebProperties webProperties) {
	this.webProperties = webProperties;
    }

    @Override
    public void addArgumentResolvers(List resolvers) {
	List> methodArgumentResolverClasses = ReflectionsFactory
		.getTypesAnnotatedWith(MethodArgumentResolver.class);
	if (CollectionUtils.isNotEmpty(methodArgumentResolverClasses)) {
	    methodArgumentResolverClasses.forEach(methodArgumentResolverClass -> {
		try {
		    resolvers.add(
			    (HandlerMethodArgumentResolver) SimpleReflectUtils.instance(methodArgumentResolverClass));
		} catch (CommonException e) {
		    throw ExceptionWrapUtils.wrapRuntime(e);
		}
	    });
	}
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
	Set staticResources = webProperties.getStaticResources();
	staticResources.stream().forEachOrdered(staticResource -> {
	    if (StringUtils.isNotBlank(staticResource.getStaticPathPattern())
		    && ArrayUtils.isNotEmpty(staticResource.getDirectoryLocations())) {
		Duration maxAge = staticResource.getMaxAge();
		CacheControl cacheControl = null;
		boolean cacheParas = true;
		if (maxAge != null) {
		    if (maxAge.getSeconds() > 0L) {
			cacheControl = CacheControl.maxAge(maxAge);
			if (staticResource.isMustRevalidate()) {
			    cacheControl = cacheControl.mustRevalidate();
			}
		    } else if (maxAge.getSeconds() == 0L) {
			cacheControl = staticResource.isNoStore() ? CacheControl.noStore() : CacheControl.noCache();
			if (staticResource.isNoStore()) {
			    cacheParas = false;
			}
		    }
		}
		if (cacheControl == null) {
		    cacheControl = CacheControl.empty();
		}
		if (cacheParas) {
		    if (staticResource.isCachePrivate()) {
			cacheControl = cacheControl.cachePrivate();
		    }
		    if (staticResource.isCachePublic()) {
			cacheControl = cacheControl.cachePublic();
		    }
		    if (staticResource.isNoTransform()) {
			cacheControl = cacheControl.noTransform();
		    }
		}
		registry.addResourceHandler(staticResource.getStaticPathPattern())
			.addResourceLocations(staticResource.getDirectoryLocations()).setCacheControl(cacheControl);
	    }
	});
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy