diff --git a/pay-java-common/src/main/java/com/egzosn/pay/common/api/AdvancedPayService.java b/pay-java-common/src/main/java/com/egzosn/pay/common/api/AdvancedPayService.java deleted file mode 100644 index dc1e5ee..0000000 --- a/pay-java-common/src/main/java/com/egzosn/pay/common/api/AdvancedPayService.java +++ /dev/null @@ -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 charges(String payeeId,PayOrder payOrder); -} diff --git a/pay-java-common/src/main/java/com/egzosn/pay/common/http/ClientHttpRequest.java b/pay-java-common/src/main/java/com/egzosn/pay/common/http/ClientHttpRequest.java index dfcdf1a..012eed5 100644 --- a/pay-java-common/src/main/java/com/egzosn/pay/common/http/ClientHttpRequest.java +++ b/pay-java-common/src/main/java/com/egzosn/pay/common/http/ClientHttpRequest.java @@ -154,7 +154,15 @@ public class ClientHttpRequest 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); diff --git a/pay-java-common/src/main/java/com/egzosn/pay/common/http/HttpConfigStorage.java b/pay-java-common/src/main/java/com/egzosn/pay/common/http/HttpConfigStorage.java index 629ebb9..f455b4a 100644 --- a/pay-java-common/src/main/java/com/egzosn/pay/common/http/HttpConfigStorage.java +++ b/pay-java-common/src/main/java/com/egzosn/pay/common/http/HttpConfigStorage.java @@ -10,14 +10,22 @@ package com.egzosn.pay.common.http; * */ 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; } /** diff --git a/pay-java-common/src/main/java/com/egzosn/pay/common/http/HttpRequestTemplate.java b/pay-java-common/src/main/java/com/egzosn/pay/common/http/HttpRequestTemplate.java index a566a80..8f18a77 100644 --- a/pay-java-common/src/main/java/com/egzosn/pay/common/http/HttpRequestTemplate.java +++ b/pay-java-common/src/main/java/com/egzosn/pay/common/http/HttpRequestTemplate.java @@ -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 postForObject(String uri, Object request, List
headeres, Class 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 doExecute(URI uri, Object request, Class responseType, MethodType method){ ClientHttpRequest 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 doExecute(URI uri, Object request, List
headers, Class responseType, MethodType method){ - ClientHttpRequest 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 请求执行 diff --git a/pay-java-common/src/main/java/com/egzosn/pay/common/http/HttpStringEntity.java b/pay-java-common/src/main/java/com/egzosn/pay/common/http/HttpStringEntity.java index bd6ee40..ef5fd67 100644 --- a/pay-java-common/src/main/java/com/egzosn/pay/common/http/HttpStringEntity.java +++ b/pay-java-common/src/main/java/com/egzosn/pay/common/http/HttpStringEntity.java @@ -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 - *
- * email egzosn@gmail.com
- * date 2017/12/20
- * 
+ *
+*               email egzosn@gmail.com
+*               date 2017/12/20
+*           
*/ public class HttpStringEntity extends StringEntity { + /** + * 请求头 + */ + private List
headers; - public HttpStringEntity(Map request, ContentType contentType) throws UnsupportedCharsetException { + /** + * 构造器 + * + * @param request 请求体 + * @param headers 请求头 + * + * @throws UnsupportedEncodingException 不支持默认的HTTP字符集 + */ + public HttpStringEntity(Map request, Header... headers) throws UnsupportedEncodingException { + this(getMapToParameters(request), headers); + + } + + /** + * 构造器 + * + * @param request 请求体 + * @param headers 请求头 + * + * @throws UnsupportedEncodingException 不支持默认的HTTP字符集 + */ + public HttpStringEntity(Map request, Map headers) throws UnsupportedEncodingException { + this(getMapToParameters(request), headers); + + } + + /** + * 构造器 + * + * @param request 请求体 + * @param contentType 内容类型 + */ + public HttpStringEntity(Map request, ContentType contentType) { super(getMapToParameters(request), contentType); } - public HttpStringEntity(Map request, String charset) throws UnsupportedCharsetException { + /** + * 构造器 + * + * @param request 请求体 + * @param charset 字符类型 + */ + public HttpStringEntity(Map request, String charset) { super(getMapToParameters(request), charset); } + /** + * 构造器 + * + * @param request 请求体 + * @param charset 字符类型 + */ public HttpStringEntity(Map request, Charset charset) { super(getMapToParameters(request), charset); } + /** + * 构造器 + * + * @param request 请求体 + * + * @throws UnsupportedEncodingException 不支持默认的HTTP字符集 + */ public HttpStringEntity(Map 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 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
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
headers) { + this.headers = headers; + } + + /** + * 设置请求头集 + * + * @param headers 请求头集 + */ + public void setHeaders(Map headers) { + for (String key : headers.keySet()) { + addHeader(new BasicHeader(key, headers.get(key))); + } }