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

com.jxrisesun.framework.boot.starter.logic.SimpleBootStarterLogic Maven / Gradle / Ivy

package com.jxrisesun.framework.boot.starter.logic;

import java.io.File;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import com.jxrisesun.framework.boot.core.constant.Constants;
import com.jxrisesun.framework.boot.starter.repeat.interceptor.RepeatSubmitInterceptor;
import com.jxrisesun.framework.core.config.properties.CoreProperties;
import com.jxrisesun.framework.core.utils.SpringUtils;
import com.jxrisesun.framework.core.utils.StringUtils;
import com.jxrisesun.framework.spring.storage.StorageEngine;
import com.jxrisesun.framework.spring.storage.StorageType;
import com.jxrisesun.framework.spring.storage.gofastdfs.GofastdfsResourceTransformer;
import com.jxrisesun.framework.spring.storage.util.StorageLogicUtils;

public class SimpleBootStarterLogic implements BootStarterLogic {
	
	private final static Logger LOGGER = LoggerFactory.getLogger(SimpleBootStarterLogic.class);

	@Override
	public void configWebResourceHandlers(ResourceHandlerRegistry registry) {
		
		this.addProfileResourceHandler(registry);
		
		this.addSwaggerResourceHandler(registry);
	}
	
	/**
	 * 添加 profile 资源处理器
	 * @param registry
	 */
	protected void addProfileResourceHandler(ResourceHandlerRegistry registry) {
		StorageType storageType = StorageType.parse(CoreProperties.getInstance().getStorageType());
		String mappingPath = CoreProperties.getInstance().getStorageMappingPath();
		if(storageType == null) {
			LOGGER.info("---------- [rs-framework-boot-starter] 不支持的文件存储类型:{},已忽略资源路径映射! ----------", CoreProperties.getInstance().getStorageType());
			return;
		}
		StorageEngine storageEngine = StorageLogicUtils.getStorageEngine();
		String mappingLocation = storageEngine.getConfig().getStorageMappingLocation();
		if(StringUtils.isEmpty(mappingPath) || StringUtils.isEmpty(mappingLocation)) {
			LOGGER.info("---------- [rs-framework-boot-starter] 存储类型:{} 未发现存储映射配置,已忽略资源路径映射! ----------", storageType.getValue());
			return;
		}
		String pathPattern = mappingPath + "/**";
		if(StorageType.LOCAL.equals(storageType)) {
			/** 访问本地文件上传路径 */
			String location = "file:" + mappingLocation + File.separator;
			registry.addResourceHandler(pathPattern)
					.addResourceLocations(location);
			LOGGER.info("---------- [rs-framework-boot-starter] 存储类型:{}, 已映射资源路径 -> {} -> {} ----------", storageType.getValue(), pathPattern, location);
		} else {
			/** 访问远程http(go-fastdfs)文件路径 */
			registry.addResourceHandler(pathPattern)
					.addResourceLocations(mappingLocation)
					.resourceChain(false)
					.addTransformer(new GofastdfsResourceTransformer());
			LOGGER.info("---------- [rs-framework-boot-starter] 存储类型:{}, 已映射资源路径 -> {} -> {} ----------", storageType.getValue(), pathPattern, mappingLocation);
		}
	}
	
	/**
	 * 添加 swagger 资源处理器
	 * @param registry
	 */
	protected void addSwaggerResourceHandler(ResourceHandlerRegistry registry) {
		/** swagger配置 */
		registry.addResourceHandler("/swagger-ui/**")
				.addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
				.setCacheControl(CacheControl.maxAge(5, TimeUnit.HOURS).cachePublic());
	}
	
	@Override
	public void configWebInterceptors(InterceptorRegistry registry) {
		this.addRepeatSubmitInterceptor(registry);
	}
	
	/**
	 * 添加重复提交拦截器
	 * @param registry
	 */
	protected void addRepeatSubmitInterceptor(InterceptorRegistry registry) {
		RepeatSubmitInterceptor repeatSubmitInterceptor = SpringUtils.getBeansOfTypeSingle(RepeatSubmitInterceptor.class);
		if(repeatSubmitInterceptor != null) {
			registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy