Skip to content

Commit 175f059

Browse files
author
Javen
committed
Refactor: move authCode from method var to class instance; proxy as an instance, too.
1 parent e43b295 commit 175f059

File tree

7 files changed

+77
-72
lines changed

7 files changed

+77
-72
lines changed

src/cn/jpush/api/JPushClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import cn.jpush.api.common.APIConnectionException;
66
import cn.jpush.api.common.APIRequestException;
7+
import cn.jpush.api.common.HttpProxy;
78
import cn.jpush.api.common.TimeUnit;
89
import cn.jpush.api.push.PushClient;
910
import cn.jpush.api.push.PushResult;
@@ -40,6 +41,11 @@ public JPushClient(String masterSecret, String appKey, int maxRetryTimes) {
4041
_reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes);
4142
}
4243

44+
public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) {
45+
_pushClient = new PushClient(masterSecret, appKey, maxRetryTimes, proxy);
46+
_reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes, proxy);
47+
}
48+
4349
/**
4450
* Create a JPush Client with global settings.
4551
*

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
import java.net.InetSocketAddress;
44
import java.net.Proxy;
55

6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
69
public class HttpProxy {
10+
private static final Logger LOG = LoggerFactory.getLogger(NativeHttpClient.class);
11+
712
private String host;
813
private int port;
914
private String username;
@@ -14,6 +19,8 @@ public class HttpProxy {
1419
public HttpProxy(String host, int port) {
1520
this.host = host;
1621
this.port = port;
22+
23+
LOG.info("Http Proxy - host:" + host + ", port:" + port);
1724
}
1825

1926
public HttpProxy(String host, int port, String username, String password) {
@@ -23,7 +30,7 @@ public HttpProxy(String host, int port, String username, String password) {
2330
authenticationNeeded = true;
2431
}
2532

26-
public Proxy getProxy() {
33+
public Proxy getNetProxy() {
2734
return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
2835
}
2936

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ public enum RequestMethod {
4949

5050
public static final int DEFAULT_MAX_RETRY_TIMES = 3;
5151

52-
public ResponseWrapper sendGet(String url, String params,
53-
String authCode) throws APIConnectionException, APIRequestException;
52+
public ResponseWrapper sendGet(String url, String params)
53+
throws APIConnectionException, APIRequestException;
5454

55-
public ResponseWrapper sendPost(String url, String content,
56-
String authCode) throws APIConnectionException, APIRequestException;
55+
public ResponseWrapper sendPost(String url, String content)
56+
throws APIConnectionException, APIRequestException;
5757

5858

5959
}

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

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,45 +26,39 @@ public class NativeHttpClient implements IHttpClient {
2626
private static final String KEYWORDS_READ_TIMED_OUT = "Read timed out";
2727

2828
private int _maxRetryTimes = 0;
29+
private String _authCode;
30+
private HttpProxy _proxy;
2931

30-
public NativeHttpClient() {
31-
this(DEFAULT_MAX_RETRY_TIMES);
32+
public NativeHttpClient(String authCode) {
33+
this(authCode, DEFAULT_MAX_RETRY_TIMES, null);
3234
}
3335

34-
public NativeHttpClient(int maxRetryTimes) {
36+
public NativeHttpClient(String authCode, int maxRetryTimes, HttpProxy proxy) {
3537
this._maxRetryTimes = maxRetryTimes;
3638
LOG.info("Created instance with _maxRetryTimes = " + _maxRetryTimes);
3739

40+
this._authCode = authCode;
41+
this._proxy = proxy;
42+
3843
initSSL();
3944
}
4045

41-
public ResponseWrapper sendGet(String url, String params,
42-
String authCode) throws APIConnectionException, APIRequestException {
43-
return sendRequest(url, params, RequestMethod.GET, authCode, null);
46+
public ResponseWrapper sendGet(String url, String params)
47+
throws APIConnectionException, APIRequestException {
48+
return sendRequest(url, params, RequestMethod.GET);
4449
}
4550

46-
public ResponseWrapper sendPost(String url, String content,
47-
String authCode) throws APIConnectionException, APIRequestException {
48-
return sendRequest(url, content, RequestMethod.POST, authCode, null);
51+
public ResponseWrapper sendPost(String url, String content)
52+
throws APIConnectionException, APIRequestException {
53+
return sendRequest(url, content, RequestMethod.POST);
4954
}
50-
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-
55+
6256
public ResponseWrapper sendRequest(String url, String content,
63-
RequestMethod method, String authCode, HttpProxy proxy) throws APIConnectionException, APIRequestException {
57+
RequestMethod method) throws APIConnectionException, APIRequestException {
6458
ResponseWrapper response = null;
6559
for (int retryTimes = 0; ; retryTimes++) {
6660
try {
67-
response = _sendRequest(url, content, method, authCode, proxy);
61+
response = _sendRequest(url, content, method);
6862
break;
6963
} catch (SocketTimeoutException e) {
7064
if (KEYWORDS_READ_TIMED_OUT.equals(e.getMessage())) {
@@ -83,7 +77,7 @@ public ResponseWrapper sendRequest(String url, String content,
8377
}
8478

8579
private ResponseWrapper _sendRequest(String url, String content,
86-
RequestMethod method, String authCode, HttpProxy proxy) throws APIConnectionException, APIRequestException,
80+
RequestMethod method) throws APIConnectionException, APIRequestException,
8781
SocketTimeoutException {
8882
LOG.debug("Send request to - " + url);
8983
if (null != content) {
@@ -98,10 +92,10 @@ private ResponseWrapper _sendRequest(String url, String content,
9892
try {
9993
URL aUrl = new URL(url);
10094

101-
if (null != proxy) {
102-
conn = (HttpURLConnection) aUrl.openConnection(proxy.getProxy());
103-
if (proxy.isAuthenticationNeeded()) {
104-
conn.addRequestProperty("Proxy-Authorization", proxy.getProxyAuthorization());
95+
if (null != _proxy) {
96+
conn = (HttpURLConnection) aUrl.openConnection(_proxy.getNetProxy());
97+
if (_proxy.isAuthenticationNeeded()) {
98+
conn.setRequestProperty("Proxy-Authorization", _proxy.getProxyAuthorization());
10599
}
106100
} else {
107101
conn = (HttpURLConnection) aUrl.openConnection();
@@ -115,7 +109,7 @@ private ResponseWrapper _sendRequest(String url, String content,
115109
conn.setRequestProperty("Connection", "Keep-Alive");
116110
conn.setRequestProperty("Accept-Charset", CHARSET);
117111
conn.setRequestProperty("Charset", CHARSET);
118-
conn.setRequestProperty("Authorization", authCode);
112+
conn.setRequestProperty("Authorization", _authCode);
119113

120114
if (RequestMethod.POST == method) {
121115
conn.setDoOutput(true); //POST Request

src/cn/jpush/api/examples/PushExample.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import cn.jpush.api.JPushClient;
77
import cn.jpush.api.common.APIConnectionException;
88
import cn.jpush.api.common.APIRequestException;
9+
import cn.jpush.api.common.HttpProxy;
910
import cn.jpush.api.push.PushResult;
1011
import cn.jpush.api.push.model.Message;
1112
import cn.jpush.api.push.model.Options;
@@ -35,7 +36,8 @@ public static void main(String[] args) {
3536

3637

3738
public static void testSendPush() {
38-
JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);
39+
HttpProxy proxy = new HttpProxy("localhost", 3128);
40+
JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3, proxy);
3941

4042
// For push, all you need do is to build PushPayload object.
4143
PushPayload payload = buildPushObject_all_all_alert();

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import cn.jpush.api.common.APIConnectionException;
44
import cn.jpush.api.common.APIRequestException;
5+
import cn.jpush.api.common.HttpProxy;
56
import cn.jpush.api.common.IHttpClient;
67
import cn.jpush.api.common.NativeHttpClient;
78
import cn.jpush.api.common.ResponseWrapper;
@@ -24,19 +25,12 @@ public class PushClient {
2425

2526
private final NativeHttpClient _httpClient;
2627

27-
// The API secret of the appKey. Please get it from JPush Web Portal
28-
private final String _masterSecret;
29-
30-
// The KEY of the Application created on JPush. Please get it from JPush Web Portal
31-
private final String _appKey;
32-
3328
// If not present, true by default.
3429
private boolean _apnsProduction = true;
3530

3631
// If not present, the default value is 86400(s) (one day)
3732
private long _timeToLive = 60 * 60 * 24;
3833

39-
4034
private boolean _globalSettingEnabled = false;
4135

4236
// Generated HTTP Basic authorization string.
@@ -53,22 +47,23 @@ public PushClient(String masterSecret, String appKey) {
5347
this(masterSecret, appKey, IHttpClient.DEFAULT_MAX_RETRY_TIMES);
5448
}
5549

50+
public PushClient(String masterSecret, String appKey, int maxRetryTimes) {
51+
this(masterSecret, appKey, maxRetryTimes, null);
52+
}
53+
5654
/**
5755
* Create a Push Client with max retry times.
5856
*
5957
* @param masterSecret API access secret of the appKey.
6058
* @param appKey The KEY of one application on JPush.
6159
* @param maxRetryTimes max retry times
6260
*/
63-
public PushClient(String masterSecret, String appKey, int maxRetryTimes) {
64-
this._masterSecret = masterSecret;
65-
this._appKey = appKey;
66-
61+
public PushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) {
6762
ServiceHelper.checkBasic(appKey, masterSecret);
6863

69-
this._authCode = ServiceHelper.getBasicAuthorization(_appKey, _masterSecret);
64+
this._authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
7065
this._baseUrl = HOST_NAME_SSL + PUSH_PATH;
71-
this._httpClient = new NativeHttpClient(maxRetryTimes);
66+
this._httpClient = new NativeHttpClient(this._authCode, maxRetryTimes, proxy);
7267
}
7368

