parent
6501d1b150
commit
8fa78111db
|
@ -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<String, String> 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<String, String> 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<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.
|
||||
*
|
||||
* @param url the url
|
||||
* @param header the header
|
||||
* @return the http response
|
||||
*/
|
||||
private static HttpResponse deleteHttpResp(String url, Map<String, String> 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 <E> 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 <E> HttpResponse huaWeiProRun(String url, String token, E obj, RequestMethod reqType) {
|
||||
try {
|
||||
HttpResponse svrResp = null;
|
||||
|
||||
Map<String, String> 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.
|
||||
*
|
||||
|
|
|
@ -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 <chenlinghy@cmhi.chinamoblie.com>
|
||||
*/
|
||||
@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;
|
||||
}
|
|
@ -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 <huangxin@cmhi.chinamoblie.com>
|
||||
*/
|
||||
@EqualsAndHashCode()
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@JsonPropertyOrder({"token"})
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public class HuaWeiLoginResp {
|
||||
/**
|
||||
* The Token.
|
||||
*/
|
||||
private String token;
|
||||
}
|
|
@ -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 <chenlinghy@cmhi.chinamoblie.com>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue