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

com.alibaba.csb.sdk.internel.HttpClientHelper Maven / Gradle / Ivy

There is a newer version: 1.1.5.11
Show newest version
package com.alibaba.csb.sdk.internel;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.InvalidParameterException;
import java.util.*;
import java.util.Map.Entry;

import com.alibaba.csb.sdk.*;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import com.alibaba.csb.sdk.ContentBody.Type;
import com.alibaba.csb.sdk.security.SignUtil;
//import com.alibaba.fastjson.JSONObject;

/**
 * HttpClient Helper Class
 *
 */
public class HttpClientHelper {
	public static void printDebugInfo(String msg) {
		if (SdkLogger.isLoggable())
			SdkLogger.print(msg);
	}

	public static void mergeParams(Map> urlParamsMap, Map paramsMap, boolean decodeFlag) throws HttpCallerException {
		if (paramsMap != null) {
			//decode all params first, due to it will be encode to construct the request URL later
			String value;
			for (Entry kv:paramsMap.entrySet()) {
				value = decodeValue(kv.getKey(), kv.getValue(), decodeFlag);
				urlParamsMap.put(kv.getKey(), Arrays.asList(value));
			}
		}
	}

	/**
	 * 根据输入的参数,关键值和扩展签名头列表 生成签名并返回最终的签名头列表
	 * @param paramsMap
	 * @param apiName
	 * @param version
	 * @param accessKey
	 * @param securityKey
	 * @param extSignHeaders 放在extSignHeaders里的kv都参与签名
	 * @return
	 */
	public static Map newParamsMap(Map> paramsMap, String apiName, String version,
			String accessKey, String securityKey, boolean timestampFlag, boolean nonceFlag,  Map extSignHeaders, final StringBuffer signDiagnosticInfo, String spiImpl) {
		return SignUtil.newParamsMap(paramsMap, apiName, version, accessKey, securityKey, timestampFlag, nonceFlag, extSignHeaders, signDiagnosticInfo, spiImpl);
	}

	public static String trimWhiteSpaces(String value) {
		if (value == null) return value;

		return value.trim();
	}

	public static String trimUrl(String requestURL) {
		int pos = requestURL.indexOf("?");
		String ret = requestURL;

		if (pos >= 0) {
			ret = requestURL.substring(0, pos);
		}

		return ret;
	}

	public static void validateParams(String apiName, String accessKey, String securityKey, Map paramsMap) throws HttpCallerException {
		if (apiName == null)
			throw new HttpCallerException(new InvalidParameterException("param apiName can not be null!"));

		if (accessKey != null && securityKey == null)
			throw new HttpCallerException(
					new InvalidParameterException("param securityKey can not be null for a given accessKey!"));

		if (paramsMap != null) {
			for (Entry kv:paramsMap.entrySet()) {
				if (kv.getValue() == null) {
					throw new HttpCallerException(new InvalidParameterException(
							String.format("bad parasMap, the value for key [ %s ] is null, please remove the key or set its value, e.g. \"\"!", kv.getKey())));
				}
			}
		}

	}
	
	private static String decodeValue(String key, String value, boolean decodeFlag) throws HttpCallerException {
		if (decodeFlag) {
			if (value == null ) {
			  throw new HttpCallerException("bad params, the value for key {"+key+"} is null!");
			}
			return URLDecoder.decode(value);
		}
		
		return value;
	}

	/**
	 * Parse URL parameters to Map, url-decode all values
	 * @param requestURL
	 * @return
	 * @throws HttpCallerException
	 */
	public static Map> parseUrlParamsMap(String requestURL, boolean decodeFlag) throws HttpCallerException {
		boolean questionMarkFlag = requestURL.contains("?");
		Map> urlParamsMap = new HashMap>();
		String key;
		String value;
		if (questionMarkFlag) {
			// parse params
			int pos = requestURL.indexOf("?");
			String paramStr = requestURL.substring(pos + 1);
			// requestURL = requestURL.substring(0, pos);
			// The caller needs to ensure the url-encode for a parameter value!!
			String[] params = paramStr.split("&");
			for (String param : params) {
				pos = param.indexOf("=");
				if (pos <= 0) {
					throw new HttpCallerException("bad request URL, url params error:" + requestURL);
				}
				key = decodeValue("", param.substring(0, pos), decodeFlag);
				value = param.substring(pos + 1);
				List values = urlParamsMap.get(key);
				if (values == null) {
					values = new ArrayList();
				}
				values.add(decodeValue(key, value, decodeFlag));
				urlParamsMap.put(key, values);
			}
		}

		return urlParamsMap;
	}
//
//	public static StringEntity jsonProcess(Map params) {
//		JSONObject jsonParam = new JSONObject();
//		for (Entry entry : params.entrySet())
//			jsonParam.put(entry.getKey(), entry.getValue());
//
//		StringEntity entity = new StringEntity(jsonParam.toString(), HTTP.UTF_8);// 解决中文乱码问题
//		entity.setContentEncoding(HTTP.UTF_8);
//		entity.setContentType("application/json");
//		return entity;
//	}

	private static void setHeaders(HttpPost httpPost, Map newParamsMap) {
		if (newParamsMap != null) {
			for(Entry kv: newParamsMap.entrySet())
			  httpPost.addHeader(kv.getKey(), kv.getValue());
		}
	}
	

	public static void setHeaders(HttpGet httpGet, Map newParamsMap) {
		if (newParamsMap != null) {
			for(Entry kv: newParamsMap.entrySet())
				httpGet.addHeader(kv.getKey(), kv.getValue());
		}
	}
	
