This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
nezha-nz-talon/src/main/java/net/geedge/confagent/util/ConfagentUtil.java
2021-07-09 17:01:27 +08:00

139 lines
4.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package net.geedge.confagent.util;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.log.Log;
@Configuration
@Order(value = 1)
public class ConfagentUtil {
private final static Log log = Log.get();
@Value("${confagent.tokenFile:config/token.auth}")
protected String tokenFile; //token文件位置
@Value("${confagent.versionFile:promtail.version}")
private String versionFile;
private static String rootPath = Tool.WebPathUtil.getRootPath();
/**
* @Description 读取token文件中的token
* @Author rui
* @Date 2021/3/25
*/
public String readToken() throws IORuntimeException {
File tf = Tool.FileUtil.file(rootPath, tokenFile);
log.info("token file path : {}" ,tf.getAbsolutePath());
String token = Tool.FileUtil.readString(tf, Tool.CharsetUtil.UTF_8);
return token;
}
/**
* @Description 读取version
* @Author rui
* @Date 2021/3/25
*/
public String readVersion() throws IORuntimeException {
String version = "1";
File tf = Tool.FileUtil.file(rootPath, versionFile);
if(tf!=null && tf.exists()) {
log.info("version file path : {}" ,tf.getAbsolutePath());
version = Tool.FileUtil.readString(tf, Tool.CharsetUtil.UTF_8);
}
return version;
}
/**
* @Description 检查token 是否和文件中的一致
* @Author rui
* @Date 2021/3/25
*/
public R checkToken(String token){
if(Tool.StrUtil.isBlank(token)){
return R.error(RCode.AUTH_TOKEN_ISNULL);
}
try{
String readToken = this.readToken();
if(Tool.StrUtil.equals(readToken,token)){
return R.ok();
}
return R.error(RCode.AUTH_TOKEN_INVALID);
}catch (IORuntimeException e){
log.error("read token file failed",e);
return R.error(RCode.AUTH_READ_TOKEN_FAILD);
}
}
/**
* @Description 加载service EnvironmentFile中的内容并整理为map
* @Author rui
* @Date 2021/3/24
*/
public static Map<String,String> loadServiceConfigFile(String path){
File file = Tool.FileUtil.file(path);
if(!Tool.FileUtil.exist(file)){
return null;
}
String readLine = Tool.FileUtil.readUtf8String(file);
//匹配有参类型 --[\w\.-]+?\s 可匹配无参类型
Pattern pattern = Pattern.compile("--([\\w\\.-]+?)='?(.+?)'?\\s+?");
Matcher matcher = pattern.matcher(readLine);
Map<String, String> result = new HashMap<>();
while(matcher.find()){
result.put(matcher.group(1).trim(),matcher.group(2).trim());
}
return result;
}
/**
* @Description 获取配置在config.conf启动参数文件中的端口
* @Author rui
* @Date 2021/3/25
*/
public static Integer getConfFilePort(String path,String key,Integer defaultValue){
Map<String, String> configs = loadServiceConfigFile(path);
if(configs!=null) {
String address = configs.get(key);
String[] split = address.split(":");
return Tool.StrUtil.isNotBlank(split[1])? Integer.parseInt(split[1]):defaultValue;
}else {
return defaultValue;
}
}
public static void writeResponse(HttpServletResponse response, Object o) {
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
Tool.IoUtil.writeUtf8 (outputStream,true, Tool.JSONUtil.parseObj(o).toString());
outputStream.flush();
}catch (IOException e){
log.error("write response failed :",e);
}finally {
Tool.IoUtil.close(outputStream);
}
}
}