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

com.dingtalk.api.DefaultDingTalkClient Maven / Gradle / Ivy

The newest version!
package com.dingtalk.api;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.taobao.api.ApiException;
import com.taobao.api.ApiRuleException;
import com.taobao.api.Constants;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.FileItem;
import com.taobao.api.TaobaoParser;
import com.taobao.api.TaobaoRequest;
import com.taobao.api.TaobaoResponse;
import com.taobao.api.TaobaoUploadRequest;
import com.taobao.api.internal.parser.json.ObjectJsonParser;
import com.taobao.api.internal.util.HttpResponseData;
import com.taobao.api.internal.util.RequestParametersHolder;
import com.taobao.api.internal.util.TaobaoHashMap;
import com.taobao.api.internal.util.TaobaoLogger;
import com.taobao.api.internal.util.TaobaoUtils;
import com.taobao.api.internal.util.WebV2Utils;

/**
 * @author chaohui.zch 2016年11月8日下午2:38:45
 */
public class DefaultDingTalkClient extends DefaultTaobaoClient implements DingTalkClient {

	protected boolean useJsonString = false; // 是否采用JsonString生成

	public DefaultDingTalkClient(String serverUrl) {
		super(serverUrl, null, null);
	}

	public  T execute(TaobaoRequest request, String accessKey, String accessSecret) throws ApiException {
		return execute(request, accessKey, accessSecret, null,null);
	}

	public  T execute(TaobaoRequest request, String accessKey, String accessSecret, String suiteTicket) throws ApiException {
		return execute(request, accessKey, accessSecret, suiteTicket,null);
	}

	public  T execute(TaobaoRequest request, String session) throws ApiException {
		if(request.getTopApiCallType() == null || request.getTopApiCallType().equals(DingTalkConstants.CALL_TYPE_TOP)) {
			return super.execute(request, session);
		} else {
			return executeOApi(request, session);
		}
	}

	public  T execute(TaobaoRequest request, String accessKey, String accessSecret, String suiteTicket, String corpId) throws ApiException {
		if(request.getTopApiCallType() == null || request.getTopApiCallType().equals(DingTalkConstants.CALL_TYPE_TOP)) {
			return super.execute(request,null);
		} else {
			return executeOApi(request,null, accessKey, accessSecret,suiteTicket, corpId);
		}
	}

	private  T executeOApi(TaobaoRequest request, String session) throws ApiException {
		return executeOApi(request, session, null, null, null, null);
	}

