com.alibaba.ocean.rawsdk.client.policy.RequestPolicy Maven / Gradle / Ivy
/**
* Project: ocean.client.java.basic
*
* File Created at 2011-10-26
* $Id: RequestPolicy.java 410052 2015-05-06 08:18:05Z hongbang.hb $
*
* Copyright 2008 Alibaba.com Croporation Limited.
* All rights reserved.
*
* This software is the confidential and proprietary information of
* Alibaba Company. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Alibaba.com.
*/
package com.alibaba.ocean.rawsdk.client.policy;
import com.alibaba.ocean.rawsdk.client.ErrorHandler;
import com.alibaba.ocean.rawsdk.util.DateUtil;
/**
* 璇锋眰绾ц闂瓥鐣�
*
* 瀹氫箟鏄惁浣跨敤绛惧悕銆佹槸鍚︿娇鐢╤ttps銆佹槸鍚﹀姞鍏ユ巿鏉冧俊鎭�瓒呮椂璁剧疆銆乭ttp鏂规硶璁剧疆绛夎姹傜骇鍒俊鎭�
*
* 鍙傝�ClientPolicy
*
*
* @author jade
* @author xiaoning.qxn
*/
public class RequestPolicy implements Cloneable {
private boolean requestSendTimestamp = false;
private boolean useHttps = true;
private Protocol requestProtocol = Protocol.param2;
private Protocol responseProtocol = Protocol.json2;
private boolean responseCompress = true;
private int requestCompressThreshold = -1;
private int timeout = 5000; // 5绉�
private HttpMethodPolicy httpMethod = HttpMethodPolicy.POST;
private String queryStringCharset = "UTF-8"; // Request URL query string
// encoder
private String contentCharset = "UTF-8"; // Request body encoder
private boolean useSignture = true;
private boolean needAuthorization = false;
private boolean accessPrivateApi = false;
private int defaultApiVersion = 1;
private ErrorHandler errorHandler;
private String dateFormat = DateUtil.DEFAULT_DATE_FORMAT_STR;
public static RequestPolicy authPolicy = null;
/**
* 鍏ㄥ眬鐨勬巿鏉冭姹傜殑request policy锛岀敤鎴峰彲鑳介渶瑕佽缃秴鏃舵椂闂达紝鍏朵粬涓嶇敤鍙�
*
* @return
*/
public static RequestPolicy getAuthPolicy() {
if (authPolicy == null) {
authPolicy = new RequestPolicy().setHttpMethod(HttpMethodPolicy.POST).setRequestProtocol(Protocol.param2).setUseHttps(true)
.setNeedAuthorization(false);
}
return authPolicy;
}
public RequestPolicy clone() {
RequestPolicy newObj = newPolicy();
newObj.requestSendTimestamp = requestSendTimestamp;
newObj.useHttps = useHttps;
newObj.requestProtocol = requestProtocol;
newObj.responseProtocol = responseProtocol;
newObj.responseCompress = responseCompress;
newObj.requestCompressThreshold = requestCompressThreshold;
newObj.timeout = timeout;
newObj.httpMethod = httpMethod;
newObj.queryStringCharset = queryStringCharset;
newObj.contentCharset = contentCharset;
newObj.useSignture = useSignture;
newObj.accessPrivateApi = accessPrivateApi;
newObj.defaultApiVersion = defaultApiVersion;
return newObj;
}
protected RequestPolicy newPolicy() {
return new RequestPolicy();
}
/**
* 璁剧疆鏄惁浣跨敤鏃堕棿鎴�
*
* @param requestSendTimestamp
* @return a reference to this object
*/
public RequestPolicy setRequestSendTimestamp(boolean requestSendTimestamp) {
this.requestSendTimestamp = requestSendTimestamp;
return this;
}
public boolean isRequestSendTimestamp() {
return requestSendTimestamp;
}
public boolean isUseHttps() {
return useHttps;
}
/**
* 璁剧疆鏄惁浣跨敤https璁块棶
*
* @param useHttps
* @return a reference to this object
*/
public RequestPolicy setUseHttps(boolean useHttps) {
this.useHttps = useHttps;
return this;
}
/**
* 璁剧疆璁块棶鍗忚,榛樿涓烘帹鑽愮殑param2
*
* @param protocol
* @return a reference to this object
*/
public RequestPolicy setRequestProtocol(Protocol protocol) {
if (protocol == null) {
throw new IllegalArgumentException("protocol can not be null");
}
this.requestProtocol = protocol;
return this;
}
public Protocol getRequestProtocol() {
return requestProtocol;
}
public boolean isResponseCompress() {
return responseCompress;
}
/**
* response鏄惁鍘嬬缉
*
* @param responseCompress
* @return a reference to this object
*/
private RequestPolicy setResponseCompress(boolean responseCompress) {
this.responseCompress = responseCompress;
return this;
}
public int getRequestCompressThreshold() {
return requestCompressThreshold;
}
/**
* 璁剧疆璇锋眰鍘嬬缉鐨勯榾鍊硷紝瓒呰繃杩欎釜闃��灏辫繘琛屽帇缂�榛樿鍊间负-1涓嶅紑鍚�寤鸿1K锛屽嵆1024
*
* @param requestCompressThreshold
* @return
*/
public RequestPolicy setRequestCompressThreshold(int requestCompressThreshold) {
this.requestCompressThreshold = requestCompressThreshold;
return this;
}
public int getTimeout() {
return timeout;
}
/**
* 璁剧疆瓒呮椂鏃堕棿,榛樿5s
*
* @param timeout
* @return a reference to this object
*/
public RequestPolicy setTimeout(int timeout) {
this.timeout = timeout;
return this;
}
/**
* 璁剧疆http鏂规硶锛岄粯璁や负鑷姩锛圓TOU锛夛紝褰搑equest涓湁鍙傛暟鏃讹紝浣跨敤post锛屽惁鍒檊et
*
* @param httpMethod
* @return a reference to this object
*/
public RequestPolicy setHttpMethod(HttpMethodPolicy httpMethod) {
this.httpMethod = httpMethod;
return this;
}
public HttpMethodPolicy getHttpMethod() {
return httpMethod;
}
public String getQueryStringCharset() {
return queryStringCharset;
}
/**
* 璁剧疆request缂栫爜锛岄粯璁や负UTF-8
*
* @param queryStringCharset
* @return a reference to this object
*/
public RequestPolicy setQueryStringCharset(String queryStringCharset) {
this.queryStringCharset = queryStringCharset;
return this;
}
public String getContentCharset() {
return contentCharset;
}
/**
* 璁剧疆cotent缂栫爜锛岄粯璁や负UTF-8
*
* @param contentCharset
* @return a reference to this object
*/
public RequestPolicy setContentCharset(String contentCharset) {
this.contentCharset = contentCharset;
return this;
}
/**
* 璁剧疆response鍗忚锛岄粯璁や负鎺ㄨ崘鐨刯son
*
* @param responseProtocol
* @return a reference to this object
*/
public RequestPolicy setResponseProtocol(Protocol responseProtocol) {
if (requestProtocol == null) {
throw new IllegalArgumentException("response protocol can not be null");
}
this.responseProtocol = responseProtocol;
return this;
}
public Protocol getResponseProtocol() {
return responseProtocol;
}
/**
* 璇锋眰HTTP鏂规硶绛栫暐
*
* @author jade
*/
public static enum HttpMethodPolicy {
/**
* 鎬绘槸浣跨敤post
*/
POST,
/**
* 鎬绘槸浣跨敤get
*/
GET;
}
public ErrorHandler getErrorHandler() {
return errorHandler;
}
/**
* 璁剧疆error handler
*
* @param errorHandler
* @return a reference to this object
*/
public RequestPolicy setErrorHandler(ErrorHandler errorHandler) {
this.errorHandler = errorHandler;
return this;
}
public boolean isUseSignture() {
return useSignture;
}
/**
* 鏄惁浣跨敤绛惧悕锛屽彇鍐充簬api瑕佹眰
*
* @param useSignture
* @return a reference to this object
*/
public RequestPolicy setUseSignture(boolean useSignture) {
this.useSignture = useSignture;
return this;
}
public boolean isNeedAuthorization() {
return needAuthorization;
}
/**
* 鏄惁鍔犲叆鎺堟潈淇℃伅锛屽彇鍐充簬api瑕佹眰
*
* @param needAuthorization
* @return a reference to this object
*/
public RequestPolicy setNeedAuthorization(boolean needAuthorization) {
this.needAuthorization = needAuthorization;
return this;
}
public boolean isAccessPrivateApi() {
return accessPrivateApi;
}
/**
* 鏄惁鍦ㄨ闂鏈塧pi锛岄粯璁や负false. 鍗宠闂畊rl浠ttp://host:port/api/...鏂瑰紡
*
* @param accessPrivateApi
*/
public RequestPolicy setAccessPrivateApi(boolean accessPrivateApi) {
this.accessPrivateApi = accessPrivateApi;
return this;
}
public int getDefaultApiVersion() {
return defaultApiVersion;
}
public String getDateFormat() {
return dateFormat;
}
public RequestPolicy setDateFormat(String dateFormat) {
this.dateFormat = dateFormat;
return this;
}
}