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