|
| 1 | +package me.chanjar.weixin.channel.api.impl; |
| 2 | + |
| 3 | +import lombok.extern.slf4j.Slf4j; |
| 4 | +import me.chanjar.weixin.channel.bean.token.StableTokenParam; |
| 5 | +import me.chanjar.weixin.channel.config.WxChannelConfig; |
| 6 | +import me.chanjar.weixin.channel.util.JsonUtils; |
| 7 | +import me.chanjar.weixin.common.util.http.HttpClientType; |
| 8 | +import me.chanjar.weixin.common.util.http.apache.ApacheBasicResponseHandler; |
| 9 | +import me.chanjar.weixin.common.util.http.hc.BasicResponseHandler; |
| 10 | +import me.chanjar.weixin.common.util.http.hc.DefaultHttpComponentsClientBuilder; |
| 11 | +import me.chanjar.weixin.common.util.http.hc.HttpComponentsClientBuilder; |
| 12 | +import org.apache.commons.lang3.StringUtils; |
| 13 | +import org.apache.hc.client5.http.classic.HttpClient; |
| 14 | +import org.apache.hc.client5.http.classic.methods.HttpGet; |
| 15 | +import org.apache.hc.client5.http.classic.methods.HttpPost; |
| 16 | +import org.apache.hc.client5.http.config.RequestConfig; |
| 17 | +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; |
| 18 | +import org.apache.hc.core5.http.ContentType; |
| 19 | +import org.apache.hc.core5.http.HttpHost; |
| 20 | +import org.apache.hc.core5.http.io.entity.StringEntity; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | + |
| 24 | +import static me.chanjar.weixin.channel.constant.WxChannelApiUrlConstants.GET_ACCESS_TOKEN_URL; |
| 25 | +import static me.chanjar.weixin.channel.constant.WxChannelApiUrlConstants.GET_STABLE_ACCESS_TOKEN_URL; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author altusea |
| 29 | + */ |
| 30 | +@Slf4j |
| 31 | +public class WxChannelServiceHttpComponentsImpl extends BaseWxChannelServiceImpl<HttpClient, HttpHost> { |
| 32 | + |
| 33 | + private CloseableHttpClient httpClient; |
| 34 | + private HttpHost httpProxy; |
| 35 | + |
| 36 | + @Override |
| 37 | + public void initHttp() { |
| 38 | + WxChannelConfig config = this.getConfig(); |
| 39 | + HttpComponentsClientBuilder apacheHttpClientBuilder = DefaultHttpComponentsClientBuilder.get(); |
| 40 | + |
| 41 | + apacheHttpClientBuilder.httpProxyHost(config.getHttpProxyHost()) |
| 42 | + .httpProxyPort(config.getHttpProxyPort()) |
| 43 | + .httpProxyUsername(config.getHttpProxyUsername()) |
| 44 | + .httpProxyPassword(config.getHttpProxyPassword().toCharArray()); |
| 45 | + |
| 46 | + if (config.getHttpProxyHost() != null && config.getHttpProxyPort() > 0) { |
| 47 | + this.httpProxy = new HttpHost(config.getHttpProxyHost(), config.getHttpProxyPort()); |
| 48 | + } |
| 49 | + |
| 50 | + this.httpClient = apacheHttpClientBuilder.build(); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public CloseableHttpClient getRequestHttpClient() { |
| 55 | + return httpClient; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public HttpHost getRequestHttpProxy() { |
| 60 | + return httpProxy; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public HttpClientType getRequestType() { |
| 65 | + return HttpClientType.HTTP_COMPONENTS; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + protected String doGetAccessTokenRequest() throws IOException { |
| 70 | + WxChannelConfig config = this.getConfig(); |
| 71 | + String url = StringUtils.isNotEmpty(config.getAccessTokenUrl()) ? config.getAccessTokenUrl() : |
| 72 | + StringUtils.isNotEmpty(config.getApiHostUrl()) ? |
| 73 | + GET_ACCESS_TOKEN_URL.replace("https://api.weixin.qq.com", config.getApiHostUrl()) : GET_ACCESS_TOKEN_URL; |
| 74 | + |
| 75 | + url = String.format(url, config.getAppid(), config.getSecret()); |
| 76 | + |
| 77 | + HttpGet httpGet = new HttpGet(url); |
| 78 | + if (this.getRequestHttpProxy() != null) { |
| 79 | + RequestConfig requestConfig = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build(); |
| 80 | + httpGet.setConfig(requestConfig); |
| 81 | + } |
| 82 | + return getRequestHttpClient().execute(httpGet, BasicResponseHandler.INSTANCE); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * 获取稳定版接口调用凭据 |
| 87 | + * |
| 88 | + * @param forceRefresh false 为普通模式, true为强制刷新模式 |
| 89 | + * @return 返回json的字符串 |
| 90 | + * @throws IOException the io exception |
| 91 | + */ |
| 92 | + @Override |
| 93 | + protected String doGetStableAccessTokenRequest(boolean forceRefresh) throws IOException { |
| 94 | + WxChannelConfig config = this.getConfig(); |
| 95 | + String url = GET_STABLE_ACCESS_TOKEN_URL; |
| 96 | + |
| 97 | + HttpPost httpPost = new HttpPost(url); |
| 98 | + if (this.getRequestHttpProxy() != null) { |
| 99 | + RequestConfig requestConfig = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build(); |
| 100 | + httpPost.setConfig(requestConfig); |
| 101 | + } |
| 102 | + StableTokenParam requestParam = new StableTokenParam(); |
| 103 | + requestParam.setAppId(config.getAppid()); |
| 104 | + requestParam.setSecret(config.getSecret()); |
| 105 | + requestParam.setGrantType("client_credential"); |
| 106 | + requestParam.setForceRefresh(forceRefresh); |
| 107 | + String requestJson = JsonUtils.encode(requestParam); |
| 108 | + assert requestJson != null; |
| 109 | + |
| 110 | + httpPost.setEntity(new StringEntity(requestJson, ContentType.APPLICATION_JSON)); |
| 111 | + return getRequestHttpClient().execute(httpPost, BasicResponseHandler.INSTANCE); |
| 112 | + } |
| 113 | +} |
0 commit comments