响应类型
- *
* @return 类型对象
- *
+ *
*
* getForObject("http://egan.in/pay/{id}/f/{type}", String.class, "1", "APP")
*
*/
- public T getForObject(String uri, Class responseType, Object... uriVariables){
+ public T getForObject(String uri, Class responseType, Object... uriVariables) {
return doExecute(URI.create(UriVariables.getUri(uri, uriVariables)), null, responseType, MethodType.GET);
}
@@ -277,101 +288,179 @@ public class HttpRequestTemplate {
* @return 类型对象
*
* 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)
*
*/
- public T getForObject(String uri, Class responseType, Map uriVariables){
+ public T getForObject(String uri, Class responseType, Map uriVariables) {
return doExecute(URI.create(UriVariables.getUri(uri, uriVariables)), null, responseType, MethodType.GET);
}
/**
* get 请求
- * @param uri 请求地址
- * @param header 请求头
+ *
+ * @param uri 请求地址
+ * @param header 请求头
* @param responseType 响应类型
* @param uriVariables 用于匹配表达式
- * @param 响应类型
- * @return 类型对象
+ * @param 响应类型
+ * @return 类型对象
*
*
- * getForObject("http://egan.in/pay/{id}/f/{type}", String.class, "1", "APP")
+ * getForObject("http://egan.in/pay/{id}/f/{type}", String.class, "1", "APP")
*
*/
- public T getForObject(String uri, HttpHeader header, Class responseType, Object... uriVariables){
+ public T getForObject(String uri, HttpHeader header, Class responseType, Object... uriVariables) {
- return doExecute(URI.create(UriVariables.getUri(uri, uriVariables)), header, responseType, MethodType.GET);
+ return getForObjectEntity(uri, header, responseType, uriVariables).getBody();
}
/**
* get 请求
*
* @param uri 请求地址
- * @param header 请求头
+ * @param header 请求头
* @param responseType 响应类型
* @param uriVariables 用于匹配表达式
- * @param 响应类型
+ * @param 响应类型
+ * @return 类型对象
+ *
+ *
+ * getForObject("http://egan.in/pay/{id}/f/{type}", String.class, "1", "APP")
+ *
+ */
+ public ResponseEntity getForObjectEntity(String uri, HttpHeader header, Class responseType, Object... uriVariables) {
+
+ return doExecuteEntity(URI.create(UriVariables.getUri(uri, uriVariables)), header, responseType, MethodType.GET);
+ }
+
+ /**
+ * get 请求
+ *
+ * @param uri 请求地址
+ * @param header 请求头
+ * @param responseType 响应类型
+ * @param uriVariables 用于匹配表达式
+ * @param 响应类型
* @return 类型对象
*
* 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)
*
*/
- public T getForObject(String uri, HttpHeader header, Class responseType, Map uriVariables){
- return doExecute(URI.create(UriVariables.getUri(uri, uriVariables)), header, responseType, MethodType.GET);
+ public T getForObject(String uri, HttpHeader header, Class responseType, Map uriVariables) {
+ return getForObjectEntity(uri, header, responseType, uriVariables).getBody();
+ }
+ /**
+ * get 请求
+ *
+ * @param uri 请求地址
+ * @param header 请求头
+ * @param responseType 响应类型
+ * @param uriVariables 用于匹配表达式
+ * @param 响应类型
+ * @return 类型对象
+ *
+ * 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)
+ *
+ */
+ public ResponseEntity getForObjectEntity(String uri, HttpHeader header, Class responseType, Map uriVariables) {
+ return doExecuteEntity(URI.create(UriVariables.getUri(uri, uriVariables)), header, responseType, MethodType.GET);
}
/**
* http 请求执行
- * @param uri 地址
- * @param request 请求数据
+ *
+ * @param uri 地址
+ * @param request 请求数据
* @param responseType 响应类型
- * @param method 请求方法
- * @param 响应类型
+ * @param method 请求方法
+ * @param 响应类型
* @return 类型对象
*/
- public T doExecute(URI uri, Object request, Class responseType, MethodType method){
+ public T doExecute(URI uri, Object request, Class responseType, MethodType method) {
+ return doExecuteEntity(uri, request, responseType, method).getBody();
+
+ }
+
+ /**
+ * http 请求执行
+ *
+ * @param uri 地址
+ * @param request 请求数据
+ * @param responseType 响应类型
+ * @param method 请求方法
+ * @param 响应类型
+ * @return 类型对象
+ */
+ public ResponseEntity doExecuteEntity(URI uri, Object request, Class responseType, MethodType method) {
+
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("uri:%s, httpMethod:%s ", uri, method.name()));
}
- ClientHttpRequest httpRequest = new ClientHttpRequest(uri ,method, request, null == configStorage ? null : configStorage.getCharset());
+ ClientHttpRequest httpRequest = new ClientHttpRequest(uri, method, request, null == configStorage ? null : configStorage.getCharset());
//判断是否有代理设置
- if (null != httpProxy){
+ if (null != httpProxy) {
httpRequest.setProxy(httpProxy);
}
httpRequest.setResponseType(responseType);
try (CloseableHttpResponse response = getHttpClient().execute(httpRequest)) {
- return httpRequest.handleResponse(response);
- }catch (IOException e){
+ int statusCode = response.getStatusLine().getStatusCode();
+ Header[] allHeaders = response.getAllHeaders();
+ T body = httpRequest.handleResponse(response);
+ return new ResponseEntity<>(statusCode, allHeaders, body);
+ }
+ catch (IOException e) {
throw new PayErrorException(new PayException("IOException", e.getLocalizedMessage()));
- }finally {
+ }
+ finally {
httpRequest.releaseConnection();
}
-
}
/**
* http 请求执行
- * @param uri 地址
- * @param request 请求数据
+ *
+ * @param uri 地址
+ * @param request 请求数据
* @param responseType 响应类型
- * @param method 请求方法
- * @param 响应类型
+ * @param method 请求方法
+ * @param 响应类型
* @return 类型对象
*/
- public T doExecute(String uri, Object request, Class responseType, MethodType method){
+ public T doExecute(String uri, Object request, Class responseType, MethodType method) {
return doExecute(URI.create(uri), request, responseType, method);
}
+
+ /**
+ * http 请求执行
+ *
+ * @param uri 地址
+ * @param request 请求数据
+ * @param responseType 响应类型
+ * @param method 请求方法
+ * @param 响应类型
+ * @return 类型对象
+ */
+ public ResponseEntity doExecuteEntity(String uri, Object request, Class responseType, MethodType method) {
+ return doExecuteEntity(URI.create(uri), request, responseType, method);
+ }
}
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 828c9be..53e5062 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,25 +1,30 @@
package com.egzosn.pay.common.http;
+import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.UnsupportedCharsetException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
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.*;
-
import static com.egzosn.pay.common.http.UriVariables.getMapToParameters;
+import com.egzosn.pay.common.util.str.StringUtils;
+
/**
* 请求实体,包含请求头,内容类型,编码类型等
*
* @author egan
- *
-* email egzosn@gmail.com
-* date 2017/12/20
-*
+ *
+ * email egzosn@gmail.com
+ * date 2017/12/20
+ *
*/
public class HttpStringEntity extends StringEntity {
/**
@@ -42,12 +47,13 @@ public class HttpStringEntity extends StringEntity {
public void requestIsEmpty(Map request) {
- if (null == request || request.isEmpty()){
+ if (null == request || request.isEmpty()) {
this.isEmpty = true;
}
}
+
public void requestIsEmpty(String request) {
- if (null == request || request.isEmpty()){
+ if (StringUtils.isEmpty(request)) {
this.isEmpty = true;
}
@@ -59,7 +65,6 @@ public class HttpStringEntity extends StringEntity {
*
* @param request 请求体
* @param headers 请求头
- *
* @throws UnsupportedEncodingException 不支持默认的HTTP字符集
*/
public HttpStringEntity(Map request, Header... headers) throws UnsupportedEncodingException {
@@ -73,7 +78,6 @@ public class HttpStringEntity extends StringEntity {
*
* @param request 请求体
* @param headers 请求头
- *
* @throws UnsupportedEncodingException 不支持默认的HTTP字符集
*/
public HttpStringEntity(Map request, Map headers) throws UnsupportedEncodingException {
@@ -119,7 +123,6 @@ public class HttpStringEntity extends StringEntity {
* 构造器
*
* @param request 请求体
- *
* @throws UnsupportedEncodingException 不支持默认的HTTP字符集
*/
public HttpStringEntity(Map request) throws UnsupportedEncodingException {
@@ -132,7 +135,6 @@ public class HttpStringEntity extends StringEntity {
*
* @param request 请求体
* @param contentType 内容类型
- *
* @throws UnsupportedCharsetException 不支持默认的HTTP字符集
*/
public HttpStringEntity(String request, ContentType contentType) throws UnsupportedCharsetException {
@@ -145,7 +147,6 @@ public class HttpStringEntity extends StringEntity {
*
* @param request 请求体
* @param charset 字符类型
- *
* @throws UnsupportedCharsetException 不支持默认的HTTP字符集
*/
public HttpStringEntity(String request, String charset) throws UnsupportedCharsetException {
@@ -169,7 +170,6 @@ public class HttpStringEntity extends StringEntity {
*
* @param request 请求体
* @param headers 请求头
- *
* @throws UnsupportedEncodingException 不支持默认的HTTP字符集
*/
public HttpStringEntity(String request, Header... headers) throws UnsupportedEncodingException {
@@ -185,7 +185,6 @@ public class HttpStringEntity extends StringEntity {
*
* @param request 请求体
* @param headers 请求头
- *
* @throws UnsupportedEncodingException 不支持默认的HTTP字符集
*/
public HttpStringEntity(String request, Map headers) throws UnsupportedEncodingException {
@@ -237,6 +236,7 @@ public class HttpStringEntity extends StringEntity {
addHeader(new BasicHeader(entry.getKey(), entry.getValue()));
}
}
+
/**
* 设置请求头
*
diff --git a/pay-java-common/src/main/java/com/egzosn/pay/common/http/ResponseEntity.java b/pay-java-common/src/main/java/com/egzosn/pay/common/http/ResponseEntity.java
new file mode 100644
index 0000000..995bc67
--- /dev/null
+++ b/pay-java-common/src/main/java/com/egzosn/pay/common/http/ResponseEntity.java
@@ -0,0 +1,33 @@
+package com.egzosn.pay.common.http;
+
+import org.apache.http.Header;
+
+/**
+ * 响应实体
+ * @author Egan
+ * email egzosn@gmail.com
+ * date 2021/8/1
+ */
+public class ResponseEntity {
+ private final int statusCode;
+ private final Header[] headers;
+ private final T body;
+
+ public ResponseEntity(int statusCode, Header[] headers, T body) {
+ this.statusCode = statusCode;
+ this.headers = headers;
+ this.body = body;
+ }
+
+ public int getStatusCode() {
+ return statusCode;
+ }
+
+ public Header[] getHeaders() {
+ return headers;
+ }
+
+ public T getBody() {
+ return body;
+ }
+}
diff --git a/pay-java-common/src/main/java/com/egzosn/pay/common/http/UriVariables.java b/pay-java-common/src/main/java/com/egzosn/pay/common/http/UriVariables.java
index 8f59338..cff3608 100644
--- a/pay-java-common/src/main/java/com/egzosn/pay/common/http/UriVariables.java
+++ b/pay-java-common/src/main/java/com/egzosn/pay/common/http/UriVariables.java
@@ -1,6 +1,8 @@
package com.egzosn.pay.common.http;
import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
@@ -11,6 +13,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import com.egzosn.pay.common.bean.result.PayException;
import com.egzosn.pay.common.exception.PayErrorException;
+import com.egzosn.pay.common.util.str.StringUtils;
/**
* URL表达式处理器
@@ -23,6 +26,7 @@ import com.egzosn.pay.common.exception.PayErrorException;
*/
public final class UriVariables {
private static final Logger LOG = LoggerFactory.getLogger(UriVariables.class);
+ public static final String QUESTION = "?";
private UriVariables() {
}
@@ -218,5 +222,28 @@ public final class UriVariables {
return str;
}
+ /**
+ * 去除域名的标准url
+ *
+ * @param url url
+ * @return 去除域名的标准url
+ */
+ public static String getCanonicalUrl(String url) {
+ if (StringUtils.isEmpty(url)) {
+ return url;
+ }
+ try {
+ URI uri = new URI(url);
+ String path = uri.getPath();
+ String encodedQuery = uri.getQuery();
+ if (StringUtils.isNotEmpty(encodedQuery)) {
+ path += QUESTION.concat(encodedQuery);
+ }
+ return path;
+ }
+ catch (URISyntaxException e) {
+ throw new PayErrorException(new PayException("failure", "去除域名的标准url失败"), e);
+ }
+ }
}