REM:
1.优化Restful公共接口
2.修改华为设备获取防护对象接口
This commit is contained in:
chenlinghy 2020-11-18 10:37:21 +08:00
parent 2e47fc9ff8
commit e120949e2c
2 changed files with 16 additions and 43 deletions

View File

@ -258,13 +258,13 @@ public class HuaWeiAbilityImpl implements DisposeAbility {
HttpResponse response = restfulInterface.queryAllZones(url, token); HttpResponse response = restfulInterface.queryAllZones(url, token);
if (response.getStatus() == ErrorCode.ERR_TOKENNOTFOUND.getCode()) { if (response.getStatus() == HttpServletResponse.SC_PRECONDITION_FAILED) {
// 重新登录获取 token // 重新登录获取 token
upgradeToken(); upgradeToken();
response = restfulInterface.queryAllZones(url, token); response = restfulInterface.queryAllZones(url, token);
} }
return response.getStatus() == ErrorCode.ERR_OK.getCode(); return response.getStatus() == HttpServletResponse.SC_OK;
} catch ( } catch (
Exception ex) { Exception ex) {
log.error(ex.getMessage()); log.error(ex.getMessage());

View File

@ -54,12 +54,12 @@ public class RestfulInterface {
* @param url the url * @param url the url
* @param header the header * @param header the header
* @param body the body * @param body the body
* @return the string * @return the http response
*/ */
private static String postJson(String url, Map<String, String> header, String body) { private static HttpResponse postJson(String url, Map<String, String> header, String body) {
HttpRequest.setGlobalTimeout(timeOutValue); HttpRequest.setGlobalTimeout(timeOutValue);
return HttpRequest.post(url).header(Header.CONTENT_TYPE, "application/json").addHeaders(header).body(body) return HttpRequest.post(url).header(Header.CONTENT_TYPE, "application/json").addHeaders(header).body(body)
.execute().body(); .execute();
} }
/** /**
@ -67,55 +67,28 @@ public class RestfulInterface {
* *
* @param url the url * @param url the url
* @param header the header * @param header the header
* @return the json
*/
private static String getJson(String url, Map<String, String> header) {
HttpRequest.setGlobalTimeout(timeOutValue);
return HttpRequest.get(url).header(Header.CONTENT_TYPE, "application/json").addHeaders(header)
.execute().body();
}
/**
* Gets http response.
*
* @param url the url
* @param header the header
* @return the http response * @return the http response
*/ */
private static HttpResponse getHttpResp(String url, Map<String, String> header) { private static HttpResponse getJson(String url, Map<String, String> header) {
HttpRequest.setGlobalTimeout(timeOutValue); HttpRequest.setGlobalTimeout(timeOutValue);
return HttpRequest.get(url).header(Header.CONTENT_TYPE, "application/json").addHeaders(header) return HttpRequest.get(url).header(Header.CONTENT_TYPE, "application/json").addHeaders(header)
.execute(); .execute();
} }
/** /**
* Put http response. * Put json.
* *
* @param url the url * @param url the url
* @param header the header * @param header the header
* @param body the body * @param body the body
* @return the http response * @return the http response
*/ */
private static HttpResponse putHttpResp(String url, Map<String, String> header, String body) { private static HttpResponse putJson(String url, Map<String, String> header, String body) {
HttpRequest.setGlobalTimeout(timeOutValue); HttpRequest.setGlobalTimeout(timeOutValue);
return HttpRequest.put(url).header(Header.CONTENT_TYPE, "application/json").addHeaders(header).body(body) return HttpRequest.put(url).header(Header.CONTENT_TYPE, "application/json").addHeaders(header).body(body)
.execute(); .execute();
} }
/**
* Post http response.
*
* @param url the url
* @param header the header
* @param body the body
* @return the http response
*/
private static HttpResponse postHttpResp(String url, Map<String, String> header, String body) {
HttpRequest.setGlobalTimeout(timeOutValue);
return HttpRequest.post(url).header(Header.CONTENT_TYPE, "application/json").addHeaders(header).body(body)
.execute();
}
/** /**
* Deletes http response. * Deletes http response.
* *
@ -123,7 +96,7 @@ public class RestfulInterface {
* @param header the header * @param header the header
* @return the http response * @return the http response
*/ */
private static HttpResponse deleteHttpResp(String url, Map<String, String> header, String body) { private static HttpResponse deleteJson(String url, Map<String, String> header, String body) {
HttpRequest.setGlobalTimeout(timeOutValue); HttpRequest.setGlobalTimeout(timeOutValue);
return HttpRequest.delete(url).header(Header.CONTENT_TYPE, "application/json").addHeaders(header).body(body) return HttpRequest.delete(url).header(Header.CONTENT_TYPE, "application/json").addHeaders(header).body(body)
.execute(); .execute();
@ -149,7 +122,7 @@ public class RestfulInterface {
log.debug("Restful request: {}: {}", url, reqJson); log.debug("Restful request: {}: {}", url, reqJson);
String svrResp = postJson(url, httpHeadMap, reqJson); String svrResp = postJson(url, httpHeadMap, reqJson).body();
if (svrResp == null) { if (svrResp == null) {
log.debug("Server return null: {}", url); log.debug("Server return null: {}", url);
@ -196,10 +169,10 @@ public class RestfulInterface {
switch (reqType) { switch (reqType) {
case GET: case GET:
svrResp = getJson(url, httpHeadMap); svrResp = getJson(url, httpHeadMap).body();
break; break;
case POST: case POST:
svrResp = postJson(url, httpHeadMap, reqJson); svrResp = postJson(url, httpHeadMap, reqJson).body();
break; break;
default: default:
log.error("Unknown method: {}", reqType); log.error("Unknown method: {}", reqType);
@ -255,16 +228,16 @@ public class RestfulInterface {
switch (reqType) { switch (reqType) {
case GET: case GET:
svrResp = getHttpResp(url, httpHeadMap); svrResp = getJson(url, httpHeadMap);
break; break;
case POST: case POST:
svrResp = postHttpResp(url, httpHeadMap, reqJson); svrResp = postJson(url, httpHeadMap, reqJson);
break; break;
case PUT: case PUT:
svrResp = putHttpResp(url, httpHeadMap, reqJson); svrResp = putJson(url, httpHeadMap, reqJson);
break; break;
case DELETE: case DELETE:
svrResp = deleteHttpResp(url, httpHeadMap, reqJson); svrResp = deleteJson(url, httpHeadMap, reqJson);
break; break;
default: default:
log.error("Unknown method: {}", reqType); log.error("Unknown method: {}", reqType);