Skip to content

Commit 0cdb46c

Browse files
author
Javen
committed
1. Move initSSL to instance creation method - no need to init for every request.
2. Check 'connect timed out' special SocketTimeoutException, and will retry 3 times by default.
1 parent 69799bf commit 0cdb46c

File tree

5 files changed

+33
-36
lines changed

5 files changed

+33
-36
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public enum RequestMethod {
3636
//设置读取超时时间
3737
public static final int DEFAULT_READ_TIMEOUT = (30 * 1000); // milliseconds
3838

39-
public static final int DEFAULT_MAX_RETRY_TIMES = 0;
39+
public static final int DEFAULT_MAX_RETRY_TIMES = 3;
4040

4141
public ResponseWrapper sendGet(String url, String params,
4242
String authCode) throws APIConnectionException, APIRequestException;

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

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
import java.io.InputStreamReader;
66
import java.io.OutputStream;
77
import java.net.HttpURLConnection;
8+
import java.net.SocketTimeoutException;
89
import java.net.URL;
910
import java.security.cert.CertificateException;
1011
import java.security.cert.X509Certificate;
1112

1213
import javax.net.ssl.HostnameVerifier;
1314
import javax.net.ssl.HttpsURLConnection;
15+
import javax.net.ssl.SSLContext;
1416
import javax.net.ssl.SSLSession;
1517
import javax.net.ssl.TrustManager;
1618
import javax.net.ssl.X509TrustManager;
@@ -30,6 +32,8 @@ public NativeHttpClient() {
3032
public NativeHttpClient(int maxRetryTimes) {
3133
this._maxRetryTimes = maxRetryTimes;
3234
LOG.info("Created instance with _maxRetryTimes = " + _maxRetryTimes);
35+
36+
initSSL();
3337
}
3438

3539
public ResponseWrapper sendGet(String url, String params,
@@ -49,19 +53,20 @@ public ResponseWrapper sendRequest(String url, String content,
4953
try {
5054
response = _sendRequest(url, content, method, authCode);
5155
break;
52-
} catch (APIConnectionException e) {
56+
} catch (SocketTimeoutException e) { // connect timed out
5357
if (retryTimes >= _maxRetryTimes) {
5458
throw new APIConnectionException(e);
5559
} else {
56-
LOG.debug("Retry again - " + (retryTimes + 1));
60+
LOG.debug("connect timed out - retry again - " + (retryTimes + 1));
5761
}
5862
}
5963
}
6064
return response;
6165
}
6266

6367
private ResponseWrapper _sendRequest(String url, String content,
64-
RequestMethod method, String authCode) throws APIConnectionException, APIRequestException {
68+
RequestMethod method, String authCode) throws APIConnectionException, APIRequestException,
69+
SocketTimeoutException {
6570
LOG.debug("Send request to - " + url);
6671
if (null != content) {
6772
LOG.debug("Request Content - " + content);
@@ -73,8 +78,6 @@ private ResponseWrapper _sendRequest(String url, String content,
7378
ResponseWrapper wrapper = new ResponseWrapper();
7479

7580
try {
76-
initSSL();
77-
7881
URL aUrl = new URL(url);
7982
conn = (HttpURLConnection) aUrl.openConnection();
8083
conn.setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT);
@@ -165,6 +168,13 @@ private ResponseWrapper _sendRequest(String url, String content,
165168
throw new APIRequestException(wrapper);
166169
}
167170

171+
} catch (SocketTimeoutException e) {
172+
if (e.getMessage().contains("connect timed out")) {
173+
throw e;
174+
}
175+
LOG.debug(IO_ERROR_MESSAGE, e);
176+
throw new APIConnectionException(IO_ERROR_MESSAGE, e);
177+
168178
} catch (IOException e) {
169179
LOG.debug(IO_ERROR_MESSAGE, e);
170180
throw new APIConnectionException(IO_ERROR_MESSAGE, e);
@@ -186,16 +196,17 @@ private ResponseWrapper _sendRequest(String url, String content,
186196
}
187197

188198
protected void initSSL() {
199+
TrustManager[] tmCerts = new javax.net.ssl.TrustManager[1];
200+
tmCerts[0] = new SimpleTrustManager();
189201
try {
190-
TrustManager[] tmCerts = new javax.net.ssl.TrustManager[1];
191-
tmCerts[0] = new SimpleTrustManager();
192-
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
193-
sc.init(null, tmCerts, null);
194-
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
195-
HostnameVerifier hv = new SimpleHostnameVerifier();
196-
HttpsURLConnection.setDefaultHostnameVerifier(hv);
202+
SSLContext sslContext = SSLContext.getInstance("SSL");
203+
sslContext.init(null, tmCerts, null);
204+
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
205+
206+
HostnameVerifier hostnameVerifier = new SimpleHostnameVerifier();
207+
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
197208
} catch (Exception e) {
198-
LOG.error("", e);
209+
LOG.error("Init SSL error", e);
199210
}
200211
}
201212

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,10 @@ public static void testSendPush() {
4545
LOG.info("Got result - " + result);
4646

4747
} catch (APIConnectionException e) {
48-
// Connection error, should retry later
49-
LOG.error("Connection error, should retry later", e);
48+
LOG.error("Connection error. Should retry later. ", e);
5049

5150
} catch (APIRequestException e) {
52-
// Should review the error, and fix the request
53-
LOG.error("Should review the error, and fix the request", e);
51+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
5452
LOG.info("HTTP Status: " + e.getStatus());
5553
LOG.info("Error Code: " + e.getErrorCode());
5654
LOG.info("Error Message: " + e.getErrorMessage());

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@ public static void testGetReport() {
3232
LOG.debug("Got result - " + result);
3333

3434
} catch (APIConnectionException e) {
35-
// Connection error, should retry later
36-
LOG.error("Connection error, should retry later", e);
35+
LOG.error("Connection error. Should retry later. ", e);
3736

3837
} catch (APIRequestException e) {
39-
// Should review the error, and fix the request
40-
LOG.error("Should review the error, and fix the request", e);
38+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
4139
LOG.info("HTTP Status: " + e.getStatus());
4240
LOG.info("Error Code: " + e.getErrorCode());
4341
LOG.info("Error Message: " + e.getErrorMessage());
@@ -51,12 +49,10 @@ public static void testGetUsers() {
5149
LOG.debug("Got result - " + result);
5250

5351
} catch (APIConnectionException e) {
54-
// Connection error, should retry later
55-
LOG.error("Connection error, should retry later", e);
52+
LOG.error("Connection error. Should retry later. ", e);
5653

5754
} catch (APIRequestException e) {
58-
// Should review the error, and fix the request
59-
LOG.error("Should review the error, and fix the request", e);
55+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
6056
LOG.info("HTTP Status: " + e.getStatus());
6157
LOG.info("Error Code: " + e.getErrorCode());
6258
LOG.info("Error Message: " + e.getErrorMessage());
@@ -70,12 +66,10 @@ public static void testGetMessages() {
7066
LOG.debug("Got result - " + result);
7167

7268
} catch (APIConnectionException e) {
73-
// Connection error, should retry later
74-
LOG.error("Connection error, should retry later", e);
69+
LOG.error("Connection error. Should retry later. ", e);
7570

7671
} catch (APIRequestException e) {
77-
// Should review the error, and fix the request
78-
LOG.error("Should review the error, and fix the request", e);
72+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
7973
LOG.info("HTTP Status: " + e.getStatus());
8074
LOG.info("Error Code: " + e.getErrorCode());
8175
LOG.info("Error Message: " + e.getErrorMessage());

src/cn/jpush/api/push/model/audience/Audience.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@
1313
public class Audience implements PushModel {
1414
private static final String ALL = "all";
1515

16-
private static final String TYPE_TAG = "tag";
17-
private static final String TYPE_TAG_AND = "tag_and";
18-
private static final String TYPE_ALIAS = "alias";
19-
private static final String TYPE_SEGMENT = "segment";
20-
private static final String TYPE_REGISTRATION_ID = "registration_id";
21-
2216
private final boolean all;
2317
private final ImmutableSet<AudienceTarget> targets;
2418

0 commit comments

Comments
 (0)