日志调用接口工具HttpClientUtil.java 增加处理数据格式方法
This commit is contained in:
@@ -13,8 +13,10 @@ import java.net.SocketTimeoutException;
|
|||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
@@ -38,7 +40,9 @@ import org.apache.http.util.EntityUtils;
|
|||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
import org.springframework.web.servlet.support.RequestContext;
|
import org.springframework.web.servlet.support.RequestContext;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.nis.exceptions.MaatConvertException;
|
import com.nis.exceptions.MaatConvertException;
|
||||||
|
import com.nis.util.Encodes;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ClassName: HttpClientBean.java
|
* @ClassName: HttpClientBean.java
|
||||||
@@ -147,6 +151,8 @@ public class HttpClientUtil {
|
|||||||
int status = response.getStatusLine().getStatusCode();
|
int status = response.getStatusLine().getStatusCode();
|
||||||
if (status == HttpStatus.SC_OK) {
|
if (status == HttpStatus.SC_OK) {
|
||||||
result = EntityUtils.toString(response.getEntity());
|
result = EntityUtils.toString(response.getEntity());
|
||||||
|
//调用处理数据方法
|
||||||
|
// result = galaxyMessageFormat(result);
|
||||||
logger.debug("获取消息成功,相应内容如下: " + result);
|
logger.debug("获取消息成功,相应内容如下: " + result);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@@ -174,6 +180,57 @@ public class HttpClientUtil {
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将接口的日志数据格式化包括 “null”,日期为“0”,特殊字符转义
|
||||||
|
* @param recv
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String galaxyMessageFormat(String recv){
|
||||||
|
//数据为ipList ip地址接口
|
||||||
|
/* String recv="{\"status\":200,\"businessCode\":2000,\"reason\":\"\",\"msg\":\"\",\"fromuri\":\"\",\"logSource\":0,\"traceCode\":\"20\","+
|
||||||
|
"\"data\":{\"pageNo\":1,\"pageSize\":20,\"count\":20,\"last\":1,\"list\":"+
|
||||||
|
"[{\"cfgId\":1,\"foundTime\":\"2018-07-27 16:21:13\",\"recvTime\":"+
|
||||||
|
"\"2018-07-27 16:21:13\",\"transProto\":\"IPv4\",\"addrType\":4,\"dIp\":\"10.0.6.200\",\"sIp\":\"10.0.6.201\","+
|
||||||
|
"\"dPort\":80,\"sPort\":500,\"service\":16,\"entranceId\":1,\"deviceId\":1,\"direction\":0,\"streamDir\":0,\"capIp\":\"<sip:good_luck666@sip.1.com>1.1.1.1\",\"addrList\":\"<sip:good_luck666@sip.jumblo.com>1.1.1.1"\n2.2.2.2\",\"userRegion\":\"1\",\"clientLocate\":\"China\"},{\"cfgId\":2,\"foundTime\":\"2018-07-27 16:21:13\",\"recvTime\":\"2018-07-27 16:21:13\",\"transProto\":\"IPv4\",\"addrType\":4,\"dIp\":\"10.0.6.200\",\"sIp\":\"10.0.6.201\",\"dPort\":80,\"sPort\":500,\"service\":16,\"entranceId\":2,\"deviceId\":2,\"direction\":0,\"streamDir\":0,\"capIp\":\"null\",\"addrList\":\"2.2.2.2\",\"userRegion\":\"2\",\"serverLocate\":\"China\",\"clientLocate\":\"China\"}]}}";
|
||||||
|
*/
|
||||||
|
Map parse = JSON.parseObject(recv,Map.class);
|
||||||
|
Map map = (Map) parse.get("data");
|
||||||
|
List reslist=new ArrayList();
|
||||||
|
List<Map<String,Object>> list= (List)map.get("list");
|
||||||
|
if(list!=null&&list.size()>0){
|
||||||
|
for (Map<String,Object> m : list) {
|
||||||
|
Map recvMap = new HashMap();
|
||||||
|
Iterator<Entry<String, Object>> iterator = m.entrySet().iterator();
|
||||||
|
while(iterator.hasNext()){
|
||||||
|
Entry<String, Object> next = iterator.next();
|
||||||
|
String key = next.getKey().toString();
|
||||||
|
Object value = next.getValue();
|
||||||
|
//处理字段为“null”情况
|
||||||
|
if(value!=null&&"null".equals(value)){
|
||||||
|
value="";
|
||||||
|
}
|
||||||
|
//处理时间字段为“0”情况
|
||||||
|
if("foundTime".equals(key)||"recvTime".equals(key)){
|
||||||
|
if(null!=value&&"0".equals(value)){
|
||||||
|
value="";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//处理特殊字符转义问题
|
||||||
|
if(null!=value){
|
||||||
|
value = Encodes.escapeHtml(value.toString());
|
||||||
|
}
|
||||||
|
recvMap.put(key, value);
|
||||||
|
}
|
||||||
|
reslist.add(recvMap);
|
||||||
|
}
|
||||||
|
map.put("list", reslist);
|
||||||
|
parse.put("data", map);
|
||||||
|
recv = JSON.toJSONString(parse);
|
||||||
|
}
|
||||||
|
return recv;
|
||||||
|
}
|
||||||
|
|
||||||
private static CloseableHttpClient httpClient = null;
|
private static CloseableHttpClient httpClient = null;
|
||||||
private static RequestConfig requestConfig;
|
private static RequestConfig requestConfig;
|
||||||
public static final int HTTP_SOCKET_TIMEOUT= 300000;
|
public static final int HTTP_SOCKET_TIMEOUT= 300000;
|
||||||
|
|||||||
Reference in New Issue
Block a user