parent
8d86d19659
commit
6974d06d77
|
@ -1,373 +1,377 @@
|
||||||
package luckyclient.execution.dispose;
|
package luckyclient.execution.dispose;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONArray;
|
||||||
import com.alibaba.fastjson.JSONException;
|
import com.alibaba.fastjson.JSONException;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.alibaba.fastjson.TypeReference;
|
import com.alibaba.fastjson.TypeReference;
|
||||||
|
|
||||||
import luckyclient.utils.LogUtil;
|
import luckyclient.utils.LogUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对参数替换进行处理
|
* 对参数替换进行处理
|
||||||
* =================================================================
|
* =================================================================
|
||||||
* 这是一个受限制的自由软件!您不能在任何未经允许的前提下对程序代码进行修改和用于商业用途;也不允许对程序代码修改后以任何形式任何目的的再发布。
|
* 这是一个受限制的自由软件!您不能在任何未经允许的前提下对程序代码进行修改和用于商业用途;也不允许对程序代码修改后以任何形式任何目的的再发布。
|
||||||
* 为了尊重作者的劳动成果,LuckyFrame关键版权信息严禁篡改 有任何疑问欢迎联系作者讨论。 QQ:1573584944 seagull
|
* 为了尊重作者的劳动成果,LuckyFrame关键版权信息严禁篡改 有任何疑问欢迎联系作者讨论。 QQ:1573584944 seagull
|
||||||
* =================================================================
|
* =================================================================
|
||||||
* @author Seagull
|
* @author Seagull
|
||||||
* @date 2019年1月15日
|
* @date 2019年1月15日
|
||||||
*/
|
*/
|
||||||
public class ChangString {
|
public class ChangString {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 替换变量中的字符
|
* 替换变量中的字符
|
||||||
*
|
*
|
||||||
* @param str
|
* @param str
|
||||||
* @param variable
|
* @param variable
|
||||||
* @param changname
|
* @param changname
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static String changparams(String str, Map<String, String> variable, String changname) {
|
public static String changparams(String str, Map<String, String> variable, String changname) {
|
||||||
try {
|
try {
|
||||||
if (null == str) {
|
if (null == str) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
str = str.replace(""", "\"");
|
str = str.replace(""", "\"");
|
||||||
str = str.replace("'", "\'");
|
str = str.replace("'", "\'");
|
||||||
// @@用来注释@的引用作用
|
// @@用来注释@的引用作用
|
||||||
int varcount = counter(str, "@") - counter(str, "@@") * 2;
|
int varcount = counter(str, "@") - counter(str, "@@") * 2;
|
||||||
|
|
||||||
// 如果存在传参,进行处理
|
// 如果存在传参,进行处理
|
||||||
if (varcount > 0) {
|
if (varcount > 0) {
|
||||||
LogUtil.APP.info("在{}【{}】中找到{}个可替换参数",changname,str,varcount);
|
LogUtil.APP.info("在{}【{}】中找到{}个可替换参数",changname,str,varcount);
|
||||||
int changcount = 0;
|
int changcount = 0;
|
||||||
|
|
||||||
// 准备将HASHMAP换成LINKMAP,对KEY进行排序,解决要先替换最长KEY的问题
|
// 准备将HASHMAP换成LINKMAP,对KEY进行排序,解决要先替换最长KEY的问题
|
||||||
List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(variable.entrySet());
|
List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(variable.entrySet());
|
||||||
// 然后通过比较器来实现排序
|
// 然后通过比较器来实现排序
|
||||||
Collections.sort(list, new Comparator<Map.Entry<String, String>>() {
|
Collections.sort(list, new Comparator<Map.Entry<String, String>>() {
|
||||||
// 按KEY长度降序排序
|
// 按KEY长度降序排序
|
||||||
@Override
|
@Override
|
||||||
public int compare(Entry<String, String> o1, Entry<String, String> o2) {
|
public int compare(Entry<String, String> o1, Entry<String, String> o2) {
|
||||||
return o2.getKey().length() - o1.getKey().length();
|
return o2.getKey().length() - o1.getKey().length();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Map<String, String> aMap2 = new LinkedHashMap<String, String>();
|
Map<String, String> aMap2 = new LinkedHashMap<String, String>();
|
||||||
for (Map.Entry<String, String> mapping : list) {
|
for (Map.Entry<String, String> mapping : list) {
|
||||||
aMap2.put(mapping.getKey(), mapping.getValue());
|
aMap2.put(mapping.getKey(), mapping.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 从参数列表中查找匹配变量
|
// 从参数列表中查找匹配变量
|
||||||
for (Map.Entry<String, String> entry : aMap2.entrySet()) {
|
for (Map.Entry<String, String> entry : aMap2.entrySet()) {
|
||||||
if (str.contains("@" + entry.getKey())) {
|
if (str.contains("@" + entry.getKey())) {
|
||||||
if (str.contains("@@" + entry.getKey())) {
|
if (str.contains("@@" + entry.getKey())) {
|
||||||
str = str.replace("@@" + entry.getKey(), "////CHANG////");
|
str = str.replace("@@" + entry.getKey(), "////CHANG////");
|
||||||
}
|
}
|
||||||
// 用来替换字符串中带了\"或是\'会导致\消失的问题
|
// 用来替换字符串中带了\"或是\'会导致\消失的问题
|
||||||
// entry.setValue(entry.getValue().replaceAll("\\\\\"",
|
// entry.setValue(entry.getValue().replaceAll("\\\\\"",
|
||||||
// "\\""));
|
// "\\""));
|
||||||
// entry.setValue(entry.getValue().replaceAll("\\\\\'",
|
// entry.setValue(entry.getValue().replaceAll("\\\\\'",
|
||||||
// "\\\\'"));
|
// "\\\\'"));
|
||||||
int viewcount = counter(str, "@" + entry.getKey());
|
int viewcount = counter(str, "@" + entry.getKey());
|
||||||
str = str.replace("@" + entry.getKey(), entry.getValue());
|
str = str.replace("@" + entry.getKey(), entry.getValue());
|
||||||
LogUtil.APP.info("将{}引用变量【@{}】替换成值【{}】",changname,entry.getKey(),entry.getValue());
|
LogUtil.APP.info("将{}引用变量【@{}】替换成值【{}】",changname,entry.getKey(),entry.getValue());
|
||||||
str = str.replace("////CHANG////", "@@" + entry.getKey());
|
str = str.replace("////CHANG////", "@@" + entry.getKey());
|
||||||
changcount = changcount + viewcount;
|
changcount = changcount + viewcount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (varcount != changcount) {
|
if (varcount != changcount) {
|
||||||
LogUtil.APP.warn(changname + "有引用变量未在参数列中找到,请检查!处理结果【{}】",str);
|
LogUtil.APP.warn(changname + "有引用变量未在参数列中找到,请检查!处理结果【{}】",str);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
str = str.replace("@@", "@");
|
str = str.replace("@@", "@");
|
||||||
//对内置函数进行处理
|
//对内置函数进行处理
|
||||||
str=ParamsManageForSteps.paramsManage(str);
|
str=ParamsManageForSteps.paramsManage(str);
|
||||||
return str;
|
return str;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LogUtil.APP.error("替换参数过程中出现异常,请检查!",e);
|
LogUtil.APP.error("替换参数过程中出现异常,请检查!",e);
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 统计字符
|
* 统计字符
|
||||||
*
|
*
|
||||||
* @param str1
|
* @param str1
|
||||||
* @param str2
|
* @param str2
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static int counter(String str1, String str2) {
|
public static int counter(String str1, String str2) {
|
||||||
int total = 0;
|
int total = 0;
|
||||||
for (String tmp = str1; tmp != null && tmp.length() >= str2.length();) {
|
for (String tmp = str1; tmp != null && tmp.length() >= str2.length();) {
|
||||||
if (tmp.indexOf(str2) == 0) {
|
if (tmp.indexOf(str2) == 0) {
|
||||||
total++;
|
total++;
|
||||||
tmp = tmp.substring(str2.length());
|
tmp = tmp.substring(str2.length());
|
||||||
} else {
|
} else {
|
||||||
tmp = tmp.substring(1);
|
tmp = tmp.substring(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断是否是数字
|
* 判断是否是数字
|
||||||
*
|
*
|
||||||
* @param str
|
* @param str
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static boolean isNumeric(String str) {
|
public static boolean isNumeric(String str) {
|
||||||
for (int i = 0; i < str.length(); i++) {
|
for (int i = 0; i < str.length(); i++) {
|
||||||
if (!Character.isDigit(str.charAt(i))) {
|
if (!Character.isDigit(str.charAt(i))) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断是否是整数
|
* 判断是否是整数
|
||||||
*
|
*
|
||||||
* @param str
|
* @param str
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static boolean isInteger(String str) {
|
public static boolean isInteger(String str) {
|
||||||
String patternStr="^[-\\+]?[\\d]*$";
|
String patternStr="^[-\\+]?[\\d]*$";
|
||||||
Pattern pattern = Pattern.compile(patternStr);
|
Pattern pattern = Pattern.compile(patternStr);
|
||||||
return pattern.matcher(str).matches();
|
return pattern.matcher(str).matches();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 替换变量类型
|
* 替换变量类型
|
||||||
*
|
*
|
||||||
* @param object
|
* @param object
|
||||||
* @param str
|
* @param str
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static Object settype(Object object, String str) {
|
public static Object settype(Object object, String str) {
|
||||||
if (object instanceof Integer) {
|
if (object instanceof Integer) {
|
||||||
return Integer.valueOf(str);
|
return Integer.valueOf(str);
|
||||||
} else if (object instanceof Boolean) {
|
} else if (object instanceof Boolean) {
|
||||||
return Boolean.valueOf(str);
|
return Boolean.valueOf(str);
|
||||||
} else if (object instanceof Long) {
|
} else if (object instanceof Long) {
|
||||||
return Long.valueOf(str);
|
return Long.valueOf(str);
|
||||||
} else if (object instanceof Timestamp) {
|
} else if (object instanceof Timestamp) {
|
||||||
return Timestamp.valueOf(str);
|
return Timestamp.valueOf(str);
|
||||||
} else if (object instanceof JSONObject) {
|
} else if (object instanceof JSONObject) {
|
||||||
return JSONObject.parseObject(str);
|
return JSONObject.parseObject(str);
|
||||||
} else if (object instanceof JSONArray) {
|
} else if (object instanceof JSONArray) {
|
||||||
return JSONArray.parseArray(str);
|
return JSONArray.parseArray(str);
|
||||||
} else {
|
} else {
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用于计数替换KEY的序号
|
* 用于计数替换KEY的序号
|
||||||
*/
|
*/
|
||||||
private static int COUNTER=1;
|
private static int COUNTER=1;
|
||||||
/**
|
/**
|
||||||
* 用于分辩是否把参数替换成功
|
* 用于分辩是否把参数替换成功
|
||||||
*/
|
*/
|
||||||
private static Boolean BCHANG=false;
|
private static Boolean BCHANG=false;
|
||||||
/**
|
/**
|
||||||
* 遍历JSON对象
|
* 遍历JSON对象
|
||||||
* @param json
|
* @param json
|
||||||
* @param key
|
* @param key
|
||||||
* @param value
|
* @param value
|
||||||
* @param keyindex
|
* @param keyindex
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static JSONObject parseJsonString(String json,String key,String value,int keyindex){
|
public static JSONObject parseJsonString(String json,String key,String value,int keyindex){
|
||||||
LinkedHashMap<String, Object> jsonMap = JSON.parseObject(json, new TypeReference<LinkedHashMap<String, Object>>(){});
|
LinkedHashMap<String, Object> jsonMap = JSON.parseObject(json, new TypeReference<LinkedHashMap<String, Object>>(){});
|
||||||
for (Map.Entry<String, Object> entry : jsonMap.entrySet()) {
|
for (Map.Entry<String, Object> entry : jsonMap.entrySet()) {
|
||||||
parseJsonMap(entry,key,value,keyindex);
|
parseJsonMap(entry,key,value,keyindex);
|
||||||
}
|
}
|
||||||
return new JSONObject(jsonMap);
|
return new JSONObject(jsonMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 替换遍历后JSON对象中的KEY
|
* 替换遍历后JSON对象中的KEY
|
||||||
* @param entry
|
* @param entry
|
||||||
* @param key
|
* @param key
|
||||||
* @param value
|
* @param value
|
||||||
* @param keyindex
|
* @param keyindex
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static Map.Entry<String, Object> parseJsonMap(Map.Entry<String, Object> entry,String key,String value,int keyindex){
|
public static Map.Entry<String, Object> parseJsonMap(Map.Entry<String, Object> entry,String key,String value,int keyindex){
|
||||||
//如果是单个map继续遍历
|
//如果是字符串型的null直接把对象设置为对象null
|
||||||
if(entry.getValue() instanceof Map){
|
if("NULL".equals(value)){
|
||||||
LinkedHashMap<String, Object> jsonMap = JSON.parseObject(entry.getValue().toString(), new TypeReference<LinkedHashMap<String, Object>>(){});
|
value = null;
|
||||||
for (Map.Entry<String, Object> entry2 : jsonMap.entrySet()) {
|
}
|
||||||
parseJsonMap(entry2,key,value,keyindex);
|
//如果是单个map继续遍历
|
||||||
}
|
if(entry.getValue() instanceof Map){
|
||||||
entry.setValue(jsonMap);
|
LinkedHashMap<String, Object> jsonMap = JSON.parseObject(entry.getValue().toString(), new TypeReference<LinkedHashMap<String, Object>>(){});
|
||||||
}
|
for (Map.Entry<String, Object> entry2 : jsonMap.entrySet()) {
|
||||||
//如果是list就提取出来
|
parseJsonMap(entry2,key,value,keyindex);
|
||||||
if(entry.getValue() instanceof List){
|
}
|
||||||
if(key.equals(entry.getKey())){
|
entry.setValue(jsonMap);
|
||||||
if(keyindex==COUNTER){
|
}
|
||||||
LogUtil.APP.info("对象原始String值:【{}】",entry.getValue());
|
//如果是list就提取出来
|
||||||
JSONArray jsonarr = JSONArray.parseArray(value);
|
if(entry.getValue() instanceof List){
|
||||||
entry.setValue(jsonarr);
|
if(key.equals(entry.getKey())){
|
||||||
LogUtil.APP.info("对象替换后String值:【{}】",entry.getValue());
|
if(keyindex==COUNTER){
|
||||||
BCHANG=true;
|
LogUtil.APP.info("对象原始String值:【{}】",entry.getValue());
|
||||||
}
|
JSONArray jsonarr = JSONArray.parseArray(value);
|
||||||
COUNTER++;
|
entry.setValue(jsonarr);
|
||||||
}else{
|
LogUtil.APP.info("对象替换后String值:【{}】",entry.getValue());
|
||||||
@SuppressWarnings("rawtypes")
|
BCHANG=true;
|
||||||
List list = (List)entry.getValue();
|
}
|
||||||
for (int i = 0; i < list.size(); i++) {
|
COUNTER++;
|
||||||
//如何还有,循环提取
|
}else{
|
||||||
try{
|
@SuppressWarnings("rawtypes")
|
||||||
list.set(i, parseJsonString(list.get(i).toString(),key,value,keyindex));
|
List list = (List)entry.getValue();
|
||||||
entry.setValue(list);
|
for (int i = 0; i < list.size(); i++) {
|
||||||
}catch(JSONException jsone){
|
//如何还有,循环提取
|
||||||
if(key.equals(entry.getKey())){
|
try{
|
||||||
if(keyindex==COUNTER){
|
list.set(i, parseJsonString(list.get(i).toString(),key,value,keyindex));
|
||||||
LogUtil.APP.info("对象原始List值:【{}】",entry.getValue());
|
entry.setValue(list);
|
||||||
JSONArray jsonarr = JSONArray.parseArray(value);
|
}catch(JSONException jsone){
|
||||||
entry.setValue(jsonarr);
|
if(key.equals(entry.getKey())){
|
||||||
LogUtil.APP.info("对象替换后List值:【{}】",entry.getValue());
|
if(keyindex==COUNTER){
|
||||||
BCHANG=true;
|
LogUtil.APP.info("对象原始List值:【{}】",entry.getValue());
|
||||||
}
|
JSONArray jsonarr = JSONArray.parseArray(value);
|
||||||
COUNTER++;
|
entry.setValue(jsonarr);
|
||||||
}
|
LogUtil.APP.info("对象替换后List值:【{}】",entry.getValue());
|
||||||
break;
|
BCHANG=true;
|
||||||
}
|
}
|
||||||
}
|
COUNTER++;
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
//如果是String就获取它的值
|
}
|
||||||
if(entry.getValue() instanceof String){
|
}
|
||||||
if(key.equals(entry.getKey())){
|
}
|
||||||
if(keyindex==COUNTER){
|
}
|
||||||
LogUtil.APP.info("对象原始String值:【{}】",entry.getValue());
|
//如果是String就获取它的值
|
||||||
entry.setValue(value);
|
if(entry.getValue() instanceof String){
|
||||||
LogUtil.APP.info("对象替换后String值:【{}】",entry.getValue());
|
if(key.equals(entry.getKey())){
|
||||||
BCHANG=true;
|
if(keyindex==COUNTER){
|
||||||
}
|
LogUtil.APP.info("对象原始String值:【{}】",entry.getValue());
|
||||||
COUNTER++;
|
entry.setValue(value);
|
||||||
}
|
LogUtil.APP.info("对象替换后String值:【{}】",entry.getValue());
|
||||||
}
|
BCHANG=true;
|
||||||
//如果是Integer就获取它的值
|
}
|
||||||
if(entry.getValue() instanceof Integer){
|
COUNTER++;
|
||||||
if(key.equals(entry.getKey())){
|
}
|
||||||
if(keyindex==COUNTER){
|
}
|
||||||
LogUtil.APP.info("对象原始Integer值:【{}】",entry.getValue());
|
//如果是Integer就获取它的值
|
||||||
entry.setValue(Integer.valueOf(value));
|
if(entry.getValue() instanceof Integer){
|
||||||
LogUtil.APP.info("对象替换后Integer值:【{}】",entry.getValue());
|
if(key.equals(entry.getKey())){
|
||||||
BCHANG=true;
|
if(keyindex==COUNTER){
|
||||||
}
|
LogUtil.APP.info("对象原始Integer值:【{}】",entry.getValue());
|
||||||
COUNTER++;
|
entry.setValue(Integer.valueOf(value));
|
||||||
}
|
LogUtil.APP.info("对象替换后Integer值:【{}】",entry.getValue());
|
||||||
}
|
BCHANG=true;
|
||||||
//如果是Long就获取它的值
|
}
|
||||||
if(entry.getValue() instanceof Long){
|
COUNTER++;
|
||||||
if(key.equals(entry.getKey())){
|
}
|
||||||
if(keyindex==COUNTER){
|
}
|
||||||
LogUtil.APP.info("对象原始Long值:【{}】",entry.getValue());
|
//如果是Long就获取它的值
|
||||||
entry.setValue(Long.valueOf(value));
|
if(entry.getValue() instanceof Long){
|
||||||
LogUtil.APP.info("对象替换后Long值:【{}】",entry.getValue());
|
if(key.equals(entry.getKey())){
|
||||||
BCHANG=true;
|
if(keyindex==COUNTER){
|
||||||
}
|
LogUtil.APP.info("对象原始Long值:【{}】",entry.getValue());
|
||||||
COUNTER++;
|
entry.setValue(Long.valueOf(value));
|
||||||
}
|
LogUtil.APP.info("对象替换后Long值:【{}】",entry.getValue());
|
||||||
}
|
BCHANG=true;
|
||||||
//如果是Double就获取它的值
|
}
|
||||||
if(entry.getValue() instanceof BigDecimal){
|
COUNTER++;
|
||||||
if(key.equals(entry.getKey())){
|
}
|
||||||
if(keyindex==COUNTER){
|
}
|
||||||
LogUtil.APP.info("对象原始BigDecimal值:【{}】",entry.getValue());
|
//如果是Double就获取它的值
|
||||||
BigDecimal bd = new BigDecimal(value);
|
if(entry.getValue() instanceof BigDecimal){
|
||||||
entry.setValue(bd);
|
if(key.equals(entry.getKey())){
|
||||||
LogUtil.APP.info("对象替换后BigDecimal值:【{}】",entry.getValue());
|
if(keyindex==COUNTER){
|
||||||
BCHANG=true;
|
LogUtil.APP.info("对象原始BigDecimal值:【{}】",entry.getValue());
|
||||||
}
|
BigDecimal bd = new BigDecimal(value);
|
||||||
COUNTER++;
|
entry.setValue(bd);
|
||||||
}
|
LogUtil.APP.info("对象替换后BigDecimal值:【{}】",entry.getValue());
|
||||||
}
|
BCHANG=true;
|
||||||
//如果是Boolean就获取它的值
|
}
|
||||||
if(entry.getValue() instanceof Boolean){
|
COUNTER++;
|
||||||
if(key.equals(entry.getKey())){
|
}
|
||||||
if(keyindex==COUNTER){
|
}
|
||||||
LogUtil.APP.info("对象原始Boolean值:【{}】",entry.getValue());
|
//如果是Boolean就获取它的值
|
||||||
entry.setValue(Boolean.valueOf(value));
|
if(entry.getValue() instanceof Boolean){
|
||||||
LogUtil.APP.info("对象替换后Boolean值:【{}】",entry.getValue());
|
if(key.equals(entry.getKey())){
|
||||||
BCHANG=true;
|
if(keyindex==COUNTER){
|
||||||
}
|
LogUtil.APP.info("对象原始Boolean值:【{}】",entry.getValue());
|
||||||
COUNTER++;
|
entry.setValue(Boolean.valueOf(value));
|
||||||
}
|
LogUtil.APP.info("对象替换后Boolean值:【{}】",entry.getValue());
|
||||||
}
|
BCHANG=true;
|
||||||
|
}
|
||||||
return entry;
|
COUNTER++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
|
||||||
* 替换json对象中指定KEY入口方法
|
return entry;
|
||||||
* @param json
|
}
|
||||||
* @param key
|
|
||||||
* @param value
|
/**
|
||||||
* @param index
|
* 替换json对象中指定KEY入口方法
|
||||||
* @return
|
* @param json
|
||||||
*/
|
* @param key
|
||||||
public static Map<String, String> changjson(String json, String key, String value,int index) {
|
* @param value
|
||||||
json=json.trim();
|
* @param index
|
||||||
LogUtil.APP.info("原始JSON:【{}】,待替换JSON KEY:【{}】,待替换JSON VALUE:【{}】,待替换JSON KEY序号:【{}】",json,key,value,index);
|
* @return
|
||||||
Map<String, String> map = new HashMap<String, String>(0);
|
*/
|
||||||
map.put("json", json);
|
public static Map<String, String> changjson(String json, String key, String value,int index) {
|
||||||
map.put("boolean", BCHANG.toString().toLowerCase());
|
json=json.trim();
|
||||||
|
LogUtil.APP.info("原始JSON:【{}】,待替换JSON KEY:【{}】,待替换JSON VALUE:【{}】,待替换JSON KEY序号:【{}】",json,key,value,index);
|
||||||
if (json.startsWith("{") && json.endsWith("}")) {
|
Map<String, String> map = new HashMap<String, String>(0);
|
||||||
try {
|
map.put("json", json);
|
||||||
JSONObject jsonStr = JSONObject.parseObject(json);
|
map.put("boolean", BCHANG.toString().toLowerCase());
|
||||||
jsonStr=parseJsonString(json,key,value,index);
|
|
||||||
if (BCHANG) {
|
if (json.startsWith("{") && json.endsWith("}")) {
|
||||||
LogUtil.APP.info("JSON字符串替换成功,新JSON:【{}】",jsonStr.toJSONString());
|
try {
|
||||||
}
|
JSONObject jsonStr = JSONObject.parseObject(json);
|
||||||
map.put("json", jsonStr.toJSONString());
|
jsonStr=parseJsonString(json,key,value,index);
|
||||||
} catch (Exception e) {
|
if (BCHANG) {
|
||||||
LogUtil.APP.error("格式化成JSON异常,请检查参数:{}",json, e);
|
LogUtil.APP.info("JSON字符串替换成功,新JSON:【{}】",jsonStr.toJSONString());
|
||||||
return map;
|
}
|
||||||
}
|
map.put("json", jsonStr.toJSONString());
|
||||||
} else if (json.startsWith("[") && json.endsWith("]")) {
|
} catch (Exception e) {
|
||||||
try {
|
LogUtil.APP.error("格式化成JSON异常,请检查参数:{}",json, e);
|
||||||
JSONArray jsonarr = JSONArray.parseArray(json);
|
return map;
|
||||||
|
}
|
||||||
for(int i=0;i<jsonarr.size();i++){
|
} else if (json.startsWith("[") && json.endsWith("]")) {
|
||||||
JSONObject jsonStr = jsonarr.getJSONObject(i);
|
try {
|
||||||
jsonStr=parseJsonString(jsonStr.toJSONString(),key,value,index);
|
JSONArray jsonarr = JSONArray.parseArray(json);
|
||||||
if(BCHANG){
|
|
||||||
jsonarr.set(i, jsonStr);
|
for(int i=0;i<jsonarr.size();i++){
|
||||||
LogUtil.APP.info("JSONARRAY字符串替换成功,新JSONARRAY:【{}】",jsonarr.toJSONString());
|
JSONObject jsonStr = jsonarr.getJSONObject(i);
|
||||||
break;
|
jsonStr=parseJsonString(jsonStr.toJSONString(),key,value,index);
|
||||||
}
|
if(BCHANG){
|
||||||
}
|
jsonarr.set(i, jsonStr);
|
||||||
map.put("json", jsonarr.toJSONString());
|
LogUtil.APP.info("JSONARRAY字符串替换成功,新JSONARRAY:【{}】",jsonarr.toJSONString());
|
||||||
|
break;
|
||||||
} catch (Exception e) {
|
}
|
||||||
LogUtil.APP.error("格式化成JSONArray异常,请检查参数:{}",json, e);
|
}
|
||||||
return map;
|
map.put("json", jsonarr.toJSONString());
|
||||||
}
|
|
||||||
}
|
} catch (Exception e) {
|
||||||
map.put("boolean", BCHANG.toString().toLowerCase());
|
LogUtil.APP.error("格式化成JSONArray异常,请检查参数:{}",json, e);
|
||||||
BCHANG=false;
|
return map;
|
||||||
COUNTER=1;
|
}
|
||||||
return map;
|
}
|
||||||
}
|
map.put("boolean", BCHANG.toString().toLowerCase());
|
||||||
|
BCHANG=false;
|
||||||
}
|
COUNTER=1;
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package luckyclient.netty;
|
package luckyclient.netty;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.net.URLDecoder;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
@ -65,8 +66,9 @@ public class ClientHandler extends ChannelHandlerAdapter {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws UnsupportedEncodingException, InterruptedException {
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws UnsupportedEncodingException, InterruptedException {
|
||||||
|
//统一转编码
|
||||||
|
String jsonStr = URLDecoder.decode(msg.toString(), "GBK");
|
||||||
//服务端消息处理,如果接收到测试任务方法,则直接产生一个http请求并发送请求到本地
|
//服务端消息处理,如果接收到测试任务方法,则直接产生一个http请求并发送请求到本地
|
||||||
String jsonStr = msg.toString();
|
|
||||||
JSONObject json = new JSONObject();
|
JSONObject json = new JSONObject();
|
||||||
try {
|
try {
|
||||||
json = JSON.parseObject(jsonStr);
|
json = JSON.parseObject(jsonStr);
|
||||||
|
@ -262,8 +264,8 @@ public class ClientHandler extends ChannelHandlerAdapter {
|
||||||
super.userEventTriggered(ctx, evt);
|
super.userEventTriggered(ctx, evt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void sendMessage(String json) throws InterruptedException {
|
public static void sendMessage(String json) throws InterruptedException {
|
||||||
ctx.channel().writeAndFlush(Unpooled.copiedBuffer((json + "$_").getBytes()));
|
ctx.channel().writeAndFlush(Unpooled.copiedBuffer((json + "$_").getBytes()));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,94 +1,94 @@
|
||||||
package luckyclient.netty;
|
package luckyclient.netty;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import io.netty.bootstrap.Bootstrap;
|
import io.netty.bootstrap.Bootstrap;
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
import io.netty.buffer.Unpooled;
|
import io.netty.buffer.Unpooled;
|
||||||
import io.netty.channel.Channel;
|
import io.netty.channel.Channel;
|
||||||
import io.netty.channel.ChannelFuture;
|
import io.netty.channel.ChannelFuture;
|
||||||
import io.netty.channel.ChannelFutureListener;
|
import io.netty.channel.ChannelFutureListener;
|
||||||
import io.netty.channel.ChannelInitializer;
|
import io.netty.channel.ChannelInitializer;
|
||||||
import io.netty.channel.ChannelOption;
|
import io.netty.channel.ChannelOption;
|
||||||
import io.netty.channel.ChannelPipeline;
|
import io.netty.channel.ChannelPipeline;
|
||||||
import io.netty.channel.EventLoop;
|
import io.netty.channel.EventLoop;
|
||||||
import io.netty.channel.EventLoopGroup;
|
import io.netty.channel.EventLoopGroup;
|
||||||
import io.netty.channel.nio.NioEventLoopGroup;
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
import io.netty.channel.socket.SocketChannel;
|
import io.netty.channel.socket.SocketChannel;
|
||||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||||
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
|
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
|
||||||
import io.netty.handler.codec.string.StringDecoder;
|
import io.netty.handler.codec.string.StringDecoder;
|
||||||
import io.netty.handler.codec.string.StringEncoder;
|
import io.netty.handler.codec.string.StringEncoder;
|
||||||
import io.netty.handler.timeout.IdleStateHandler;
|
import io.netty.handler.timeout.IdleStateHandler;
|
||||||
import luckyclient.utils.config.SysConfig;
|
import luckyclient.utils.config.SysConfig;
|
||||||
|
|
||||||
public class NettyClient {
|
public class NettyClient {
|
||||||
private static final String NETTY_SERVER_IP= SysConfig.getConfiguration().getProperty("server.web.ip");
|
private static final String NETTY_SERVER_IP= SysConfig.getConfiguration().getProperty("server.web.ip");
|
||||||
|
|
||||||
private static final int NETTY_SERVER_PORT=Integer.parseInt(SysConfig.getConfiguration().getProperty("netty.server.port"));
|
private static final int NETTY_SERVER_PORT=Integer.parseInt(SysConfig.getConfiguration().getProperty("netty.server.port"));
|
||||||
|
|
||||||
protected static Channel channel;
|
protected static Channel channel;
|
||||||
|
|
||||||
private static ChannelFuture connect;
|
private static ChannelFuture connect;
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(NettyClient.class);
|
private static final Logger log = LoggerFactory.getLogger(NettyClient.class);
|
||||||
|
|
||||||
private static ClientHandler clientHandler;
|
private static ClientHandler clientHandler;
|
||||||
public static void start() throws InterruptedException{
|
public static void start() throws InterruptedException{
|
||||||
EventLoopGroup group = new NioEventLoopGroup();
|
EventLoopGroup group = new NioEventLoopGroup();
|
||||||
try {
|
try {
|
||||||
Bootstrap b = new Bootstrap();
|
Bootstrap b = new Bootstrap();
|
||||||
clientHandler=new ClientHandler();
|
clientHandler=new ClientHandler();
|
||||||
b.group(group)
|
b.group(group)
|
||||||
.channel(NioSocketChannel.class)
|
.channel(NioSocketChannel.class)
|
||||||
.option(ChannelOption.SO_KEEPALIVE,true)
|
.option(ChannelOption.SO_KEEPALIVE,true)
|
||||||
.handler(new ChannelInitializer<SocketChannel>() {
|
.handler(new ChannelInitializer<SocketChannel>() {
|
||||||
@Override
|
@Override
|
||||||
public void initChannel(SocketChannel ch) throws Exception {
|
public void initChannel(SocketChannel ch) throws Exception {
|
||||||
ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
|
ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
|
||||||
ChannelPipeline p = ch.pipeline();
|
ChannelPipeline p = ch.pipeline();
|
||||||
p.addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
|
p.addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
|
||||||
p.addLast("decoder", new StringDecoder(Charset.forName("GBK")));
|
p.addLast("decoder", new StringDecoder(Charset.forName("UTF-8")));
|
||||||
p.addLast("encoder", new StringEncoder(Charset.forName("UTF-8")));
|
p.addLast("encoder", new StringEncoder(Charset.forName("GBK")));
|
||||||
p.addLast(new IdleStateHandler(1,0,0,TimeUnit.SECONDS));
|
p.addLast(new IdleStateHandler(1,0,0,TimeUnit.SECONDS));
|
||||||
p.addLast(clientHandler);
|
p.addLast(clientHandler);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
//连接服务端
|
//连接服务端
|
||||||
connect = b.connect(NETTY_SERVER_IP, NETTY_SERVER_PORT);
|
connect = b.connect(NETTY_SERVER_IP, NETTY_SERVER_PORT);
|
||||||
//断线重连
|
//断线重连
|
||||||
connect.addListener(new ChannelFutureListener() {
|
connect.addListener(new ChannelFutureListener() {
|
||||||
@Override
|
@Override
|
||||||
public void operationComplete(ChannelFuture channelFuture) throws Exception {
|
public void operationComplete(ChannelFuture channelFuture) throws Exception {
|
||||||
if (!channelFuture.isSuccess()) {
|
if (!channelFuture.isSuccess()) {
|
||||||
final EventLoop loop = channelFuture.channel().eventLoop();
|
final EventLoop loop = channelFuture.channel().eventLoop();
|
||||||
loop.schedule(new Runnable() {
|
loop.schedule(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
log.error("服务端链接不上,开始重连操作...");
|
log.error("服务端链接不上,开始重连操作...");
|
||||||
start();
|
start();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, 1L, TimeUnit.SECONDS);
|
}, 1L, TimeUnit.SECONDS);
|
||||||
} else {
|
} else {
|
||||||
channel = channelFuture.channel();
|
channel = channelFuture.channel();
|
||||||
log.info("服务端链接成功...");
|
log.info("服务端链接成功...");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
//不关闭通道
|
//不关闭通道
|
||||||
//group.shutdownGracefully();
|
//group.shutdownGracefully();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -24,6 +24,10 @@ import luckyclient.utils.config.SysConfig;
|
||||||
*/
|
*/
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class RunService {
|
public class RunService {
|
||||||
|
/*
|
||||||
|
* 注意:请不要在此处使用IDEA运行(Run)客户端,客户端的启动方式,请参照官网文档
|
||||||
|
* 如果IDEA工具上运行时出现乱码,请设置VM options,添加-Dfile.encoding=GBK即可
|
||||||
|
* */
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(RunService.class);
|
private static final Logger log = LoggerFactory.getLogger(RunService.class);
|
||||||
private static final Boolean NETTY_MODEL= BooleanUtil.toBoolean(SysConfig.getConfiguration().getProperty("netty.model"));
|
private static final Boolean NETTY_MODEL= BooleanUtil.toBoolean(SysConfig.getConfiguration().getProperty("netty.model"));
|
||||||
|
|
Loading…
Reference in New Issue