7469
/**
@@ -98,13 +93,13 @@ public PushResult sendPush(PushPayload pushPayload) throws APIConnectionExceptio
9893
pushPayload.resetOptionsApnsProduction(_apnsProduction);
9994
}
10095

101-
ResponseWrapper response = _httpClient.sendPost(_baseUrl, pushPayload.toString(), _authCode);
96+
ResponseWrapper response = _httpClient.sendPost(_baseUrl, pushPayload.toString());
10297

10398
return PushResult.fromResponse(response);
10499
}
105100

106101
public PushResult sendPush(String payloadString) throws APIConnectionException, APIRequestException {
107-
ResponseWrapper response = _httpClient.sendPost(_baseUrl, payloadString, _authCode);
102+
ResponseWrapper response = _httpClient.sendPost(_baseUrl, payloadString);
108103

109104
return PushResult.fromResponse(response);
110105
}

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

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import cn.jpush.api.common.APIConnectionException;
77
import cn.jpush.api.common.APIRequestException;
8+
import cn.jpush.api.common.HttpProxy;
89
import cn.jpush.api.common.IHttpClient;
910
import cn.jpush.api.common.NativeHttpClient;
1011
import cn.jpush.api.common.ResponseWrapper;
@@ -19,51 +20,51 @@ public class ReportClient {
1920
private static final String REPORT_MESSAGE_PATH = "/v3/messages";
2021

2122
private final NativeHttpClient _httpClient;
23+
private final String _authCode;
24+
25+
public ReportClient(String masterSecret, String appKey) {
26+
this(masterSecret, appKey, IHttpClient.DEFAULT_MAX_RETRY_TIMES, null);
27+
}
2228

23-
private String _masterSecret;
24-
private String _appKey;
25-
2629
public ReportClient(String masterSecret, String appKey, int maxRetryTimes) {
27-
this._masterSecret = masterSecret;
28-
this._appKey = appKey;
29-
30+
this(masterSecret, appKey, maxRetryTimes, null);
31+
}
32+
33+
public ReportClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) {
3034
ServiceHelper.checkBasic(appKey, masterSecret);
35+
_authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
3136

32-
_httpClient = new NativeHttpClient(maxRetryTimes);
37+
_httpClient = new NativeHttpClient(_authCode, maxRetryTimes, proxy);
3338
}
3439

35-
public ReportClient(String masterSecret, String appKey) {
36-
this(masterSecret, appKey, IHttpClient.DEFAULT_MAX_RETRY_TIMES);
37-
}
38-
3940

40-
public ReceivedsResult getReceiveds(String[] msgIdArray) throws APIConnectionException, APIRequestException {
41+
public ReceivedsResult getReceiveds(String[] msgIdArray)
42+
throws APIConnectionException, APIRequestException {
4143
return getReceiveds(StringUtils.arrayToString(msgIdArray));
4244
}
4345

44-
public ReceivedsResult getReceiveds(String msgIds) throws APIConnectionException, APIRequestException {
46+
public ReceivedsResult getReceiveds(String msgIds)
47+
throws APIConnectionException, APIRequestException {
4548
checkMsgids(msgIds);
46-
String authCode = ServiceHelper.getBasicAuthorization(_appKey, _masterSecret);
4749

4850
String url = REPORT_HOST_NAME + REPORT_RECEIVE_PATH + "?msg_ids=" + msgIds;
49-
ResponseWrapper response = _httpClient.sendGet(url, null, authCode);
51+
ResponseWrapper response = _httpClient.sendGet(url, null);
5052

5153
return ReceivedsResult.fromResponse(response);
5254
}
5355

54-
public MessagesResult getMessages(String msgIds) throws APIConnectionException, APIRequestException {
56+
public MessagesResult getMessages(String msgIds)
57+
throws APIConnectionException, APIRequestException {
5558
checkMsgids(msgIds);
56-
String authCode = ServiceHelper.getBasicAuthorization(_appKey, _masterSecret);
5759

5860
String url = REPORT_HOST_NAME + REPORT_MESSAGE_PATH + "?msg_ids=" + msgIds;
59-
ResponseWrapper response = _httpClient.sendGet(url, null, authCode);
61+
ResponseWrapper response = _httpClient.sendGet(url, null);
6062

6163
return MessagesResult.fromResponse(response);
6264
}
6365

64-
public UsersResult getUsers(TimeUnit timeUnit, String start, int duration) throws APIConnectionException, APIRequestException {
65-
String authCode = ServiceHelper.getBasicAuthorization(_appKey, _masterSecret);
66-
66+
public UsersResult getUsers(TimeUnit timeUnit, String start, int duration)
67+
throws APIConnectionException, APIRequestException {
6768
String startEncoded = null;
6869
try {
6970
startEncoded = URLEncoder.encode(start, "utf-8");
@@ -73,7 +74,7 @@ public UsersResult getUsers(TimeUnit timeUnit, String start, int duration) throw
7374
String url = REPORT_HOST_NAME + REPORT_USER_PATH
7475
+ "?time_unit=" + timeUnit.toString()
7576
+ "&start=" + startEncoded + "&duration=" + duration;
76-
ResponseWrapper response = _httpClient.sendGet(url, null, authCode);
77+
ResponseWrapper response = _httpClient.sendGet(url, null);
7778

7879
return UsersResult.fromResponse(response);
7980
}

0 commit comments

Comments
 (0)