181 lines
5.0 KiB
Java
181 lines
5.0 KiB
Java
package com.tiesheng.util.config;
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
import cn.hutool.core.comparator.VersionComparator;
|
|
import cn.hutool.core.io.FileUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.URLUtil;
|
|
import cn.hutool.extra.spring.SpringUtil;
|
|
import cn.hutool.http.HttpGlobalConfig;
|
|
import cn.hutool.log.LogFactory;
|
|
import com.tiesheng.util.exception.ApiException;
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.core.io.Resource;
|
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
|
|
|
import javax.annotation.PostConstruct;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* @author hao
|
|
*/
|
|
@Configuration
|
|
@ConfigurationProperties(prefix = "tiesheng.global")
|
|
public class GlobalConfig {
|
|
|
|
private String host;
|
|
private String service;
|
|
private String version;
|
|
private String uploadDir = System.getProperty("user.dir");
|
|
|
|
/**
|
|
* 其他属性
|
|
*/
|
|
private HashMap<String, String> ext;
|
|
|
|
|
|
@PostConstruct
|
|
public void init() {
|
|
// 默认10s的超时时间
|
|
HttpGlobalConfig.setTimeout(10 * 1000);
|
|
// 最多重定向3次
|
|
HttpGlobalConfig.setMaxRedirectCount(3);
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
// 逻辑方法
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
* 获取扩展属性内容
|
|
*
|
|
* @param extkey
|
|
* @return
|
|
*/
|
|
public String getExtValue(String extkey) {
|
|
if (getExt() == null) {
|
|
return "";
|
|
}
|
|
return getExt().get(extkey);
|
|
}
|
|
|
|
public String getContextPath() {
|
|
String context = SpringUtil.getProperty("server.servlet.context-path");
|
|
if (StrUtil.isEmpty(context)) {
|
|
context = "";
|
|
}
|
|
return context;
|
|
}
|
|
|
|
|
|
/**
|
|
* 构建完整的路径
|
|
*
|
|
* @param path
|
|
* @return
|
|
*/
|
|
public String buildPath(String path) {
|
|
if (StrUtil.isEmpty(path)) {
|
|
path = "";
|
|
}
|
|
if (!StrUtil.isEmpty(host) && !StrUtil.startWith(path, "http")) {
|
|
path = host + getContextPath() + path;
|
|
}
|
|
return path;
|
|
}
|
|
|
|
|
|
/**
|
|
* 重定向
|
|
*
|
|
* @param htmlDir 资源目录
|
|
* @param route
|
|
*/
|
|
public void redirect(String htmlDir, String route, HttpServletResponse response) {
|
|
if (!StrUtil.endWith(htmlDir, "/")) {
|
|
htmlDir = htmlDir + "/";
|
|
}
|
|
PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
|
|
try {
|
|
List<String> versions = new ArrayList<>();
|
|
|
|
// jar包中的资源
|
|
Resource[] resources = patternResolver.getResources(String.format("classpath*:static/%s*/index.html", htmlDir));
|
|
for (Resource resource : resources) {
|
|
String path = FileUtil.normalize(resource.getURL().getPath());
|
|
versions.add(StrUtil.subBetween(path, htmlDir, "/index.html"));
|
|
}
|
|
|
|
// 目录中的资源
|
|
String folder = String.format("%s/static/%s", System.getProperty("user.dir"), htmlDir);
|
|
if (FileUtil.exist(folder)) {
|
|
File[] files = FileUtil.ls(folder);
|
|
for (File file : files) {
|
|
String normalize = FileUtil.normalize(file.getAbsolutePath());
|
|
versions.add(StrUtil.subAfter(normalize, htmlDir, true));
|
|
}
|
|
}
|
|
|
|
if (CollUtil.isEmpty(versions)) {
|
|
throw new ApiException("无法重定向,请检查资源");
|
|
}
|
|
CollUtil.sort(versions, (o1, o2) -> -VersionComparator.INSTANCE.compare(o1, o2));
|
|
String path = buildPath(String.format("/%s%s/index.html#%s", htmlDir, versions.get(0), route));
|
|
response.sendRedirect(path);
|
|
} catch (IOException e) {
|
|
LogFactory.get().info(e);
|
|
}
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
// setter\getter
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
public String getUploadDir() {
|
|
return uploadDir;
|
|
}
|
|
|
|
public void setUploadDir(String uploadDir) {
|
|
this.uploadDir = uploadDir;
|
|
}
|
|
|
|
public String getHost() {
|
|
return host;
|
|
}
|
|
|
|
public void setHost(String host) {
|
|
this.host = host;
|
|
}
|
|
|
|
public String getService() {
|
|
return service;
|
|
}
|
|
|
|
public void setService(String service) {
|
|
this.service = service;
|
|
}
|
|
|
|
public String getVersion() {
|
|
return version;
|
|
}
|
|
|
|
public void setVersion(String version) {
|
|
this.version = version;
|
|
}
|
|
|
|
public HashMap<String, String> getExt() {
|
|
return ext;
|
|
}
|
|
|
|
public void setExt(HashMap<String, String> ext) {
|
|
this.ext = ext;
|
|
}
|
|
}
|