org.cattleframework.webmvc.CattleWebMvcConfigurer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cattle-webmvc Show documentation
Show all versions of cattle-webmvc Show documentation
Cattle framework webmvc component pom
The 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 org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.cattleframework.aop.utils.ReflectionsFactory;
import org.cattleframework.aop.utils.SimpleReflectUtils;
import org.cattleframework.web.WebProperties;
import org.cattleframework.web.WebProperties.StaticResource;
import org.cattleframework.web.WebProperties.StaticResourceCache;
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.ResourceHandlerRegistration;
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 -> resolvers
.add((HandlerMethodArgumentResolver) SimpleReflectUtils.instance(methodArgumentResolverClass)));
}
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
List staticResources = webProperties.getStaticResources();
if (CollectionUtils.isEmpty(staticResources)) {
return;
}
StaticResourceCache cache = webProperties.getStaticResourceCache();
CacheControl cacheControl = null;
if (cache != null) {
Duration maxAge = cache.getMaxAge();
if (maxAge != null) {
if (maxAge.getSeconds() > 0L) {
cacheControl = CacheControl.maxAge(maxAge);
if (cache.isMustRevalidate()) {
cacheControl = cacheControl.mustRevalidate();
}
} else if (maxAge.getSeconds() == 0L) {
cacheControl = cache.isNoStore() ? CacheControl.noStore() : CacheControl.noCache();
}
}
if (cacheControl != null) {
if (cache.isCachePrivate()) {
cacheControl = cacheControl.cachePrivate();
}
if (cache.isCachePublic()) {
cacheControl = cacheControl.cachePublic();
}
if (cache.isNoTransform()) {
cacheControl = cacheControl.noTransform();
}
}
}
for (StaticResource staticResource : staticResources) {
if (StringUtils.isNotBlank(staticResource.getUrlPattern())
&& CollectionUtils.isNotEmpty(staticResource.getDirectoryLocations())) {
ResourceHandlerRegistration registration = registry.addResourceHandler(staticResource.getUrlPattern())
.addResourceLocations(staticResource.getDirectoryLocations().toArray(new String[0]));
if (cacheControl != null) {
registration.setCacheControl(cacheControl);
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy