configserviceutil中的response关闭放在最外层try中

This commit is contained in:
DuanDongmei
2018-11-22 11:35:09 +08:00
parent a1298c4080
commit 1592f2608f
3 changed files with 348 additions and 380 deletions

View File

@@ -104,11 +104,13 @@ public class ConfigServiceUtil {
* @return
*/
public static List<Integer> getId(int type,int num) throws MaatConvertException {
Response response=null;
List<Integer> list = new ArrayList();
try {
logger.warn("get ids start");
long start=System.currentTimeMillis();
String result = null;
String url = "";
List<Integer> list = new ArrayList();
if (type == 1) {
url = Constants.SERVICE_URL+Constants.CONFIG_ID_SOURCES+"?sourceName=CONFIG_COMPILE&num="+num;
} else if (type == 2) {
@@ -121,13 +123,9 @@ public class ConfigServiceUtil {
logger.info("getId url:"+url);
//获取响应结果
Builder header = wt.request(MediaType.APPLICATION_JSON).header("Content-Type", MediaType.APPLICATION_JSON);
Response response=null;
try {
response= header.get();
} catch (Exception e) {
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
}
if(response != null && response.getStatus() == 200){
@@ -142,52 +140,46 @@ public class ConfigServiceUtil {
if(!StringUtil.isEmpty(bean.getData())){
list = bean.getData().getPzIdList();
}else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
}
}else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
}
}else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
}
if (response != null) {
response.close();
}
long end=System.currentTimeMillis();
logger.warn("get ids finish,cost:"+(end-start));
} catch (Exception e) {
throw e;
}finally {
if (response != null) {
response.close();
}
}
return list;
}
/**
* MAAT配置提交
* @param params
* @return
* @throws Exception
* @throws MaatConvertException
*/
public static ToMaatResult postMaatCfg(String params) throws MaatConvertException{
String result = null;
Response response=null;
ToMaatResult bean = null;
try {
response.close();
String result = null;
String url = Constants.SERVICE_URL+Constants.MAAT_CFG;
//创建连接
WebTarget wt = ClientUtil.getWebTarger(url);
logger.info("postMaatCfg url:"+url);
//获取响应结果
Builder header = wt.request(MediaType.APPLICATION_JSON).header("Content-Type", MediaType.APPLICATION_JSON);
Response response=null;
try {
response= header.post(Entity.entity(params, MediaType.APPLICATION_JSON));
} catch (Exception e) {
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
}
if(response != null && response.getStatus() == 200){
@@ -198,38 +190,38 @@ public class ConfigServiceUtil {
JSONObject resObject = JSONObject.fromObject(result,config);
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
}else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
}
} catch (Exception e) {
throw e;
}finally {
if (response != null) {
response.close();
}
}
return bean;
}
/**
* 回调配置提交
* @param params
* @return
* @throws Exception
* @throws MaatConvertException
*/
public static ToMaatResult postCallbackCfg(String params) throws MaatConvertException{
String result = null;
Response response=null;
ToMaatResult bean = null;
try {
String result = null;
String url = Constants.SERVICE_URL+Constants.CALLBACK_CFG;
//创建连接
WebTarget wt = ClientUtil.getWebTarger(url);
logger.info("postCallbackCfg url:"+url);
//获取响应结果
Builder header = wt.request(MediaType.APPLICATION_JSON).header("Content-Type", MediaType.APPLICATION_JSON);
Response response=null;
try {
response= header.post(Entity.entity(params, MediaType.APPLICATION_JSON));
} catch (Exception e) {
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
}
if(response != null && response.getStatus() == 200){
@@ -237,14 +229,15 @@ public class ConfigServiceUtil {
JSONObject resObject = JSONObject.fromObject(result);
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
}else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
}
} catch (Exception e) {
throw e;
}finally {
if (response != null) {
response.close();
}
}
return bean;
}
@@ -252,11 +245,13 @@ public class ConfigServiceUtil {
* 文件配置提交
* @param params
* @return
* @throws Exception
* @throws MaatConvertException
*/
public static ToMaatResult postFileCfg(String params,File file,String fileDesc) throws Exception{
String result = null;
public static ToMaatResult postFileCfg(String params,File file,String fileDesc) throws MaatConvertException{
Response response=null;
ToMaatResult bean = null;
try {
String result = null;
String url = Constants.SERVICE_URL+Constants.FILE_UPLOAD_CFG;
//创建连接
WebTarget wt = ClientUtil.getWebTarger(url);
@@ -265,13 +260,9 @@ public class ConfigServiceUtil {
FileDataBodyPart bodyPart=new FileDataBodyPart("file",file);
formDataMultiPart.bodyPart(bodyPart);
Builder header = wt.request(MediaType.APPLICATION_JSON).header("File-Desc",fileDesc);
Response response=null;
try {
response= header.post(Entity.entity(formDataMultiPart, formDataMultiPart.getMediaType()));
} catch (Exception e) {
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
}
if(response != null && response.getStatus() == 200){
@@ -279,14 +270,15 @@ public class ConfigServiceUtil {
JSONObject resObject = JSONObject.fromObject(result);
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
}else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
}
} catch (Exception e) {
throw e;
} finally {
if (response != null) {
response.close();
}
}
return bean;
}
@@ -296,9 +288,11 @@ public class ConfigServiceUtil {
* @param type,1表示maat配置取消2表示回调配置取消
* @return
*/
public static ToMaatResult delete(String params,Integer type) throws Exception{
String result = null;
public static ToMaatResult delete(String params,Integer type) throws MaatConvertException{
Response response=null;
ToMaatResult bean = null;
try {
String result = null;
String url = Constants.SERVICE_URL;
if(type==1){
url = url+Constants.MAAT_CFG;
@@ -310,13 +304,9 @@ public class ConfigServiceUtil {
logger.info("delete url:"+url);
//获取响应结果
Builder header = wt.request(MediaType.APPLICATION_JSON).header("Content-Type", MediaType.APPLICATION_JSON);
Response response=null;
try {
response= header.delete();
} catch (Exception e) {
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
}
if(response != null && response.getStatus() == 200){
@@ -324,14 +314,15 @@ public class ConfigServiceUtil {
JSONObject resObject = JSONObject.fromObject(result);
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
}else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
}
} catch (Exception e) {
throw e;
} finally {
if (response != null) {
response.close();
}
}
return bean;
}
@@ -342,8 +333,10 @@ public class ConfigServiceUtil {
* @return
*/
public static ToMaatResult put(String params,Integer type) throws MaatConvertException{
String result = null;
ToMaatResult bean = null;
Response response=null;
try {
String result = null;
String url = Constants.SERVICE_URL;
if(type==1){
url = url+Constants.MAAT_CFG;
@@ -357,13 +350,9 @@ public class ConfigServiceUtil {
logger.info("put url:"+url);
//获取响应结果
Builder header = wt.request(MediaType.APPLICATION_JSON).header("Content-Type", MediaType.APPLICATION_JSON);
Response response=null;
try {
response= header.put(Entity.entity(params, MediaType.APPLICATION_JSON));
} catch (Exception e) {
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
}
if(response !=null && response.getStatus() == 200){
@@ -371,14 +360,15 @@ public class ConfigServiceUtil {
JSONObject resObject = JSONObject.fromObject(result);
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
}else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
}
} catch (Exception e) {
throw e;
}finally {
if (response != null) {
response.close();
}
}
return bean;
}
/**
@@ -389,7 +379,7 @@ public class ConfigServiceUtil {
*/
public static String patch(String params,Integer type) throws MaatConvertException{
String result = null;
ToMaatResult bean = null;
try {
String url = Constants.SERVICE_URL;
if(type==1){
url = url+Constants.MAAT_CFG;
@@ -401,16 +391,16 @@ public class ConfigServiceUtil {
logger.info("put url:"+url);
//创建连接
try {
ClientUtil.initClient();
Client client=ClientUtil.getClient();
result = HttpClientUtil.patch(url, params);
// JSONObject resObject = JSONObject.fromObject(result);
// bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
//获取响应结果
} catch (Exception e) {
e.printStackTrace();
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+result);
}
} catch (Exception e) {
throw e;
}
return result;
}
@@ -418,11 +408,13 @@ public class ConfigServiceUtil {
* 信息获取
* @param params
* @return
* @throws Exception
* @throws MaatConvertException
*/
public static ToMaatResult getFileDigest(String params,File file,String fileDesc) throws MaatConvertException{
String result = null;
ToMaatResult bean = null;
Response response=null;
try {
String result = null;
String url = Constants.SERVICE_URL+Constants.FILE_DIGEST_CFG;
//创建连接
WebTarget wt = ClientUtil.getWebTarger(url);
@@ -431,13 +423,9 @@ public class ConfigServiceUtil {
FileDataBodyPart bodyPart=new FileDataBodyPart("file",file);
formDataMultiPart.bodyPart(bodyPart);
Builder header = wt.request(MediaType.APPLICATION_JSON).header("File-Desc",fileDesc);
Response response=null;
try {
response= header.post(Entity.entity(formDataMultiPart, formDataMultiPart.getMediaType()));
} catch (Exception e) {
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
}
if(response !=null && response.getStatus() == 200){
@@ -446,14 +434,15 @@ public class ConfigServiceUtil {
JSONObject resObject = JSONObject.fromObject(result);
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
}else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
}
} catch (Exception e) {
throw e;
} finally {
if (response != null) {
response.close();
}
}
return bean;
}
/**
@@ -463,23 +452,21 @@ public class ConfigServiceUtil {
* @throws MaatConvertException
*/
public static ToMaatResult postGroupReuseSources(String params) throws MaatConvertException{
ToMaatResult bean = null;
Response response=null;
try {
logger.warn("postGroupReuseSources start");
long start=System.currentTimeMillis();
String result = null;
ToMaatResult bean = null;
String url = Constants.SERVICE_URL+Constants.GROUP_REUSE_SOURCES;
//创建连接
WebTarget wt = ClientUtil.getWebTarger(url);
logger.info("postGroupReuseSources url:"+url);
//获取响应结果
Builder header = wt.request(MediaType.APPLICATION_JSON).header("Content-Type", MediaType.APPLICATION_JSON);
Response response=null;
try {
response= header.post(Entity.entity(params, MediaType.APPLICATION_JSON));
} catch (Exception e) {
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
}
if(response !=null && response.getStatus() == 200){
@@ -489,16 +476,17 @@ public class ConfigServiceUtil {
JSONObject resObject = JSONObject.fromObject(result,config);
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
}else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
}
if (response != null) {
response.close();
}
long end=System.currentTimeMillis();
logger.warn("postGroupReuseSources end,cost:"+(end-start));
} catch (Exception e) {
throw e;
} finally {
if (response != null) {
response.close();
}
}
return bean;
}
/**
@@ -517,10 +505,12 @@ public class ConfigServiceUtil {
* @since 1.0.0
*/
public static String getReport(int type,String ids,String serviceIds,String searchReportStartTime,String searchReportEndTime) throws MaatConvertException{
String result = null;
Response response=null;
try {
if(StringUtils.isBlank(ids)||StringUtils.isBlank(serviceIds)){
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
}
String result = null;
UriBuilder builder=UriBuilder.fromPath(Constants.LOG_BASE_URL+Constants.NTC_PZ_REPORT);
builder.queryParam("searchBusinessType",type);
builder.queryParam("searchService",serviceIds);
@@ -538,33 +528,32 @@ public class ConfigServiceUtil {
WebTarget wt = client.target(uri);
logger.info("getReport url:"+uri.toString());
Builder header = wt.request();
Response response=null;
try {
response= header.get();
} catch (Exception e) {
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
}
if(response !=null && response.getStatus() == 200){
result= response.readEntity(String.class);
}else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
}
} catch (Exception e) {
throw e;
} finally {
if (response != null) {
response.close();
}
}
return result;
}
public static String getReport(String reportUrl, SearchReport searchCondition) throws MaatConvertException, UnsupportedEncodingException{
public static String getReport(String reportUrl, SearchReport searchCondition) throws MaatConvertException{
// if(StringUtils.isBlank(searchCondition.getSearchService())){
// throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
// }
Response response=null;
String result = null;
try {
UriBuilder builder=UriBuilder.fromPath(reportUrl);
builder.queryParam("searchBusinessType",searchCondition.getSearchBusinessType());
if(StringUtils.isNotBlank(searchCondition.getSearchService())) {
@@ -601,26 +590,23 @@ public class ConfigServiceUtil {
Client client=ClientUtil.getClient();
WebTarget wt = client.target(uri);
Builder header = wt.request();
Response response=null;
try {
response= header.get();
} catch (Exception e) {
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
}
if(response !=null && response.getStatus() == 200){
result= response.readEntity(String.class);
}else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
}
} catch (Exception e) {
throw e;
} finally {
if (response != null) {
response.close();
}
}
return result;
}
public static void main(String[] args) {

View File

@@ -60,6 +60,7 @@ public class ConfigureStatisticsController extends BaseController{
//2、根据来函分页
RequestInfo requestInfo=new RequestInfo();
requestInfo.setIsAudit(1);
requestInfo.setIsValid(1);
Page requestPage = requestInfoService.findRequestInfo(new Page(request, response,"r"),requestInfo);
List<RequestInfo> requestInfos=requestPage.getList();
//3、根据当前页的requestInfo信息查询request统计信息

View File

@@ -563,25 +563,6 @@
<div for="userRegion1"></div>
</div>
</div>
<!-- 域名拦截增加拦截强度 -->
<c:if test="${_cfg.functionId eq 201}">
<div class="col-md-6">
<div class="form-group">
<label class="control-label col-md-3"><font color="red">*</font>
<spring:message code="intercept_intensity" />
</label>
<div class="col-md-6">
<select name="userRegion5"
class="selectpicker show-tick form-control required">
<c:forEach items="${fns:getDictList('INTERCEPT_DOMAIN_INTENSITY')}" var="intensityC">
<option value="${intensityC.itemCode}" <c:if test="${_cfg.userRegion5==intensityC.itemCode || (empty _cfg.userRegion5 && intensityC.itemCode eq 1)}">selected</c:if>><spring:message code="${intensityC.itemValue}"/></option>
</c:forEach>
</select>
</div>
<div for="userRegion5"></div>
</div>
</div>
</c:if>
<div class="col-md-6 droprate">
<div class="form-group ">
<label class="control-label col-md-3"><font color="red">*</font><spring:message code="ratelimit"/></label>
@@ -661,7 +642,7 @@
<%-- action 为128 白名单时 --%>
<c:if test="${_cfg.functionId eq 201}">
<div class="row whiteListAction hidden">
<h4 class="form-section">
<%-- <h4 class="form-section">
<spring:message code="action_region" />
<small></small>
</h4>
@@ -681,7 +662,7 @@
</div>
<div for="userRegion5"></div>
</div>
</div>
</div> --%>
</div>
</c:if>
<c:set var="ipCfgIndex" value="0"></c:set>