All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dahuatech.icc.oauth.http.AbstractIccRequest Maven / Gradle / Ivy

There is a newer version: 1.0.13.7
Show newest version
package com.dahuatech.icc.oauth.http;

import com.dahuatech.hutool.http.Method;
import com.dahuatech.icc.common.ParamValidEnum;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.oauth.model.v202010.OauthConfigBaseInfo;
import com.dahuatech.icc.oauth.exception.BusinessException;
import com.dahuatech.icc.util.StringUtils;
import com.dahuatech.icc.oauth.profile.GrantType;
import com.dahuatech.icc.oauth.model.v202010.OauthConfigUserPwdInfo;

import java.util.Map;

/**
 * @author 232676
 * @since 1.0.0 2020-10-24 20:59:11
 */
public abstract class AbstractIccRequest extends IccHttpHttpRequest {
    protected OauthConfigBaseInfo oauthConfigBaseInfo;

    public OauthConfigBaseInfo getOauthConfigBaseInfo() {
        return oauthConfigBaseInfo;
    }

    public void setOauthConfigBaseInfo(OauthConfigBaseInfo oauthConfigBaseInfo) {
        this.oauthConfigBaseInfo = oauthConfigBaseInfo;
    }

    public AbstractIccRequest(){}

    public void valid(){
        //鉴权参数校验
        baseCheck();
    }

    public void baseCheck(){
        if(this == null){
            throw new BusinessException(ParamValidEnum.OAUTH_PARAM_NOT_EMPTY_ERROR.getCode(),"请求对象为空", "GroupInfoAddRequest");
        }
        if(oauthConfigBaseInfo == null){
            throw new BusinessException(ParamValidEnum.OAUTH_PARAM_NOT_EMPTY_ERROR.getCode(), "鉴权信息为空", "oauthConfigBaseInfo");
        }
        if(oauthConfigBaseInfo.getGrantType() == null){
            throw new BusinessException(ParamValidEnum.OAUTH_PARAM_NOT_EMPTY_ERROR.getCode(), "鉴权类型为空", "grantType");
        }
        if(StringUtils.isEmpty(oauthConfigBaseInfo.getClientId())){
            throw new BusinessException(ParamValidEnum.OAUTH_PARAM_NOT_EMPTY_ERROR.getCode(), "申请访问凭证id为空", "clientId");
        }
        if(StringUtils.isEmpty(oauthConfigBaseInfo.getClientSecret())){
            throw new BusinessException(ParamValidEnum.OAUTH_PARAM_NOT_EMPTY_ERROR.getCode(), "申请访问凭证密钥为空", "clientSecret");
        }
        if(oauthConfigBaseInfo.getHttpConfigInfo() == null){
            throw new BusinessException(ParamValidEnum.OAUTH_PARAM_NOT_EMPTY_ERROR.getCode(), "平台信息为空", "httpConfigInfo");
        }
        if(StringUtils.isEmpty(oauthConfigBaseInfo.getHttpConfigInfo().getHost())){
            throw new BusinessException(ParamValidEnum.OAUTH_PARAM_NOT_EMPTY_ERROR.getCode(), "平台信息|host为空", "host");
        }
        if(oauthConfigBaseInfo.getGrantType() == GrantType.password){//客户端
            OauthConfigUserPwdInfo oauthConfigUserPwdInfo = (OauthConfigUserPwdInfo)oauthConfigBaseInfo;
            if(StringUtils.isEmpty(oauthConfigUserPwdInfo.getPassword())){
                throw new BusinessException(ParamValidEnum.OAUTH_PARAM_NOT_EMPTY_ERROR.getCode(), "平台信息|密码为空", "password");
            }
            if(StringUtils.isEmpty(oauthConfigUserPwdInfo.getUsername())){
                throw new BusinessException(ParamValidEnum.OAUTH_PARAM_NOT_EMPTY_ERROR.getCode(), "平台信息|用户名为空", "username");
            }
        }
    }

  public AbstractIccRequest(String url, Method method){
    super(url, method, Boolean.TRUE);
  }

  public AbstractIccRequest(String url, Method method, boolean needAuth) {
    super(url, method, needAuth);
  }

  public AbstractIccRequest(String url, Method method, boolean needAuth, String body)
      throws ClientException {
    super(url, method, needAuth, body);
  }

  /**
   * 请求body,json,请求发送前会把map的值转换成json
   *
   * @param name 名称
   * @param value 值
   */
  protected void putBodyParameter(String name, Object value) {
    setParameter(this.bodyParameters, name, value);
  }

  /**
   * 表单格式,添加参数
   *
   * @param map map参数
   * @param name 名称
   * @param value 值
   */
  protected void setParameter(Map map, String name, Object value) {
    if (null == map || name == null || value == null) {
      return;
    }
    map.put(name, value);
  }

  /**
   * 返回结果对象
   *
   * @return Response对象
   */
  public abstract Class getResponseClass();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy