|
| 1 | +package com.wechat.pay.java.core.http.apache; |
| 2 | + |
| 3 | +import static java.util.Objects.requireNonNull; |
| 4 | +import static org.apache.http.entity.ContentType.APPLICATION_JSON; |
| 5 | + |
| 6 | +import com.wechat.pay.java.core.auth.Credential; |
| 7 | +import com.wechat.pay.java.core.auth.Validator; |
| 8 | +import com.wechat.pay.java.core.exception.HttpException; |
| 9 | +import com.wechat.pay.java.core.exception.MalformedMessageException; |
| 10 | +import com.wechat.pay.java.core.exception.ServiceException; |
| 11 | +import com.wechat.pay.java.core.http.AbstractHttpClient; |
| 12 | +import com.wechat.pay.java.core.http.FileRequestBody; |
| 13 | +import com.wechat.pay.java.core.http.HttpRequest; |
| 14 | +import com.wechat.pay.java.core.http.JsonRequestBody; |
| 15 | +import com.wechat.pay.java.core.http.OriginalResponse; |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.InputStream; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.concurrent.ConcurrentHashMap; |
| 20 | +import org.apache.http.Header; |
| 21 | +import org.apache.http.HttpEntity; |
| 22 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 23 | +import org.apache.http.client.methods.HttpDelete; |
| 24 | +import org.apache.http.client.methods.HttpGet; |
| 25 | +import org.apache.http.client.methods.HttpPatch; |
| 26 | +import org.apache.http.client.methods.HttpPost; |
| 27 | +import org.apache.http.client.methods.HttpPut; |
| 28 | +import org.apache.http.entity.ContentType; |
| 29 | +import org.apache.http.entity.StringEntity; |
| 30 | +import org.apache.http.entity.mime.HttpMultipartMode; |
| 31 | +import org.apache.http.entity.mime.MultipartEntityBuilder; |
| 32 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 33 | +import org.apache.http.util.EntityUtils; |
| 34 | +import org.slf4j.Logger; |
| 35 | +import org.slf4j.LoggerFactory; |
| 36 | + |
| 37 | +public class ApacheHttpClientAdapter extends AbstractHttpClient { |
| 38 | + |
| 39 | + private static final Logger logger = LoggerFactory.getLogger(ApacheHttpClientAdapter.class); |
| 40 | + private static final String META_NAME = "meta"; |
| 41 | + private static final String FILE_NAME = "file"; |
| 42 | + |
| 43 | + private final CloseableHttpClient apacheHttpClient; |
| 44 | + |
| 45 | + public ApacheHttpClientAdapter( |
| 46 | + Credential credential, Validator validator, CloseableHttpClient client) { |
| 47 | + super(credential, validator); |
| 48 | + this.apacheHttpClient = requireNonNull(client); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + protected String getHttpClientInfo() { |
| 53 | + return "apachehttp/" + apacheHttpClient.getClass().getPackage().getImplementationVersion(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public OriginalResponse innerExecute(HttpRequest wechatPayRequest) { |
| 58 | + try { |
| 59 | + CloseableHttpResponse apacheHttpResponse = |
| 60 | + apacheHttpClient.execute(buildApacheHttpRequest(wechatPayRequest)); |
| 61 | + return assembleOriginalResponse(wechatPayRequest, apacheHttpResponse); |
| 62 | + } catch (IOException e) { |
| 63 | + throw new HttpException(wechatPayRequest, e); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private org.apache.http.client.methods.HttpUriRequest buildApacheHttpRequest( |
| 68 | + HttpRequest wechatPayRequest) { |
| 69 | + String url = wechatPayRequest.getUrl().toString(); |
| 70 | + org.apache.http.client.methods.HttpUriRequest apacheHttpRequest; |
| 71 | + |
| 72 | + switch (wechatPayRequest.getHttpMethod().name()) { |
| 73 | + case "GET": |
| 74 | + apacheHttpRequest = new HttpGet(url); |
| 75 | + break; |
| 76 | + case "POST": |
| 77 | + apacheHttpRequest = new HttpPost(url); |
| 78 | + ((HttpPost) apacheHttpRequest).setEntity(buildApacheHttpEntity(wechatPayRequest.getBody())); |
| 79 | + break; |
| 80 | + case "PUT": |
| 81 | + apacheHttpRequest = new HttpPut(url); |
| 82 | + ((HttpPut) apacheHttpRequest).setEntity(buildApacheHttpEntity(wechatPayRequest.getBody())); |
| 83 | + break; |
| 84 | + case "PATCH": |
| 85 | + apacheHttpRequest = new HttpPatch(url); |
| 86 | + ((HttpPatch) apacheHttpRequest) |
| 87 | + .setEntity(buildApacheHttpEntity(wechatPayRequest.getBody())); |
| 88 | + break; |
| 89 | + case "DELETE": |
| 90 | + apacheHttpRequest = new HttpDelete(url); |
| 91 | + break; |
| 92 | + default: |
| 93 | + throw new IllegalArgumentException( |
| 94 | + "Unsupported HTTP method: " + wechatPayRequest.getHttpMethod().name()); |
| 95 | + } |
| 96 | + Map<String, String> headers = wechatPayRequest.getHeaders().getHeaders(); |
| 97 | + headers.forEach(apacheHttpRequest::addHeader); |
| 98 | + return apacheHttpRequest; |
| 99 | + } |
| 100 | + |
| 101 | + private HttpEntity buildApacheHttpEntity( |
| 102 | + com.wechat.pay.java.core.http.RequestBody wechatPayRequestBody) { |
| 103 | + // 处理空请求体的情况 |
| 104 | + if (wechatPayRequestBody == null) { |
| 105 | + return new StringEntity("", ""); |
| 106 | + } |
| 107 | + |
| 108 | + if (wechatPayRequestBody instanceof JsonRequestBody) { |
| 109 | + return new StringEntity( |
| 110 | + ((JsonRequestBody) wechatPayRequestBody).getBody(), |
| 111 | + ContentType.create(wechatPayRequestBody.getContentType())); |
| 112 | + } |
| 113 | + if (wechatPayRequestBody instanceof FileRequestBody) { |
| 114 | + FileRequestBody fileRequestBody = (FileRequestBody) wechatPayRequestBody; |
| 115 | + MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create(); |
| 116 | + entityBuilder.setMode(HttpMultipartMode.RFC6532); |
| 117 | + entityBuilder.addTextBody(META_NAME, fileRequestBody.getMeta(), APPLICATION_JSON); |
| 118 | + entityBuilder.addBinaryBody( |
| 119 | + FILE_NAME, |
| 120 | + fileRequestBody.getFile(), |
| 121 | + ContentType.create(fileRequestBody.getContentType()), |
| 122 | + fileRequestBody.getFileName()); |
| 123 | + return entityBuilder.build(); |
| 124 | + } |
| 125 | + logger.error( |
| 126 | + "When an http request is sent and the apache request body is constructed, the requestBody" |
| 127 | + + " parameter" |
| 128 | + + " type cannot be found,requestBody class name[{}]", |
| 129 | + wechatPayRequestBody.getClass().getName()); |
| 130 | + return null; |
| 131 | + } |
| 132 | + |
| 133 | + private OriginalResponse assembleOriginalResponse( |
| 134 | + HttpRequest wechatPayRequest, CloseableHttpResponse apacheHttpResponse) throws IOException { |
| 135 | + Map<String, String> responseHeaders = assembleResponseHeader(apacheHttpResponse); |
| 136 | + HttpEntity entity = apacheHttpResponse.getEntity(); |
| 137 | + try { |
| 138 | + String responseBody = entity != null ? EntityUtils.toString(entity) : null; |
| 139 | + return new OriginalResponse.Builder() |
| 140 | + .request(wechatPayRequest) |
| 141 | + .headers(responseHeaders) |
| 142 | + .statusCode(apacheHttpResponse.getStatusLine().getStatusCode()) |
| 143 | + .contentType(entity != null ? entity.getContentType().getValue() : null) |
| 144 | + .body(responseBody) |
| 145 | + .build(); |
| 146 | + } catch (IOException e) { |
| 147 | + throw new MalformedMessageException( |
| 148 | + String.format( |
| 149 | + "Assemble OriginalResponse,get responseBody failed.%nHttpRequest[%s]", |
| 150 | + wechatPayRequest)); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private Map<String, String> assembleResponseHeader(CloseableHttpResponse apacheHttpResponse) { |
| 155 | + Map<String, String> responseHeaders = new ConcurrentHashMap<>(); |
| 156 | + Header[] headers = apacheHttpResponse.getAllHeaders(); |
| 157 | + for (Header header : headers) { |
| 158 | + responseHeaders.put(header.getName(), header.getValue()); |
| 159 | + } |
| 160 | + return responseHeaders; |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + protected InputStream innerDownload(HttpRequest httpRequest) { |
| 165 | + try { |
| 166 | + CloseableHttpResponse apacheHttpResponse = |
| 167 | + apacheHttpClient.execute(buildApacheHttpRequest(httpRequest)); |
| 168 | + if (isInvalidHttpCode(apacheHttpResponse.getStatusLine().getStatusCode())) { |
| 169 | + throw new ServiceException( |
| 170 | + httpRequest, apacheHttpResponse.getStatusLine().getStatusCode(), ""); |
| 171 | + } |
| 172 | + InputStream responseBodyStream = null; |
| 173 | + if (apacheHttpResponse.getEntity() != null) { |
| 174 | + responseBodyStream = apacheHttpResponse.getEntity().getContent(); |
| 175 | + } |
| 176 | + return responseBodyStream; |
| 177 | + } catch (IOException e) { |
| 178 | + throw new HttpException(httpRequest, e); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments