com.dahuatech.icc.oauth.http.IccClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-sdk-oauth Show documentation
Show all versions of java-sdk-oauth Show documentation
Dahua ICC Open API SDK for Java
package com.dahuatech.icc.oauth.http;
import com.dahuatech.hutool.core.util.StrUtil;
import com.dahuatech.hutool.http.HttpResponse;
import com.dahuatech.hutool.json.JSONUtil;
import com.dahuatech.hutool.log.Log;
import com.dahuatech.hutool.log.LogFactory;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.exception.ServerException;
import com.dahuatech.icc.oauth.handle.InitVersionProcessor;
import com.dahuatech.icc.oauth.handle.LoadHttpConfigInfo;
import com.dahuatech.icc.oauth.handle.TokenHandlerProcessor;
import com.dahuatech.icc.oauth.model.v202010.OauthConfigBaseInfo;
import com.dahuatech.icc.oauth.profile.GrantType;
import com.dahuatech.icc.oauth.profile.IccProfile;
import com.dahuatech.icc.oauth.unmarshaller.JsonUnmashaller;
import com.dahuatech.icc.oauth.unmarshaller.Unmarshaller;
import com.dahuatech.icc.oauth.utils.LogUtils;
public class IccClient extends AbstractIClient{
private static final Log logger = LogFactory.get();
private TokenHandlerProcessor tokenHandleSingle;
private Unmarshaller unmarshaller;
private OauthConfigBaseInfo oauthConfigBaseInfo;
public IccClient() throws ClientException {
LoadHttpConfigInfo.getInstance().loadIccSdkProperties();
tokenHandleSingle = TokenHandlerProcessor.getInstance();
unmarshaller = new JsonUnmashaller();
this.oauthConfigBaseInfo = LoadHttpConfigInfo.getInstance().getOauthConfigBaseInfo();
}
public IccClient(OauthConfigBaseInfo oauthConfigBaseInfo) throws ClientException {
this.oauthConfigBaseInfo = oauthConfigBaseInfo;
this.tokenHandleSingle = TokenHandlerProcessor.getInstance();
this.unmarshaller = new JsonUnmashaller();
InitVersionProcessor.getInstance().initSystemVersion(oauthConfigBaseInfo.getHttpConfigInfo());
}
@Override
public String doAction(IccHttpHttpRequest request) throws ClientException, ServerException {
IccHttpHttpRequest iccHttpRequest = signRequest(request);
String httpResult = iccHttpRequest.execute();
logger.debug("response httpResult =[{}]", httpResult);
return httpResult;
}
@Override
public T doAction(IccHttpHttpRequest request, Class tClass)
throws ClientException, ServerException {
IccHttpHttpRequest iccHttpRequest = signRequest(request);
String uuid = request.getRequestUniqueCode();
LogUtils.printIn(logger,request);
HttpResponse httpResult = iccHttpRequest.executeResponse();
String result = httpResult.body();
LogUtils.printOut(logger,uuid,result);
T t = unmarshal(tClass, result);
/** 返回原字符串,回填result字段 */
t.setResult(result);
if (!t.isSuccess()) {
if("27001000".equals(t.getCode()) || "27001007".equals(t.getCode())){
TokenHandlerProcessor.getInstance().clearExpiredToken(oauthConfigBaseInfo);
// doAction(request,tClass);
}
}
return t;
}
@Override
public synchronized IccTokenResponse.IccToken getAccessToken(GrantType grantType) {
/** false 不续缓存的生存时间 */
IccTokenResponse.IccToken token = tokenHandleSingle.getTokenCache(oauthConfigBaseInfo);
if (token != null) {
return token;
}
token = tokenHandleSingle.oauth(oauthConfigBaseInfo);
return token;
}
@Override
public IccTokenResponse.IccToken getAccessToken() {
/** false 不续缓存的生存时间 */
IccTokenResponse.IccToken token = tokenHandleSingle.getTokenCache(oauthConfigBaseInfo);
if (token != null) {
return token;
}
token = tokenHandleSingle.oauth(oauthConfigBaseInfo);
return token;
}
@Override
IccHttpHttpRequest signRequest(IccHttpHttpRequest request) {
if (request.isNeedAuth()) {
IccTokenResponse.IccToken token = getAccessToken();
request.header("Authorization", "bearer " + (token != null ? token.getAccess_token() : ""));
// 密码鉴权类型设置User-Id请求头
if (GrantType.password == oauthConfigBaseInfo.getGrantType()) {
request.header(
"User-Id", (token != null ? token.getUserId() : IccProfile.CONFIG_CLIENT_USERID));
}
if (GrantType.client_credentials == oauthConfigBaseInfo.getGrantType()) {
// 默认设置请求UserId
if (IccProfile.CONFIG_CLIENT_USERID_ENABLE) {
request.header("User-Id", IccProfile.CONFIG_CLIENT_USERID);
}
}
}
/**
* bodyParameters优先级大于body
*
* 1、从request.body(""),json字符串
*
*
2、request.bodyParameters,map类型
*/
if (request.getBodyParameters().size() > 0 && StrUtil.isBlank(request.getBody())) {
request.body(JSONUtil.toJsonStr(request.getBodyParameters()));
}
return request;
}
@Override
public T unmarshal(Class clasz, String content)
throws ClientException {
return unmarshaller.unmarshal(clasz, content);
}
public OauthConfigBaseInfo getOauthConfigBaseInfo() {
return oauthConfigBaseInfo;
}
}