Skip to content

Commit

Permalink
格式化
Browse files Browse the repository at this point in the history
  • Loading branch information
Masutangu committed Nov 25, 2024
1 parent 4e05da9 commit ed47b59
Show file tree
Hide file tree
Showing 4 changed files with 1,350 additions and 1,292 deletions.
Original file line number Diff line number Diff line change
@@ -1,41 +1,28 @@
package com.wechat.pay.java.core.http;

import java.net.Proxy;

import static java.util.Objects.requireNonNull;

import java.util.concurrent.TimeUnit;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.auth.Credential;
import com.wechat.pay.java.core.auth.Validator;
import com.wechat.pay.java.core.http.apache.ApacheHttpClientAdapter;
import com.wechat.pay.java.core.http.okhttp.OkHttpClientAdapter;
import com.wechat.pay.java.core.http.okhttp.OkHttpMultiDomainInterceptor;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

/**
* 默认HttpClient构造器
*/
public class ApacheHttpClientBuilder
implements AbstractHttpClientBuilder<ApacheHttpClientBuilder> {
/** 默认HttpClient构造器 */
public class ApacheHttpClientBuilder implements AbstractHttpClientBuilder<ApacheHttpClientBuilder> {

private Credential credential;
private Validator validator;


private CloseableHttpClient customizeApacheHttpClient;
private static final OkHttpMultiDomainInterceptor multiDomainInterceptor =
new OkHttpMultiDomainInterceptor();

static PoolingHttpClientConnectionManager apacheHttpClientConnectionManager =
new PoolingHttpClientConnectionManager();

new PoolingHttpClientConnectionManager();

private CloseableHttpClient initDefaultApacheHttpClient() {
return HttpClientBuilder.create()
Expand Down Expand Up @@ -111,11 +98,12 @@ public AbstractHttpClient build() {
requireNonNull(validator);

CloseableHttpClient httpclient =
customizeApacheHttpClient == null ? initDefaultApacheHttpClient()
customizeApacheHttpClient == null
? initDefaultApacheHttpClient()
: customizeApacheHttpClient;
// 每次都会创建一个 httpclient 实例,和 okhttp 不同
// You can customize a shared OkH(ttpClient instance with newBuilder(). This builds a client
// that shares the same connection po)ol, thread pools, and configuration.
// that shares the same connection po)ol, thread pools, and configuration.
return new ApacheHttpClientAdapter(credential, validator, httpclient);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@
import static java.util.Objects.requireNonNull;
import static org.apache.http.entity.ContentType.APPLICATION_JSON;

import com.wechat.pay.java.core.auth.Credential;
import com.wechat.pay.java.core.auth.Validator;
import com.wechat.pay.java.core.exception.HttpException;
import com.wechat.pay.java.core.exception.MalformedMessageException;
import com.wechat.pay.java.core.exception.ServiceException;
import com.wechat.pay.java.core.http.AbstractHttpClient;
import com.wechat.pay.java.core.http.FileRequestBody;
import com.wechat.pay.java.core.http.HttpRequest;
import com.wechat.pay.java.core.http.JsonRequestBody;
import com.wechat.pay.java.core.http.OriginalResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
Expand All @@ -25,17 +34,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wechat.pay.java.core.auth.Credential;
import com.wechat.pay.java.core.auth.Validator;
import com.wechat.pay.java.core.exception.HttpException;
import com.wechat.pay.java.core.exception.MalformedMessageException;
import com.wechat.pay.java.core.exception.ServiceException;
import com.wechat.pay.java.core.http.AbstractHttpClient;
import com.wechat.pay.java.core.http.FileRequestBody;
import com.wechat.pay.java.core.http.HttpRequest;
import com.wechat.pay.java.core.http.JsonRequestBody;
import com.wechat.pay.java.core.http.OriginalResponse;

public class ApacheHttpClientAdapter extends AbstractHttpClient {

private static final Logger logger = LoggerFactory.getLogger(ApacheHttpClientAdapter.class);
Expand All @@ -44,8 +42,8 @@ public class ApacheHttpClientAdapter extends AbstractHttpClient {

private final CloseableHttpClient apacheHttpClient;

public ApacheHttpClientAdapter(Credential credential, Validator validator,
CloseableHttpClient client) {
public ApacheHttpClientAdapter(
Credential credential, Validator validator, CloseableHttpClient client) {
super(credential, validator);
this.apacheHttpClient = requireNonNull(client);
}
Expand All @@ -58,8 +56,8 @@ protected String getHttpClientInfo() {
@Override
public OriginalResponse innerExecute(HttpRequest wechatPayRequest) {
try {
CloseableHttpResponse apacheHttpResponse = apacheHttpClient.execute(
buildApacheHttpRequest(wechatPayRequest));
CloseableHttpResponse apacheHttpResponse =
apacheHttpClient.execute(buildApacheHttpRequest(wechatPayRequest));
return assembleOriginalResponse(wechatPayRequest, apacheHttpResponse);
} catch (IOException e) {
throw new HttpException(wechatPayRequest, e);
Expand All @@ -72,27 +70,28 @@ private org.apache.http.client.methods.HttpUriRequest buildApacheHttpRequest(
org.apache.http.client.methods.HttpUriRequest apacheHttpRequest;

switch (wechatPayRequest.getHttpMethod().name()) {
case "GET":
apacheHttpRequest = new HttpGet(url);
break;
case "POST":
apacheHttpRequest = new HttpPost(url);
((HttpPost) apacheHttpRequest).setEntity(buildApacheHttpEntity(wechatPayRequest.getBody()));
break;
case "PUT":
apacheHttpRequest = new HttpPut(url);
((HttpPut) apacheHttpRequest).setEntity(buildApacheHttpEntity(wechatPayRequest.getBody()));
break;
case "PATCH":
apacheHttpRequest = new HttpPatch(url);
((HttpPatch) apacheHttpRequest).setEntity(buildApacheHttpEntity(wechatPayRequest.getBody()));
break;
case "DELETE":
apacheHttpRequest = new HttpDelete(url);
break;
default:
throw new IllegalArgumentException(
"Unsupported HTTP method: " + wechatPayRequest.getHttpMethod().name());
case "GET":
apacheHttpRequest = new HttpGet(url);
break;
case "POST":
apacheHttpRequest = new HttpPost(url);
((HttpPost) apacheHttpRequest).setEntity(buildApacheHttpEntity(wechatPayRequest.getBody()));
break;
case "PUT":
apacheHttpRequest = new HttpPut(url);
((HttpPut) apacheHttpRequest).setEntity(buildApacheHttpEntity(wechatPayRequest.getBody()));
break;
case "PATCH":
apacheHttpRequest = new HttpPatch(url);
((HttpPatch) apacheHttpRequest)
.setEntity(buildApacheHttpEntity(wechatPayRequest.getBody()));
break;
case "DELETE":
apacheHttpRequest = new HttpDelete(url);
break;
default:
throw new IllegalArgumentException(
"Unsupported HTTP method: " + wechatPayRequest.getHttpMethod().name());
}
Map<String, String> headers = wechatPayRequest.getHeaders().getHeaders();
headers.forEach(apacheHttpRequest::addHeader);
Expand All @@ -116,20 +115,23 @@ private HttpEntity buildApacheHttpEntity(
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.setMode(HttpMultipartMode.RFC6532);
entityBuilder.addTextBody(META_NAME, fileRequestBody.getMeta(), APPLICATION_JSON);
entityBuilder.addBinaryBody(FILE_NAME, fileRequestBody.getFile(),
ContentType.create(fileRequestBody.getContentType()), fileRequestBody.getFileName());
entityBuilder.addBinaryBody(
FILE_NAME,
fileRequestBody.getFile(),
ContentType.create(fileRequestBody.getContentType()),
fileRequestBody.getFileName());
return entityBuilder.build();
}
logger.error(
"When an http request is sent and the apache request body is constructed, the requestBody"
+ " parameter"
"When an http request is sent and the apache request body is constructed, the requestBody"
+ " parameter"
+ " type cannot be found,requestBody class name[{}]",
wechatPayRequestBody.getClass().getName());
return null;
}

private OriginalResponse assembleOriginalResponse(HttpRequest wechatPayRequest,
CloseableHttpResponse apacheHttpResponse) throws IOException {
private OriginalResponse assembleOriginalResponse(
HttpRequest wechatPayRequest, CloseableHttpResponse apacheHttpResponse) throws IOException {
Map<String, String> responseHeaders = assembleResponseHeader(apacheHttpResponse);
HttpEntity entity = apacheHttpResponse.getEntity();
try {
Expand Down Expand Up @@ -161,11 +163,11 @@ private Map<String, String> assembleResponseHeader(CloseableHttpResponse apacheH
@Override
protected InputStream innerDownload(HttpRequest httpRequest) {
try {
CloseableHttpResponse apacheHttpResponse = apacheHttpClient.execute(
buildApacheHttpRequest(httpRequest));
CloseableHttpResponse apacheHttpResponse =
apacheHttpClient.execute(buildApacheHttpRequest(httpRequest));
if (isInvalidHttpCode(apacheHttpResponse.getStatusLine().getStatusCode())) {
throw new ServiceException(httpRequest, apacheHttpResponse.getStatusLine().getStatusCode(),
"");
throw new ServiceException(
httpRequest, apacheHttpResponse.getStatusLine().getStatusCode(), "");
}
InputStream responseBodyStream = null;
if (apacheHttpResponse.getEntity() != null) {
Expand Down
Loading

0 comments on commit ed47b59

Please sign in to comment.