	private  T executeOApi(TaobaoRequest request, String session, String accessKey, String accessSecret, String suiteTicket, String corpId) throws ApiException {
		long start = System.currentTimeMillis();
		// 构建响应解释器
		TaobaoParser parser = null;
		if (this.needEnableParser) {
			parser = new ObjectJsonParser(request.getResponseClass(), true);
		}

		// 本地校验请求参数
		if (this.needCheckRequest) {
			try {
				request.check();
			} catch (ApiRuleException e) {
				T localResponse = null;
				try {
					localResponse = request.getResponseClass().newInstance();
				} catch (Exception xe) {
					throw new ApiException(xe);
				}
				try {
					Class clazz = localResponse.getClass();
					Method errcodeMethod = clazz.getDeclaredMethod("setErrcode", Long.class);
					Method errmsgMethod = clazz.getDeclaredMethod("setErrmsg", String.class);
					errcodeMethod.invoke(localResponse, Long.parseLong(e.getErrCode()));
					errmsgMethod.invoke(localResponse, e.getErrMsg());
					return localResponse;
				}catch (Exception e2){
					throw new ApiException(e2);
				}
			}
		}

		RequestParametersHolder requestHolder = new RequestParametersHolder();
		TaobaoHashMap appParams = new TaobaoHashMap(request.getTextParams());
		requestHolder.setApplicationParams(appParams);

		// 添加协议级请求参数
		TaobaoHashMap protocalMustParams = new TaobaoHashMap();
		protocalMustParams.put(DingTalkConstants.ACCESS_TOKEN, session);
		requestHolder.setProtocalMustParams(protocalMustParams);

		try {
			String fullUrl;
			// 签名优先
			if(accessKey != null) {
				Long timestamp = System.currentTimeMillis();
				// 验证签名有效性
				String canonicalString = DingTalkSignatureUtil.getCanonicalStringForIsv(timestamp, suiteTicket);
				String signature = DingTalkSignatureUtil.computeSignature(accessSecret, canonicalString);
				Map ps = new HashMap();
				ps.put("accessKey", accessKey);
				ps.put("signature", signature);
				ps.put("timestamp", timestamp+"");
				if(suiteTicket != null) {
					ps.put("suiteTicket", suiteTicket);
				}
				if(corpId != null){
					ps.put("corpId", corpId);
				}

				String queryStr =DingTalkSignatureUtil.paramToQueryString(ps, "utf-8");
				if (this.serverUrl.indexOf("?") > 0) {
					fullUrl = this.serverUrl + "&"+queryStr;
				} else {
					fullUrl = this.serverUrl + "?"+queryStr;
				}
			}else{
				if (this.serverUrl.indexOf("?") > 0) {
					fullUrl = this.serverUrl + (session != null && session.length() > 0 ? ("&access_token=" + session) : "");
				} else {
					fullUrl = this.serverUrl + (session != null && session.length() > 0 ? ("?access_token=" + session) : "");
				}
			}

			HttpResponseData data = null;
			// 是否需要压缩响应
			if (this.useGzipEncoding) {
				request.getHeaderMap().put(Constants.ACCEPT_ENCODING, Constants.CONTENT_ENCODING_GZIP);
			}

			if("GET".equals(request.getTopHttpMethod())) {
				data = WebV2Utils.doGet(fullUrl, appParams, connectTimeout, readTimeout);
			} else {
				// 是否需要上传文件
				if (request instanceof TaobaoUploadRequest) {
					TaobaoUploadRequest uRequest = (TaobaoUploadRequest) request;
					Map fileParams = TaobaoUtils.cleanupMap(uRequest.getFileParams());
					data = WebV2Utils.doPost(fullUrl, appParams, fileParams, Constants.CHARSET_UTF8, connectTimeout, readTimeout, request.getHeaderMap());
				} else {

					Map jsonParams = new HashMap();
					for (Map.Entry paramEntry : appParams.entrySet()) {
						String key = paramEntry.getKey();
						String value = paramEntry.getValue();
						if(this.useJsonString) {
							jsonParams.put(key, value);
						} else {
							if (value.startsWith("[") && value.endsWith("]")) {
								List> childMap = (List>) TaobaoUtils.jsonToObject(value);
								jsonParams.put(key, childMap);
							} else if (value.startsWith("{") && value.endsWith("}")) {
								Map childMap = (Map) TaobaoUtils.jsonToObject(value);
								jsonParams.put(key, childMap);
							} else {
								jsonParams.put(key, value);
							}
						}
					}

					data = WebV2Utils.doPostWithJson(fullUrl, jsonParams, Constants.CHARSET_UTF8, connectTimeout, readTimeout);
				}
			}
			requestHolder.setResponseBody(data.getBody());
			requestHolder.setResponseHeaders(data.getHeaders());
		} catch (IOException e) {
			TaobaoLogger.logApiError("_dingtalk_", request.getApiMethodName(), serverUrl, requestHolder.getAllParams(), System.currentTimeMillis() - start, e.toString());
			throw new ApiException(e);
		}

		T tRsp = null;
		if (this.needEnableParser) {
			tRsp = parser.parse(requestHolder.getResponseBody(), Constants.RESPONSE_TYPE_DINGTALK_OAPI);
			tRsp.setBody(requestHolder.getResponseBody());
		} else {
			try {
				tRsp = request.getResponseClass().newInstance();
				tRsp.setBody(requestHolder.getResponseBody());
			} catch (Exception e) {
				throw new ApiException(e);
			}
		}

		tRsp.setParams(appParams);
		if (!tRsp.isSuccess()) {
			TaobaoLogger.logApiError("_dingtalk_", request.getApiMethodName(), serverUrl, requestHolder.getAllParams(), System.currentTimeMillis() - start, tRsp.getBody());
		}
		return tRsp;
	}

	public boolean isUseJsonString() {
		return useJsonString;
	}

	public void setUseJsonString(boolean useJsonString) {
		this.useJsonString = useJsonString;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy