From 04ec788d076b0a3a8e8c9a4dae2493085bb3567d Mon Sep 17 00:00:00 2001
From: Charming <charmingoh@users.noreply.github.com>
Date: Tue, 1 May 2018 21:43:34 +0800
Subject: [PATCH] =?UTF-8?q?#567=20=E5=BE=AE=E4=BF=A1=E5=BC=80=E6=94=BE?=
 =?UTF-8?q?=E5=B9=B3=E5=8F=B0=E5=A2=9E=E5=8A=A0=20HTTP=20proxy=20=E6=9C=BA?=
 =?UTF-8?q?=E5=88=B6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* 微信开放平台:1. WxOpenInRedisConfigStorage 支持 JedisPool/JedisSentinelPool 等 Pool<Jedis> 的子类;2. WxOpenInRedisConfigStorage 增加 keyPrefix 以支持可配置的前缀;

* 微信开放平台:增加小程序代码模板库管理

* 小程序:增加代码管理相关 API

* 小程序:增加修改服务器地址、成员管理 API

* 小程序:增加数据分析相关 API

* 微信开放平台:增加 HTTP proxy 机制
---
 .../weixin/open/api/WxOpenConfigStorage.java  |  9 +++-
 .../api/impl/WxOpenInMemoryConfigStorage.java | 49 +++++++++++++++++--
 .../api/impl/WxOpenServiceAbstractImpl.java   |  6 +++
 .../WxOpenServiceApacheHttpClientImpl.java    | 19 +++++--
 4 files changed, 75 insertions(+), 8 deletions(-)

diff --git a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenConfigStorage.java b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenConfigStorage.java
index 26fef9ded..b0d833b29 100644
--- a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenConfigStorage.java
+++ b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenConfigStorage.java
@@ -38,6 +38,14 @@ public interface WxOpenConfigStorage {
 
   void updateComponentAccessTokent(WxOpenComponentAccessToken componentAccessToken);
 
+  String getHttpProxyHost();
+
+  int getHttpProxyPort();
+
+  String getHttpProxyUsername();
+
+  String getHttpProxyPassword();
+
   WxMpConfigStorage getWxMpConfigStorage(String appId);
 
   WxMaConfig getWxMaConfig(String appId);
@@ -117,5 +125,4 @@ public interface WxOpenConfigStorage {
    * @param expiresInSeconds 过期时间,以秒为单位
    */
   void updateCardApiTicket(String appId, String cardApiTicket, int expiresInSeconds);
-
 }
diff --git a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenInMemoryConfigStorage.java b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenInMemoryConfigStorage.java
index 4063abc36..56e1d9a1c 100644
--- a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenInMemoryConfigStorage.java
+++ b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenInMemoryConfigStorage.java
@@ -30,6 +30,11 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
   private String componentAccessToken;
   private long componentExpiresTime;
 
+  private String httpProxyHost;
+  private int httpProxyPort;
+  private String httpProxyUsername;
+  private String httpProxyPassword;
+
   private Map<String, Token> authorizerRefreshTokens = new Hashtable<>();
   private Map<String, Token> authorizerAccessTokens = new Hashtable<>();
   private Map<String, Token> jsapiTickets = new Hashtable<>();
@@ -105,6 +110,42 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
     updateComponentAccessTokent(componentAccessToken.getComponentAccessToken(), componentAccessToken.getExpiresIn());
   }
 
