mirror of
https://gitee.com/egzosn/pay-java-parent.git
synced 2026-05-08 03:56:48 +08:00
sorry,文件遗漏
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
package in.egan.pay.common.http;
|
||||
|
||||
import in.egan.pay.common.bean.MethodType;
|
||||
import in.egan.pay.common.util.str.StringUtils;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* http请求工具
|
||||
* @author: egan
|
||||
* @email egzosn@gmail.com
|
||||
* @date 2017/3/3 21:33
|
||||
*/
|
||||
public class HttpRequestTemplate {
|
||||
|
||||
protected CloseableHttpClient httpClient;
|
||||
|
||||
protected HttpHost httpProxy;
|
||||
|
||||
public HttpHost getHttpProxy() {
|
||||
return httpProxy;
|
||||
}
|
||||
|
||||
public CloseableHttpClient getHttpClient() {
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
public HttpRequestTemplate(HttpConfigStorage configStorage) {
|
||||
setHttpConfigStorage(configStorage);
|
||||
}
|
||||
|
||||
public HttpRequestTemplate() {
|
||||
setHttpConfigStorage(null);
|
||||
}
|
||||
|
||||
public HttpRequestTemplate setHttpConfigStorage(HttpConfigStorage configStorage){
|
||||
|
||||
if (null == configStorage){
|
||||
httpClient = HttpClients.createDefault();
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
if (StringUtils.isNotBlank(configStorage.getHttpProxyHost())) {
|
||||
// 使用代理服务器
|
||||
if (StringUtils.isNotBlank(configStorage.getHttpProxyUsername())) {
|
||||
// 需要用户认证的代理服务器
|
||||
CredentialsProvider credsProvider = new BasicCredentialsProvider();
|
||||
credsProvider.setCredentials(
|
||||
new AuthScope(configStorage.getHttpProxyHost(), configStorage.getHttpProxyPort()),
|
||||
new UsernamePasswordCredentials(configStorage.getHttpProxyUsername(), configStorage.getHttpProxyPassword()));
|
||||
httpClient = HttpClients
|
||||
.custom()
|
||||
.setDefaultCredentialsProvider(credsProvider)
|
||||
.build();
|
||||
} else {
|
||||
// 无需用户认证的代理服务器
|
||||
httpClient = HttpClients.createDefault();
|
||||
}
|
||||
httpProxy = new HttpHost(configStorage.getHttpProxyHost(), configStorage.getHttpProxyPort());
|
||||
} else {
|
||||
httpClient = HttpClients.createDefault();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* post
|
||||
* @param uri 请求地址
|
||||
* @param request
|
||||
* @param responseType 为响应类(需要自己依据响应格式来确定)
|
||||
* @param <T>
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public <T> T postForObject(String uri, Object request, Class<T> responseType, Object... uriVariables){
|
||||
return doExecute(URI.create(UriVariables.getUri(uri, uriVariables)), request, responseType, MethodType.POST);
|
||||
}
|
||||
|
||||
public <T> T postForObject(String uri, Object request, Class<T> responseType, Map<String, Object> uriVariables) {
|
||||
return doExecute(URI.create(UriVariables.getUri(uri, uriVariables)), request, responseType, MethodType.POST);
|
||||
}
|
||||
|
||||
public <T> T postForObject(URI uri, Object request, Class<T> responseType){
|
||||
return doExecute(uri, request, responseType, MethodType.POST);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* get 请求
|
||||
* @param uri 请求地址
|
||||
* @param responseType 响应类型
|
||||
* @param uriVariables 用于匹配表达式
|
||||
* @param <T> 响应类型
|
||||
* @return
|
||||
*
|
||||
* <code>
|
||||
* getForObject("http://egan.in/pay/{id}/f/{type}", String.class, "1", "APP")
|
||||
* </code>
|
||||
*/
|
||||
public <T> T getForObject(String uri, Class<T> responseType, Object... uriVariables){
|
||||
|
||||
return doExecute(URI.create(UriVariables.getUri(uri, uriVariables)), null, responseType, MethodType.GET);
|
||||
}
|
||||
|
||||
/**
|
||||
* get 请求
|
||||
*
|
||||
* @param uri 请求地址
|
||||
* @param responseType 响应类型
|
||||
* @param uriVariables 用于匹配表达式
|
||||
* @param <T> 响应类型
|
||||
* @return
|
||||
* <code>
|
||||
* Map<String, String> uriVariables = new HashMap<String, String>();
|
||||
* uriVariables.put("id", "1");
|
||||
* uriVariables.put("type", "APP");
|
||||
* getForObject("http://egan.in/pay/{id}/f/{type}", String.class, uriVariables)
|
||||
* </code>
|
||||
*/
|
||||
public <T> T getForObject(String uri, Class<T> responseType, Map<String, ?> uriVariables){
|
||||
return doExecute(URI.create(UriVariables.getUri(uri, uriVariables)), null, responseType, MethodType.GET);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* http 请求执行
|
||||
* @param uri 地址
|
||||
* @param request 请求数据
|
||||
* @param responseType 响应类型
|
||||
* @param method 请求方法
|
||||
* @param <T>
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public <T>T doExecute(URI uri, Object request, Class<T> responseType, MethodType method){
|
||||
ClientHttpRequest<T> httpRequest = new ClientHttpRequest(uri ,method, request);
|
||||
httpRequest.setProxy(httpProxy).setResponseType(responseType);
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpRequest)) {
|
||||
return httpRequest.handleResponse(response);
|
||||
}catch ( IOException e){
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
httpRequest.releaseConnection();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* http 请求执行
|
||||
* @param uri 地址
|
||||
* @param request 请求数据
|
||||
* @param responseType 响应类型
|
||||
* @param method 请求方法
|
||||
* @param <T>
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public <T>T doExecute(String uri, Object request, Class<T> responseType, MethodType method){
|
||||
return doExecute(URI.create(uri), request, responseType, method);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package in.egan.pay.common.http;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* URL表达式处理器
|
||||
*
|
||||
* @author: egan
|
||||
* @email egzosn@gmail.com
|
||||
* @date 2017/3/5 10:07
|
||||
*/
|
||||
public class UriVariables {
|
||||
|
||||
/**
|
||||
* 依次匹配
|
||||
* @param uri
|
||||
* @param uriVariables
|
||||
* @return
|
||||
* <code>
|
||||
* System.out.println(getUri("http://egan.in/{a}/ba/{a1}?{bb}={a1}", "no1", "no2", "no3", "no4"));
|
||||
* 结果 http://egan.in/no1/ba/no2?no3=no4
|
||||
* </code>
|
||||
*
|
||||
*/
|
||||
public static String getUri(String uri, Object... uriVariables) {
|
||||
|
||||
if (null == uriVariables){
|
||||
return uri;
|
||||
}
|
||||
for (Object variable : uriVariables){
|
||||
if (null == variable){
|
||||
continue;
|
||||
}
|
||||
uri = uri.replaceFirst("\\{\\w+\\}", variable.toString());
|
||||
}
|
||||
return uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* 匹配Map.key
|
||||
* @param uri
|
||||
* @param uriVariables
|
||||
* @return
|
||||
* <code>
|
||||
* Map<String, Object> uriVariable = new HashMap<>();
|
||||
* uriVariable.put("a", "no1");
|
||||
* uriVariable.put("a1", "no2");
|
||||
* uriVariable.put("bb", "no3");
|
||||
* System.out.println(getUri("http://egan.in/{a}/ba/{a1}?{bb}={a1}", uriVariable));
|
||||
* 结果 http://egan.in/no1/ba/no2?no3=no2
|
||||
* </code>
|
||||
*/
|
||||
public static String getUri(String uri, Map<String, Object> uriVariables) {
|
||||
|
||||
if (null == uriVariables){
|
||||
return uri;
|
||||
}
|
||||
for (String key : uriVariables.keySet()){
|
||||
Object uriVariable = uriVariables.get(key);
|
||||
if (null == uriVariable){
|
||||
continue;
|
||||
}
|
||||
|
||||
uri = uri.replace("{" + key + "}", uriVariable.toString());
|
||||
}
|
||||
return uri;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -52,6 +52,18 @@ public enum SignUtils {
|
||||
public boolean verify(String text, String sign, String publicKey, String characterEncoding) {
|
||||
return in.egan.pay.common.util.sign.encrypt.RSA.verify(text, sign, publicKey, characterEncoding);
|
||||
}
|
||||
},
|
||||
|
||||
RSA2 {
|
||||
@Override
|
||||
public String createSign(String content, String key, String characterEncoding) {
|
||||
return in.egan.pay.common.util.sign.encrypt.RSA2.sign(content, key, characterEncoding);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean verify(String text, String sign, String publicKey, String characterEncoding) {
|
||||
return in.egan.pay.common.util.sign.encrypt.RSA2.verify(text, sign, publicKey, characterEncoding);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user