Skip to content

微信开放平台:增加 HTTP proxy 机制 #567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 1, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -117,5 +125,4 @@ public interface WxOpenConfigStorage {
* @param expiresInSeconds 过期时间,以秒为单位
*/
void updateCardApiTicket(String appId, String cardApiTicket, int expiresInSeconds);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<>();
Expand Down Expand Up @@ -105,6 +110,42 @@ public void updateComponentAccessTokent(WxOpenComponentAccessToken componentAcce
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);
Expand Down Expand Up @@ -377,22 +418,22 @@ public String getOauth2redirectUri() {

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ public WxOpenConfigStorage getWxOpenConfigStorage() {
@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;

Expand All @@ -14,8 +15,21 @@
* @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() {
Expand All @@ -41,5 +55,4 @@ public String get(String url, String queryParam) throws WxErrorException {
public String post(String url, String postData) throws WxErrorException {
return execute(SimplePostRequestExecutor.create(this), url, postData);
}

}