| @@ -1,46 +0,0 @@ | |||
| package me.chanjar.weixin.common.util.http; | |||
| import java.io.IOException; | |||
| import org.apache.http.HttpHost; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import jodd.http.HttpConnectionProvider; | |||
| import jodd.http.ProxyInfo; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo; | |||
| import okhttp3.ConnectionPool; | |||
| /** | |||
| * Created by ecoolper on 2017/4/27. | |||
| */ | |||
| public abstract class AbstractRequestExecutor<T, E> implements RequestExecutor<T, E> { | |||
| @Override | |||
| public T execute(RequestHttp requestHttp, String uri, E data) throws WxErrorException, IOException { | |||
| switch (requestHttp.getRequestType()) { | |||
| case apacheHttp: { | |||
| //apache-http请求 | |||
| CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient(); | |||
| HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy(); | |||
| return executeApache(httpClient, httpProxy, uri, data); | |||
| } | |||
| case joddHttp: { | |||
| //jodd-http请求 | |||
| HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient(); | |||
| ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy(); | |||
| return executeJodd(provider, proxyInfo, uri, data); | |||
| } | |||
| case okHttp: { | |||
| //okhttp请求 | |||
| ConnectionPool pool = (ConnectionPool) requestHttp.getRequestHttpClient(); | |||
| OkhttpProxyInfo proxyInfo = (OkhttpProxyInfo) requestHttp.getRequestHttpProxy(); | |||
| return executeOkhttp(pool, proxyInfo, uri, data); | |||
| } | |||
| default: | |||
| //TODO 这里需要抛出异常,需要优化 | |||
| return null; | |||
| } | |||
| } | |||
| } | |||
| @@ -8,8 +8,11 @@ import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.fs.FileUtils; | |||
| import me.chanjar.weixin.common.util.http.apache.ApacheMediaDownloadRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler; | |||
| import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; | |||
| import me.chanjar.weixin.common.util.http.jodd.JoddMediaDownloadRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.okhttp.OkMediaDownloadRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo; | |||
| import okhttp3.*; | |||
| @@ -37,207 +40,26 @@ import java.util.regex.Pattern; | |||
| * | |||
| * @author Daniel Qian | |||
| */ | |||
| public class MediaDownloadRequestExecutor extends AbstractRequestExecutor<File, String> { | |||
| private File tmpDirFile; | |||
| public MediaDownloadRequestExecutor(File tmpDirFile) { | |||
| this.tmpDirFile = tmpDirFile; | |||
| } | |||
| private String getFileName(HttpResponse response) throws WxErrorException { | |||
| String content = response.header("Content-disposition"); | |||
| if (content == null || content.length() == 0) { | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build()); | |||
| } | |||
| Pattern p = Pattern.compile(".*filename=\"(.*)\""); | |||
| Matcher m = p.matcher(content); | |||
| if (m.matches()) { | |||
| return m.group(1); | |||
| } | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build()); | |||
| } | |||
| private String getFileName(CloseableHttpResponse response) throws WxErrorException { | |||
| Header[] contentDispositionHeader = response.getHeaders("Content-disposition"); | |||
| if (contentDispositionHeader == null || contentDispositionHeader.length == 0) { | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build()); | |||
| } | |||
| Pattern p = Pattern.compile(".*filename=\"(.*)\""); | |||
| Matcher m = p.matcher(contentDispositionHeader[0].getValue()); | |||
| if (m.matches()) { | |||
| return m.group(1); | |||
| } | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build()); | |||
| } | |||
| /** | |||
| * apache-http实现方式 | |||
| * | |||
| * @param httpclient | |||
| * @param httpProxy | |||
| * @param uri | |||
| * @param queryParam | |||
| * @return | |||
| * @throws WxErrorException | |||
| * @throws IOException | |||
| */ | |||
| public File executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, IOException { | |||
| if (queryParam != null) { | |||
| if (uri.indexOf('?') == -1) { | |||
| uri += '?'; | |||
| } | |||
| uri += uri.endsWith("?") ? queryParam : '&' + queryParam; | |||
| } | |||
| HttpGet httpGet = new HttpGet(uri); | |||
| if (httpProxy != null) { | |||
| RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); | |||
| httpGet.setConfig(config); | |||
| } | |||
| try (CloseableHttpResponse response = httpclient.execute(httpGet); | |||
| InputStream inputStream = InputStreamResponseHandler.INSTANCE | |||
| .handleResponse(response)) { | |||
| Header[] contentTypeHeader = response.getHeaders("Content-Type"); | |||
| if (contentTypeHeader != null && contentTypeHeader.length > 0) { | |||
| if (contentTypeHeader[0].getValue().startsWith(ContentType.APPLICATION_JSON.getMimeType())) { | |||
| // application/json; encoding=utf-8 下载媒体文件出错 | |||
| String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
| throw new WxErrorException(WxError.fromJson(responseContent)); | |||
| } | |||
| } | |||
| String fileName = getFileName(response); | |||
| if (StringUtils.isBlank(fileName)) { | |||
| public abstract class MediaDownloadRequestExecutor<H, P> implements RequestExecutor<File, String> { | |||
| public static RequestExecutor<File, String> create(RequestHttp requestHttp, File tmpDirFile){ | |||
| switch (requestHttp.getRequestType()){ | |||
| case apacheHttp: | |||
| return new ApacheMediaDownloadRequestExecutor(requestHttp, tmpDirFile); | |||
| case joddHttp: | |||
| return new JoddMediaDownloadRequestExecutor(requestHttp, tmpDirFile); | |||
| case okHttp: | |||
| return new OkMediaDownloadRequestExecutor(requestHttp, tmpDirFile); | |||
| default: | |||
| return null; | |||
| } | |||
| String[] nameAndExt = fileName.split("\\."); | |||
| return FileUtils.createTmpFile(inputStream, nameAndExt[0], nameAndExt[1], this.tmpDirFile); | |||
| } finally { | |||
| httpGet.releaseConnection(); | |||
| } | |||
| } | |||
| protected RequestHttp<H, P> requestHttp; | |||
| protected File tmpDirFile; | |||
| /** | |||
| * jodd-http实现方式 | |||
| * | |||
| * @param provider | |||
| * @param proxyInfo | |||
| * @param uri | |||
| * @param queryParam | |||
| * @return | |||
| * @throws WxErrorException | |||
| * @throws IOException | |||
| */ | |||
| public File executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String queryParam) throws WxErrorException, IOException { | |||
| if (queryParam != null) { | |||
| if (uri.indexOf('?') == -1) { | |||
| uri += '?'; | |||
| } | |||
| uri += uri.endsWith("?") ? queryParam : '&' + queryParam; | |||
| } | |||
| HttpRequest request = HttpRequest.get(uri); | |||
| if (proxyInfo != null) { | |||
| provider.useProxy(proxyInfo); | |||
| } | |||
| request.withConnectionProvider(provider); | |||
| HttpResponse response = request.send(); | |||
| String contentType = response.header("Content-Type"); | |||
| if (contentType != null && contentType.startsWith("application/json")) { | |||
| // application/json; encoding=utf-8 下载媒体文件出错 | |||
| throw new WxErrorException(WxError.fromJson(response.bodyText())); | |||
| } | |||
| String fileName = getFileName(response); | |||
| if (StringUtils.isBlank(fileName)) { | |||
| return null; | |||
| } | |||
| InputStream inputStream = new ByteArrayInputStream(response.bodyBytes()); | |||
| String[] nameAndExt = fileName.split("\\."); | |||
| return FileUtils.createTmpFile(inputStream, nameAndExt[0], nameAndExt[1], this.tmpDirFile); | |||
| } | |||
| /** | |||
| * okhttp现实方式 | |||
| * | |||
| * @param pool | |||
| * @param proxyInfo | |||
| * @param uri | |||
| * @param queryParam | |||
| * @return | |||
| * @throws WxErrorException | |||
| * @throws IOException | |||
| */ | |||
| public File executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, String queryParam) throws WxErrorException, IOException { | |||
| if (queryParam != null) { | |||
| if (uri.indexOf('?') == -1) { | |||
| uri += '?'; | |||
| } | |||
| uri += uri.endsWith("?") ? queryParam : '&' + queryParam; | |||
| } | |||
| OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(pool); | |||
| //设置代理 | |||
| if (proxyInfo != null) { | |||
| clientBuilder.proxy(proxyInfo.getProxy()); | |||
| } | |||
| //设置授权 | |||
| clientBuilder.authenticator(new Authenticator() { | |||
| @Override | |||
| public Request authenticate(Route route, Response response) throws IOException { | |||
| String credential = Credentials.basic(proxyInfo.getProxyUsername(), proxyInfo.getProxyPassword()); | |||
| return response.request().newBuilder() | |||
| .header("Authorization", credential) | |||
| .build(); | |||
| } | |||
| }); | |||
| //得到httpClient | |||
| OkHttpClient client = clientBuilder.build(); | |||
| Request request = new Request.Builder().url(uri).get().build(); | |||
| Response response = client.newCall(request).execute(); | |||
| String contentType = response.header("Content-Type"); | |||
| if (contentType != null && contentType.startsWith("application/json")) { | |||
| // application/json; encoding=utf-8 下载媒体文件出错 | |||
| throw new WxErrorException(WxError.fromJson(response.body().string())); | |||
| } | |||
| String fileName = getFileName(response); | |||
| if (StringUtils.isBlank(fileName)) { | |||
| return null; | |||
| } | |||
| InputStream inputStream = new ByteArrayInputStream(response.body().bytes()); | |||
| String[] nameAndExt = fileName.split("\\."); | |||
| return FileUtils.createTmpFile(inputStream, nameAndExt[0], nameAndExt[1], this.tmpDirFile); | |||
| } | |||
| private String getFileName(Response response) throws WxErrorException { | |||
| String content = response.header("Content-disposition"); | |||
| if (content == null || content.length() == 0) { | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build()); | |||
| } | |||
| Pattern p = Pattern.compile(".*filename=\"(.*)\""); | |||
| Matcher m = p.matcher(content); | |||
| if (m.matches()) { | |||
| return m.group(1); | |||
| } | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build()); | |||
| public MediaDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) { | |||
| this.tmpDirFile = tmpDirFile; | |||
| } | |||
| } | |||
| @@ -7,8 +7,11 @@ import jodd.http.ProxyInfo; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.apache.ApacheMediaUploadRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; | |||
| import me.chanjar.weixin.common.util.http.jodd.JoddMediaUploadRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.okhttp.OkMediaUploadRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo; | |||
| import okhttp3.*; | |||
| @@ -32,117 +35,24 @@ import java.net.Proxy; | |||
| * | |||
| * @author Daniel Qian | |||
| */ | |||
| public class MediaUploadRequestExecutor extends AbstractRequestExecutor<WxMediaUploadResult, File> { | |||
| public abstract class MediaUploadRequestExecutor<H, P> implements RequestExecutor<WxMediaUploadResult, File> { | |||
| protected RequestHttp<H,P> requestHttp; | |||
| /** | |||
| * apache-http实现方式 | |||
| * | |||
| * @param httpclient | |||
| * @param httpProxy | |||
| * @param uri | |||
| * @param file | |||
| * @return | |||
| * @throws WxErrorException | |||
| * @throws IOException | |||
| */ | |||
| public WxMediaUploadResult executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File file) throws WxErrorException, IOException { | |||
| HttpPost httpPost = new HttpPost(uri); | |||
| if (httpProxy != null) { | |||
| RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); | |||
| httpPost.setConfig(config); | |||
| } | |||
| if (file != null) { | |||
| HttpEntity entity = MultipartEntityBuilder | |||
| .create() | |||
| .addBinaryBody("media", file) | |||
| .setMode(HttpMultipartMode.RFC6532) | |||
| .build(); | |||
| httpPost.setEntity(entity); | |||
| httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString()); | |||
| } | |||
| try (CloseableHttpResponse response = httpclient.execute(httpPost)) { | |||
| String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return WxMediaUploadResult.fromJson(responseContent); | |||
| } finally { | |||
| httpPost.releaseConnection(); | |||
| } | |||
| public MediaUploadRequestExecutor(RequestHttp requestHttp){ | |||
| this.requestHttp =requestHttp; | |||
| } | |||
| /** | |||
| * jodd-http实现方式 | |||
| * | |||
| * @param provider | |||
| * @param proxyInfo | |||
| * @param uri | |||
| * @param file | |||
| * @return | |||
| * @throws WxErrorException | |||
| * @throws IOException | |||
| */ | |||
| public WxMediaUploadResult executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, File file) throws WxErrorException, IOException { | |||
| HttpRequest request = HttpRequest.post(uri); | |||
| if (proxyInfo != null) { | |||
| provider.useProxy(proxyInfo); | |||
| public static RequestExecutor<WxMediaUploadResult, File> create(RequestHttp requestHttp){ | |||
| switch (requestHttp.getRequestType()){ | |||
| case apacheHttp: | |||
| return new ApacheMediaUploadRequestExecutor(requestHttp); | |||
| case joddHttp: | |||
| return new JoddMediaUploadRequestExecutor(requestHttp); | |||
| case okHttp: | |||
| return new OkMediaUploadRequestExecutor(requestHttp); | |||
| default: | |||
| return null; | |||
| } | |||
| request.withConnectionProvider(provider); | |||
| request.form("media", file); | |||
| HttpResponse response = request.send(); | |||
| String responseContent = response.bodyText(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return WxMediaUploadResult.fromJson(responseContent); | |||
| } | |||
| /** | |||
| * okhttp现实方式 | |||
| * | |||
| * @param pool | |||
| * @param proxyInfo | |||
| * @param uri | |||
| * @param file | |||
| * @return | |||
| * @throws WxErrorException | |||
| * @throws IOException | |||
| */ | |||
| public WxMediaUploadResult executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, File file) throws WxErrorException, IOException { | |||
| OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(pool); | |||
| //设置代理 | |||
| if (proxyInfo != null) { | |||
| clientBuilder.proxy(proxyInfo.getProxy()); | |||
| } | |||
| //设置授权 | |||
| clientBuilder.authenticator(new Authenticator() { | |||
| @Override | |||
| public Request authenticate(Route route, Response response) throws IOException { | |||
| String credential = Credentials.basic(proxyInfo.getProxyUsername(), proxyInfo.getProxyPassword()); | |||
| return response.request().newBuilder() | |||
| .header("Authorization", credential) | |||
| .build(); | |||
| } | |||
| }); | |||
| //得到httpClient | |||
| OkHttpClient client = clientBuilder.build(); | |||
| RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), file); | |||
| RequestBody body = new MultipartBody.Builder().addFormDataPart("media", null, fileBody).build(); | |||
| Request request = new Request.Builder().url(uri).post(body).build(); | |||
| Response response = client.newCall(request).execute(); | |||
| String responseContent = response.body().string(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return WxMediaUploadResult.fromJson(responseContent); | |||
| } | |||
| } | |||
| @@ -26,7 +26,7 @@ public interface RequestExecutor<T, E> { | |||
| * @throws WxErrorException | |||
| * @throws IOException | |||
| */ | |||
| T execute(RequestHttp requestHttp, String uri, E data) throws WxErrorException, IOException; | |||
| T execute(String uri, E data) throws WxErrorException, IOException; | |||
| /** | |||
| * apache-http实现方式 | |||
| @@ -37,10 +37,10 @@ public interface RequestExecutor<T, E> { | |||
| * @return | |||
| * @throws WxErrorException | |||
| * @throws IOException | |||
| */ | |||
| *//* | |||
| T executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, E data) throws WxErrorException, IOException; | |||
| /** | |||
| *//** | |||
| * jodd-http实现方式 | |||
| * @param provider | |||
| * @param proxyInfo | |||
| @@ -49,11 +49,11 @@ public interface RequestExecutor<T, E> { | |||
| * @return | |||
| * @throws WxErrorException | |||
| * @throws IOException | |||
| */ | |||
| *//* | |||
| T executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, E data) throws WxErrorException, IOException; | |||
| /** okhttp实现方式 | |||
| *//** okhttp实现方式 | |||
| * @param pool | |||
| * @param proxyInfo | |||
| * @param uri | |||
| @@ -61,7 +61,7 @@ public interface RequestExecutor<T, E> { | |||
| * @return | |||
| * @throws WxErrorException | |||
| * @throws IOException | |||
| */ | |||
| *//* | |||
| T executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, E data) throws WxErrorException, IOException; | |||
| */ | |||
| } | |||
| @@ -6,8 +6,11 @@ import jodd.http.HttpResponse; | |||
| import jodd.http.ProxyInfo; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.apache.ApacheSimpleGetRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; | |||
| import me.chanjar.weixin.common.util.http.jodd.JoddSimpleGetRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.okhttp.OkSimpleGetRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo; | |||
| import okhttp3.*; | |||
| @@ -27,126 +30,25 @@ import java.util.concurrent.TimeUnit; | |||
| * | |||
| * @author Daniel Qian | |||
| */ | |||
| public class SimpleGetRequestExecutor extends AbstractRequestExecutor<String, String> { | |||
| public abstract class SimpleGetRequestExecutor<H, P> implements RequestExecutor<String, String> { | |||
| protected RequestHttp<H,P> requestHttp; | |||
| /** | |||
| * apache-http实现方式 | |||
| * | |||
| * @param httpclient | |||
| * @param httpProxy | |||
| * @param uri | |||
| * @param queryParam | |||
| * @return | |||
| * @throws WxErrorException | |||
| * @throws IOException | |||
| */ | |||
| public String executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, IOException { | |||
| if (queryParam != null) { | |||
| if (uri.indexOf('?') == -1) { | |||
| uri += '?'; | |||
| } | |||
| uri += uri.endsWith("?") ? queryParam : '&' + queryParam; | |||
| } | |||
| HttpGet httpGet = new HttpGet(uri); | |||
| if (httpProxy != null) { | |||
| RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); | |||
| httpGet.setConfig(config); | |||
| } | |||
| try (CloseableHttpResponse response = httpclient.execute(httpGet)) { | |||
| String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return responseContent; | |||
| } finally { | |||
| httpGet.releaseConnection(); | |||
| } | |||
| public SimpleGetRequestExecutor(RequestHttp requestHttp){ | |||
| this.requestHttp =requestHttp; | |||
| } | |||
| /** | |||
| * jodd-http实现方式 | |||
| * | |||
| * @param provider | |||
| * @param proxyInfo | |||
| * @param uri | |||
| * @param queryParam | |||
| * @return | |||
| * @throws WxErrorException | |||
| * @throws IOException | |||
| */ | |||
| public String executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String queryParam) throws WxErrorException, IOException { | |||
| if (queryParam != null) { | |||
| if (uri.indexOf('?') == -1) { | |||
| uri += '?'; | |||
| } | |||
| uri += uri.endsWith("?") ? queryParam : '&' + queryParam; | |||
| } | |||
| HttpRequest request = HttpRequest.get(uri); | |||
| if (proxyInfo != null) { | |||
| provider.useProxy(proxyInfo); | |||
| } | |||
| request.withConnectionProvider(provider); | |||
| HttpResponse response = request.send(); | |||
| String responseContent = response.bodyText(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return responseContent; | |||
| } | |||
| /** | |||
| * okHttp实现方式 | |||
| * | |||
| * @param pool | |||
| * @param proxyInfo | |||
| * @param uri | |||
| * @param queryParam | |||
| * @return | |||
| * @throws WxErrorException | |||
| * @throws IOException | |||
| */ | |||
| public String executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, String queryParam) throws WxErrorException, IOException { | |||
| if (queryParam != null) { | |||
| if (uri.indexOf('?') == -1) { | |||
| uri += '?'; | |||
| } | |||
| uri += uri.endsWith("?") ? queryParam : '&' + queryParam; | |||
| } | |||
| OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(pool); | |||
| //设置代理 | |||
| if (proxyInfo != null) { | |||
| clientBuilder.proxy(proxyInfo.getProxy()); | |||
| } | |||
| //设置授权 | |||
| clientBuilder.authenticator(new Authenticator() { | |||
| @Override | |||
| public Request authenticate(Route route, Response response) throws IOException { | |||
| String credential = Credentials.basic(proxyInfo.getProxyUsername(), proxyInfo.getProxyPassword()); | |||
| return response.request().newBuilder() | |||
| .header("Authorization", credential) | |||
| .build(); | |||
| } | |||
| }); | |||
| //得到httpClient | |||
| OkHttpClient client =clientBuilder.build(); | |||
| Request request = new Request.Builder().url(uri).build(); | |||
| Response response = client.newCall(request).execute(); | |||
| String responseContent = response.body().string(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| public static RequestExecutor<String, String> create(RequestHttp requestHttp){ | |||
| switch(requestHttp.getRequestType()){ | |||
| case apacheHttp: | |||
| return new ApacheSimpleGetRequestExecutor(requestHttp); | |||
| case joddHttp: | |||
| return new JoddSimpleGetRequestExecutor(requestHttp); | |||
| case okHttp: | |||
| return new OkSimpleGetRequestExecutor(requestHttp); | |||
| default: | |||
| return null; | |||
| } | |||
| return responseContent; | |||
| } | |||
| } | |||
| @@ -6,8 +6,12 @@ import jodd.http.HttpResponse; | |||
| import jodd.http.ProxyInfo; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.apache.ApacheSimpleGetRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.apache.ApacheSimplePostRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; | |||
| import me.chanjar.weixin.common.util.http.jodd.JoddSimplePostRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.okhttp.OkSimplePostRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo; | |||
| import okhttp3.*; | |||
| @@ -27,142 +31,24 @@ import java.io.IOException; | |||
| * | |||
| * @author Daniel Qian | |||
| */ | |||
| public class SimplePostRequestExecutor extends AbstractRequestExecutor<String, String> { | |||
| public abstract class SimplePostRequestExecutor<H, P> implements RequestExecutor<String, String> { | |||
| protected RequestHttp<H,P> requestHttp; | |||
| /** | |||
| * apache-http实现方式 | |||
| * | |||
| * @param httpclient | |||
| * @param httpProxy | |||
| * @param uri | |||
| * @param postEntity | |||
| * @return | |||
| * @throws WxErrorException | |||
| * @throws IOException | |||
| */ | |||
| public String executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String postEntity) throws WxErrorException, IOException { | |||
| HttpPost httpPost = new HttpPost(uri); | |||
| if (httpProxy != null) { | |||
| RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); | |||
| httpPost.setConfig(config); | |||
| } | |||
| if (postEntity != null) { | |||
| StringEntity entity = new StringEntity(postEntity, Consts.UTF_8); | |||
| httpPost.setEntity(entity); | |||
| } | |||
| try (CloseableHttpResponse response = httpclient.execute(httpPost)) { | |||
| String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
| if (responseContent.isEmpty()) { | |||
| throw new WxErrorException( | |||
| WxError.newBuilder().setErrorCode(9999).setErrorMsg("无响应内容") | |||
| .build()); | |||
| } | |||
| if (responseContent.startsWith("<xml>")) { | |||
| //xml格式输出直接返回 | |||
| return responseContent; | |||
| } | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return responseContent; | |||
| } finally { | |||
| httpPost.releaseConnection(); | |||
| } | |||
| } | |||
| /** | |||
| * jodd-http实现方式 | |||
| * | |||
| * @param provider | |||
| * @param proxyInfo | |||
| * @param uri | |||
| * @param postEntity | |||
| * @return | |||
| * @throws WxErrorException | |||
| * @throws IOException | |||
| */ | |||
| public String executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String postEntity) throws WxErrorException, IOException { | |||
| HttpRequest request = HttpRequest.post(uri); | |||
| if (proxyInfo != null) { | |||
| provider.useProxy(proxyInfo); | |||
| } | |||
| request.withConnectionProvider(provider); | |||
| if (postEntity != null) { | |||
| request.bodyText(postEntity); | |||
| } | |||
| HttpResponse response = request.send(); | |||
| String responseContent = response.bodyText(); | |||
| if (responseContent.isEmpty()) { | |||
| throw new WxErrorException( | |||
| WxError.newBuilder().setErrorCode(9999).setErrorMsg("无响应内容") | |||
| .build()); | |||
| } | |||
| if (responseContent.startsWith("<xml>")) { | |||
| //xml格式输出直接返回 | |||
| return responseContent; | |||
| } | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return responseContent; | |||
| public SimplePostRequestExecutor(RequestHttp requestHttp){ | |||
| this.requestHttp =requestHttp; | |||
| } | |||
| /** | |||
| * okHttp实现方式 | |||
| * | |||
| * @param pool | |||
| * @param proxyInfo | |||
| * @param uri | |||
| * @param postEntity | |||
| * @return | |||
| * @throws WxErrorException | |||
| * @throws IOException | |||
| */ | |||
| public String executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, String postEntity) throws WxErrorException, IOException { | |||
| OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(pool); | |||
| //设置代理 | |||
| if (proxyInfo != null) { | |||
| clientBuilder.proxy(proxyInfo.getProxy()); | |||
| public static RequestExecutor<String, String> create(RequestHttp requestHttp){ | |||
| switch (requestHttp.getRequestType()){ | |||
| case apacheHttp: | |||
| return new ApacheSimplePostRequestExecutor(requestHttp); | |||
| case joddHttp: | |||
| return new JoddSimplePostRequestExecutor(requestHttp); | |||
| case okHttp: | |||
| return new OkSimplePostRequestExecutor(requestHttp); | |||
| default: | |||
| return null; | |||
| } | |||
| //设置授权 | |||
| clientBuilder.authenticator(new Authenticator() { | |||
| @Override | |||
| public Request authenticate(Route route, Response response) throws IOException { | |||
| String credential = Credentials.basic(proxyInfo.getProxyUsername(), proxyInfo.getProxyPassword()); | |||
| return response.request().newBuilder() | |||
| .header("Authorization", credential) | |||
| .build(); | |||
| } | |||
| }); | |||
| //得到httpClient | |||
| OkHttpClient client = clientBuilder.build(); | |||
| MediaType mediaType = MediaType.parse("text/plain; charset=utf-8"); | |||
| RequestBody body = RequestBody.create(mediaType, postEntity); | |||
| Request request = new Request.Builder().url(uri).post(body).build(); | |||
| Response response = client.newCall(request).execute(); | |||
| String responseContent = response.body().string(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return responseContent; | |||
| } | |||
| } | |||
| @@ -0,0 +1,90 @@ | |||
| package me.chanjar.weixin.common.util.http.apache; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.util.regex.Matcher; | |||
| import java.util.regex.Pattern; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.apache.http.Header; | |||
| import org.apache.http.HttpHost; | |||
| import org.apache.http.client.config.RequestConfig; | |||
| import org.apache.http.client.methods.CloseableHttpResponse; | |||
| import org.apache.http.client.methods.HttpGet; | |||
| import org.apache.http.entity.ContentType; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.fs.FileUtils; | |||
| import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class ApacheMediaDownloadRequestExecutor extends MediaDownloadRequestExecutor<CloseableHttpClient, HttpHost> { | |||
| public ApacheMediaDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) { | |||
| super(requestHttp, tmpDirFile); | |||
| } | |||
| @Override | |||
| public File execute(String uri, String queryParam) throws WxErrorException, IOException { | |||
| if (queryParam != null) { | |||
| if (uri.indexOf('?') == -1) { | |||
| uri += '?'; | |||
| } | |||
| uri += uri.endsWith("?") ? queryParam : '&' + queryParam; | |||
| } | |||
| HttpGet httpGet = new HttpGet(uri); | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build(); | |||
| httpGet.setConfig(config); | |||
| } | |||
| try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpGet); | |||
| InputStream inputStream = InputStreamResponseHandler.INSTANCE | |||
| .handleResponse(response)) { | |||
| Header[] contentTypeHeader = response.getHeaders("Content-Type"); | |||
| if (contentTypeHeader != null && contentTypeHeader.length > 0) { | |||
| if (contentTypeHeader[0].getValue().startsWith(ContentType.APPLICATION_JSON.getMimeType())) { | |||
| // application/json; encoding=utf-8 下载媒体文件出错 | |||
| String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
| throw new WxErrorException(WxError.fromJson(responseContent)); | |||
| } | |||
| } | |||
| String fileName = getFileName(response); | |||
| if (StringUtils.isBlank(fileName)) { | |||
| return null; | |||
| } | |||
| String[] nameAndExt = fileName.split("\\."); | |||
| return FileUtils.createTmpFile(inputStream, nameAndExt[0], nameAndExt[1], super.tmpDirFile); | |||
| } finally { | |||
| httpGet.releaseConnection(); | |||
| } | |||
| } | |||
| private String getFileName(CloseableHttpResponse response) throws WxErrorException { | |||
| Header[] contentDispositionHeader = response.getHeaders("Content-disposition"); | |||
| if (contentDispositionHeader == null || contentDispositionHeader.length == 0) { | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build()); | |||
| } | |||
| Pattern p = Pattern.compile(".*filename=\"(.*)\""); | |||
| Matcher m = p.matcher(contentDispositionHeader[0].getValue()); | |||
| if (m.matches()) { | |||
| return m.group(1); | |||
| } | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build()); | |||
| } | |||
| } | |||
| @@ -0,0 +1,58 @@ | |||
| package me.chanjar.weixin.common.util.http.apache; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import org.apache.http.HttpEntity; | |||
| import org.apache.http.HttpHost; | |||
| import org.apache.http.client.config.RequestConfig; | |||
| import org.apache.http.client.methods.CloseableHttpResponse; | |||
| import org.apache.http.client.methods.HttpPost; | |||
| import org.apache.http.entity.ContentType; | |||
| import org.apache.http.entity.mime.HttpMultipartMode; | |||
| import org.apache.http.entity.mime.MultipartEntityBuilder; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class ApacheMediaUploadRequestExecutor extends MediaUploadRequestExecutor<CloseableHttpClient, HttpHost> { | |||
| public ApacheMediaUploadRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public WxMediaUploadResult execute(String uri, File file) throws WxErrorException, IOException { | |||
| HttpPost httpPost = new HttpPost(uri); | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build(); | |||
| httpPost.setConfig(config); | |||
| } | |||
| if (file != null) { | |||
| HttpEntity entity = MultipartEntityBuilder | |||
| .create() | |||
| .addBinaryBody("media", file) | |||
| .setMode(HttpMultipartMode.RFC6532) | |||
| .build(); | |||
| httpPost.setEntity(entity); | |||
| httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString()); | |||
| } | |||
| try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { | |||
| String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return WxMediaUploadResult.fromJson(responseContent); | |||
| } finally { | |||
| httpPost.releaseConnection(); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,52 @@ | |||
| package me.chanjar.weixin.common.util.http.apache; | |||
| import java.io.IOException; | |||
| import org.apache.http.HttpHost; | |||
| import org.apache.http.client.config.RequestConfig; | |||
| import org.apache.http.client.methods.CloseableHttpResponse; | |||
| import org.apache.http.client.methods.HttpGet; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; | |||
| /** | |||
| * Created by ecoolper on 2017/5/4. | |||
| */ | |||
| public class ApacheSimpleGetRequestExecutor extends SimpleGetRequestExecutor<CloseableHttpClient, HttpHost> { | |||
| public ApacheSimpleGetRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public String execute(String uri, String queryParam) throws WxErrorException, IOException { | |||
| if (queryParam != null) { | |||
| if (uri.indexOf('?') == -1) { | |||
| uri += '?'; | |||
| } | |||
| uri += uri.endsWith("?") ? queryParam : '&' + queryParam; | |||
| } | |||
| HttpGet httpGet = new HttpGet(uri); | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build(); | |||
| httpGet.setConfig(config); | |||
| } | |||
| try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpGet)) { | |||
| String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return responseContent; | |||
| } finally { | |||
| httpGet.releaseConnection(); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,63 @@ | |||
| package me.chanjar.weixin.common.util.http.apache; | |||
| import java.io.IOException; | |||
| import org.apache.http.Consts; | |||
| import org.apache.http.HttpHost; | |||
| import org.apache.http.client.config.RequestConfig; | |||
| import org.apache.http.client.methods.CloseableHttpResponse; | |||
| import org.apache.http.client.methods.HttpPost; | |||
| import org.apache.http.entity.StringEntity; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor; | |||
| /** | |||
| * Created by ecoolper on 2017/5/4. | |||
| */ | |||
| public class ApacheSimplePostRequestExecutor extends SimplePostRequestExecutor<CloseableHttpClient, HttpHost> { | |||
| public ApacheSimplePostRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public String execute(String uri, String postEntity) throws WxErrorException, IOException { | |||
| HttpPost httpPost = new HttpPost(uri); | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build(); | |||
| httpPost.setConfig(config); | |||
| } | |||
| if (postEntity != null) { | |||
| StringEntity entity = new StringEntity(postEntity, Consts.UTF_8); | |||
| httpPost.setEntity(entity); | |||
| } | |||
| try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { | |||
| String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
| if (responseContent.isEmpty()) { | |||
| throw new WxErrorException( | |||
| WxError.newBuilder().setErrorCode(9999).setErrorMsg("无响应内容") | |||
| .build()); | |||
| } | |||
| if (responseContent.startsWith("<xml>")) { | |||
| //xml格式输出直接返回 | |||
| return responseContent; | |||
| } | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return responseContent; | |||
| } finally { | |||
| httpPost.releaseConnection(); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,79 @@ | |||
| package me.chanjar.weixin.common.util.http.jodd; | |||
| import java.io.ByteArrayInputStream; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.util.regex.Matcher; | |||
| import java.util.regex.Pattern; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import jodd.http.HttpConnectionProvider; | |||
| import jodd.http.HttpRequest; | |||
| import jodd.http.HttpResponse; | |||
| import jodd.http.ProxyInfo; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.fs.FileUtils; | |||
| import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class JoddMediaDownloadRequestExecutor extends MediaDownloadRequestExecutor<HttpConnectionProvider, ProxyInfo> { | |||
| public JoddMediaDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) { | |||
| super(requestHttp, tmpDirFile); | |||
| } | |||
| @Override | |||
| public File execute(String uri, String queryParam) throws WxErrorException, IOException { | |||
| if (queryParam != null) { | |||
| if (uri.indexOf('?') == -1) { | |||
| uri += '?'; | |||
| } | |||
| uri += uri.endsWith("?") ? queryParam : '&' + queryParam; | |||
| } | |||
| HttpRequest request = HttpRequest.get(uri); | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy()); | |||
| } | |||
| request.withConnectionProvider(requestHttp.getRequestHttpClient()); | |||
| HttpResponse response = request.send(); | |||
| String contentType = response.header("Content-Type"); | |||
| if (contentType != null && contentType.startsWith("application/json")) { | |||
| // application/json; encoding=utf-8 下载媒体文件出错 | |||
| throw new WxErrorException(WxError.fromJson(response.bodyText())); | |||
| } | |||
| String fileName = getFileName(response); | |||
| if (StringUtils.isBlank(fileName)) { | |||
| return null; | |||
| } | |||
| InputStream inputStream = new ByteArrayInputStream(response.bodyBytes()); | |||
| String[] nameAndExt = fileName.split("\\."); | |||
| return FileUtils.createTmpFile(inputStream, nameAndExt[0], nameAndExt[1], super.tmpDirFile); | |||
| } | |||
| private String getFileName(HttpResponse response) throws WxErrorException { | |||
| String content = response.header("Content-disposition"); | |||
| if (content == null || content.length() == 0) { | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build()); | |||
| } | |||
| Pattern p = Pattern.compile(".*filename=\"(.*)\""); | |||
| Matcher m = p.matcher(content); | |||
| if (m.matches()) { | |||
| return m.group(1); | |||
| } | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build()); | |||
| } | |||
| } | |||
| @@ -0,0 +1,41 @@ | |||
| package me.chanjar.weixin.common.util.http.jodd; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import jodd.http.HttpConnectionProvider; | |||
| import jodd.http.HttpRequest; | |||
| import jodd.http.HttpResponse; | |||
| import jodd.http.ProxyInfo; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class JoddMediaUploadRequestExecutor extends MediaUploadRequestExecutor<HttpConnectionProvider, ProxyInfo> { | |||
| public JoddMediaUploadRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public WxMediaUploadResult execute(String uri, File file) throws WxErrorException, IOException { | |||
| HttpRequest request = HttpRequest.post(uri); | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy()); | |||
| } | |||
| request.withConnectionProvider(requestHttp.getRequestHttpClient()); | |||
| request.form("media", file); | |||
| HttpResponse response = request.send(); | |||
| String responseContent = response.bodyText(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return WxMediaUploadResult.fromJson(responseContent); | |||
| } | |||
| } | |||
| @@ -0,0 +1,54 @@ | |||
| package me.chanjar.weixin.common.util.http.jodd; | |||
| import java.io.IOException; | |||
| import org.apache.http.HttpHost; | |||
| import org.apache.http.client.config.RequestConfig; | |||
| import org.apache.http.client.methods.CloseableHttpResponse; | |||
| import org.apache.http.client.methods.HttpGet; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import jodd.http.HttpConnectionProvider; | |||
| import jodd.http.HttpRequest; | |||
| import jodd.http.HttpResponse; | |||
| import jodd.http.ProxyInfo; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; | |||
| /** | |||
| * Created by ecoolper on 2017/5/4. | |||
| */ | |||
| public class JoddSimpleGetRequestExecutor extends SimpleGetRequestExecutor<HttpConnectionProvider, ProxyInfo> { | |||
| public JoddSimpleGetRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public String execute(String uri, String queryParam) throws WxErrorException, IOException { | |||
| if (queryParam != null) { | |||
| if (uri.indexOf('?') == -1) { | |||
| uri += '?'; | |||
| } | |||
| uri += uri.endsWith("?") ? queryParam : '&' + queryParam; | |||
| } | |||
| HttpRequest request = HttpRequest.get(uri); | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy()); | |||
| } | |||
| request.withConnectionProvider(requestHttp.getRequestHttpClient()); | |||
| HttpResponse response = request.send(); | |||
| String responseContent = response.bodyText(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return responseContent; | |||
| } | |||
| } | |||
| @@ -0,0 +1,58 @@ | |||
| package me.chanjar.weixin.common.util.http.jodd; | |||
| import java.io.IOException; | |||
| import jodd.http.HttpConnectionProvider; | |||
| import jodd.http.HttpRequest; | |||
| import jodd.http.HttpResponse; | |||
| import jodd.http.ProxyInfo; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor; | |||
| /** | |||
| * Created by ecoolper on 2017/5/4. | |||
| */ | |||
| public class JoddSimplePostRequestExecutor extends SimplePostRequestExecutor<HttpConnectionProvider, ProxyInfo> { | |||
| public JoddSimplePostRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public String execute(String uri, String postEntity) throws WxErrorException, IOException { | |||
| HttpConnectionProvider provider = requestHttp.getRequestHttpClient(); | |||
| ProxyInfo proxyInfo = requestHttp.getRequestHttpProxy(); | |||
| HttpRequest request = HttpRequest.post(uri); | |||
| if (proxyInfo != null) { | |||
| provider.useProxy(proxyInfo); | |||
| } | |||
| request.withConnectionProvider(provider); | |||
| if (postEntity != null) { | |||
| request.bodyText(postEntity); | |||
| } | |||
| HttpResponse response = request.send(); | |||
| String responseContent = response.bodyText(); | |||
| if (responseContent.isEmpty()) { | |||
| throw new WxErrorException( | |||
| WxError.newBuilder().setErrorCode(9999).setErrorMsg("无响应内容") | |||
| .build()); | |||
| } | |||
| if (responseContent.startsWith("<xml>")) { | |||
| //xml格式输出直接返回 | |||
| return responseContent; | |||
| } | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return responseContent; | |||
| } | |||
| } | |||
| @@ -0,0 +1,92 @@ | |||
| package me.chanjar.weixin.common.util.http.okhttp; | |||
| import java.io.ByteArrayInputStream; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.util.regex.Matcher; | |||
| import java.util.regex.Pattern; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.fs.FileUtils; | |||
| import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import okhttp3.*; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class OkMediaDownloadRequestExecutor extends MediaDownloadRequestExecutor<ConnectionPool, OkhttpProxyInfo> { | |||
| public OkMediaDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) { | |||
| super(requestHttp, tmpDirFile); | |||
| } | |||
| @Override | |||
| public File execute(String uri, String queryParam) throws WxErrorException, IOException { | |||
| if (queryParam != null) { | |||
| if (uri.indexOf('?') == -1) { | |||
| uri += '?'; | |||
| } | |||
| uri += uri.endsWith("?") ? queryParam : '&' + queryParam; | |||
| } | |||
| OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(requestHttp.getRequestHttpClient()); | |||
| //设置代理 | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| clientBuilder.proxy(requestHttp.getRequestHttpProxy().getProxy()); | |||
| } | |||
| //设置授权 | |||
| clientBuilder.authenticator(new Authenticator() { | |||
| @Override | |||
| public Request authenticate(Route route, Response response) throws IOException { | |||
| String credential = Credentials.basic(requestHttp.getRequestHttpProxy().getProxyUsername(), requestHttp.getRequestHttpProxy().getProxyPassword()); | |||
| return response.request().newBuilder() | |||
| .header("Authorization", credential) | |||
| .build(); | |||
| } | |||
| }); | |||
| //得到httpClient | |||
| OkHttpClient client = clientBuilder.build(); | |||
| Request request = new Request.Builder().url(uri).get().build(); | |||
| Response response = client.newCall(request).execute(); | |||
| String contentType = response.header("Content-Type"); | |||
| if (contentType != null && contentType.startsWith("application/json")) { | |||
| // application/json; encoding=utf-8 下载媒体文件出错 | |||
| throw new WxErrorException(WxError.fromJson(response.body().string())); | |||
| } | |||
| String fileName = getFileName(response); | |||
| if (StringUtils.isBlank(fileName)) { | |||
| return null; | |||
| } | |||
| InputStream inputStream = new ByteArrayInputStream(response.body().bytes()); | |||
| String[] nameAndExt = fileName.split("\\."); | |||
| return FileUtils.createTmpFile(inputStream, nameAndExt[0], nameAndExt[1], super.tmpDirFile); | |||
| } | |||
| private String getFileName(Response response) throws WxErrorException { | |||
| String content = response.header("Content-disposition"); | |||
| if (content == null || content.length() == 0) { | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build()); | |||
| } | |||
| Pattern p = Pattern.compile(".*filename=\"(.*)\""); | |||
| Matcher m = p.matcher(content); | |||
| if (m.matches()) { | |||
| return m.group(1); | |||
| } | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build()); | |||
| } | |||
| } | |||
| @@ -0,0 +1,56 @@ | |||
| package me.chanjar.weixin.common.util.http.okhttp; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import okhttp3.*; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class OkMediaUploadRequestExecutor extends MediaUploadRequestExecutor<ConnectionPool, OkhttpProxyInfo> { | |||
| public OkMediaUploadRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public WxMediaUploadResult execute(String uri, File file) throws WxErrorException, IOException { | |||
| OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(requestHttp.getRequestHttpClient()); | |||
| //设置代理 | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| clientBuilder.proxy(requestHttp.getRequestHttpProxy().getProxy()); | |||
| } | |||
| //设置授权 | |||
| clientBuilder.authenticator(new Authenticator() { | |||
| @Override | |||
| public Request authenticate(Route route, Response response) throws IOException { | |||
| String credential = Credentials.basic(requestHttp.getRequestHttpProxy().getProxyUsername(), requestHttp.getRequestHttpProxy().getProxyPassword()); | |||
| return response.request().newBuilder() | |||
| .header("Authorization", credential) | |||
| .build(); | |||
| } | |||
| }); | |||
| //得到httpClient | |||
| OkHttpClient client = clientBuilder.build(); | |||
| RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), file); | |||
| RequestBody body = new MultipartBody.Builder().addFormDataPart("media", null, fileBody).build(); | |||
| Request request = new Request.Builder().url(uri).post(body).build(); | |||
| Response response = client.newCall(request).execute(); | |||
| String responseContent = response.body().string(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return WxMediaUploadResult.fromJson(responseContent); | |||
| } | |||
| } | |||
| @@ -0,0 +1,63 @@ | |||
| package me.chanjar.weixin.common.util.http.okhttp; | |||
| import java.io.IOException; | |||
| import jodd.http.HttpConnectionProvider; | |||
| import jodd.http.HttpRequest; | |||
| import jodd.http.HttpResponse; | |||
| import jodd.http.ProxyInfo; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; | |||
| import okhttp3.*; | |||
| /** | |||
| * Created by ecoolper on 2017/5/4. | |||
| */ | |||
| public class OkSimpleGetRequestExecutor extends SimpleGetRequestExecutor<ConnectionPool, OkhttpProxyInfo> { | |||
| public OkSimpleGetRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public String execute(String uri, String queryParam) throws WxErrorException, IOException { | |||
| if (queryParam != null) { | |||
| if (uri.indexOf('?') == -1) { | |||
| uri += '?'; | |||
| } | |||
| uri += uri.endsWith("?") ? queryParam : '&' + queryParam; | |||
| } | |||
| OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(requestHttp.getRequestHttpClient()); | |||
| //设置代理 | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| clientBuilder.proxy(requestHttp.getRequestHttpProxy().getProxy()); | |||
| } | |||
| //设置授权 | |||
| clientBuilder.authenticator(new Authenticator() { | |||
| @Override | |||
| public Request authenticate(Route route, Response response) throws IOException { | |||
| String credential = Credentials.basic(requestHttp.getRequestHttpProxy().getProxyUsername(), requestHttp.getRequestHttpProxy().getProxyPassword()); | |||
| return response.request().newBuilder() | |||
| .header("Authorization", credential) | |||
| .build(); | |||
| } | |||
| }); | |||
| //得到httpClient | |||
| OkHttpClient client =clientBuilder.build(); | |||
| Request request = new Request.Builder().url(uri).build(); | |||
| Response response = client.newCall(request).execute(); | |||
| String responseContent = response.body().string(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return responseContent; | |||
| } | |||
| } | |||
| @@ -0,0 +1,59 @@ | |||
| package me.chanjar.weixin.common.util.http.okhttp; | |||
| import java.io.IOException; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor; | |||
| import okhttp3.*; | |||
| /** | |||
| * Created by ecoolper on 2017/5/4. | |||
| */ | |||
| public class OkSimplePostRequestExecutor extends SimplePostRequestExecutor<ConnectionPool,OkhttpProxyInfo>{ | |||
| public OkSimplePostRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public String execute(String uri, String postEntity) throws WxErrorException, IOException { | |||
| ConnectionPool pool = requestHttp.getRequestHttpClient(); | |||
| final OkhttpProxyInfo proxyInfo = requestHttp.getRequestHttpProxy(); | |||
| OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(pool); | |||
| //设置代理 | |||
| if (proxyInfo != null) { | |||
| clientBuilder.proxy(proxyInfo.getProxy()); | |||
| } | |||
| //设置授权 | |||
| clientBuilder.authenticator(new Authenticator() { | |||
| @Override | |||
| public Request authenticate(Route route, Response response) throws IOException { | |||
| String credential = Credentials.basic(proxyInfo.getProxyUsername(), proxyInfo.getProxyPassword()); | |||
| return response.request().newBuilder() | |||
| .header("Authorization", credential) | |||
| .build(); | |||
| } | |||
| }); | |||
| //得到httpClient | |||
| OkHttpClient client = clientBuilder.build(); | |||
| MediaType mediaType = MediaType.parse("text/plain; charset=utf-8"); | |||
| RequestBody body = RequestBody.create(mediaType, postEntity); | |||
| Request request = new Request.Builder().url(uri).post(body).build(); | |||
| Response response = client.newCall(request).execute(); | |||
| String responseContent = response.body().string(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return responseContent; | |||
| } | |||
| } | |||
| @@ -93,7 +93,7 @@ public abstract class AbstractWxCpServiceImpl<H, P> implements WxCpService, Requ | |||
| synchronized (this.globalJsapiTicketRefreshLock) { | |||
| if (this.configStorage.isJsapiTicketExpired()) { | |||
| String url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket"; | |||
| String responseContent = execute(new SimpleGetRequestExecutor(), url, null); | |||
| String responseContent = execute(SimpleGetRequestExecutor.create(this), url, null); | |||
| JsonElement tmpJsonElement = new JsonParser().parse(responseContent); | |||
| JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject(); | |||
| String jsapiTicket = tmpJsonObject.get("ticket").getAsString(); | |||
| @@ -187,14 +187,14 @@ public abstract class AbstractWxCpServiceImpl<H, P> implements WxCpService, Requ | |||
| @Override | |||
| public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException { | |||
| String url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?type=" + mediaType; | |||
| return execute(new MediaUploadRequestExecutor(), url, file); | |||
| return execute(MediaUploadRequestExecutor.create(this), url, file); | |||
| } | |||
| @Override | |||
| public File mediaDownload(String mediaId) throws WxErrorException { | |||
| String url = "https://qyapi.weixin.qq.com/cgi-bin/media/get"; | |||
| return execute( | |||
| new MediaDownloadRequestExecutor( | |||
| MediaDownloadRequestExecutor.create(this, | |||
| this.configStorage.getTmpDirFile()), | |||
| url, "media_id=" + mediaId); | |||
| } | |||
| @@ -204,7 +204,7 @@ public abstract class AbstractWxCpServiceImpl<H, P> implements WxCpService, Requ | |||
| public Integer departCreate(WxCpDepart depart) throws WxErrorException { | |||
| String url = "https://qyapi.weixin.qq.com/cgi-bin/department/create"; | |||
| String responseContent = execute( | |||
| new SimplePostRequestExecutor(), | |||
| SimplePostRequestExecutor.create(this), | |||
| url, | |||
| depart.toJson()); | |||
| JsonElement tmpJsonElement = new JsonParser().parse(responseContent); | |||
| @@ -475,12 +475,12 @@ public abstract class AbstractWxCpServiceImpl<H, P> implements WxCpService, Requ | |||
| @Override | |||
| public String get(String url, String queryParam) throws WxErrorException { | |||
| return execute(new SimpleGetRequestExecutor(), url, queryParam); | |||
| return execute(SimpleGetRequestExecutor.create(this), url, queryParam); | |||
| } | |||
| @Override | |||
| public String post(String url, String postData) throws WxErrorException { | |||
| return execute(new SimplePostRequestExecutor(), url, postData); | |||
| return execute(SimplePostRequestExecutor.create(this), url, postData); | |||
| } | |||
| /** | |||
| @@ -530,7 +530,7 @@ public abstract class AbstractWxCpServiceImpl<H, P> implements WxCpService, Requ | |||
| String uriWithAccessToken = uri + (uri.contains("?") ? "&" : "?") + "access_token=" + accessToken; | |||
| try { | |||
| T result = executor.execute(this, uriWithAccessToken, data); | |||
| T result = executor.execute(uriWithAccessToken, data); | |||
| this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", uriWithAccessToken, data, result); | |||
| return result; | |||
| } catch (WxErrorException e) { | |||
| @@ -8,7 +8,7 @@ import me.chanjar.weixin.mp.bean.result.WxMpCardResult; | |||
| * 卡券相关接口 | |||
| * @author YuJian(mgcnrx11@hotmail.com) on 01/11/2016 | |||
| */ | |||
| public interface WxMpCardService<H, P> { | |||
| public interface WxMpCardService { | |||
| /** | |||
| * 得到WxMpService | |||
| @@ -4,6 +4,7 @@ import me.chanjar.weixin.common.bean.WxJsapiSignature; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.mp.bean.*; | |||
| import me.chanjar.weixin.mp.bean.result.*; | |||
| @@ -414,5 +415,11 @@ public interface WxMpService { | |||
| */ | |||
| void initHttp(); | |||
| /** | |||
| * | |||
| * @return | |||
| */ | |||
| RequestHttp getRequestHttp(); | |||
| } | |||
| @@ -45,6 +45,7 @@ public abstract class AbstractWxMpServiceImpl<H, P> implements WxMpService, Requ | |||
| private int retrySleepMillis = 1000; | |||
| private int maxRetryTimes = 5; | |||
| @Override | |||
| public boolean checkSignature(String timestamp, String nonce, String signature) { | |||
| try { | |||
| @@ -71,7 +72,7 @@ public abstract class AbstractWxMpServiceImpl<H, P> implements WxMpService, Requ | |||
| } | |||
| if (this.getWxMpConfigStorage().isJsapiTicketExpired()) { | |||
| String responseContent = execute(new SimpleGetRequestExecutor(), WxMpService.GET_JSAPI_TICKET_URL, null); | |||
| String responseContent = execute(SimpleGetRequestExecutor.create(this), WxMpService.GET_JSAPI_TICKET_URL, null); | |||
| JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent); | |||
| JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject(); | |||
| String jsapiTicket = tmpJsonObject.get("ticket").getAsString(); | |||
| @@ -165,8 +166,8 @@ public abstract class AbstractWxMpServiceImpl<H, P> implements WxMpService, Requ | |||
| private WxMpOAuth2AccessToken getOAuth2AccessToken(String url) throws WxErrorException { | |||
| try { | |||
| RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | |||
| String responseText = executor.execute(this, url, null); | |||
| RequestExecutor<String, String> executor = SimpleGetRequestExecutor.create(this); | |||
| String responseText = executor.execute(url, null); | |||
| return WxMpOAuth2AccessToken.fromJson(responseText); | |||
| } catch (IOException e) { | |||
| throw new RuntimeException(e); | |||
| @@ -194,8 +195,8 @@ public abstract class AbstractWxMpServiceImpl<H, P> implements WxMpService, Requ | |||
| String url = String.format(WxMpService.OAUTH2_USERINFO_URL, oAuth2AccessToken.getAccessToken(), oAuth2AccessToken.getOpenId(), lang); | |||
| try { | |||
| RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | |||
| String responseText = executor.execute(this, url, null); | |||
| RequestExecutor<String, String> executor = SimpleGetRequestExecutor.create(this); | |||
| String responseText = executor.execute(url, null); | |||
| return WxMpUser.fromJson(responseText); | |||
| } catch (IOException e) { | |||
| throw new RuntimeException(e); | |||
| @@ -207,8 +208,8 @@ public abstract class AbstractWxMpServiceImpl<H, P> implements WxMpService, Requ | |||
| String url = String.format(WxMpService.OAUTH2_VALIDATE_TOKEN_URL, oAuth2AccessToken.getAccessToken(), oAuth2AccessToken.getOpenId()); | |||
| try { | |||
| RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | |||
| executor.execute(this, url, null); | |||
| RequestExecutor<String, String> executor = SimpleGetRequestExecutor.create(this); | |||
| executor.execute(url, null); | |||
| } catch (IOException e) { | |||
| throw new RuntimeException(e); | |||
| } catch (WxErrorException e) { | |||
| @@ -231,12 +232,12 @@ public abstract class AbstractWxMpServiceImpl<H, P> implements WxMpService, Requ | |||
| @Override | |||
| public String get(String url, String queryParam) throws WxErrorException { | |||
| return execute(new SimpleGetRequestExecutor(), url, queryParam); | |||
| return execute(SimpleGetRequestExecutor.create(this), url, queryParam); | |||
| } | |||
| @Override | |||
| public String post(String url, String postData) throws WxErrorException { | |||
| return execute(new SimplePostRequestExecutor(), url, postData); | |||
| return execute(SimplePostRequestExecutor.create(this), url, postData); | |||
| } | |||
| /** | |||
| @@ -283,7 +284,7 @@ public abstract class AbstractWxMpServiceImpl<H, P> implements WxMpService, Requ | |||
| String uriWithAccessToken = uri + (uri.contains("?") ? "&" : "?") + "access_token=" + accessToken; | |||
| try { | |||
| T result = executor.execute(this, uriWithAccessToken, data); | |||
| T result = executor.execute(uriWithAccessToken, data); | |||
| this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", uriWithAccessToken, data, result); | |||
| return result; | |||
| } catch (WxErrorException e) { | |||
| @@ -15,8 +15,6 @@ import me.chanjar.weixin.mp.api.WxMpCardService; | |||
| import me.chanjar.weixin.mp.api.WxMpService; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpCardResult; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| import org.apache.http.HttpHost; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| @@ -26,7 +24,7 @@ import java.util.concurrent.locks.Lock; | |||
| /** | |||
| * Created by Binary Wang on 2016/7/27. | |||
| */ | |||
| public class WxMpCardServiceImpl implements WxMpCardService<CloseableHttpClient, HttpHost> { | |||
| public class WxMpCardServiceImpl implements WxMpCardService { | |||
| private final Logger log = LoggerFactory.getLogger(WxMpCardServiceImpl.class); | |||
| @@ -81,7 +79,7 @@ public class WxMpCardServiceImpl implements WxMpCardService<CloseableHttpClient, | |||
| if (this.getWxMpService().getWxMpConfigStorage().isCardApiTicketExpired()) { | |||
| String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=wx_card"; | |||
| String responseContent = this.wxMpService.execute(new SimpleGetRequestExecutor(), url, null); | |||
| String responseContent = this.wxMpService.execute(SimpleGetRequestExecutor.create(this.getWxMpService().getRequestHttp()), url, null); | |||
| JsonElement tmpJsonElement = new JsonParser().parse(responseContent); | |||
| JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject(); | |||
| String cardApiTicket = tmpJsonObject.get("ticket").getAsString(); | |||
| @@ -67,7 +67,7 @@ public class WxMpKefuServiceImpl implements WxMpKefuService { | |||
| @Override | |||
| public boolean kfAccountUploadHeadImg(String kfAccount, File imgFile) throws WxErrorException { | |||
| WxMediaUploadResult responseContent = this.wxMpService | |||
| .execute(new MediaUploadRequestExecutor(), String.format(KFACCOUNT_UPLOAD_HEAD_IMG, kfAccount), imgFile); | |||
| .execute(MediaUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), String.format(KFACCOUNT_UPLOAD_HEAD_IMG, kfAccount), imgFile); | |||
| return responseContent != null; | |||
| } | |||
| @@ -46,14 +46,14 @@ public class WxMpMaterialServiceImpl implements WxMpMaterialService { | |||
| @Override | |||
| public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException { | |||
| String url = MEDIA_API_URL_PREFIX + "/upload?type=" + mediaType; | |||
| return this.wxMpService.execute(new MediaUploadRequestExecutor(), url, file); | |||
| return this.wxMpService.execute(MediaUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), url, file); | |||
| } | |||
| @Override | |||
| public File mediaDownload(String media_id) throws WxErrorException { | |||
| String url = MEDIA_API_URL_PREFIX + "/get"; | |||
| return this.wxMpService.execute( | |||
| new MediaDownloadRequestExecutor(this.wxMpService.getWxMpConfigStorage().getTmpDirFile()), | |||
| MediaDownloadRequestExecutor.create(this.wxMpService.getRequestHttp(), this.wxMpService.getWxMpConfigStorage().getTmpDirFile()), | |||
| url, | |||
| "media_id=" + media_id); | |||
| } | |||
| @@ -61,13 +61,13 @@ public class WxMpMaterialServiceImpl implements WxMpMaterialService { | |||
| @Override | |||
| public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException { | |||
| String url = MEDIA_API_URL_PREFIX + "/uploadimg"; | |||
| return this.wxMpService.execute(new MediaImgUploadRequestExecutor(), url, file); | |||
| return this.wxMpService.execute(MediaImgUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), url, file); | |||
| } | |||
| @Override | |||
| public WxMpMaterialUploadResult materialFileUpload(String mediaType, WxMpMaterial material) throws WxErrorException { | |||
| String url = MATERIAL_API_URL_PREFIX + "/add_material?type=" + mediaType; | |||
| return this.wxMpService.execute(new MaterialUploadRequestExecutor(), url, material); | |||
| return this.wxMpService.execute(MaterialUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), url, material); | |||
| } | |||
| @Override | |||
| @@ -83,19 +83,19 @@ public class WxMpMaterialServiceImpl implements WxMpMaterialService { | |||
| @Override | |||
| public InputStream materialImageOrVoiceDownload(String media_id) throws WxErrorException { | |||
| String url = MATERIAL_API_URL_PREFIX + "/get_material"; | |||
| return this.wxMpService.execute(new MaterialVoiceAndImageDownloadRequestExecutor(this.wxMpService.getWxMpConfigStorage().getTmpDirFile()), url, media_id); | |||
| return this.wxMpService.execute(MaterialVoiceAndImageDownloadRequestExecutor.create(this.wxMpService.getRequestHttp(),this.wxMpService.getWxMpConfigStorage().getTmpDirFile()), url, media_id); | |||
| } | |||
| @Override | |||
| public WxMpMaterialVideoInfoResult materialVideoInfo(String media_id) throws WxErrorException { | |||
| String url = MATERIAL_API_URL_PREFIX + "/get_material"; | |||
| return this.wxMpService.execute(new MaterialVideoInfoRequestExecutor(), url, media_id); | |||
| return this.wxMpService.execute(MaterialVideoInfoRequestExecutor.create(this.wxMpService.getRequestHttp()), url, media_id); | |||
| } | |||
| @Override | |||
| public WxMpMaterialNews materialNewsInfo(String media_id) throws WxErrorException { | |||
| String url = MATERIAL_API_URL_PREFIX + "/get_material"; | |||
| return this.wxMpService.execute(new MaterialNewsInfoRequestExecutor(), url, media_id); | |||
| return this.wxMpService.execute(MaterialNewsInfoRequestExecutor.create(this.wxMpService.getRequestHttp()), url, media_id); | |||
| } | |||
| @Override | |||
| @@ -113,7 +113,7 @@ public class WxMpMaterialServiceImpl implements WxMpMaterialService { | |||
| @Override | |||
| public boolean materialDelete(String media_id) throws WxErrorException { | |||
| String url = MATERIAL_API_URL_PREFIX + "/del_material"; | |||
| return this.wxMpService.execute(new MaterialDeleteRequestExecutor(), url, media_id); | |||
| return this.wxMpService.execute(MaterialDeleteRequestExecutor.create(this.wxMpService.getRequestHttp()), url, media_id); | |||
| } | |||
| @Override | |||
| @@ -89,7 +89,7 @@ public class WxMpQrcodeServiceImpl implements WxMpQrcodeService { | |||
| @Override | |||
| public File qrCodePicture(WxMpQrCodeTicket ticket) throws WxErrorException { | |||
| String url = "https://mp.weixin.qq.com/cgi-bin/showqrcode"; | |||
| return this.wxMpService.execute(new QrCodeRequestExecutor(), url, ticket); | |||
| return this.wxMpService.execute(QrCodeRequestExecutor.create(this.wxMpService.getRequestHttp()), url, ticket); | |||
| } | |||
| @Override | |||
| @@ -28,7 +28,7 @@ public class WxMpUserBlacklistServiceImpl implements WxMpUserBlacklistService { | |||
| JsonObject jsonObject = new JsonObject(); | |||
| jsonObject.addProperty("begin_openid", nextOpenid); | |||
| String url = API_BLACKLIST_PREFIX + "/getblacklist"; | |||
| String responseContent = this.wxMpService.execute(new SimplePostRequestExecutor(), url, jsonObject.toString()); | |||
| String responseContent = this.wxMpService.execute(SimplePostRequestExecutor.create(this.wxMpService.getRequestHttp()), url, jsonObject.toString()); | |||
| return WxMpUserBlacklistGetResult.fromJson(responseContent); | |||
| } | |||
| @@ -37,7 +37,7 @@ public class WxMpUserBlacklistServiceImpl implements WxMpUserBlacklistService { | |||
| Map<String, Object> map = new HashMap<>(); | |||
| map.put("openid_list", openidList); | |||
| String url = API_BLACKLIST_PREFIX + "/batchblacklist"; | |||
| this.wxMpService.execute(new SimplePostRequestExecutor(), url, new Gson().toJson(map)); | |||
| this.wxMpService.execute(SimplePostRequestExecutor.create(this.wxMpService.getRequestHttp()), url, new Gson().toJson(map)); | |||
| } | |||
| @Override | |||
| @@ -45,6 +45,6 @@ public class WxMpUserBlacklistServiceImpl implements WxMpUserBlacklistService { | |||
| Map<String, Object> map = new HashMap<>(); | |||
| map.put("openid_list", openidList); | |||
| String url = API_BLACKLIST_PREFIX + "/batchunblacklist"; | |||
| this.wxMpService.execute(new SimplePostRequestExecutor(), url, new Gson().toJson(map)); | |||
| this.wxMpService.execute(SimplePostRequestExecutor.create(this.wxMpService.getRequestHttp()), url, new Gson().toJson(map)); | |||
| } | |||
| } | |||
| @@ -71,6 +71,11 @@ public class WxMpServiceImpl extends AbstractWxMpServiceImpl<CloseableHttpClient | |||
| this.httpClient = apacheHttpClientBuilder.build(); | |||
| } | |||
| @Override | |||
| public RequestHttp getRequestHttp() { | |||
| return this; | |||
| } | |||
| @Override | |||
| public String getAccessToken(boolean forceRefresh) throws WxErrorException { | |||
| Lock lock = this.getWxMpConfigStorage().getAccessTokenLock(); | |||
| @@ -6,6 +6,7 @@ import me.chanjar.weixin.common.bean.WxAccessToken; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.HttpType; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.mp.api.*; | |||
| import me.chanjar.weixin.mp.api.impl.*; | |||
| @@ -45,6 +46,11 @@ public class WxMpServiceImpl extends AbstractWxMpServiceImpl<HttpConnectionProvi | |||
| httpClient = JoddHttp.httpConnectionProvider; | |||
| } | |||
| @Override | |||
| public RequestHttp getRequestHttp() { | |||
| return this; | |||
| } | |||
| @Override | |||
| public String getAccessToken(boolean forceRefresh) throws WxErrorException { | |||
| @@ -4,6 +4,7 @@ import me.chanjar.weixin.common.bean.WxAccessToken; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.HttpType; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo; | |||
| import me.chanjar.weixin.mp.api.WxMpConfigStorage; | |||
| import me.chanjar.weixin.mp.api.WxMpService; | |||
| @@ -94,5 +95,10 @@ public class WxMpServiceImpl extends AbstractWxMpServiceImpl<ConnectionPool, Okh | |||
| httpClient = new ConnectionPool(); | |||
| } | |||
| @Override | |||
| public RequestHttp getRequestHttp() { | |||
| return this; | |||
| } | |||
| } | |||
| @@ -6,7 +6,6 @@ import jodd.http.HttpResponse; | |||
| import jodd.http.ProxyInfo; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.AbstractRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; | |||
| @@ -14,6 +13,9 @@ import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; | |||
| import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo; | |||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
| import me.chanjar.weixin.mp.util.http.apache.ApacheMaterialDeleteRequestExecutor; | |||
| import me.chanjar.weixin.mp.util.http.jodd.JoddMaterialDeleteRequestExecutor; | |||
| import me.chanjar.weixin.mp.util.http.okhttp.OkhttpMaterialDeleteRequestExecutor; | |||
| import okhttp3.*; | |||
| import org.apache.http.HttpHost; | |||
| @@ -27,87 +29,23 @@ import java.io.IOException; | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
| public class MaterialDeleteRequestExecutor extends AbstractRequestExecutor<Boolean, String> { | |||
| public abstract class MaterialDeleteRequestExecutor<H,P> implements RequestExecutor<Boolean, String> { | |||
| protected RequestHttp<H,P> requestHttp; | |||
| public MaterialDeleteRequestExecutor() { | |||
| super(); | |||
| } | |||
| @Override | |||
| public Boolean executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, | |||
| String materialId) throws WxErrorException, IOException { | |||
| HttpPost httpPost = new HttpPost(uri); | |||
| if (httpProxy != null) { | |||
| RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); | |||
| httpPost.setConfig(config); | |||
| } | |||
| Map<String, String> params = new HashMap<>(); | |||
| params.put("media_id", materialId); | |||
| httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); | |||
| try (CloseableHttpResponse response = httpclient.execute(httpPost)) { | |||
| String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return true; | |||
| } | |||
| } finally { | |||
| httpPost.releaseConnection(); | |||
| } | |||
| } | |||
| @Override | |||
| public Boolean executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String materialId) throws WxErrorException, IOException { | |||
| HttpRequest request = HttpRequest.post(uri); | |||
| if (proxyInfo != null) { | |||
| provider.useProxy(proxyInfo); | |||
| } | |||
| request.withConnectionProvider(provider); | |||
| request.query("media_id", materialId); | |||
| HttpResponse response = request.send(); | |||
| String responseContent = response.bodyText(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return true; | |||
| } | |||
| public MaterialDeleteRequestExecutor(RequestHttp requestHttp){ | |||
| this.requestHttp =requestHttp; | |||
| } | |||
| @Override | |||
| public Boolean executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, String materialId) throws WxErrorException, IOException { | |||
| OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(pool); | |||
| //设置代理 | |||
| if (proxyInfo != null) { | |||
| clientBuilder.proxy(proxyInfo.getProxy()); | |||
| } | |||
| //设置授权 | |||
| clientBuilder.authenticator(new Authenticator() { | |||
| @Override | |||
| public Request authenticate(Route route, Response response) throws IOException { | |||
| String credential = Credentials.basic(proxyInfo.getProxyUsername(), proxyInfo.getProxyPassword()); | |||
| return response.request().newBuilder() | |||
| .header("Authorization", credential) | |||
| .build(); | |||
| } | |||
| }); | |||
| //得到httpClient | |||
| OkHttpClient client = clientBuilder.build(); | |||
| RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build(); | |||
| Request request = new Request.Builder().url(uri).post(requestBody).build(); | |||
| Response response = client.newCall(request).execute(); | |||
| String responseContent = response.body().string(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return true; | |||
| public static RequestExecutor<Boolean, String> create(RequestHttp requestHttp){ | |||
| switch (requestHttp.getRequestType()){ | |||
| case apacheHttp: | |||
| return new ApacheMaterialDeleteRequestExecutor(requestHttp); | |||
| case joddHttp: | |||
| return new JoddMaterialDeleteRequestExecutor(requestHttp); | |||
| case okHttp: | |||
| return new OkhttpMaterialDeleteRequestExecutor(requestHttp); | |||
| default: | |||
| return null; | |||
| } | |||
| } | |||
| @@ -6,7 +6,6 @@ import jodd.http.HttpResponse; | |||
| import jodd.http.ProxyInfo; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.AbstractRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; | |||
| @@ -15,6 +14,9 @@ import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo; | |||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
| import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews; | |||
| import me.chanjar.weixin.mp.util.http.apache.ApacheMaterialNewsInfoRequestExecutor; | |||
| import me.chanjar.weixin.mp.util.http.jodd.JoddMaterialNewsInfoRequestExecutor; | |||
| import me.chanjar.weixin.mp.util.http.okhttp.OkhttpMaterialNewsInfoRequestExecutor; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| import okhttp3.*; | |||
| @@ -29,88 +31,24 @@ import java.io.IOException; | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
| public class MaterialNewsInfoRequestExecutor extends AbstractRequestExecutor<WxMpMaterialNews, String> { | |||
| public abstract class MaterialNewsInfoRequestExecutor<H,P> implements RequestExecutor<WxMpMaterialNews, String> { | |||
| protected RequestHttp<H,P> requestHttp; | |||
| public MaterialNewsInfoRequestExecutor() { | |||
| super(); | |||
| public MaterialNewsInfoRequestExecutor(RequestHttp requestHttp){ | |||
| this.requestHttp =requestHttp; | |||
| } | |||
| @Override | |||
| public WxMpMaterialNews executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, IOException { | |||
| HttpPost httpPost = new HttpPost(uri); | |||
| if (httpProxy != null) { | |||
| RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); | |||
| httpPost.setConfig(config); | |||
| } | |||
| Map<String, String> params = new HashMap<>(); | |||
| params.put("media_id", materialId); | |||
| httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); | |||
| try (CloseableHttpResponse response = httpclient.execute(httpPost)) { | |||
| String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class); | |||
| } | |||
| } finally { | |||
| httpPost.releaseConnection(); | |||
| } | |||
| } | |||
| @Override | |||
| public WxMpMaterialNews executeJodd(HttpConnectionProvider httpclient, ProxyInfo httpProxy, String uri, String materialId) throws WxErrorException, IOException { | |||
| HttpRequest request = HttpRequest.post(uri); | |||
| if (httpProxy != null) { | |||
| httpclient.useProxy(httpProxy); | |||
| } | |||
| request.withConnectionProvider(httpclient); | |||
| request.query("media_id", materialId); | |||
| HttpResponse response = request.send(); | |||
| String responseContent = response.bodyText(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class); | |||
| } | |||
| } | |||
| @Override | |||
| public WxMpMaterialNews executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, String materialId) throws WxErrorException, IOException { | |||
| OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(pool); | |||
| //设置代理 | |||
| if (proxyInfo != null) { | |||
| clientBuilder.proxy(proxyInfo.getProxy()); | |||
| } | |||
| //设置授权 | |||
| clientBuilder.authenticator(new Authenticator() { | |||
| @Override | |||
| public Request authenticate(Route route, Response response) throws IOException { | |||
| String credential = Credentials.basic(proxyInfo.getProxyUsername(), proxyInfo.getProxyPassword()); | |||
| return response.request().newBuilder() | |||
| .header("Authorization", credential) | |||
| .build(); | |||
| } | |||
| }); | |||
| //得到httpClient | |||
| OkHttpClient client = clientBuilder.build(); | |||
| RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build(); | |||
| Request request = new Request.Builder().url(uri).post(requestBody).build(); | |||
| Response response = client.newCall(request).execute(); | |||
| String responseContent = response.body().string(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class); | |||
| public static RequestExecutor<WxMpMaterialNews, String> create(RequestHttp requestHttp){ | |||
| switch (requestHttp.getRequestType()){ | |||
| case apacheHttp: | |||
| return new ApacheMaterialNewsInfoRequestExecutor(requestHttp); | |||
| case joddHttp: | |||
| return new JoddMaterialNewsInfoRequestExecutor(requestHttp); | |||
| case okHttp: | |||
| return new OkhttpMaterialNewsInfoRequestExecutor(requestHttp); | |||
| default: | |||
| //TODO 需要优化抛出异常 | |||
| return null; | |||
| } | |||
| } | |||
| @@ -6,7 +6,6 @@ import jodd.http.HttpResponse; | |||
| import jodd.http.ProxyInfo; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.AbstractRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; | |||
| @@ -16,6 +15,9 @@ import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo; | |||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
| import me.chanjar.weixin.mp.bean.material.WxMpMaterial; | |||
| import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult; | |||
| import me.chanjar.weixin.mp.util.http.apache.ApacheMaterialUploadRequestExecutor; | |||
| import me.chanjar.weixin.mp.util.http.jodd.JoddMaterialUploadRequestExecutor; | |||
| import me.chanjar.weixin.mp.util.http.okhttp.OkhttpMaterialUploadRequestExecutor; | |||
| import okhttp3.*; | |||
| import org.apache.http.HttpHost; | |||
| @@ -32,127 +34,22 @@ import java.io.FileNotFoundException; | |||
| import java.io.IOException; | |||
| import java.util.Map; | |||
| public class MaterialUploadRequestExecutor extends AbstractRequestExecutor<WxMpMaterialUploadResult, WxMpMaterial> { | |||
| @Override | |||
| public WxMpMaterialUploadResult executeJodd(HttpConnectionProvider provider, ProxyInfo httpProxy, String uri, WxMpMaterial material) throws WxErrorException, IOException { | |||
| HttpRequest request = HttpRequest.post(uri); | |||
| if (httpProxy != null) { | |||
| provider.useProxy(httpProxy); | |||
| } | |||
| request.withConnectionProvider(provider); | |||
| if (material == null) { | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法请求,material参数为空").build()); | |||
| } | |||
| File file = material.getFile(); | |||
| if (file == null || !file.exists()) { | |||
| throw new FileNotFoundException(); | |||
| } | |||
| request.form("media", file); | |||
| Map<String, String> form = material.getForm(); | |||
| if (material.getForm() != null) { | |||
| request.form("description", WxGsonBuilder.create().toJson(form)); | |||
| } | |||
| HttpResponse response = request.send(); | |||
| String responseContent = response.bodyText(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return WxMpMaterialUploadResult.fromJson(responseContent); | |||
| } | |||
| } | |||
| @Override | |||
| public WxMpMaterialUploadResult executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, WxMpMaterial material) throws WxErrorException, IOException { | |||
| OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(pool); | |||
| //设置代理 | |||
| if (proxyInfo != null) { | |||
| clientBuilder.proxy(proxyInfo.getProxy()); | |||
| } | |||
| //设置授权 | |||
| clientBuilder.authenticator(new Authenticator() { | |||
| @Override | |||
| public Request authenticate(Route route, Response response) throws IOException { | |||
| String credential = Credentials.basic(proxyInfo.getProxyUsername(), proxyInfo.getProxyPassword()); | |||
| return response.request().newBuilder() | |||
| .header("Authorization", credential) | |||
| .build(); | |||
| } | |||
| }); | |||
| //得到httpClient | |||
| OkHttpClient client = clientBuilder.build(); | |||
| if (material == null) { | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法请求,material参数为空").build()); | |||
| } | |||
| File file = material.getFile(); | |||
| if (file == null || !file.exists()) { | |||
| throw new FileNotFoundException(); | |||
| } | |||
| RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), file); | |||
| MultipartBody.Builder bodyBuilder = new MultipartBody.Builder().addFormDataPart("media", null, fileBody); | |||
| Map<String, String> form = material.getForm(); | |||
| if (material.getForm() != null) { | |||
| bodyBuilder.addFormDataPart("description", WxGsonBuilder.create().toJson(form)); | |||
| } | |||
| RequestBody body =bodyBuilder.build(); | |||
| Request request = new Request.Builder().url(uri).post(body).build(); | |||
| Response response = client.newCall(request).execute(); | |||
| String responseContent = response.body().string(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return WxMpMaterialUploadResult.fromJson(responseContent); | |||
| } | |||
| public abstract class MaterialUploadRequestExecutor<H,P> implements RequestExecutor<WxMpMaterialUploadResult, WxMpMaterial> { | |||
| protected RequestHttp<H,P> requestHttp; | |||
| public MaterialUploadRequestExecutor(RequestHttp requestHttp){ | |||
| this.requestHttp =requestHttp; | |||
| } | |||
| @Override | |||
| public WxMpMaterialUploadResult executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, | |||
| WxMpMaterial material) throws WxErrorException, IOException { | |||
| HttpPost httpPost = new HttpPost(uri); | |||
| if (httpProxy != null) { | |||
| RequestConfig response = RequestConfig.custom().setProxy(httpProxy).build(); | |||
| httpPost.setConfig(response); | |||
| } | |||
| if (material == null) { | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法请求,material参数为空").build()); | |||
| } | |||
| File file = material.getFile(); | |||
| if (file == null || !file.exists()) { | |||
| throw new FileNotFoundException(); | |||
| } | |||
| MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); | |||
| multipartEntityBuilder | |||
| .addBinaryBody("media", file) | |||
| .setMode(HttpMultipartMode.RFC6532); | |||
| Map<String, String> form = material.getForm(); | |||
| if (material.getForm() != null) { | |||
| multipartEntityBuilder.addTextBody("description", WxGsonBuilder.create().toJson(form)); | |||
| } | |||
| httpPost.setEntity(multipartEntityBuilder.build()); | |||
| httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString()); | |||
| try (CloseableHttpResponse response = httpclient.execute(httpPost)) { | |||
| String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return WxMpMaterialUploadResult.fromJson(responseContent); | |||
| } | |||
| } finally { | |||
| httpPost.releaseConnection(); | |||
| public static RequestExecutor<WxMpMaterialUploadResult, WxMpMaterial> create(RequestHttp requestHttp){ | |||
| switch (requestHttp.getRequestType()){ | |||
| case apacheHttp: | |||
| return new ApacheMaterialUploadRequestExecutor(requestHttp); | |||
| case joddHttp: | |||
| return new JoddMaterialUploadRequestExecutor(requestHttp); | |||
| case okHttp: | |||
| return new OkhttpMaterialUploadRequestExecutor(requestHttp); | |||
| default: | |||
| return null; | |||
| } | |||
| } | |||
| @@ -6,7 +6,6 @@ import jodd.http.HttpResponse; | |||
| import jodd.http.ProxyInfo; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.AbstractRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; | |||
| @@ -14,8 +13,10 @@ import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; | |||
| import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo; | |||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
| import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult; | |||
| import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult; | |||
| import me.chanjar.weixin.mp.util.http.apache.ApacheMaterialVideoInfoRequestExecutor; | |||
| import me.chanjar.weixin.mp.util.http.jodd.JoddMaterialVideoInfoRequestExecutor; | |||
| import me.chanjar.weixin.mp.util.http.okhttp.OkhttpMaterialVideoInfoRequestExecutor; | |||
| import okhttp3.*; | |||
| import org.apache.http.HttpHost; | |||
| @@ -29,87 +30,23 @@ import java.io.IOException; | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
| public class MaterialVideoInfoRequestExecutor extends AbstractRequestExecutor<WxMpMaterialVideoInfoResult, String> { | |||
| public MaterialVideoInfoRequestExecutor() { | |||
| super(); | |||
| } | |||
| @Override | |||
| public WxMpMaterialVideoInfoResult executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String materialId) throws WxErrorException, IOException { | |||
| HttpRequest request = HttpRequest.post(uri); | |||
| if (proxyInfo != null) { | |||
| provider.useProxy(proxyInfo); | |||
| } | |||
| request.withConnectionProvider(provider); | |||
| request.query("media_id", materialId); | |||
| HttpResponse response = request.send(); | |||
| String responseContent = response.bodyText(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return WxMpMaterialVideoInfoResult.fromJson(responseContent); | |||
| } | |||
| } | |||
| @Override | |||
| public WxMpMaterialVideoInfoResult executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, String materialId) throws WxErrorException, IOException { | |||
| OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(pool); | |||
| //设置代理 | |||
| if (proxyInfo != null) { | |||
| clientBuilder.proxy(proxyInfo.getProxy()); | |||
| } | |||
| //设置授权 | |||
| clientBuilder.authenticator(new Authenticator() { | |||
| @Override | |||
| public Request authenticate(Route route, Response response) throws IOException { | |||
| String credential = Credentials.basic(proxyInfo.getProxyUsername(), proxyInfo.getProxyPassword()); | |||
| return response.request().newBuilder() | |||
| .header("Authorization", credential) | |||
| .build(); | |||
| } | |||
| }); | |||
| //得到httpClient | |||
| OkHttpClient client = clientBuilder.build(); | |||
| RequestBody requestBody =new FormBody.Builder().add("media_id", materialId).build(); | |||
| Request request = new Request.Builder().url(uri).post(requestBody).build(); | |||
| Response response = client.newCall(request).execute(); | |||
| String responseContent = response.body().string(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return WxMpMaterialVideoInfoResult.fromJson(responseContent); | |||
| } | |||
| public abstract class MaterialVideoInfoRequestExecutor<H,P> implements RequestExecutor<WxMpMaterialVideoInfoResult, String> { | |||
| protected RequestHttp<H,P> requestHttp; | |||
| public MaterialVideoInfoRequestExecutor(RequestHttp requestHttp){ | |||
| this.requestHttp =requestHttp; | |||
| } | |||
| @Override | |||
| public WxMpMaterialVideoInfoResult executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, | |||
| String materialId) throws WxErrorException, IOException { | |||
| HttpPost httpPost = new HttpPost(uri); | |||
| if (httpProxy != null) { | |||
| RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); | |||
| httpPost.setConfig(config); | |||
| } | |||
| Map<String, String> params = new HashMap<>(); | |||
| params.put("media_id", materialId); | |||
| httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); | |||
| try (CloseableHttpResponse response = httpclient.execute(httpPost)) { | |||
| String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return WxMpMaterialVideoInfoResult.fromJson(responseContent); | |||
| } | |||
| } finally { | |||
| httpPost.releaseConnection(); | |||
| public static RequestExecutor<WxMpMaterialVideoInfoResult, String> create(RequestHttp requestHttp){ | |||
| switch (requestHttp.getRequestType()){ | |||
| case apacheHttp: | |||
| return new ApacheMaterialVideoInfoRequestExecutor(requestHttp); | |||
| case joddHttp: | |||
| return new JoddMaterialVideoInfoRequestExecutor(requestHttp); | |||
| case okHttp: | |||
| return new OkhttpMaterialVideoInfoRequestExecutor(requestHttp); | |||
| default: | |||
| return null; | |||
| } | |||
| } | |||
| } | |||
| @@ -7,13 +7,14 @@ import jodd.http.ProxyInfo; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.AbstractRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler; | |||
| import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo; | |||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
| import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult; | |||
| import me.chanjar.weixin.mp.util.http.apache.ApacheMaterialVoiceAndImageDownloadRequestExecutor; | |||
| import me.chanjar.weixin.mp.util.http.jodd.JoddMaterialVoiceAndImageDownloadRequestExecutor; | |||
| import me.chanjar.weixin.mp.util.http.okhttp.OkhttpMaterialVoiceAndImageDownloadRequestExecutor; | |||
| import okhttp3.*; | |||
| import org.apache.commons.io.IOUtils; | |||
| @@ -31,119 +32,27 @@ import java.io.InputStream; | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
| public class MaterialVoiceAndImageDownloadRequestExecutor extends AbstractRequestExecutor<InputStream, String> { | |||
| public abstract class MaterialVoiceAndImageDownloadRequestExecutor<H, P> implements RequestExecutor<InputStream, String> { | |||
| protected RequestHttp<H, P> requestHttp; | |||
| protected File tmpDirFile; | |||
| public MaterialVoiceAndImageDownloadRequestExecutor() { | |||
| super(); | |||
| } | |||
| public MaterialVoiceAndImageDownloadRequestExecutor(File tmpDirFile) { | |||
| super(); | |||
| } | |||
| @Override | |||
| public InputStream executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String materialId) throws WxErrorException, IOException { | |||
| HttpRequest request = HttpRequest.post(uri); | |||
| if (proxyInfo != null) { | |||
| provider.useProxy(proxyInfo); | |||
| } | |||
| request.withConnectionProvider(provider); | |||
| request.query("media_id", materialId); | |||
| HttpResponse response = request.send(); | |||
| try (InputStream inputStream = new ByteArrayInputStream(response.bodyBytes())) { | |||
| // 下载媒体文件出错 | |||
| byte[] responseContent = IOUtils.toByteArray(inputStream); | |||
| String responseContentString = new String(responseContent, "UTF-8"); | |||
| if (responseContentString.length() < 100) { | |||
| try { | |||
| WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class); | |||
| if (wxError.getErrorCode() != 0) { | |||
| throw new WxErrorException(wxError); | |||
| } | |||
| } catch (com.google.gson.JsonSyntaxException ex) { | |||
| return new ByteArrayInputStream(responseContent); | |||
| } | |||
| } | |||
| return new ByteArrayInputStream(responseContent); | |||
| } | |||
| public MaterialVoiceAndImageDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) { | |||
| this.requestHttp = requestHttp; | |||
| this.tmpDirFile = tmpDirFile; | |||
| } | |||
| @Override | |||
| public InputStream executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, String materialId) throws WxErrorException, IOException { | |||
| OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(pool); | |||
| //设置代理 | |||
| if (proxyInfo != null) { | |||
| clientBuilder.proxy(proxyInfo.getProxy()); | |||
| } | |||
| //设置授权 | |||
| clientBuilder.authenticator(new Authenticator() { | |||
| @Override | |||
| public Request authenticate(Route route, Response response) throws IOException { | |||
| String credential = Credentials.basic(proxyInfo.getProxyUsername(), proxyInfo.getProxyPassword()); | |||
| return response.request().newBuilder() | |||
| .header("Authorization", credential) | |||
| .build(); | |||
| } | |||
| }); | |||
| //得到httpClient | |||
| OkHttpClient client = clientBuilder.build(); | |||
| RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build(); | |||
| Request request = new Request.Builder().url(uri).get().post(requestBody).build(); | |||
| Response response = client.newCall(request).execute(); | |||
| try (InputStream inputStream = new ByteArrayInputStream(response.body().bytes())) { | |||
| // 下载媒体文件出错 | |||
| byte[] responseContent = IOUtils.toByteArray(inputStream); | |||
| String responseContentString = new String(responseContent, "UTF-8"); | |||
| if (responseContentString.length() < 100) { | |||
| try { | |||
| WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class); | |||
| if (wxError.getErrorCode() != 0) { | |||
| throw new WxErrorException(wxError); | |||
| } | |||
| } catch (com.google.gson.JsonSyntaxException ex) { | |||
| return new ByteArrayInputStream(responseContent); | |||
| } | |||
| } | |||
| return new ByteArrayInputStream(responseContent); | |||
| } | |||
| } | |||
| @Override | |||
| public InputStream executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, | |||
| String materialId) throws WxErrorException, IOException { | |||
| HttpPost httpPost = new HttpPost(uri); | |||
| if (httpProxy != null) { | |||
| RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); | |||
| httpPost.setConfig(config); | |||
| } | |||
| Map<String, String> params = new HashMap<>(); | |||
| params.put("media_id", materialId); | |||
| httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); | |||
| try (CloseableHttpResponse response = httpclient.execute(httpPost); | |||
| InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);) { | |||
| // 下载媒体文件出错 | |||
| byte[] responseContent = IOUtils.toByteArray(inputStream); | |||
| String responseContentString = new String(responseContent, "UTF-8"); | |||
| if (responseContentString.length() < 100) { | |||
| try { | |||
| WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class); | |||
| if (wxError.getErrorCode() != 0) { | |||
| throw new WxErrorException(wxError); | |||
| } | |||
| } catch (com.google.gson.JsonSyntaxException ex) { | |||
| return new ByteArrayInputStream(responseContent); | |||
| } | |||
| } | |||
| return new ByteArrayInputStream(responseContent); | |||
| } finally { | |||
| httpPost.releaseConnection(); | |||
| public static RequestExecutor<InputStream, String> create(RequestHttp requestHttp, File tmpDirFile) { | |||
| switch (requestHttp.getRequestType()) { | |||
| case apacheHttp: | |||
| return new ApacheMaterialVoiceAndImageDownloadRequestExecutor(requestHttp, tmpDirFile); | |||
| case joddHttp: | |||
| return new JoddMaterialVoiceAndImageDownloadRequestExecutor(requestHttp, tmpDirFile); | |||
| case okHttp: | |||
| return new OkhttpMaterialVoiceAndImageDownloadRequestExecutor(requestHttp, tmpDirFile); | |||
| default: | |||
| return null; | |||
| } | |||
| } | |||
| @@ -5,13 +5,8 @@ import jodd.http.HttpRequest; | |||
| import jodd.http.HttpResponse; | |||
| import jodd.http.ProxyInfo; | |||
| import jodd.util.MimeTypes; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.fs.FileUtils; | |||
| import me.chanjar.weixin.common.util.http.AbstractRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; | |||
| @@ -19,6 +14,9 @@ import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; | |||
| import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo; | |||
| import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult; | |||
| import me.chanjar.weixin.mp.util.http.apache.ApacheMediaImgUploadRequestExecutor; | |||
| import me.chanjar.weixin.mp.util.http.jodd.JoddMediaImgUploadRequestExecutor; | |||
| import me.chanjar.weixin.mp.util.http.okhttp.OkhttpMediaImgUploadRequestExecutor; | |||
| import okhttp3.*; | |||
| import org.apache.http.HttpEntity; | |||
| @@ -31,104 +29,28 @@ import org.apache.http.entity.mime.HttpMultipartMode; | |||
| import org.apache.http.entity.mime.MultipartEntityBuilder; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import java.io.ByteArrayInputStream; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.util.UUID; | |||
| /** | |||
| * @author miller | |||
| */ | |||
| public class MediaImgUploadRequestExecutor extends AbstractRequestExecutor<WxMediaImgUploadResult, File> { | |||
| @Override | |||
| public WxMediaImgUploadResult executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, File data) throws WxErrorException, IOException { | |||
| if (data == null) { | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("文件对象为空").build()); | |||
| } | |||
| HttpRequest request = HttpRequest.post(uri); | |||
| if (proxyInfo != null) { | |||
| provider.useProxy(proxyInfo); | |||
| } | |||
| request.withConnectionProvider(provider); | |||
| request.form("media", data); | |||
| HttpResponse response = request.send(); | |||
| String responseContent = response.bodyText(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return WxMediaImgUploadResult.fromJson(responseContent); | |||
| } | |||
| @Override | |||
| public WxMediaImgUploadResult executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, File data) throws WxErrorException, IOException { | |||
| OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(pool); | |||
| //设置代理 | |||
| if (proxyInfo != null) { | |||
| clientBuilder.proxy(proxyInfo.getProxy()); | |||
| } | |||
| //设置授权 | |||
| clientBuilder.authenticator(new Authenticator() { | |||
| @Override | |||
| public Request authenticate(Route route, Response response) throws IOException { | |||
| String credential = Credentials.basic(proxyInfo.getProxyUsername(), proxyInfo.getProxyPassword()); | |||
| return response.request().newBuilder() | |||
| .header("Authorization", credential) | |||
| .build(); | |||
| } | |||
| }); | |||
| //得到httpClient | |||
| OkHttpClient client = clientBuilder.build(); | |||
| RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), data); | |||
| RequestBody body = new MultipartBody.Builder().addFormDataPart("media", null, fileBody).build(); | |||
| Request request = new Request.Builder().url(uri).post(body).build(); | |||
| Response response = client.newCall(request).execute(); | |||
| String responseContent = response.body().string(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return WxMediaImgUploadResult.fromJson(responseContent); | |||
| public abstract class MediaImgUploadRequestExecutor<H,P> implements RequestExecutor<WxMediaImgUploadResult, File> { | |||
| protected RequestHttp<H,P> requestHttp; | |||
| public MediaImgUploadRequestExecutor(RequestHttp requestHttp){ | |||
| this.requestHttp =requestHttp; | |||
| } | |||
| @Override | |||
| public WxMediaImgUploadResult executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, | |||
| File data) throws WxErrorException, IOException { | |||
| if (data == null) { | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("文件对象为空").build()); | |||
| } | |||
| HttpPost httpPost = new HttpPost(uri); | |||
| if (httpProxy != null) { | |||
| RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); | |||
| httpPost.setConfig(config); | |||
| } | |||
| HttpEntity entity = MultipartEntityBuilder | |||
| .create() | |||
| .addBinaryBody("media", data) | |||
| .setMode(HttpMultipartMode.RFC6532) | |||
| .build(); | |||
| httpPost.setEntity(entity); | |||
| httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString()); | |||
| try (CloseableHttpResponse response = httpclient.execute(httpPost)) { | |||
| String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return WxMediaImgUploadResult.fromJson(responseContent); | |||
| public static RequestExecutor<WxMediaImgUploadResult, File> create(RequestHttp requestHttp){ | |||
| switch (requestHttp.getRequestType()){ | |||
| case apacheHttp: | |||
| return new ApacheMediaImgUploadRequestExecutor(requestHttp); | |||
| case joddHttp: | |||
| return new JoddMediaImgUploadRequestExecutor(requestHttp); | |||
| case okHttp: | |||
| return new OkhttpMediaImgUploadRequestExecutor(requestHttp); | |||
| default: | |||
| return null; | |||
| } | |||
| } | |||
| @@ -9,13 +9,16 @@ import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.fs.FileUtils; | |||
| import me.chanjar.weixin.common.util.http.AbstractRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler; | |||
| import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; | |||
| import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket; | |||
| import me.chanjar.weixin.mp.util.http.apache.ApacheQrCodeRequestExecutor; | |||
| import me.chanjar.weixin.mp.util.http.jodd.JoddQrCodeRequestExecutor; | |||
| import me.chanjar.weixin.mp.util.http.okhttp.OkhttpQrCodeRequestExecutor; | |||
| import okhttp3.*; | |||
| import org.apache.http.Header; | |||
| @@ -38,102 +41,25 @@ import java.util.UUID; | |||
| * | |||
| * @author chanjarster | |||
| */ | |||
| public class QrCodeRequestExecutor extends AbstractRequestExecutor<File, WxMpQrCodeTicket> { | |||
| public abstract class QrCodeRequestExecutor<H, P> implements RequestExecutor<File,WxMpQrCodeTicket> { | |||
| protected RequestHttp<H, P> requestHttp; | |||
| @Override | |||
| public File executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, WxMpQrCodeTicket ticket) throws WxErrorException, IOException { | |||
| if (ticket != null) { | |||
| if (uri.indexOf('?') == -1) { | |||
| uri += '?'; | |||
| } | |||
| uri += uri.endsWith("?") | |||
| ? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8") | |||
| : "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8"); | |||
| } | |||
| HttpRequest request = HttpRequest.get(uri); | |||
| if (proxyInfo != null) { | |||
| provider.useProxy(proxyInfo); | |||
| } | |||
| request.withConnectionProvider(provider); | |||
| HttpResponse response = request.send(); | |||
| String contentTypeHeader = response.header("Content-Type"); | |||
| if (MimeTypes.MIME_TEXT_PLAIN.equals(contentTypeHeader)) { | |||
| String responseContent = response.bodyText(); | |||
| throw new WxErrorException(WxError.fromJson(responseContent)); | |||
| } | |||
| try (InputStream inputStream = new ByteArrayInputStream(response.bodyBytes())) { | |||
| return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg"); | |||
| } | |||
| } | |||
| @Override | |||
| public File executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, WxMpQrCodeTicket data) throws WxErrorException, IOException { | |||
| OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(pool); | |||
| //设置代理 | |||
| if (proxyInfo != null) { | |||
| clientBuilder.proxy(proxyInfo.getProxy()); | |||
| } | |||
| //设置授权 | |||
| clientBuilder.authenticator(new Authenticator() { | |||
| @Override | |||
| public Request authenticate(Route route, Response response) throws IOException { | |||
| String credential = Credentials.basic(proxyInfo.getProxyUsername(), proxyInfo.getProxyPassword()); | |||
| return response.request().newBuilder() | |||
| .header("Authorization", credential) | |||
| .build(); | |||
| } | |||
| }); | |||
| //得到httpClient | |||
| OkHttpClient client = clientBuilder.build(); | |||
| Request request = new Request.Builder().url(uri).get().build(); | |||
| Response response = client.newCall(request).execute(); | |||
| String contentTypeHeader = response.header("Content-Type"); | |||
| if (MimeTypes.MIME_TEXT_PLAIN.equals(contentTypeHeader)) { | |||
| String responseContent = response.body().string(); | |||
| throw new WxErrorException(WxError.fromJson(responseContent)); | |||
| } | |||
| try (InputStream inputStream = new ByteArrayInputStream(response.body().bytes())) { | |||
| return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg"); | |||
| } | |||
| public QrCodeRequestExecutor(RequestHttp requestHttp){ | |||
| this.requestHttp =requestHttp; | |||
| } | |||
| @Override | |||
| public File executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, | |||
| WxMpQrCodeTicket ticket) throws WxErrorException, IOException { | |||
| if (ticket != null) { | |||
| if (uri.indexOf('?') == -1) { | |||
| uri += '?'; | |||
| } | |||
| uri += uri.endsWith("?") | |||
| ? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8") | |||
| : "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8"); | |||
| } | |||
| HttpGet httpGet = new HttpGet(uri); | |||
| if (httpProxy != null) { | |||
| RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); | |||
| httpGet.setConfig(config); | |||
| } | |||
| try (CloseableHttpResponse response = httpclient.execute(httpGet); | |||
| InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);) { | |||
| Header[] contentTypeHeader = response.getHeaders("Content-Type"); | |||
| if (contentTypeHeader != null && contentTypeHeader.length > 0) { | |||
| // 出错 | |||
| if (ContentType.TEXT_PLAIN.getMimeType().equals(contentTypeHeader[0].getValue())) { | |||
| String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
| throw new WxErrorException(WxError.fromJson(responseContent)); | |||
| } | |||
| } | |||
| return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg"); | |||
| } finally { | |||
| httpGet.releaseConnection(); | |||
| public static RequestExecutor<File,WxMpQrCodeTicket> create(RequestHttp requestHttp){ | |||
| switch (requestHttp.getRequestType()){ | |||
| case apacheHttp: | |||
| return new ApacheQrCodeRequestExecutor(requestHttp); | |||
| case joddHttp: | |||
| return new JoddQrCodeRequestExecutor(requestHttp); | |||
| case okHttp: | |||
| return new OkhttpQrCodeRequestExecutor(requestHttp); | |||
| default: | |||
| //TODO 需要优化,最好抛出异常 | |||
| return null; | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,52 @@ | |||
| package me.chanjar.weixin.mp.util.http.apache; | |||
| import java.io.IOException; | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
| import org.apache.http.HttpHost; | |||
| import org.apache.http.client.config.RequestConfig; | |||
| import org.apache.http.client.methods.CloseableHttpResponse; | |||
| import org.apache.http.client.methods.HttpPost; | |||
| import org.apache.http.entity.StringEntity; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; | |||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
| import me.chanjar.weixin.mp.util.http.MaterialDeleteRequestExecutor; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class ApacheMaterialDeleteRequestExecutor extends MaterialDeleteRequestExecutor<CloseableHttpClient,HttpHost> { | |||
| public ApacheMaterialDeleteRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public Boolean execute(String uri, String materialId) throws WxErrorException, IOException { | |||
| HttpPost httpPost = new HttpPost(uri); | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build(); | |||
| httpPost.setConfig(config); | |||
| } | |||
| Map<String, String> params = new HashMap<>(); | |||
| params.put("media_id", materialId); | |||
| httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); | |||
| try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { | |||
| String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return true; | |||
| } | |||
| } finally { | |||
| httpPost.releaseConnection(); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,54 @@ | |||
| package me.chanjar.weixin.mp.util.http.apache; | |||
| import java.io.IOException; | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
| import org.apache.http.HttpHost; | |||
| import org.apache.http.client.config.RequestConfig; | |||
| import org.apache.http.client.methods.CloseableHttpResponse; | |||
| import org.apache.http.client.methods.HttpPost; | |||
| import org.apache.http.entity.StringEntity; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; | |||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
| import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews; | |||
| import me.chanjar.weixin.mp.util.http.MaterialNewsInfoRequestExecutor; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class ApacheMaterialNewsInfoRequestExecutor extends MaterialNewsInfoRequestExecutor<CloseableHttpClient,HttpHost> { | |||
| public ApacheMaterialNewsInfoRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public WxMpMaterialNews execute(String uri, String materialId) throws WxErrorException, IOException { | |||
| HttpPost httpPost = new HttpPost(uri); | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build(); | |||
| httpPost.setConfig(config); | |||
| } | |||
| Map<String, String> params = new HashMap<>(); | |||
| params.put("media_id", materialId); | |||
| httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); | |||
| try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { | |||
| String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class); | |||
| } | |||
| } finally { | |||
| httpPost.releaseConnection(); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,75 @@ | |||
| package me.chanjar.weixin.mp.util.http.apache; | |||
| import java.io.File; | |||
| import java.io.FileNotFoundException; | |||
| import java.io.IOException; | |||
| import java.util.Map; | |||
| import org.apache.http.HttpHost; | |||
| import org.apache.http.client.config.RequestConfig; | |||
| import org.apache.http.client.methods.CloseableHttpResponse; | |||
| import org.apache.http.client.methods.HttpPost; | |||
| import org.apache.http.entity.ContentType; | |||
| import org.apache.http.entity.mime.HttpMultipartMode; | |||
| import org.apache.http.entity.mime.MultipartEntityBuilder; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; | |||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
| import me.chanjar.weixin.mp.bean.material.WxMpMaterial; | |||
| import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult; | |||
| import me.chanjar.weixin.mp.util.http.MaterialUploadRequestExecutor; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class ApacheMaterialUploadRequestExecutor extends MaterialUploadRequestExecutor<CloseableHttpClient,HttpHost> { | |||
| public ApacheMaterialUploadRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public WxMpMaterialUploadResult execute(String uri, WxMpMaterial material) throws WxErrorException, IOException { | |||
| HttpPost httpPost = new HttpPost(uri); | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| RequestConfig response = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build(); | |||
| httpPost.setConfig(response); | |||
| } | |||
| if (material == null) { | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法请求,material参数为空").build()); | |||
| } | |||
| File file = material.getFile(); | |||
| if (file == null || !file.exists()) { | |||
| throw new FileNotFoundException(); | |||
| } | |||
| MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); | |||
| multipartEntityBuilder | |||
| .addBinaryBody("media", file) | |||
| .setMode(HttpMultipartMode.RFC6532); | |||
| Map<String, String> form = material.getForm(); | |||
| if (material.getForm() != null) { | |||
| multipartEntityBuilder.addTextBody("description", WxGsonBuilder.create().toJson(form)); | |||
| } | |||
| httpPost.setEntity(multipartEntityBuilder.build()); | |||
| httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString()); | |||
| try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { | |||
| String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return WxMpMaterialUploadResult.fromJson(responseContent); | |||
| } | |||
| } finally { | |||
| httpPost.releaseConnection(); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,53 @@ | |||
| package me.chanjar.weixin.mp.util.http.apache; | |||
| import java.io.IOException; | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
| import org.apache.http.HttpHost; | |||
| import org.apache.http.client.config.RequestConfig; | |||
| import org.apache.http.client.methods.CloseableHttpResponse; | |||
| import org.apache.http.client.methods.HttpPost; | |||
| import org.apache.http.entity.StringEntity; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; | |||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
| import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult; | |||
| import me.chanjar.weixin.mp.util.http.MaterialVideoInfoRequestExecutor; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class ApacheMaterialVideoInfoRequestExecutor extends MaterialVideoInfoRequestExecutor<CloseableHttpClient,HttpHost> { | |||
| public ApacheMaterialVideoInfoRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public WxMpMaterialVideoInfoResult execute(String uri, String materialId) throws WxErrorException, IOException { | |||
| HttpPost httpPost = new HttpPost(uri); | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build(); | |||
| httpPost.setConfig(config); | |||
| } | |||
| Map<String, String> params = new HashMap<>(); | |||
| params.put("media_id", materialId); | |||
| httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); | |||
| try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { | |||
| String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return WxMpMaterialVideoInfoResult.fromJson(responseContent); | |||
| } | |||
| } finally { | |||
| httpPost.releaseConnection(); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,64 @@ | |||
| package me.chanjar.weixin.mp.util.http.apache; | |||
| import java.io.ByteArrayInputStream; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
| import org.apache.commons.io.IOUtils; | |||
| import org.apache.http.HttpHost; | |||
| import org.apache.http.client.config.RequestConfig; | |||
| import org.apache.http.client.methods.CloseableHttpResponse; | |||
| import org.apache.http.client.methods.HttpPost; | |||
| import org.apache.http.entity.StringEntity; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler; | |||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
| import me.chanjar.weixin.mp.util.http.MaterialVoiceAndImageDownloadRequestExecutor; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class ApacheMaterialVoiceAndImageDownloadRequestExecutor extends MaterialVoiceAndImageDownloadRequestExecutor<CloseableHttpClient,HttpHost> { | |||
| public ApacheMaterialVoiceAndImageDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) { | |||
| super(requestHttp, tmpDirFile); | |||
| } | |||
| @Override | |||
| public InputStream execute(String uri, String materialId) throws WxErrorException, IOException { | |||
| HttpPost httpPost = new HttpPost(uri); | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build(); | |||
| httpPost.setConfig(config); | |||
| } | |||
| Map<String, String> params = new HashMap<>(); | |||
| params.put("media_id", materialId); | |||
| httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); | |||
| try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost); | |||
| InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);) { | |||
| // 下载媒体文件出错 | |||
| byte[] responseContent = IOUtils.toByteArray(inputStream); | |||
| String responseContentString = new String(responseContent, "UTF-8"); | |||
| if (responseContentString.length() < 100) { | |||
| try { | |||
| WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class); | |||
| if (wxError.getErrorCode() != 0) { | |||
| throw new WxErrorException(wxError); | |||
| } | |||
| } catch (com.google.gson.JsonSyntaxException ex) { | |||
| return new ByteArrayInputStream(responseContent); | |||
| } | |||
| } | |||
| return new ByteArrayInputStream(responseContent); | |||
| } finally { | |||
| httpPost.releaseConnection(); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,61 @@ | |||
| package me.chanjar.weixin.mp.util.http.apache; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import org.apache.http.HttpEntity; | |||
| import org.apache.http.HttpHost; | |||
| import org.apache.http.client.config.RequestConfig; | |||
| import org.apache.http.client.methods.CloseableHttpResponse; | |||
| import org.apache.http.client.methods.HttpPost; | |||
| import org.apache.http.entity.ContentType; | |||
| import org.apache.http.entity.mime.HttpMultipartMode; | |||
| import org.apache.http.entity.mime.MultipartEntityBuilder; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; | |||
| import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult; | |||
| import me.chanjar.weixin.mp.util.http.MediaImgUploadRequestExecutor; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class ApacheMediaImgUploadRequestExecutor extends MediaImgUploadRequestExecutor<CloseableHttpClient,HttpHost> { | |||
| public ApacheMediaImgUploadRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public WxMediaImgUploadResult execute(String uri, File data) throws WxErrorException, IOException { | |||
| if (data == null) { | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("文件对象为空").build()); | |||
| } | |||
| HttpPost httpPost = new HttpPost(uri); | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build(); | |||
| httpPost.setConfig(config); | |||
| } | |||
| HttpEntity entity = MultipartEntityBuilder | |||
| .create() | |||
| .addBinaryBody("media", data) | |||
| .setMode(HttpMultipartMode.RFC6532) | |||
| .build(); | |||
| httpPost.setEntity(entity); | |||
| httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString()); | |||
| try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { | |||
| String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return WxMediaImgUploadResult.fromJson(responseContent); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,66 @@ | |||
| package me.chanjar.weixin.mp.util.http.apache; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.net.URLEncoder; | |||
| import java.util.UUID; | |||
| import org.apache.http.Header; | |||
| import org.apache.http.HttpHost; | |||
| import org.apache.http.client.config.RequestConfig; | |||
| import org.apache.http.client.methods.CloseableHttpResponse; | |||
| import org.apache.http.client.methods.HttpGet; | |||
| import org.apache.http.entity.ContentType; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.fs.FileUtils; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler; | |||
| import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket; | |||
| import me.chanjar.weixin.mp.util.http.QrCodeRequestExecutor; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class ApacheQrCodeRequestExecutor extends QrCodeRequestExecutor<CloseableHttpClient,HttpHost> { | |||
| public ApacheQrCodeRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public File execute(String uri, WxMpQrCodeTicket ticket) throws WxErrorException, IOException { | |||
| if (ticket != null) { | |||
| if (uri.indexOf('?') == -1) { | |||
| uri += '?'; | |||
| } | |||
| uri += uri.endsWith("?") | |||
| ? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8") | |||
| : "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8"); | |||
| } | |||
| HttpGet httpGet = new HttpGet(uri); | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build(); | |||
| httpGet.setConfig(config); | |||
| } | |||
| try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpGet); | |||
| InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);) { | |||
| Header[] contentTypeHeader = response.getHeaders("Content-Type"); | |||
| if (contentTypeHeader != null && contentTypeHeader.length > 0) { | |||
| // 出错 | |||
| if (ContentType.TEXT_PLAIN.getMimeType().equals(contentTypeHeader[0].getValue())) { | |||
| String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
| throw new WxErrorException(WxError.fromJson(responseContent)); | |||
| } | |||
| } | |||
| return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg"); | |||
| } finally { | |||
| httpGet.releaseConnection(); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,40 @@ | |||
| package me.chanjar.weixin.mp.util.http.jodd; | |||
| import java.io.IOException; | |||
| import jodd.http.HttpConnectionProvider; | |||
| import jodd.http.HttpRequest; | |||
| import jodd.http.HttpResponse; | |||
| import jodd.http.ProxyInfo; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.mp.util.http.MaterialDeleteRequestExecutor; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class JoddMaterialDeleteRequestExecutor extends MaterialDeleteRequestExecutor<HttpConnectionProvider,ProxyInfo> { | |||
| public JoddMaterialDeleteRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public Boolean execute(String uri, String materialId) throws WxErrorException, IOException { | |||
| HttpRequest request = HttpRequest.post(uri); | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy()); | |||
| } | |||
| request.withConnectionProvider(requestHttp.getRequestHttpClient()); | |||
| request.query("media_id", materialId); | |||
| HttpResponse response = request.send(); | |||
| String responseContent = response.bodyText(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return true; | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,43 @@ | |||
| package me.chanjar.weixin.mp.util.http.jodd; | |||
| import java.io.IOException; | |||
| import jodd.http.HttpConnectionProvider; | |||
| import jodd.http.HttpRequest; | |||
| import jodd.http.HttpResponse; | |||
| import jodd.http.ProxyInfo; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews; | |||
| import me.chanjar.weixin.mp.util.http.MaterialNewsInfoRequestExecutor; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class JoddMaterialNewsInfoRequestExecutor extends MaterialNewsInfoRequestExecutor<HttpConnectionProvider,ProxyInfo> { | |||
| public JoddMaterialNewsInfoRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public WxMpMaterialNews execute(String uri, String materialId) throws WxErrorException, IOException { | |||
| HttpRequest request = HttpRequest.post(uri); | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy()); | |||
| } | |||
| request.withConnectionProvider(requestHttp.getRequestHttpClient()); | |||
| request.query("media_id", materialId); | |||
| HttpResponse response = request.send(); | |||
| String responseContent = response.bodyText(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,59 @@ | |||
| package me.chanjar.weixin.mp.util.http.jodd; | |||
| import java.io.File; | |||
| import java.io.FileNotFoundException; | |||
| import java.io.IOException; | |||
| import java.util.Map; | |||
| import jodd.http.HttpConnectionProvider; | |||
| import jodd.http.HttpRequest; | |||
| import jodd.http.HttpResponse; | |||
| import jodd.http.ProxyInfo; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
| import me.chanjar.weixin.mp.bean.material.WxMpMaterial; | |||
| import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult; | |||
| import me.chanjar.weixin.mp.util.http.MaterialUploadRequestExecutor; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class JoddMaterialUploadRequestExecutor extends MaterialUploadRequestExecutor<HttpConnectionProvider,ProxyInfo> { | |||
| public JoddMaterialUploadRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public WxMpMaterialUploadResult execute(String uri, WxMpMaterial material) throws WxErrorException, IOException { | |||
| HttpRequest request = HttpRequest.post(uri); | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy()); | |||
| } | |||
| request.withConnectionProvider(requestHttp.getRequestHttpClient()); | |||
| if (material == null) { | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法请求,material参数为空").build()); | |||
| } | |||
| File file = material.getFile(); | |||
| if (file == null || !file.exists()) { | |||
| throw new FileNotFoundException(); | |||
| } | |||
| request.form("media", file); | |||
| Map<String, String> form = material.getForm(); | |||
| if (material.getForm() != null) { | |||
| request.form("description", WxGsonBuilder.create().toJson(form)); | |||
| } | |||
| HttpResponse response = request.send(); | |||
| String responseContent = response.bodyText(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return WxMpMaterialUploadResult.fromJson(responseContent); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,41 @@ | |||
| package me.chanjar.weixin.mp.util.http.jodd; | |||
| import java.io.IOException; | |||
| import jodd.http.HttpConnectionProvider; | |||
| import jodd.http.HttpRequest; | |||
| import jodd.http.HttpResponse; | |||
| import jodd.http.ProxyInfo; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult; | |||
| import me.chanjar.weixin.mp.util.http.MaterialVideoInfoRequestExecutor; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class JoddMaterialVideoInfoRequestExecutor extends MaterialVideoInfoRequestExecutor<HttpConnectionProvider,ProxyInfo> { | |||
| public JoddMaterialVideoInfoRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public WxMpMaterialVideoInfoResult execute(String uri, String materialId) throws WxErrorException, IOException { | |||
| HttpRequest request = HttpRequest.post(uri); | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy()); | |||
| } | |||
| request.withConnectionProvider(requestHttp.getRequestHttpClient()); | |||
| request.query("media_id", materialId); | |||
| HttpResponse response = request.send(); | |||
| String responseContent = response.bodyText(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return WxMpMaterialVideoInfoResult.fromJson(responseContent); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,56 @@ | |||
| package me.chanjar.weixin.mp.util.http.jodd; | |||
| import java.io.ByteArrayInputStream; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import org.apache.commons.io.IOUtils; | |||
| import jodd.http.HttpConnectionProvider; | |||
| import jodd.http.HttpRequest; | |||
| import jodd.http.HttpResponse; | |||
| import jodd.http.ProxyInfo; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
| import me.chanjar.weixin.mp.util.http.MaterialVoiceAndImageDownloadRequestExecutor; | |||
| import me.chanjar.weixin.mp.util.http.okhttp.OkhttpMaterialVoiceAndImageDownloadRequestExecutor; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class JoddMaterialVoiceAndImageDownloadRequestExecutor extends MaterialVoiceAndImageDownloadRequestExecutor<HttpConnectionProvider,ProxyInfo> { | |||
| public JoddMaterialVoiceAndImageDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) { | |||
| super(requestHttp, tmpDirFile); | |||
| } | |||
| @Override | |||
| public InputStream execute(String uri, String materialId) throws WxErrorException, IOException { | |||
| HttpRequest request = HttpRequest.post(uri); | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy()); | |||
| } | |||
| request.withConnectionProvider(requestHttp.getRequestHttpClient()); | |||
| request.query("media_id", materialId); | |||
| HttpResponse response = request.send(); | |||
| try (InputStream inputStream = new ByteArrayInputStream(response.bodyBytes())) { | |||
| // 下载媒体文件出错 | |||
| byte[] responseContent = IOUtils.toByteArray(inputStream); | |||
| String responseContentString = new String(responseContent, "UTF-8"); | |||
| if (responseContentString.length() < 100) { | |||
| try { | |||
| WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class); | |||
| if (wxError.getErrorCode() != 0) { | |||
| throw new WxErrorException(wxError); | |||
| } | |||
| } catch (com.google.gson.JsonSyntaxException ex) { | |||
| return new ByteArrayInputStream(responseContent); | |||
| } | |||
| } | |||
| return new ByteArrayInputStream(responseContent); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,46 @@ | |||
| package me.chanjar.weixin.mp.util.http.jodd; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import jodd.http.HttpConnectionProvider; | |||
| import jodd.http.HttpRequest; | |||
| import jodd.http.HttpResponse; | |||
| import jodd.http.ProxyInfo; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult; | |||
| import me.chanjar.weixin.mp.util.http.MediaImgUploadRequestExecutor; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class JoddMediaImgUploadRequestExecutor extends MediaImgUploadRequestExecutor<HttpConnectionProvider,ProxyInfo> { | |||
| public JoddMediaImgUploadRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public WxMediaImgUploadResult execute(String uri, File data) throws WxErrorException, IOException { | |||
| if (data == null) { | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("文件对象为空").build()); | |||
| } | |||
| HttpRequest request = HttpRequest.post(uri); | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy()); | |||
| } | |||
| request.withConnectionProvider(requestHttp.getRequestHttpClient()); | |||
| request.form("media", data); | |||
| HttpResponse response = request.send(); | |||
| String responseContent = response.bodyText(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return WxMediaImgUploadResult.fromJson(responseContent); | |||
| } | |||
| } | |||
| @@ -0,0 +1,57 @@ | |||
| package me.chanjar.weixin.mp.util.http.jodd; | |||
| import java.io.ByteArrayInputStream; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.net.URLEncoder; | |||
| import java.util.UUID; | |||
| import jodd.http.HttpConnectionProvider; | |||
| import jodd.http.HttpRequest; | |||
| import jodd.http.HttpResponse; | |||
| import jodd.http.ProxyInfo; | |||
| import jodd.util.MimeTypes; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.fs.FileUtils; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket; | |||
| import me.chanjar.weixin.mp.util.http.QrCodeRequestExecutor; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class JoddQrCodeRequestExecutor extends QrCodeRequestExecutor<HttpConnectionProvider,ProxyInfo> { | |||
| public JoddQrCodeRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public File execute(String uri, WxMpQrCodeTicket ticket) throws WxErrorException, IOException { | |||
| if (ticket != null) { | |||
| if (uri.indexOf('?') == -1) { | |||
| uri += '?'; | |||
| } | |||
| uri += uri.endsWith("?") | |||
| ? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8") | |||
| : "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8"); | |||
| } | |||
| HttpRequest request = HttpRequest.get(uri); | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy()); | |||
| } | |||
| request.withConnectionProvider(requestHttp.getRequestHttpClient()); | |||
| HttpResponse response = request.send(); | |||
| String contentTypeHeader = response.header("Content-Type"); | |||
| if (MimeTypes.MIME_TEXT_PLAIN.equals(contentTypeHeader)) { | |||
| String responseContent = response.bodyText(); | |||
| throw new WxErrorException(WxError.fromJson(responseContent)); | |||
| } | |||
| try (InputStream inputStream = new ByteArrayInputStream(response.bodyBytes())) { | |||
| return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg"); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,54 @@ | |||
| package me.chanjar.weixin.mp.util.http.okhttp; | |||
| import java.io.IOException; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo; | |||
| import me.chanjar.weixin.mp.util.http.MaterialDeleteRequestExecutor; | |||
| import okhttp3.*; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class OkhttpMaterialDeleteRequestExecutor extends MaterialDeleteRequestExecutor<ConnectionPool, OkhttpProxyInfo> { | |||
| public OkhttpMaterialDeleteRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public Boolean execute(String uri, String materialId) throws WxErrorException, IOException { | |||
| OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(requestHttp.getRequestHttpClient()); | |||
| //设置代理 | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| clientBuilder.proxy(requestHttp.getRequestHttpProxy().getProxy()); | |||
| } | |||
| //设置授权 | |||
| clientBuilder.authenticator(new Authenticator() { | |||
| @Override | |||
| public Request authenticate(Route route, Response response) throws IOException { | |||
| String credential = Credentials.basic(requestHttp.getRequestHttpProxy().getProxyUsername(), requestHttp.getRequestHttpProxy().getProxyPassword()); | |||
| return response.request().newBuilder() | |||
| .header("Authorization", credential) | |||
| .build(); | |||
| } | |||
| }); | |||
| //得到httpClient | |||
| OkHttpClient client = clientBuilder.build(); | |||
| RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build(); | |||
| Request request = new Request.Builder().url(uri).post(requestBody).build(); | |||
| Response response = client.newCall(request).execute(); | |||
| String responseContent = response.body().string(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return true; | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,55 @@ | |||
| package me.chanjar.weixin.mp.util.http.okhttp; | |||
| import java.io.IOException; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo; | |||
| import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews; | |||
| import me.chanjar.weixin.mp.util.http.MaterialNewsInfoRequestExecutor; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| import okhttp3.*; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class OkhttpMaterialNewsInfoRequestExecutor extends MaterialNewsInfoRequestExecutor<ConnectionPool, OkhttpProxyInfo> { | |||
| public OkhttpMaterialNewsInfoRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public WxMpMaterialNews execute(String uri, String materialId) throws WxErrorException, IOException { | |||
| OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(requestHttp.getRequestHttpClient()); | |||
| //设置代理 | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| clientBuilder.proxy(requestHttp.getRequestHttpProxy().getProxy()); | |||
| } | |||
| //设置授权 | |||
| clientBuilder.authenticator(new Authenticator() { | |||
| @Override | |||
| public Request authenticate(Route route, Response response) throws IOException { | |||
| String credential = Credentials.basic(requestHttp.getRequestHttpProxy().getProxyUsername(), requestHttp.getRequestHttpProxy().getProxyPassword()); | |||
| return response.request().newBuilder() | |||
| .header("Authorization", credential) | |||
| .build(); | |||
| } | |||
| }); | |||
| //得到httpClient | |||
| OkHttpClient client = clientBuilder.build(); | |||
| RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build(); | |||
| Request request = new Request.Builder().url(uri).post(requestBody).build(); | |||
| Response response = client.newCall(request).execute(); | |||
| String responseContent = response.body().string(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,80 @@ | |||
| package me.chanjar.weixin.mp.util.http.okhttp; | |||
| import java.io.File; | |||
| import java.io.FileNotFoundException; | |||
| import java.io.IOException; | |||
| import java.util.Map; | |||
| import org.apache.http.client.config.RequestConfig; | |||
| import org.apache.http.client.methods.CloseableHttpResponse; | |||
| import org.apache.http.client.methods.HttpPost; | |||
| import org.apache.http.entity.ContentType; | |||
| import org.apache.http.entity.mime.HttpMultipartMode; | |||
| import org.apache.http.entity.mime.MultipartEntityBuilder; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; | |||
| import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo; | |||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
| import me.chanjar.weixin.mp.bean.material.WxMpMaterial; | |||
| import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult; | |||
| import me.chanjar.weixin.mp.util.http.MaterialUploadRequestExecutor; | |||
| import okhttp3.*; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class OkhttpMaterialUploadRequestExecutor extends MaterialUploadRequestExecutor<ConnectionPool, OkhttpProxyInfo> { | |||
| public OkhttpMaterialUploadRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public WxMpMaterialUploadResult execute(String uri, WxMpMaterial material) throws WxErrorException, IOException { | |||
| OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(requestHttp.getRequestHttpClient()); | |||
| //设置代理 | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| clientBuilder.proxy(requestHttp.getRequestHttpProxy().getProxy()); | |||
| } | |||
| //设置授权 | |||
| clientBuilder.authenticator(new Authenticator() { | |||
| @Override | |||
| public Request authenticate(Route route, Response response) throws IOException { | |||
| String credential = Credentials.basic(requestHttp.getRequestHttpProxy().getProxyUsername(), requestHttp.getRequestHttpProxy().getProxyPassword()); | |||
| return response.request().newBuilder() | |||
| .header("Authorization", credential) | |||
| .build(); | |||
| } | |||
| }); | |||
| //得到httpClient | |||
| OkHttpClient client = clientBuilder.build(); | |||
| if (material == null) { | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法请求,material参数为空").build()); | |||
| } | |||
| File file = material.getFile(); | |||
| if (file == null || !file.exists()) { | |||
| throw new FileNotFoundException(); | |||
| } | |||
| RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), file); | |||
| MultipartBody.Builder bodyBuilder = new MultipartBody.Builder().addFormDataPart("media", null, fileBody); | |||
| Map<String, String> form = material.getForm(); | |||
| if (material.getForm() != null) { | |||
| bodyBuilder.addFormDataPart("description", WxGsonBuilder.create().toJson(form)); | |||
| } | |||
| RequestBody body =bodyBuilder.build(); | |||
| Request request = new Request.Builder().url(uri).post(body).build(); | |||
| Response response = client.newCall(request).execute(); | |||
| String responseContent = response.body().string(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return WxMpMaterialUploadResult.fromJson(responseContent); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,52 @@ | |||
| package me.chanjar.weixin.mp.util.http.okhttp; | |||
| import java.io.IOException; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo; | |||
| import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult; | |||
| import me.chanjar.weixin.mp.util.http.MaterialVideoInfoRequestExecutor; | |||
| import okhttp3.*; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class OkhttpMaterialVideoInfoRequestExecutor extends MaterialVideoInfoRequestExecutor<ConnectionPool, OkhttpProxyInfo> { | |||
| public OkhttpMaterialVideoInfoRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public WxMpMaterialVideoInfoResult execute(String uri, String materialId) throws WxErrorException, IOException { | |||
| OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(requestHttp.getRequestHttpClient()); | |||
| //设置代理 | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| clientBuilder.proxy(requestHttp.getRequestHttpProxy().getProxy()); | |||
| } | |||
| //设置授权 | |||
| clientBuilder.authenticator(new Authenticator() { | |||
| @Override | |||
| public Request authenticate(Route route, Response response) throws IOException { | |||
| String credential = Credentials.basic(requestHttp.getRequestHttpProxy().getProxyUsername(), requestHttp.getRequestHttpProxy().getProxyPassword()); | |||
| return response.request().newBuilder() | |||
| .header("Authorization", credential) | |||
| .build(); | |||
| } | |||
| }); | |||
| //得到httpClient | |||
| OkHttpClient client = clientBuilder.build(); | |||
| RequestBody requestBody =new FormBody.Builder().add("media_id", materialId).build(); | |||
| Request request = new Request.Builder().url(uri).post(requestBody).build(); | |||
| Response response = client.newCall(request).execute(); | |||
| String responseContent = response.body().string(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } else { | |||
| return WxMpMaterialVideoInfoResult.fromJson(responseContent); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,68 @@ | |||
| package me.chanjar.weixin.mp.util.http.okhttp; | |||
| import java.io.ByteArrayInputStream; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import org.apache.commons.io.IOUtils; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo; | |||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
| import me.chanjar.weixin.mp.util.http.MaterialVoiceAndImageDownloadRequestExecutor; | |||
| import okhttp3.*; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class OkhttpMaterialVoiceAndImageDownloadRequestExecutor extends MaterialVoiceAndImageDownloadRequestExecutor<ConnectionPool, OkhttpProxyInfo> { | |||
| public OkhttpMaterialVoiceAndImageDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) { | |||
| super(requestHttp, tmpDirFile); | |||
| } | |||
| @Override | |||
| public InputStream execute(String uri, String materialId) throws WxErrorException, IOException { | |||
| OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(requestHttp.getRequestHttpClient()); | |||
| //设置代理 | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| clientBuilder.proxy(requestHttp.getRequestHttpProxy().getProxy()); | |||
| } | |||
| //设置授权 | |||
| clientBuilder.authenticator(new Authenticator() { | |||
| @Override | |||
| public Request authenticate(Route route, Response response) throws IOException { | |||
| String credential = Credentials.basic(requestHttp.getRequestHttpProxy().getProxyUsername(), requestHttp.getRequestHttpProxy().getProxyPassword()); | |||
| return response.request().newBuilder() | |||
| .header("Authorization", credential) | |||
| .build(); | |||
| } | |||
| }); | |||
| //得到httpClient | |||
| OkHttpClient client = clientBuilder.build(); | |||
| RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build(); | |||
| Request request = new Request.Builder().url(uri).get().post(requestBody).build(); | |||
| Response response = client.newCall(request).execute(); | |||
| try (InputStream inputStream = new ByteArrayInputStream(response.body().bytes())) { | |||
| // 下载媒体文件出错 | |||
| byte[] responseContent = IOUtils.toByteArray(inputStream); | |||
| String responseContentString = new String(responseContent, "UTF-8"); | |||
| if (responseContentString.length() < 100) { | |||
| try { | |||
| WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class); | |||
| if (wxError.getErrorCode() != 0) { | |||
| throw new WxErrorException(wxError); | |||
| } | |||
| } catch (com.google.gson.JsonSyntaxException ex) { | |||
| return new ByteArrayInputStream(responseContent); | |||
| } | |||
| } | |||
| return new ByteArrayInputStream(responseContent); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,56 @@ | |||
| package me.chanjar.weixin.mp.util.http.okhttp; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo; | |||
| import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult; | |||
| import me.chanjar.weixin.mp.util.http.MediaImgUploadRequestExecutor; | |||
| import okhttp3.*; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class OkhttpMediaImgUploadRequestExecutor extends MediaImgUploadRequestExecutor<ConnectionPool, OkhttpProxyInfo> { | |||
| public OkhttpMediaImgUploadRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public WxMediaImgUploadResult execute(String uri, File data) throws WxErrorException, IOException { | |||
| OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(requestHttp.getRequestHttpClient()); | |||
| //设置代理 | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| clientBuilder.proxy(requestHttp.getRequestHttpProxy().getProxy()); | |||
| } | |||
| //设置授权 | |||
| clientBuilder.authenticator(new Authenticator() { | |||
| @Override | |||
| public Request authenticate(Route route, Response response) throws IOException { | |||
| String credential = Credentials.basic(requestHttp.getRequestHttpProxy().getProxyUsername(), requestHttp.getRequestHttpProxy().getProxyPassword()); | |||
| return response.request().newBuilder() | |||
| .header("Authorization", credential) | |||
| .build(); | |||
| } | |||
| }); | |||
| //得到httpClient | |||
| OkHttpClient client = clientBuilder.build(); | |||
| RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), data); | |||
| RequestBody body = new MultipartBody.Builder().addFormDataPart("media", null, fileBody).build(); | |||
| Request request = new Request.Builder().url(uri).post(body).build(); | |||
| Response response = client.newCall(request).execute(); | |||
| String responseContent = response.body().string(); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return WxMediaImgUploadResult.fromJson(responseContent); | |||
| } | |||
| } | |||
| @@ -0,0 +1,58 @@ | |||
| package me.chanjar.weixin.mp.util.http.okhttp; | |||
| import java.io.ByteArrayInputStream; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.util.UUID; | |||
| import jodd.util.MimeTypes; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.fs.FileUtils; | |||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||
| import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket; | |||
| import me.chanjar.weixin.mp.util.http.QrCodeRequestExecutor; | |||
| import okhttp3.*; | |||
| /** | |||
| * Created by ecoolper on 2017/5/5. | |||
| */ | |||
| public class OkhttpQrCodeRequestExecutor extends QrCodeRequestExecutor<ConnectionPool, OkhttpProxyInfo> { | |||
| public OkhttpQrCodeRequestExecutor(RequestHttp requestHttp) { | |||
| super(requestHttp); | |||
| } | |||
| @Override | |||
| public File execute(String uri, WxMpQrCodeTicket data) throws WxErrorException, IOException { | |||
| OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(requestHttp.getRequestHttpClient()); | |||
| //设置代理 | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| clientBuilder.proxy(requestHttp.getRequestHttpProxy().getProxy()); | |||
| } | |||
| //设置授权 | |||
| clientBuilder.authenticator(new Authenticator() { | |||
| @Override | |||
| public Request authenticate(Route route, Response response) throws IOException { | |||
| String credential = Credentials.basic(requestHttp.getRequestHttpProxy().getProxyUsername(), requestHttp.getRequestHttpProxy().getProxyPassword()); | |||
| return response.request().newBuilder() | |||
| .header("Authorization", credential) | |||
| .build(); | |||
| } | |||
| }); | |||
| //得到httpClient | |||
| OkHttpClient client = clientBuilder.build(); | |||
| Request request = new Request.Builder().url(uri).get().build(); | |||
| Response response = client.newCall(request).execute(); | |||
| String contentTypeHeader = response.header("Content-Type"); | |||
| if (MimeTypes.MIME_TEXT_PLAIN.equals(contentTypeHeader)) { | |||
| String responseContent = response.body().string(); | |||
| throw new WxErrorException(WxError.fromJson(responseContent)); | |||
| } | |||
| try (InputStream inputStream = new ByteArrayInputStream(response.body().bytes())) { | |||
| return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg"); | |||
| } | |||
| } | |||
| } | |||