请求工具增加一部分日志,加入ssl证书管理器

This commit is contained in:
egan
2018-07-05 15:23:29 +08:00
parent 821e306fd1
commit 250bcb0d33
2 changed files with 24 additions and 8 deletions

View File

@@ -4,8 +4,10 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.egzosn.pay.common.bean.MethodType;
import com.egzosn.pay.common.bean.result.PayException;
import com.egzosn.pay.common.util.XML;
import com.egzosn.pay.common.exception.PayErrorException;
import com.egzosn.pay.common.util.XML;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpResponseException;
@@ -14,13 +16,11 @@ import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.Map;
import static com.egzosn.pay.common.http.UriVariables.getMapToParameters;
/**
@@ -32,7 +32,7 @@ import static com.egzosn.pay.common.http.UriVariables.getMapToParameters;
* </pre>
*/
public class ClientHttpRequest<T> extends HttpEntityEnclosingRequestBase implements org.apache.http.client.ResponseHandler<T>{
protected final Log log = LogFactory.getLog(ClientHttpRequest.class);
public static final ContentType APPLICATION_FORM_URLENCODED_UTF_8 = ContentType.create("application/x-www-form-urlencoded", Consts.UTF_8);;
@@ -157,6 +157,7 @@ public class ClientHttpRequest<T> extends HttpEntityEnclosingRequestBase impleme
if (request instanceof HttpHeader){
HttpHeader entity = (HttpHeader)request;
if (null != entity.getHeaders() ){
log.debug("header : " + JSON.toJSONString(entity.getHeaders()));
for (Header header : entity.getHeaders()){
addHeader(header);
}
@@ -167,6 +168,7 @@ public class ClientHttpRequest<T> extends HttpEntityEnclosingRequestBase impleme
setEntity(entity);
}
if (null != entity.getHeaders() ){
log.debug("header : " + JSON.toJSONString(entity.getHeaders()));
for (Header header : entity.getHeaders()){
addHeader(header);
}
@@ -174,13 +176,18 @@ public class ClientHttpRequest<T> extends HttpEntityEnclosingRequestBase impleme
} else if (request instanceof HttpEntity){
setEntity((HttpEntity)request);
} else if (request instanceof Map) {
StringEntity entity = new StringEntity(getMapToParameters((Map) request), APPLICATION_FORM_URLENCODED_UTF_8);
String parameters = getMapToParameters((Map) request);
log.debug("Parameter : " + parameters);
StringEntity entity = new StringEntity(parameters, APPLICATION_FORM_URLENCODED_UTF_8);
setEntity(entity);
} else if (request instanceof String) {
log.debug("Parameter : " + request);
StringEntity entity = new StringEntity((String) request, APPLICATION_FORM_URLENCODED_UTF_8);
setEntity(entity);
} else {
StringEntity entity = new StringEntity(JSON.toJSONString(request), ContentType.APPLICATION_JSON);
String body = JSON.toJSONString(request);
log.debug("body : " + request);
StringEntity entity = new StringEntity(body, ContentType.APPLICATION_JSON);
setEntity(entity);
}

View File

@@ -2,7 +2,8 @@ package com.egzosn.pay.common.http;
import com.egzosn.pay.common.bean.MethodType;
import com.egzosn.pay.common.util.str.StringUtils;
import org.apache.http.Header;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
@@ -20,12 +21,12 @@ import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContexts;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import java.io.*;
import java.net.URI;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.List;
import java.util.Map;
/**
@@ -38,6 +39,8 @@ import java.util.Map;
*/
public class HttpRequestTemplate {
protected final Log log = LogFactory.getLog(HttpRequestTemplate.class);
protected CloseableHttpClient httpClient;
protected PoolingHttpClientConnectionManager connectionManager;
@@ -109,6 +112,10 @@ public class HttpRequestTemplate {
char[] password = configStorage.getStorePassword().toCharArray();
//指定PKCS12的密码
keyStore.load(instream, password);
// 实例化密钥库 & 初始化密钥工厂
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(keyStore, password);
// 创建 SSLContext
SSLContext sslcontext = SSLContexts.custom()
.loadKeyMaterial(keyStore, password).build();
@@ -161,6 +168,7 @@ public class HttpRequestTemplate {
if (0 == configStorage.getMaxTotal() || 0 == configStorage.getDefaultMaxPerRoute()){
return null;
}
log.info(String.format("Initialize the PoolingHttpClientConnectionManager -- maxTotal:%s, defaultMaxPerRoute:%s", configStorage.getMaxTotal(), configStorage.getDefaultMaxPerRoute()));
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create()
.register("https", createSSL(configStorage))
.register("http", new PlainConnectionSocketFactory())
@@ -309,6 +317,7 @@ public class HttpRequestTemplate {
* @return 类型对象
*/
public <T>T doExecute(URI uri, Object request, Class<T> responseType, MethodType method){
log.debug(String.format("uri:%s, httpMethod:%s ", uri));
ClientHttpRequest<T> httpRequest = new ClientHttpRequest(uri ,method, request);
//判断是否有代理设置
if (null == httpProxy){