|
| 1 | +package me.chanjar.weixin.open.executor; |
| 2 | + |
| 3 | +import jodd.http.HttpConnectionProvider; |
| 4 | +import jodd.http.HttpRequest; |
| 5 | +import jodd.http.HttpResponse; |
| 6 | +import jodd.http.ProxyInfo; |
| 7 | +import lombok.Data; |
| 8 | +import me.chanjar.weixin.common.enums.WxType; |
| 9 | +import me.chanjar.weixin.common.error.WxError; |
| 10 | +import me.chanjar.weixin.common.error.WxErrorException; |
| 11 | +import me.chanjar.weixin.common.util.http.RequestExecutor; |
| 12 | +import me.chanjar.weixin.common.util.http.RequestHttp; |
| 13 | +import me.chanjar.weixin.common.util.http.ResponseHandler; |
| 14 | +import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; |
| 15 | +import okhttp3.*; |
| 16 | +import org.apache.commons.io.IOUtils; |
| 17 | +import org.apache.http.HttpEntity; |
| 18 | +import org.apache.http.HttpHost; |
| 19 | +import org.apache.http.client.config.RequestConfig; |
| 20 | +import org.apache.http.client.methods.*; |
| 21 | +import org.apache.http.entity.ContentType; |
| 22 | +import org.apache.http.entity.mime.HttpMultipartMode; |
| 23 | +import org.apache.http.entity.mime.MultipartEntityBuilder; |
| 24 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 25 | + |
| 26 | +import java.io.ByteArrayInputStream; |
| 27 | +import java.io.IOException; |
| 28 | +import java.io.InputStream; |
| 29 | +import java.nio.charset.StandardCharsets; |
| 30 | +import java.util.Objects; |
| 31 | + |
| 32 | +/** |
| 33 | + * 通用的上传请求执行器 |
| 34 | + */ |
| 35 | +public class GenericUploadRequestExecutor implements RequestExecutor<String, InputStream> { |
| 36 | + |
| 37 | + private final Executor<?, ?> executor; |
| 38 | + |
| 39 | + /** |
| 40 | + * 构造通用执行器 |
| 41 | + * |
| 42 | + * @param requestHttp http请求 |
| 43 | + * @param httpMethod http方法(POST PUT PATCH) |
| 44 | + * @param paramName 参数名 |
| 45 | + * @param fileName 文件名 |
| 46 | + */ |
| 47 | + @SuppressWarnings("all") |
| 48 | + public GenericUploadRequestExecutor(RequestHttp<?, ?> requestHttp, String httpMethod, String paramName, String fileName) { |
| 49 | + switch (requestHttp.getRequestType()) { |
| 50 | + case APACHE_HTTP: |
| 51 | + executor = new ApacheExecutor(); |
| 52 | + break; |
| 53 | + case OK_HTTP: |
| 54 | + executor = new OkExecutor(); |
| 55 | + break; |
| 56 | + case JODD_HTTP: |
| 57 | + executor = new JoddExecutor(); |
| 58 | + break; |
| 59 | + default: |
| 60 | + throw new UnsupportedOperationException("使用了暂不支持的HTTP客户端:" + requestHttp.getRequestType()); |
| 61 | + } |
| 62 | + executor.setRequestHttp((RequestHttp) requestHttp); |
| 63 | + executor.setHttpMethod(httpMethod); |
| 64 | + executor.setParamName(paramName); |
| 65 | + executor.setFileName(fileName); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public String execute(String uri, InputStream data, WxType wxType) throws WxErrorException, IOException { |
| 70 | + String json = executor.execute(uri, data, wxType); |
| 71 | + WxError error = WxError.fromJson(json, wxType); |
| 72 | + if (error.getErrorCode() != 0) { |
| 73 | + throw new WxErrorException(error); |
| 74 | + } |
| 75 | + return json; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void execute(String uri, InputStream data, ResponseHandler<String> handler, WxType wxType) throws WxErrorException, IOException { |
| 80 | + handler.handle(this.execute(uri, data, wxType)); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * 内部请求执行器 |
| 85 | + * |
| 86 | + * @param <CLIENT> http客户端 |
| 87 | + * @param <PROXY> http代理 |
| 88 | + */ |
| 89 | + @Data |
| 90 | + public static abstract class Executor<CLIENT, PROXY> { |
| 91 | + |
| 92 | + private RequestHttp<CLIENT, PROXY> requestHttp; |
| 93 | + private String httpMethod; |
| 94 | + private String paramName; |
| 95 | + private String fileName; |
| 96 | + |
| 97 | + public abstract String execute(String uri, InputStream data, WxType wxType) throws WxErrorException, IOException; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * 阿帕奇执行器 |
| 102 | + */ |
| 103 | + public static class ApacheExecutor extends Executor<CloseableHttpClient, HttpHost> { |
| 104 | + |
| 105 | + @Override |
| 106 | + public String execute(String uri, InputStream data, WxType wxType) throws WxErrorException, IOException { |
| 107 | + HttpEntityEnclosingRequestBase bodyRequest; |
| 108 | + switch (getHttpMethod()) { |
| 109 | + case "POST": |
| 110 | + bodyRequest = new HttpPost(uri); |
| 111 | + break; |
| 112 | + case "PUT": |
| 113 | + bodyRequest = new HttpPut(uri); |
| 114 | + break; |
| 115 | + case "PATCH": |
| 116 | + bodyRequest = new HttpPatch(uri); |
| 117 | + break; |
| 118 | + default: |
| 119 | + throw new IllegalAccessError("不支持的请求方式:" + getHttpMethod()); |
| 120 | + } |
| 121 | + if (getRequestHttp().getRequestHttpProxy() != null) { |
| 122 | + RequestConfig config = RequestConfig.custom().setProxy(getRequestHttp().getRequestHttpProxy()).build(); |
| 123 | + bodyRequest.setConfig(config); |
| 124 | + } |
| 125 | + |
| 126 | + HttpEntity entity = MultipartEntityBuilder |
| 127 | + .create() |
| 128 | + .addBinaryBody(getParamName(), data, ContentType.create("multipart/form-data", StandardCharsets.UTF_8), getFileName()) |
| 129 | + .setMode(HttpMultipartMode.RFC6532) |
| 130 | + .build(); |
| 131 | + bodyRequest.setEntity(entity); |
| 132 | + bodyRequest.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString()); |
| 133 | + |
| 134 | + try (CloseableHttpResponse response = getRequestHttp().getRequestHttpClient().execute(bodyRequest)) { |
| 135 | + return Utf8ResponseHandler.INSTANCE.handleResponse(response); |
| 136 | + } finally { |
| 137 | + bodyRequest.releaseConnection(); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * ok执行器 |
| 144 | + */ |
| 145 | + public static class OkExecutor extends Executor<OkHttpClient, HttpHost> { |
| 146 | + |
| 147 | + @Override |
| 148 | + public String execute(String uri, InputStream data, WxType wxType) throws WxErrorException, IOException { |
| 149 | + OkHttpClient client = getRequestHttp().getRequestHttpClient(); |
| 150 | + |
| 151 | + byte[] bytes = data instanceof ByteArrayInputStream ? ((ByteArrayInputStream) data).readAllBytes() : IOUtils.toByteArray(data); |
| 152 | + RequestBody body = new MultipartBody.Builder() |
| 153 | + .setType(Objects.requireNonNull(MediaType.parse("multipart/form-data"))) |
| 154 | + .addFormDataPart("media", getFileName(), RequestBody.create(bytes, MediaType.parse("application/octet-stream"))) |
| 155 | + .build(); |
| 156 | + |
| 157 | + Request request = new Request.Builder().url(uri).method(getHttpMethod(), body).build(); |
| 158 | + Response response = client.newCall(request).execute(); |
| 159 | + return response.body().string(); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * jodd执行器 |
| 165 | + */ |
| 166 | + public static class JoddExecutor extends Executor<HttpConnectionProvider, ProxyInfo> { |
| 167 | + |
| 168 | + @Override |
| 169 | + public String execute(String uri, InputStream data, WxType wxType) throws WxErrorException, IOException { |
| 170 | + HttpRequest request = HttpRequest.post(uri); |
| 171 | + if (getRequestHttp().getRequestHttpProxy() != null) { |
| 172 | + getRequestHttp().getRequestHttpClient().useProxy(getRequestHttp().getRequestHttpProxy()); |
| 173 | + } |
| 174 | + request.withConnectionProvider(getRequestHttp().getRequestHttpClient()); |
| 175 | + |
| 176 | + byte[] bytes = data instanceof ByteArrayInputStream ? ((ByteArrayInputStream) data).readAllBytes() : IOUtils.toByteArray(data); |
| 177 | + request.form(getParamName(), data); |
| 178 | + |
| 179 | + HttpResponse response = request.send(); |
| 180 | + response.charset(StandardCharsets.UTF_8.name()); |
| 181 | + return response.bodyText(); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments