diff --git a/src/main/java/com/dispose/restful/RestfulInterface.java b/src/main/java/com/dispose/restful/RestfulInterface.java index 3aedafce..1cd51374 100644 --- a/src/main/java/com/dispose/restful/RestfulInterface.java +++ b/src/main/java/com/dispose/restful/RestfulInterface.java @@ -2,6 +2,7 @@ package com.dispose.restful; import cn.hutool.http.Header; import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; import com.dispose.common.ConstValue; import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO; import com.fasterxml.jackson.core.JsonProcessingException; @@ -74,6 +75,60 @@ public class RestfulInterface { .execute().body(); } + /** + * Gets http response. + * + * @param url the url + * @param header the header + * @return the http response + */ + private static HttpResponse getHttpResp(String url, Map header) { + HttpRequest.setGlobalTimeout(timeOutValue); + return HttpRequest.get(url).header(Header.CONTENT_TYPE, "application/json").addHeaders(header) + .execute(); + } + + /** + * Put http response. + * + * @param url the url + * @param header the header + * @param body the body + * @return the http response + */ + private static HttpResponse putHttpResp(String url, Map header, String body) { + HttpRequest.setGlobalTimeout(timeOutValue); + return HttpRequest.put(url).header(Header.CONTENT_TYPE, "application/json").addHeaders(header).body(body) + .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 header, String body) { + HttpRequest.setGlobalTimeout(timeOutValue); + return HttpRequest.post(url).header(Header.CONTENT_TYPE, "application/json").addHeaders(header).body(body) + .execute(); + } + + /** + * Deletes http response. + * + * @param url the url + * @param header the header + * @return the http response + */ + private static HttpResponse deleteHttpResp(String url, Map header, String body) { + HttpRequest.setGlobalTimeout(timeOutValue); + return HttpRequest.delete(url).header(Header.CONTENT_TYPE, "application/json").addHeaders(header).body(body) + .execute(); + } + /** * Protocol run t. * @@ -171,6 +226,65 @@ public class RestfulInterface { } } + + /** + * huawei pro run protocol resp dto. + * + * @param the type parameter + * @param url the url + * @param token the token + * @param obj the obj + * @param reqType the req type + * @return the t + */ + public static HttpResponse huaWeiProRun(String url, String token, E obj, RequestMethod reqType) { + try { + HttpResponse svrResp = null; + + Map httpHeadMap = new HashMap<>(2); + httpHeadMap.put(String.valueOf(Header.CONNECTION), "keep-alive"); + httpHeadMap.put(String.valueOf(Header.ACCEPT), "*/*"); + + if (token != null && token.length() > 0) { + httpHeadMap.put("X-Auth-Token", token); + } + + String reqJson = OBJECT_MAPPER.writeValueAsString(obj); + + log.debug("Restful request: {}, {}: {}", url, token, reqJson); + + switch (reqType) { + case GET: + svrResp = getHttpResp(url, httpHeadMap); + break; + case POST: + svrResp = postHttpResp(url, httpHeadMap, reqJson); + break; + case PUT: + svrResp = putHttpResp(url, httpHeadMap, reqJson); + break; + case DELETE: + svrResp = deleteHttpResp(url, httpHeadMap, reqJson); + break; + default: + log.error("Unknown method: {}", reqType); + break; + } + + if (svrResp == null) { + log.debug("Server return null: {}", url); + return null; + } + + log.debug("Restful response: {}, {}: {}", url, token, svrResp.body()); + + return svrResp; + } catch (JsonProcessingException e) { + log.info("System exception: ", e); + return null; + } + } + /** * Create resp type type. * diff --git a/src/main/java/com/huawei/dispose/common/HuaWeiLoginReq.java b/src/main/java/com/huawei/dispose/common/HuaWeiLoginReq.java new file mode 100644 index 00000000..c9e2b86d --- /dev/null +++ b/src/main/java/com/huawei/dispose/common/HuaWeiLoginReq.java @@ -0,0 +1,30 @@ +package com.huawei.dispose.common; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * The type Hua wei login req. + * + * @author + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonPropertyOrder({"username", "password"}) +@JsonInclude(JsonInclude.Include.NON_NULL) +public class HuaWeiLoginReq { + /** + * The User name. + */ + private String username; + /** + * The Password. + */ + private String password; +} diff --git a/src/main/java/com/huawei/dispose/common/HuaWeiLoginResp.java b/src/main/java/com/huawei/dispose/common/HuaWeiLoginResp.java new file mode 100644 index 00000000..78a9b355 --- /dev/null +++ b/src/main/java/com/huawei/dispose/common/HuaWeiLoginResp.java @@ -0,0 +1,26 @@ +package com.huawei.dispose.common; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +/** + * The type Hua wei login rsp. + * + * @author + */ +@EqualsAndHashCode() +@Data +@NoArgsConstructor +@AllArgsConstructor +@JsonPropertyOrder({"token"}) +@JsonInclude(JsonInclude.Include.NON_NULL) +public class HuaWeiLoginResp { + /** + * The Token. + */ + private String token; +} diff --git a/src/main/java/com/huawei/dispose/protocol/HuaWeiInterface.java b/src/main/java/com/huawei/dispose/protocol/HuaWeiInterface.java new file mode 100644 index 00000000..7476ff9d --- /dev/null +++ b/src/main/java/com/huawei/dispose/protocol/HuaWeiInterface.java @@ -0,0 +1,28 @@ +package com.huawei.dispose.protocol; + +import cn.hutool.http.HttpResponse; +import com.dispose.restful.RestfulInterface; +import com.huawei.dispose.common.HuaWeiLoginReq; +import org.springframework.web.bind.annotation.RequestMethod; + +/** + * The type Hua Wei interface. + * + * @author + */ +public class HuaWeiInterface { + /** + * Login protocol resp dto. + * + * @param baseUrlPath the base url path + * @param username the username + * @param password the password + * @return the http resp + */ + public HttpResponse auth(String baseUrlPath, String username, String password) { + return RestfulInterface.huaWeiProRun(baseUrlPath + "/auth", + null, + new HuaWeiLoginReq(username, password), + RequestMethod.POST); + } +}