Skip to content

Commit e43b295

Browse files
author
Javen
committed
Basically implemented http proxy
1 parent 79e13b9 commit e43b295

File tree

5 files changed

+69
-12
lines changed

5 files changed

+69
-12
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cn.jpush.api.common;
2+
3+
import java.net.InetSocketAddress;
4+
import java.net.Proxy;
5+
6+
public class HttpProxy {
7+
private String host;
8+
private int port;
9+
private String username;
10+
private String password;
11+
12+
private boolean authenticationNeeded = false;
13+
14+
public HttpProxy(String host, int port) {
15+
this.host = host;
16+
this.port = port;
17+
}
18+
19+
public HttpProxy(String host, int port, String username, String password) {
20+
this(host, port);
21+
this.username = username;
22+
this.password = password;
23+
authenticationNeeded = true;
24+
}
25+
26+
public Proxy getProxy() {
27+
return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
28+
}
29+
30+
public boolean isAuthenticationNeeded() {
31+
return this.authenticationNeeded;
32+
}
33+
34+
public String getProxyAuthorization() {
35+
return ServiceHelper.getBasicAuthorization(username, password);
36+
}
37+
}

src/cn/jpush/api/common/NativeHttpClient.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,31 @@ public NativeHttpClient(int maxRetryTimes) {
4040

4141
public ResponseWrapper sendGet(String url, String params,
4242
String authCode) throws APIConnectionException, APIRequestException {
43-
return sendRequest(url, params, RequestMethod.GET, authCode);
43+
return sendRequest(url, params, RequestMethod.GET, authCode, null);
4444
}
4545

4646
public ResponseWrapper sendPost(String url, String content,
4747
String authCode) throws APIConnectionException, APIRequestException {
48-
return sendRequest(url, content, RequestMethod.POST, authCode);
48+
return sendRequest(url, content, RequestMethod.POST, authCode, null);
4949
}
5050

51+
public ResponseWrapper sendGet(String url, String params,
52+
String authCode, HttpProxy proxy) throws APIConnectionException, APIRequestException {
53+
return sendRequest(url, params, RequestMethod.GET, authCode, proxy);
54+
}
55+
56+
public ResponseWrapper sendPost(String url, String content,
57+
String authCode, HttpProxy proxy) throws APIConnectionException, APIRequestException {
58+
return sendRequest(url, content, RequestMethod.POST, authCode, proxy);
59+
}
60+
61+
5162
public ResponseWrapper sendRequest(String url, String content,
52-
RequestMethod method, String authCode) throws APIConnectionException, APIRequestException {
63+
RequestMethod method, String authCode, HttpProxy proxy) throws APIConnectionException, APIRequestException {
5364
ResponseWrapper response = null;
5465
for (int retryTimes = 0; ; retryTimes++) {
5566
try {
56-
response = _sendRequest(url, content, method, authCode);
67+
response = _sendRequest(url, content, method, authCode, proxy);
5768
break;
5869
} catch (SocketTimeoutException e) {
5970
if (KEYWORDS_READ_TIMED_OUT.equals(e.getMessage())) {
@@ -72,7 +83,7 @@ public ResponseWrapper sendRequest(String url, String content,
7283
}
7384

7485
private ResponseWrapper _sendRequest(String url, String content,
75-
RequestMethod method, String authCode) throws APIConnectionException, APIRequestException,
86+
RequestMethod method, String authCode, HttpProxy proxy) throws APIConnectionException, APIRequestException,
7687
SocketTimeoutException {
7788
LOG.debug("Send request to - " + url);
7889
if (null != content) {
@@ -86,7 +97,16 @@ private ResponseWrapper _sendRequest(String url, String content,
8697

8798
try {
8899
URL aUrl = new URL(url);
89-
conn = (HttpURLConnection) aUrl.openConnection();
100+
101+
if (null != proxy) {
102+
conn = (HttpURLConnection) aUrl.openConnection(proxy.getProxy());
103+
if (proxy.isAuthenticationNeeded()) {
104+
conn.addRequestProperty("Proxy-Authorization", proxy.getProxyAuthorization());
105+
}
106+
} else {
107+
conn = (HttpURLConnection) aUrl.openConnection();
108+
}
109+
90110
conn.setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT);
91111
conn.setReadTimeout(DEFAULT_READ_TIMEOUT);
92112
conn.setUseCaches(false);

src/cn/jpush/api/common/ServiceHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public static int generateSendno() {
2929
return RANDOM.nextInt((MAX - MIN) + 1) + MIN;
3030
}
3131

32-
public static String getAuthorizationBase64(String appKey, String masterSecret) {
33-
String encodeKey = appKey + ":" + masterSecret;
32+
public static String getBasicAuthorization(String username, String password) {
33+
String encodeKey = username + ":" + password;
3434
return BASIC_PREFIX + " " + String.valueOf(Base64.encode(encodeKey.getBytes()));
3535
}
3636

src/cn/jpush/api/push/PushClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public PushClient(String masterSecret, String appKey, int maxRetryTimes) {
6666

6767
ServiceHelper.checkBasic(appKey, masterSecret);
6868

69-
this._authCode = ServiceHelper.getAuthorizationBase64(_appKey, _masterSecret);
69+
this._authCode = ServiceHelper.getBasicAuthorization(_appKey, _masterSecret);
7070
this._baseUrl = HOST_NAME_SSL + PUSH_PATH;
7171
this._httpClient = new NativeHttpClient(maxRetryTimes);
7272
}

src/cn/jpush/api/report/ReportClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public ReceivedsResult getReceiveds(String[] msgIdArray) throws APIConnectionExc
4343

4444
public ReceivedsResult getReceiveds(String msgIds) throws APIConnectionException, APIRequestException {
4545
checkMsgids(msgIds);
46-
String authCode = ServiceHelper.getAuthorizationBase64(_appKey, _masterSecret);
46+
String authCode = ServiceHelper.getBasicAuthorization(_appKey, _masterSecret);
4747

4848
String url = REPORT_HOST_NAME + REPORT_RECEIVE_PATH + "?msg_ids=" + msgIds;
4949
ResponseWrapper response = _httpClient.sendGet(url, null, authCode);
@@ -53,7 +53,7 @@ public ReceivedsResult getReceiveds(String msgIds) throws APIConnectionException
5353

5454
public MessagesResult getMessages(String msgIds) throws APIConnectionException, APIRequestException {
5555
checkMsgids(msgIds);
56-
String authCode = ServiceHelper.getAuthorizationBase64(_appKey, _masterSecret);
56+
String authCode = ServiceHelper.getBasicAuthorization(_appKey, _masterSecret);
5757

5858
String url = REPORT_HOST_NAME + REPORT_MESSAGE_PATH + "?msg_ids=" + msgIds;
5959
ResponseWrapper response = _httpClient.sendGet(url, null, authCode);
@@ -62,7 +62,7 @@ public MessagesResult getMessages(String msgIds) throws APIConnectionException,
6262
}
6363

6464
public UsersResult getUsers(TimeUnit timeUnit, String start, int duration) throws APIConnectionException, APIRequestException {
65-
String authCode = ServiceHelper.getAuthorizationBase64(_appKey, _masterSecret);
65+
String authCode = ServiceHelper.getBasicAuthorization(_appKey, _masterSecret);
6666

6767
String startEncoded = null;
6868
try {

0 commit comments

Comments
 (0)