+  @Override
+  public String getHttpProxyHost() {
+    return httpProxyHost;
+  }
+
+  public void setHttpProxyHost(String httpProxyHost) {
+    this.httpProxyHost = httpProxyHost;
+  }
+
+  @Override
+  public int getHttpProxyPort() {
+    return httpProxyPort;
+  }
+
+  public void setHttpProxyPort(int httpProxyPort) {
+    this.httpProxyPort = httpProxyPort;
+  }
+
+  @Override
+  public String getHttpProxyUsername() {
+    return httpProxyUsername;
+  }
+
+  public void setHttpProxyUsername(String httpProxyUsername) {
+    this.httpProxyUsername = httpProxyUsername;
+  }
+
+  @Override
+  public String getHttpProxyPassword() {
+    return httpProxyPassword;
+  }
+
+  public void setHttpProxyPassword(String httpProxyPassword) {
+    this.httpProxyPassword = httpProxyPassword;
+  }
+
   @Override
   public WxMpConfigStorage getWxMpConfigStorage(String appId) {
     return new WxOpenInnerConfigStorage(this, appId);
@@ -377,22 +418,22 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
 
     @Override
     public String getHttpProxyHost() {
-      return null;
+      return this.wxOpenConfigStorage.getHttpProxyHost();
     }
 
     @Override
     public int getHttpProxyPort() {
-      return 0;
+      return this.wxOpenConfigStorage.getHttpProxyPort();
     }
 
     @Override
     public String getHttpProxyUsername() {
-      return null;
+      return this.wxOpenConfigStorage.getHttpProxyUsername();
     }
 
     @Override
     public String getHttpProxyPassword() {
-      return null;
+      return this.wxOpenConfigStorage.getHttpProxyPassword();
     }
 
     @Override
diff --git a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenServiceAbstractImpl.java b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenServiceAbstractImpl.java
index 5ace633f3..f7f8dc115 100644
--- a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenServiceAbstractImpl.java
+++ b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenServiceAbstractImpl.java
@@ -33,8 +33,14 @@ public abstract class WxOpenServiceAbstractImpl<H, P> implements WxOpenService,
   @Override
   public void setWxOpenConfigStorage(WxOpenConfigStorage wxOpenConfigStorage) {
     this.wxOpenConfigStorage = wxOpenConfigStorage;
+    this.initHttp();
   }
 
+  /**
+   * 初始化 RequestHttp
+   */
+  public abstract void initHttp();
+
   protected synchronized <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
     try {
       T result = executor.execute(uri, data);
diff --git a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenServiceApacheHttpClientImpl.java b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenServiceApacheHttpClientImpl.java
index 58921e695..ea1558613 100644
--- a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenServiceApacheHttpClientImpl.java
+++ b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenServiceApacheHttpClientImpl.java
@@ -5,6 +5,7 @@ import me.chanjar.weixin.common.util.http.HttpType;
 import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
 import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
 import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
+import me.chanjar.weixin.open.api.WxOpenConfigStorage;
 import org.apache.http.HttpHost;
 import org.apache.http.impl.client.CloseableHttpClient;
 
@@ -14,8 +15,21 @@ import org.apache.http.impl.client.CloseableHttpClient;
  * @author <a href="https://github.com/007gzs">007</a>
  */
 public class WxOpenServiceApacheHttpClientImpl extends WxOpenServiceAbstractImpl<CloseableHttpClient, HttpHost> {
-  private CloseableHttpClient httpClient = DefaultApacheHttpClientBuilder.get().build();
-  private HttpHost httpProxy = null;
+  private CloseableHttpClient httpClient;
+  private HttpHost httpProxy;
+
+  @Override
+  public void initHttp() {
+    WxOpenConfigStorage configStorage = this.getWxOpenConfigStorage();
+    if (configStorage.getHttpProxyHost() != null && configStorage.getHttpProxyPort() > 0) {
+      this.httpProxy = new HttpHost(configStorage.getHttpProxyHost(), configStorage.getHttpProxyPort());
+    }
+    this.httpClient = DefaultApacheHttpClientBuilder.get()
+      .httpProxyHost(configStorage.getHttpProxyHost())
+      .httpProxyPort(configStorage.getHttpProxyPort())
+      .httpProxyUsername(configStorage.getHttpProxyUsername())
+      .httpProxyPassword(configStorage.getHttpProxyPassword()).build();
+  }
 
   @Override
   public CloseableHttpClient getRequestHttpClient() {
@@ -41,5 +55,4 @@ public class WxOpenServiceApacheHttpClientImpl extends WxOpenServiceAbstractImpl
   public String post(String url, String postData) throws WxErrorException {
     return execute(SimplePostRequestExecutor.create(this), url, postData);
   }
-
 }