增加日志查询错误提示

This commit is contained in:
chenjinsong
2018-06-20 11:13:13 +08:00
parent c4e4928f31
commit e21380a7f1
12 changed files with 146 additions and 67 deletions

View File

@@ -9,12 +9,15 @@
package com.nis.util.httpclient; package com.nis.util.httpclient;
import java.io.IOException; import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair; import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ClientProtocolException;
@@ -24,6 +27,7 @@ import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder; import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler; import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
@@ -31,6 +35,9 @@ import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair; import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.springframework.web.servlet.support.RequestContext;
import com.nis.exceptions.MaatConvertException;
/** /**
* @ClassName: HttpClientBean.java * @ClassName: HttpClientBean.java
@@ -118,7 +125,9 @@ public class HttpClientUtil {
* @param params 参数列表 * @param params 参数列表
* @return 查询结果数据json * @return 查询结果数据json
*/ */
public static String getMsg(String destUrl, Map<String, Object> params) { public static String getMsg(String destUrl, Map<String, Object> params, HttpServletRequest req) {
RequestContext requestContext = new RequestContext(req);
CloseableHttpResponse response = null; CloseableHttpResponse response = null;
String result = null; String result = null;
try { try {
@@ -134,10 +143,20 @@ public class HttpClientUtil {
if (status == HttpStatus.SC_OK) { if (status == HttpStatus.SC_OK) {
result = EntityUtils.toString(response.getEntity()); result = EntityUtils.toString(response.getEntity());
logger.debug("获取消息成功,相应内容如下: " + result); logger.debug("获取消息成功,相应内容如下: " + result);
} } else {
throw new MaatConvertException(requestContext.getMessage("request_service_failed") + ":" + status);
}
} catch (SocketTimeoutException e) {
logger.error("获取消息失败 ", e);
throw new MaatConvertException(requestContext.getMessage("request_service_failed") + ":timeout");
} catch (MaatConvertException e) {
throw e;
} catch (ConnectTimeoutException e) {
logger.error("获取消息失败 ", e);
throw new MaatConvertException(requestContext.getMessage("request_service_failed") + ":timeout");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); logger.error("获取消息失败 ", e);
logger.debug("获取消息失败,相应内容如下: " + e); throw new MaatConvertException(requestContext.getMessage("request_service_failed"));
} finally { } finally {
try { try {
if (response != null) { if (response != null) {

View File

@@ -31,22 +31,22 @@ public class DnsLogController extends BaseController {
@RequestMapping("list") @RequestMapping("list")
public String list(@ModelAttribute("log") NtcDnsLog log, Model model, HttpServletRequest request, HttpServletResponse response) { public String list(@ModelAttribute("log") NtcDnsLog log, Model model, HttpServletRequest request, HttpServletResponse response) {
Page<NtcDnsLog> page = new Page<NtcDnsLog>(request, response);
Map<String, Object> params = new HashMap<String, Object>();
params.put("pageSize", page.getPageSize());
params.put("pageNo", page.getPageNo());
if (StringUtils.isNotBlank(log.getqName())) {
params.put("searchQName", log.getqName());
}
initLogSearchValue(log, params);
String url = Constants.LOG_BASE_URL + Constants.NTC_DNS_LOG;
String recv = HttpClientUtil.getMsg(url, params);
logger.info("查询结果:" + recv);
Gson gson = new GsonBuilder().create();
try { try {
Page<NtcDnsLog> page = new Page<NtcDnsLog>(request, response);
Map<String, Object> params = new HashMap<String, Object>();
params.put("pageSize", page.getPageSize());
params.put("pageNo", page.getPageNo());
if (StringUtils.isNotBlank(log.getqName())) {
params.put("searchQName", log.getqName());
}
initLogSearchValue(log, params);
String url = Constants.LOG_BASE_URL + Constants.NTC_DNS_LOG;
String recv = HttpClientUtil.getMsg(url, params, request);
logger.info("查询结果:" + recv);
Gson gson = new GsonBuilder().create();
LogRecvData<NtcDnsLog> fromJson = gson.fromJson(recv, new TypeToken<LogRecvData<NtcDnsLog>>(){}.getType()); LogRecvData<NtcDnsLog> fromJson = gson.fromJson(recv, new TypeToken<LogRecvData<NtcDnsLog>>(){}.getType());
if (fromJson.getStatus().intValue() == 200) { if (fromJson.getStatus().intValue() == 200) {
BeanUtils.copyProperties(fromJson.getData(), page); BeanUtils.copyProperties(fromJson.getData(), page);
@@ -59,6 +59,7 @@ public class DnsLogController extends BaseController {
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("查询失败", e); logger.error("查询失败", e);
addMessage(model, e.getMessage());
} }
return "/log/ntc/dnsList"; return "/log/ntc/dnsList";

View File

@@ -31,22 +31,22 @@ public class HttpLogController extends BaseController {
@RequestMapping("list") @RequestMapping("list")
public String list(@ModelAttribute("log") NtcHttpLog log, Model model, HttpServletRequest request, HttpServletResponse response) { public String list(@ModelAttribute("log") NtcHttpLog log, Model model, HttpServletRequest request, HttpServletResponse response) {
Page<NtcHttpLog> page = new Page<NtcHttpLog>(request, response);
Map<String, Object> params = new HashMap<String, Object>();
params.put("pageSize", page.getPageSize());
params.put("pageNo", page.getPageNo());
if (StringUtils.isNotBlank(log.getUrl())) {
params.put("searchUrl", log.getUrl());
}
initLogSearchValue(log, params);
String url = Constants.LOG_BASE_URL + Constants.NTC_HTTP_LOG;
String recv = HttpClientUtil.getMsg(url, params);
logger.info("查询结果:" + recv);
Gson gson = new GsonBuilder().create();
try { try {
Page<NtcHttpLog> page = new Page<NtcHttpLog>(request, response);
Map<String, Object> params = new HashMap<String, Object>();
params.put("pageSize", page.getPageSize());
params.put("pageNo", page.getPageNo());
if (StringUtils.isNotBlank(log.getUrl())) {
params.put("searchUrl", log.getUrl());
}
initLogSearchValue(log, params);
String url = Constants.LOG_BASE_URL + Constants.NTC_HTTP_LOG;
String recv = HttpClientUtil.getMsg(url, params, request);
logger.info("查询结果:" + recv);
Gson gson = new GsonBuilder().create();
LogRecvData<NtcHttpLog> fromJson = gson.fromJson(recv, new TypeToken<LogRecvData<NtcHttpLog>>(){}.getType()); LogRecvData<NtcHttpLog> fromJson = gson.fromJson(recv, new TypeToken<LogRecvData<NtcHttpLog>>(){}.getType());
if (fromJson.getStatus().intValue() == 200) { if (fromJson.getStatus().intValue() == 200) {
BeanUtils.copyProperties(fromJson.getData(), page); BeanUtils.copyProperties(fromJson.getData(), page);
@@ -59,6 +59,7 @@ public class HttpLogController extends BaseController {
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("查询失败", e); logger.error("查询失败", e);
addMessage(model, e.getMessage());
} }
return "/log/ntc/httpList"; return "/log/ntc/httpList";

View File

@@ -9,20 +9,22 @@ import java.util.Map;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.util.FileUtil; import org.aspectj.util.FileUtil;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.nis.domain.Page; import com.nis.domain.Page;
import com.nis.domain.log.NtcIpLog; import com.nis.domain.log.NtcIpLog;
import com.nis.domain.maat.LogRecvData; import com.nis.domain.maat.LogRecvData;
import com.nis.exceptions.MaatConvertException;
import com.nis.util.Constants; import com.nis.util.Constants;
import com.nis.util.httpclient.HttpClientUtil; import com.nis.util.httpclient.HttpClientUtil;
import com.nis.web.controller.BaseController; import com.nis.web.controller.BaseController;
@@ -33,24 +35,53 @@ public class IpLogController extends BaseController {
@RequestMapping("list") @RequestMapping("list")
public String list(@ModelAttribute("log") NtcIpLog log, Model model, HttpServletRequest request, HttpServletResponse response) { public String list(@ModelAttribute("log") NtcIpLog log, Model model, HttpServletRequest request, HttpServletResponse response) {
try {
Page<NtcIpLog> page = new Page<NtcIpLog>(request, response);
Map<String, Object> params = new HashMap<String, Object>();
params.put("pageSize", page.getPageSize());
params.put("pageNo", page.getPageNo());
initLogSearchValue(log, params);
String url = Constants.LOG_BASE_URL + Constants.NTC_IP_LOG;
String recv = HttpClientUtil.getMsg(url, params, request);
logger.info("查询结果:" + recv);
if (StringUtils.isNotBlank(recv)) {
Gson gson = new GsonBuilder().create();
LogRecvData<NtcIpLog> fromJson = gson.fromJson(recv, new TypeToken<LogRecvData<NtcIpLog>>(){}.getType());
if (fromJson.getStatus().intValue() == 200) {
BeanUtils.copyProperties(fromJson.getData(), page);
List<NtcIpLog> list = page.getList();
for (NtcIpLog l : list) {
l.setFunctionId(log.getFunctionId());
setLogAction(l);
}
model.addAttribute("page", page);
}
}
} catch (Exception e) {
logger.error("查询失败", e);
addMessage(model, e.getMessage());
}
return "/log/ntc/ipList";
}
@RequestMapping("testlist")
public String testlist(@ModelAttribute("log") NtcIpLog log, Model model, HttpServletRequest request, HttpServletResponse response) {
Page<NtcIpLog> page = new Page<NtcIpLog>(request, response); Page<NtcIpLog> page = new Page<NtcIpLog>(request, response);
Map<String, Object> params = new HashMap<String, Object>(); Map<String, Object> params = new HashMap<String, Object>();
params.put("pageSize", page.getPageSize()); params.put("pageSize", page.getPageSize());
params.put("pageNo", page.getPageNo()); params.put("pageNo", page.getPageNo());
initLogSearchValue(log, params); initLogSearchValue(log, params);
String url = Constants.LOG_BASE_URL + Constants.NTC_IP_LOG; String url = "http://10.0.6.121:8080/gwall/test/logTest";
String recv = HttpClientUtil.getMsg(url, params);
logger.info("查询结果:" + recv);
// try {
// String path = request.getClass().getClassLoader().getResource("").getPath();
// recv = FileUtil.readAsString(new File(path + "com/nis/web/test/logTest.txt"));
// } catch (IOException e) {
// e.printStackTrace();
// }
Gson gson = new GsonBuilder().create();
try { try {
String recv = HttpClientUtil.getMsg(url, params, request);
logger.info("查询结果:" + recv);
Gson gson = new GsonBuilder().create();
LogRecvData<NtcIpLog> fromJson = gson.fromJson(recv, new TypeToken<LogRecvData<NtcIpLog>>(){}.getType()); LogRecvData<NtcIpLog> fromJson = gson.fromJson(recv, new TypeToken<LogRecvData<NtcIpLog>>(){}.getType());
if (fromJson.getStatus().intValue() == 200) { if (fromJson.getStatus().intValue() == 200) {
BeanUtils.copyProperties(fromJson.getData(), page); BeanUtils.copyProperties(fromJson.getData(), page);
@@ -63,8 +94,10 @@ public class IpLogController extends BaseController {
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("查询失败", e); logger.error("查询失败", e);
addMessage(model, e.getMessage());
} }
return "/log/ntc/ipList"; return "/log/ntc/ipList";
} }
} }

View File

@@ -53,7 +53,7 @@ public class IpsecController extends BaseController{
String url = ""; String url = "";
url = Constants.LOG_BASE_URL+Constants.NTC_IPSEC_LOG; url = Constants.LOG_BASE_URL+Constants.NTC_IPSEC_LOG;
String jsonString = HttpClientUtil.getMsg(url,params); String jsonString = HttpClientUtil.getMsg(url,params,request);
Gson gson = new GsonBuilder().create(); Gson gson = new GsonBuilder().create();
//gson泛型支持 //gson泛型支持
@@ -70,8 +70,8 @@ public class IpsecController extends BaseController{
logger.info("查询Ipsec日志成功"); logger.info("查询Ipsec日志成功");
} }
} catch (Exception e) { } catch (Exception e) {
logger.info("查询Ipsec日志失败"); logger.info("查询Ipsec日志失败", e);
e.printStackTrace(); addMessage(model, e.getMessage());
} }
return "/log/ntc/ipsecList"; return "/log/ntc/ipsecList";
} }

View File

@@ -42,7 +42,7 @@ public class L2tpLogController extends BaseController {
try { try {
// 请求接口 // 请求接口
String url = Constants.LOG_BASE_URL + Constants.NTC_L2TP_LOG; String url = Constants.LOG_BASE_URL + Constants.NTC_L2TP_LOG;
String resJson = HttpClientUtil.getMsg(url, params); String resJson = HttpClientUtil.getMsg(url, params, request);
Gson gson = new GsonBuilder().create(); Gson gson = new GsonBuilder().create();
LogRecvData<NtcL2tpLog> fromJson = gson.fromJson(resJson, new TypeToken<LogRecvData<NtcL2tpLog>>() {}.getType()); LogRecvData<NtcL2tpLog> fromJson = gson.fromJson(resJson, new TypeToken<LogRecvData<NtcL2tpLog>>() {}.getType());
if(fromJson.getStatus().intValue() == 200) { if(fromJson.getStatus().intValue() == 200) {
@@ -55,9 +55,9 @@ public class L2tpLogController extends BaseController {
} }
model.addAttribute("page", page); model.addAttribute("page", page);
} }
} catch (JsonSyntaxException e) { } catch (Exception e) {
logger.info("L2TP日志查询失败"); logger.info("L2TP日志查询失败", e);
e.printStackTrace(); addMessage(model, e.getMessage());
} }
return "/log/ntc/l2tpLogList"; return "/log/ntc/l2tpLogList";

View File

@@ -53,7 +53,7 @@ public class OpenVpnController extends BaseController{
String url = ""; String url = "";
url = Constants.LOG_BASE_URL+Constants.NTC_OPENVPN_LOG; url = Constants.LOG_BASE_URL+Constants.NTC_OPENVPN_LOG;
String jsonString = HttpClientUtil.getMsg(url,params); String jsonString = HttpClientUtil.getMsg(url,params,request);
Gson gson = new GsonBuilder().create(); Gson gson = new GsonBuilder().create();
//gson泛型支持 //gson泛型支持
@@ -70,8 +70,8 @@ public class OpenVpnController extends BaseController{
logger.info("查询OpenVpn日志成功"); logger.info("查询OpenVpn日志成功");
} }
} catch (Exception e) { } catch (Exception e) {
logger.info("查询OpenVpn日志失败"); logger.info("查询OpenVpn日志失败", e);
e.printStackTrace(); addMessage(model, e.getMessage());
} }
return "/log/ntc/openVpnList"; return "/log/ntc/openVpnList";
} }

View File

@@ -42,7 +42,7 @@ public class PptpLogController extends BaseController {
try { try {
// 请求接口 // 请求接口
String url = Constants.LOG_BASE_URL + Constants.NTC_PPTP_LOG; String url = Constants.LOG_BASE_URL + Constants.NTC_PPTP_LOG;
String resJson = HttpClientUtil.getMsg(url, params); String resJson = HttpClientUtil.getMsg(url, params, request);
Gson gson = new GsonBuilder().create(); Gson gson = new GsonBuilder().create();
LogRecvData<NtcPptpLog> fromJson = gson.fromJson(resJson, new TypeToken<LogRecvData<NtcPptpLog>>() {}.getType()); LogRecvData<NtcPptpLog> fromJson = gson.fromJson(resJson, new TypeToken<LogRecvData<NtcPptpLog>>() {}.getType());
if(fromJson.getStatus().intValue() == 200) { if(fromJson.getStatus().intValue() == 200) {
@@ -56,8 +56,8 @@ public class PptpLogController extends BaseController {
model.addAttribute("page", page); model.addAttribute("page", page);
} }
} catch (JsonSyntaxException e) { } catch (JsonSyntaxException e) {
logger.info("PPTP日志查询失败"); logger.info("PPTP日志查询失败", e);
e.printStackTrace(); addMessage(model, e.getMessage());
} }
return "/log/ntc/pptpLogList"; return "/log/ntc/pptpLogList";

View File

@@ -53,7 +53,7 @@ public class SshController extends BaseController{
String url = ""; String url = "";
url = Constants.LOG_BASE_URL+Constants.NTC_SSH_LOG; url = Constants.LOG_BASE_URL+Constants.NTC_SSH_LOG;
String jsonString = HttpClientUtil.getMsg(url,params); String jsonString = HttpClientUtil.getMsg(url,params,request);
Gson gson = new GsonBuilder().create(); Gson gson = new GsonBuilder().create();
//gson泛型支持 //gson泛型支持
@@ -70,8 +70,8 @@ public class SshController extends BaseController{
logger.info("查询Ssh日志成功"); logger.info("查询Ssh日志成功");
} }
} catch (Exception e) { } catch (Exception e) {
logger.info("查询Ssh日志失败"); logger.info("查询Ssh日志失败", e);
e.printStackTrace(); addMessage(model, e.getMessage());
} }
return "/log/ntc/sshList"; return "/log/ntc/sshList";
} }

View File

@@ -45,7 +45,7 @@ public class SslLogController extends BaseController {
try { try {
// 请求接口 // 请求接口
String url = Constants.LOG_BASE_URL + Constants.NTC_SSL_LOG; String url = Constants.LOG_BASE_URL + Constants.NTC_SSL_LOG;
String resJson = HttpClientUtil.getMsg(url, params); String resJson = HttpClientUtil.getMsg(url, params, request);
Gson gson = new GsonBuilder().create(); Gson gson = new GsonBuilder().create();
LogRecvData<NtcSslLog> fromJson = gson.fromJson(resJson, new TypeToken<LogRecvData<NtcSslLog>>() {}.getType()); LogRecvData<NtcSslLog> fromJson = gson.fromJson(resJson, new TypeToken<LogRecvData<NtcSslLog>>() {}.getType());
if(fromJson.getStatus().intValue() == 200) { if(fromJson.getStatus().intValue() == 200) {
@@ -58,9 +58,9 @@ public class SslLogController extends BaseController {
} }
model.addAttribute("page", page); model.addAttribute("page", page);
} }
} catch (JsonSyntaxException e) { } catch (Exception e) {
logger.info("SSL日志查询失败"); logger.info("SSL日志查询失败", e);
e.printStackTrace(); addMessage(model, e.getMessage());
} }
return "/log/ntc/sslLogList"; return "/log/ntc/sslLogList";

View File

@@ -3,7 +3,12 @@ package com.nis.web.test;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.util.FileUtil; import org.aspectj.util.FileUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
@@ -12,8 +17,28 @@ import com.nis.domain.Page;
import com.nis.domain.log.NtcIpLog; import com.nis.domain.log.NtcIpLog;
import com.nis.domain.maat.LogRecvData; import com.nis.domain.maat.LogRecvData;
@Controller
@RequestMapping("test")
public class LogTest { public class LogTest {
@ResponseBody
@RequestMapping("logTest")
public String testservice(HttpServletRequest request) {
String recv = "";
try {
String path = request.getClass().getClassLoader().getResource("").getPath();
recv = FileUtil.readAsString(new File(path + "com/nis/web/test/logTest.txt"));
} catch (IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(61000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "";
}
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
String readAsString = FileUtil.readAsString(new File("d:/test/logTest.txt")); String readAsString = FileUtil.readAsString(new File("d:/test/logTest.txt"));

View File

@@ -22,7 +22,7 @@
"serverPort":"8080", "serverPort":"8080",
"clientPort":"8080", "clientPort":"8080",
"serviceType":10, "serviceType":10,
"entranceId":112, "entranceId":21,
"deviceId":2, "deviceId":2,
"direction":1, "direction":1,
"streamType":0, "streamType":0,