	public static String genCurlHeaders(Map newParamsMap) {
		if (newParamsMap != null) {
			StringBuffer sb = new StringBuffer();
			for(Entry kv: newParamsMap.entrySet())
				sb.append("-H \"").append(kv.getKey()).append(":").append(kv.getValue()).append("\"  ");
			
			return sb.toString();
		}else
			return "";
	}
	
	public static String createPostCurlString(String url, Map params, Map headerParams, ContentBody cb, Map directHheaderParamsMap) {
		StringBuffer sb = new StringBuffer("curl ");
		
		//透传的http headers
		sb.append(genCurlHeaders(directHheaderParamsMap));
		
		sb.append(genCurlHeaders(headerParams));
		
		if (params != null) {
			StringBuffer postSB = new StringBuffer();
			for(Entry e:params.entrySet()){
				if(postSB.length()>0)
					postSB.append("&");
				postSB.append(e.getKey()).append("=").append(URLEncoder.encode(e.getValue()));
			}
			if (postSB.length() > 0) {
				sb.append(" -d \"");
				postSB.append("\"");
				sb.append(postSB.toString());
			} else {
				sb.append("--data ''");
			}
		} else  {
			// set params as
			//FIXME need this ??
			sb.append("--data '");
			sb.append(urlEncodedString(toNVP(params), HTTP.UTF_8));
			sb.append("'");
		}
		
		sb.append(" --insecure ");
		sb.append("\"");
		sb.append(url);
		sb.append("\"");
		return sb.toString();
	}
	
	private static String urlEncodedString(List parameters, String charset) {
		return URLEncodedUtils.format(parameters,
                charset != null ? charset : HTTP.DEF_CONTENT_CHARSET.name());
	}

	public static HttpPost createPost(final String url, Map urlParams, Map headerParams, ContentBody cb) {
		//set both cb and urlParams
		String newUrl = url;
		if (cb != null && urlParams != null)
		if (urlParams != null) {
			List nvps = toNVP(urlParams);
			String newParamStr = urlEncodedString(nvps, HTTP.UTF_8);
			if (!url.contains("?")) {
				newUrl =  String.format("%s?%s", url, newParamStr);
			} else {
				newUrl =  String.format("%s&%s", url, newParamStr);
			} 
		}
		printDebugInfo("new requestURL=" + newUrl);
		HttpPost httpost = new HttpPost(newUrl);
		setHeaders(httpost, headerParams);
		if (cb == null) {
			List nvps = toNVP(urlParams);
			try {
				httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		} else {
			if (cb.getContentType() == Type.JSON) {
				StringEntity entity = new StringEntity((String)cb.getContentBody(), HTTP.UTF_8);// 解决中文乱码问题
				entity.setContentType(Type.JSON.getContentType());
				httpost.setEntity(entity);
			}else {
				//binary
				httpost.setHeader(HTTP.CONTENT_TYPE, Type.BINARY.getContentType());  
		        ByteArrayEntity be = new ByteArrayEntity((byte[])cb.getContentBody());  
		        httpost.setEntity(be); 
			}
		}

		return httpost;
	}

	private static List toNVP(Map urlParams) {
		List nvps = new ArrayList();

		//fix NPE
		if (urlParams != null) {
			Set keySet = urlParams.keySet();
			for (String key : keySet) {
				nvps.add(new BasicNameValuePair(key, urlParams.get(key)));
			}
		}
		return nvps;
	}

	public static void setDirectHeaders(HttpPost httpPost, Map directHheaderParamsMap) {
		if (directHheaderParamsMap == null) {
			//do nothing
			return;
		} else {
			for(Entry kv:directHheaderParamsMap.entrySet()) {
				if (kv.getKey() == null) {
					//log.info("ignore empty key");
				} else {
					if (HTTP.CONTENT_TYPE.equals(kv.getKey()) || !httpPost.containsHeader(kv.getKey())) {
					  // direct header has no chance to overwrite the normal headers, except it is the content-type
					  httpPost.addHeader(kv.getKey(), kv.getValue());
					}
				}
			}
		}
	}

	public static String getUrlPathInfo(String url) throws HttpCallerException{
		URL urlStr = null;
		try {
			urlStr = new URL(url);
		}catch (Exception e){
			throw new HttpCallerException("url is unformat, url is " + url);
		}
		String path = urlStr.getPath();
		return path;
	}



	public static String fetchResHeaders(final HttpResponse response) {
		if (response != null) {
			StringBuffer body = new StringBuffer();
			//add response http status
			body.append(String.format("\"%s\":\"%s\"", "HTTP-STATUS", response.getStatusLine()));
			for (Header header:response.getAllHeaders()) {
				if(body.length() > 0)
					body.append(",");
				body.append(String.format("\"%s\":\"%s\"", header.getName(), header.getValue()));
			}
			return String.format("{%s}", body.toString());
		}

		return null;
	}


	public static String generateAsEncodeRequestUrl(String requestURL, Map> urlParamsMap) {

		requestURL = HttpClientHelper.trimUrl(requestURL);

		StringBuffer params = new StringBuffer();
		for (Entry> kv : urlParamsMap.entrySet()) {
			if (params.length() > 0) {
				params.append("&");
			}
			if (kv.getValue() != null) {
				List vlist = kv.getValue();
				for (String v : vlist) {
					params.append(URLEncoder.encode(kv.getKey())).append("=").append(URLEncoder.encode(v));
				}
			}
		}

		String newRequestURL = requestURL;
		if (params.length() > 0)
			newRequestURL += "?" + params.toString();

		HttpClientHelper.printDebugInfo("-- requestURL=" + newRequestURL);
		return newRequestURL;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy