mirror of
https://gitee.com/egzosn/pay-java-parent.git
synced 2026-05-11 07:57:12 +08:00
http请求增加请求头的实现方式
拆分网络代理与请求授权(auth)。
This commit is contained in:
@@ -1,32 +0,0 @@
|
||||
package com.egzosn.pay.common.api;
|
||||
|
||||
import com.egzosn.pay.common.bean.AuthPageType;
|
||||
import com.egzosn.pay.common.bean.PayOrder;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 高级支付接口
|
||||
* @author Actinia
|
||||
* @email hayesfu@qq.com
|
||||
* @date 2018/1/18
|
||||
*
|
||||
*/
|
||||
|
||||
public interface AdvancedPayService extends PayService{
|
||||
/**
|
||||
* 获取授权页面
|
||||
* @param payeeId 收款id
|
||||
* @param authPageType 授权类型
|
||||
* @return 返回请求结果
|
||||
*/
|
||||
String getAuthorizationPage(String payeeId,AuthPageType authPageType);
|
||||
|
||||
/**
|
||||
* 发起授权
|
||||
* @param payeeId 收款id
|
||||
* @param payOrder 订单信息
|
||||
* @return 返回请求结果
|
||||
*/
|
||||
Map<String ,Object> charges(String payeeId,PayOrder payOrder);
|
||||
}
|
||||
@@ -154,7 +154,15 @@ public class ClientHttpRequest<T> extends HttpEntityEnclosingRequestBase impleme
|
||||
if (null == request){
|
||||
return this;
|
||||
}
|
||||
if (request instanceof HttpEntity){
|
||||
if (request instanceof HttpStringEntity){
|
||||
HttpStringEntity entity = (HttpStringEntity)request;
|
||||
setEntity(entity);
|
||||
if (null != entity.getHeaders() ){
|
||||
for (Header header : entity.getHeaders()){
|
||||
addHeader(header);
|
||||
}
|
||||
}
|
||||
} 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);
|
||||
|
||||
@@ -10,14 +10,22 @@ package com.egzosn.pay.common.http;
|
||||
* </pre>
|
||||
*/
|
||||
public class HttpConfigStorage {
|
||||
//http代理地址
|
||||
/**
|
||||
* http代理地址
|
||||
*/
|
||||
protected String httpProxyHost;
|
||||
//代理端口
|
||||
/**
|
||||
* 代理端口
|
||||
*/
|
||||
protected int httpProxyPort;
|
||||
//代理用户名
|
||||
protected String httpProxyUsername;
|
||||
//代理密码
|
||||
protected String httpProxyPassword;
|
||||
/**
|
||||
* 请求授权用户名
|
||||
*/
|
||||
protected String authUsername;
|
||||
/**
|
||||
* 请求授权密码
|
||||
*/
|
||||
protected String authPassword;
|
||||
|
||||
/**
|
||||
* @see #keystore 是否为https请求所需的证书(PKCS12)的地址,默认为地址,否则为证书信息串
|
||||
@@ -54,28 +62,68 @@ public class HttpConfigStorage {
|
||||
public void setHttpProxyPort(int httpProxyPort) {
|
||||
this.httpProxyPort = httpProxyPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求授权用户名
|
||||
* @return 用户名
|
||||
*/
|
||||
public String getAuthUsername() {
|
||||
return authUsername;
|
||||
}
|
||||
|
||||
public void setAuthUsername(String authUsername) {
|
||||
this.authUsername = authUsername;
|
||||
}
|
||||
/**
|
||||
* 请求授权密码
|
||||
* @return 密码
|
||||
*/
|
||||
public String getAuthPassword() {
|
||||
return authPassword;
|
||||
}
|
||||
|
||||
public void setAuthPassword(String authPassword) {
|
||||
this.authPassword = authPassword;
|
||||
}
|
||||
|
||||
/**
|
||||
* 代理用户名
|
||||
* @return 代理用户名
|
||||
* @see #getAuthUsername()
|
||||
*/
|
||||
@Deprecated
|
||||
public String getHttpProxyUsername() {
|
||||
return httpProxyUsername;
|
||||
return authUsername;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置代理用户名
|
||||
* @param httpProxyUsername 代理用户名
|
||||
* @see #setAuthUsername(String)
|
||||
*/
|
||||
@Deprecated
|
||||
public void setHttpProxyUsername(String httpProxyUsername) {
|
||||
this.httpProxyUsername = httpProxyUsername;
|
||||
this.authUsername = httpProxyUsername;
|
||||
}
|
||||
|
||||
/**
|
||||
* 代理密码
|
||||
* @return 代理密码
|
||||
* @see #getAuthPassword()
|
||||
*/
|
||||
@Deprecated
|
||||
public String getHttpProxyPassword() {
|
||||
return httpProxyPassword;
|
||||
return authPassword;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置代理密码
|
||||
* @param httpProxyPassword 代理密码
|
||||
* @see #setAuthPassword(String)
|
||||
*/
|
||||
@Deprecated
|
||||
public void setHttpProxyPassword(String httpProxyPassword) {
|
||||
this.httpProxyPassword = httpProxyPassword;
|
||||
this.authPassword = httpProxyPassword;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -69,7 +69,7 @@ public class HttpRequestTemplate {
|
||||
*/
|
||||
public SSLConnectionSocketFactory createSSL( HttpConfigStorage configStorage){
|
||||
|
||||
if (StringUtils.isEmpty(configStorage.getKeystorePath()) || StringUtils.isEmpty(configStorage.getKeystorePath())){
|
||||
if (StringUtils.isEmpty(configStorage.getKeystore())){
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -100,21 +100,19 @@ public class HttpRequestTemplate {
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建代理服务器
|
||||
* 创建凭据提供程序
|
||||
* @param configStorage 请求配置
|
||||
* @return 代理服务器配置
|
||||
* @return 凭据提供程序
|
||||
*/
|
||||
public CredentialsProvider createProxy(HttpConfigStorage configStorage){
|
||||
public CredentialsProvider createCredentialsProvider(HttpConfigStorage configStorage){
|
||||
|
||||
if (StringUtils.isNotBlank(configStorage.getHttpProxyHost())) {
|
||||
|
||||
// URI uri = URI.create(configStorage.getHttpProxyHost());
|
||||
//http代理地址设置
|
||||
httpProxy = new HttpHost(configStorage.getHttpProxyHost(),configStorage.httpProxyPort);;
|
||||
}
|
||||
|
||||
|
||||
if (StringUtils.isBlank(configStorage.getHttpProxyUsername())) {
|
||||
if (StringUtils.isBlank(configStorage.getAuthUsername())) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -122,7 +120,7 @@ public class HttpRequestTemplate {
|
||||
CredentialsProvider credsProvider = new BasicCredentialsProvider();
|
||||
credsProvider.setCredentials(
|
||||
AuthScope.ANY,
|
||||
new UsernamePasswordCredentials(configStorage.getHttpProxyUsername(), configStorage.getHttpProxyPassword()));
|
||||
new UsernamePasswordCredentials(configStorage.getAuthUsername(), configStorage.getAuthPassword()));
|
||||
|
||||
|
||||
return credsProvider;
|
||||
@@ -144,8 +142,8 @@ public class HttpRequestTemplate {
|
||||
|
||||
httpClient = HttpClients
|
||||
.custom()
|
||||
//设置代理或网络提供者
|
||||
.setDefaultCredentialsProvider(createProxy(configStorage))
|
||||
//网络提供者
|
||||
.setDefaultCredentialsProvider(createCredentialsProvider(configStorage))
|
||||
//设置httpclient的SSLSocketFactory
|
||||
.setSSLSocketFactory(createSSL(configStorage))
|
||||
.build();
|
||||
@@ -176,9 +174,7 @@ public class HttpRequestTemplate {
|
||||
return doExecute(uri, request, responseType, MethodType.POST);
|
||||
}
|
||||
|
||||
public <T> T postForObject(String uri, Object request, List<Header> headeres, Class<T> responseType, Object... uriVariables){
|
||||
return doExecute(URI.create(UriVariables.getUri(uri, uriVariables)), request,headeres, responseType, MethodType.POST);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -233,6 +229,10 @@ public class HttpRequestTemplate {
|
||||
*/
|
||||
public <T>T doExecute(URI uri, Object request, Class<T> responseType, MethodType method){
|
||||
ClientHttpRequest<T> httpRequest = new ClientHttpRequest(uri ,method, request);
|
||||
//判断是否有代理设置
|
||||
if (null == httpProxy){
|
||||
httpRequest.setProxy(httpProxy);
|
||||
}
|
||||
httpRequest.setResponseType(responseType);
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpRequest)) {
|
||||
return httpRequest.handleResponse(response);
|
||||
@@ -244,23 +244,6 @@ public class HttpRequestTemplate {
|
||||
return null;
|
||||
}
|
||||
|
||||
public <T>T doExecute(URI uri, Object request, List<Header> headers, Class<T> responseType, MethodType method){
|
||||
ClientHttpRequest<T> httpRequest = new ClientHttpRequest(uri ,method, request);
|
||||
httpRequest.setResponseType(responseType);
|
||||
if(headers != null){
|
||||
for(Header header : headers){
|
||||
httpRequest.addHeader(header);
|
||||
}
|
||||
}
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpRequest)) {
|
||||
return httpRequest.handleResponse(response);
|
||||
}catch (IOException e){
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
httpRequest.releaseConnection();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* http 请求执行
|
||||
|
||||
@@ -1,56 +1,203 @@
|
||||
package com.egzosn.pay.common.http;
|
||||
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.UnsupportedCharsetException;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
import static com.egzosn.pay.common.http.UriVariables.getMapToParameters;
|
||||
|
||||
/**
|
||||
* 请求实体
|
||||
* 请求实体,包含请求头,内容类型,编码类型等
|
||||
*
|
||||
* @author egan
|
||||
* <pre>
|
||||
* email egzosn@gmail.com
|
||||
* date 2017/12/20
|
||||
* </pre>
|
||||
* <pre>
|
||||
* email egzosn@gmail.com
|
||||
* date 2017/12/20
|
||||
* </pre>
|
||||
*/
|
||||
public class HttpStringEntity extends StringEntity {
|
||||
/**
|
||||
* 请求头
|
||||
*/
|
||||
private List<Header> headers;
|
||||
|
||||
public HttpStringEntity(Map<String, Object> request, ContentType contentType) throws UnsupportedCharsetException {
|
||||
/**
|
||||
* 构造器
|
||||
*
|
||||
* @param request 请求体
|
||||
* @param headers 请求头
|
||||
*
|
||||
* @throws UnsupportedEncodingException 不支持默认的HTTP字符集
|
||||
*/
|
||||
public HttpStringEntity(Map<String, Object> request, Header... headers) throws UnsupportedEncodingException {
|
||||
this(getMapToParameters(request), headers);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造器
|
||||
*
|
||||
* @param request 请求体
|
||||
* @param headers 请求头
|
||||
*
|
||||
* @throws UnsupportedEncodingException 不支持默认的HTTP字符集
|
||||
*/
|
||||
public HttpStringEntity(Map<String, Object> request, Map<String, String> headers) throws UnsupportedEncodingException {
|
||||
this(getMapToParameters(request), headers);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造器
|
||||
*
|
||||
* @param request 请求体
|
||||
* @param contentType 内容类型
|
||||
*/
|
||||
public HttpStringEntity(Map<String, Object> request, ContentType contentType) {
|
||||
super(getMapToParameters(request), contentType);
|
||||
}
|
||||
|
||||
public HttpStringEntity(Map<String, Object> request, String charset) throws UnsupportedCharsetException {
|
||||
/**
|
||||
* 构造器
|
||||
*
|
||||
* @param request 请求体
|
||||
* @param charset 字符类型
|
||||
*/
|
||||
public HttpStringEntity(Map<String, Object> request, String charset) {
|
||||
super(getMapToParameters(request), charset);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造器
|
||||
*
|
||||
* @param request 请求体
|
||||
* @param charset 字符类型
|
||||
*/
|
||||
public HttpStringEntity(Map<String, Object> request, Charset charset) {
|
||||
super(getMapToParameters(request), charset);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造器
|
||||
*
|
||||
* @param request 请求体
|
||||
*
|
||||
* @throws UnsupportedEncodingException 不支持默认的HTTP字符集
|
||||
*/
|
||||
public HttpStringEntity(Map<String, Object> request) throws UnsupportedEncodingException {
|
||||
super(getMapToParameters(request));
|
||||
}
|
||||
|
||||
public HttpStringEntity(String string, ContentType contentType) throws UnsupportedCharsetException {
|
||||
super(string, contentType);
|
||||
/**
|
||||
* 构造器
|
||||
*
|
||||
* @param request 请求体
|
||||
* @param contentType 内容类型
|
||||
*
|
||||
* @throws UnsupportedEncodingException 不支持默认的HTTP字符集
|
||||
*/
|
||||
public HttpStringEntity(String request, ContentType contentType) throws UnsupportedCharsetException {
|
||||
super(request, contentType);
|
||||
}
|
||||
|
||||
public HttpStringEntity(String string, String charset) throws UnsupportedCharsetException {
|
||||
super(string, charset);
|
||||
/**
|
||||
* 构造器
|
||||
*
|
||||
* @param request 请求体
|
||||
* @param charset 字符类型
|
||||
*
|
||||
* @throws UnsupportedEncodingException 不支持默认的HTTP字符集
|
||||
*/
|
||||
public HttpStringEntity(String request, String charset) throws UnsupportedCharsetException {
|
||||
super(request, charset);
|
||||
}
|
||||
|
||||
public HttpStringEntity(String string, Charset charset) {
|
||||
super(string, charset);
|
||||
/**
|
||||
* 构造器
|
||||
*
|
||||
* @param request 请求体
|
||||
* @param charset 字符类型
|
||||
*/
|
||||
public HttpStringEntity(String request, Charset charset) {
|
||||
super(request, charset);
|
||||
}
|
||||
|
||||
public HttpStringEntity(String string) throws UnsupportedEncodingException {
|
||||
super(string);
|
||||
/**
|
||||
* 构造器
|
||||
*
|
||||
* @param request 请求体
|
||||
* @param headers 请求头
|
||||
*
|
||||
* @throws UnsupportedEncodingException 不支持默认的HTTP字符集
|
||||
*/
|
||||
public HttpStringEntity(String request, Header... headers) throws UnsupportedEncodingException {
|
||||
super(request);
|
||||
if (null == headers) {
|
||||
this.headers = Arrays.asList(headers);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造器
|
||||
*
|
||||
* @param request 请求体
|
||||
* @param headers 请求头
|
||||
*
|
||||
* @throws UnsupportedEncodingException 不支持默认的HTTP字符集
|
||||
*/
|
||||
public HttpStringEntity(String request, Map<String, String> headers) throws UnsupportedEncodingException {
|
||||
super(request);
|
||||
this.headers = new ArrayList<>();
|
||||
for (String key : headers.keySet()) {
|
||||
this.headers.add(new BasicHeader(key, headers.get(key)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求头集
|
||||
*
|
||||
* @return 请求头集
|
||||
*/
|
||||
public List<Header> getHeaders() {
|
||||
return headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加请求头
|
||||
*
|
||||
* @param header 请求头
|
||||
*/
|
||||
public void addHeader(Header header) {
|
||||
if (null == this.headers) {
|
||||
this.headers = new ArrayList<>();
|
||||
}
|
||||
this.headers.add(header);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置请求头集
|
||||
*
|
||||
* @param headers 请求头集
|
||||
*/
|
||||
public void setHeaders(List<Header> headers) {
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置请求头集
|
||||
*
|
||||
* @param headers 请求头集
|
||||
*/
|
||||
public void setHeaders(Map<String, String> headers) {
|
||||
for (String key : headers.keySet()) {
|
||||
addHeader(new BasicHeader(key, headers.get(key)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user