Skip to content

Commit 2fabb7d

Browse files
committed
add ssl version to ClientConfig
1 parent dcd48a8 commit 2fabb7d

File tree

8 files changed

+231
-75
lines changed

8 files changed

+231
-75
lines changed

example/main/java/cn/jpush/api/examples/DevcieExample.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package cn.jpush.api.examples;
22

3-
import cn.jpush.api.common.resp.DefaultResult;
4-
import cn.jpush.api.device.OnlineStatus;
5-
import org.slf4j.Logger;
6-
import org.slf4j.LoggerFactory;
7-
83
import cn.jpush.api.JPushClient;
4+
import cn.jpush.api.common.ClientConfig;
95
import cn.jpush.api.common.resp.APIConnectionException;
106
import cn.jpush.api.common.resp.APIRequestException;
7+
import cn.jpush.api.common.resp.DefaultResult;
8+
import cn.jpush.api.device.OnlineStatus;
119
import cn.jpush.api.device.TagAliasResult;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
1212

1313
import java.util.Map;
1414

@@ -27,7 +27,8 @@ public class DevcieExample {
2727

2828
public static void main(String[] args) {
2929
// testGetDeviceTagAlias();
30-
testGetUserOnlineStatus();
30+
// testGetUserOnlineStatus();
31+
testCustomClient();
3132
}
3233

3334
public static void testGetDeviceTagAlias() {
@@ -77,6 +78,17 @@ public static void testBindMobile() {
7778
LOG.info("Error Message: " + e.getErrorMessage());
7879
}
7980
}
81+
82+
public static void testCustomClient() {
83+
ClientConfig config = ClientConfig.getInstance();
84+
config.setMaxRetryTimes(5);
85+
config.setConnectionTimeout(10 * 1000);// 10 seconds
86+
config.setSSLVersion("TLSv1.1");
87+
88+
ClientConfig.setReadTimeout(ClientConfig.getInstance(), 30 * 1000);// 30 seconds
89+
90+
JPushClient jPushClient = new JPushClient(masterSecret, appKey, null, config);
91+
}
8092

8193
}
8294

src/main/java/cn/jpush/api/JPushClient.java

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,16 @@ public JPushClient(String masterSecret, String appKey) {
5454
_deviceClient = new DeviceClient(masterSecret, appKey);
5555
_scheduleClient = new ScheduleClient(masterSecret, appKey);
5656
}
57-
57+
58+
@Deprecated
5859
public JPushClient(String masterSecret, String appKey, int maxRetryTimes) {
5960
_pushClient = new PushClient(masterSecret, appKey, maxRetryTimes);
6061
_reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes);
6162
_deviceClient = new DeviceClient(masterSecret, appKey, maxRetryTimes);
6263
_scheduleClient = new ScheduleClient(masterSecret, appKey, maxRetryTimes);
6364
}
64-
65+
66+
@Deprecated
6567
public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) {
6668
_pushClient = new PushClient(masterSecret, appKey, maxRetryTimes, proxy);
6769
_reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes, proxy);
@@ -81,10 +83,15 @@ public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPr
8183
* @param conf The client configuration. Can use ClientConfig.getInstance() as default.
8284
*/
8385
public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy, ClientConfig conf) {
84-
_pushClient = new PushClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
85-
_reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
86-
_deviceClient = new DeviceClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
87-
_scheduleClient = new ScheduleClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
86+
this(masterSecret, appKey, proxy, conf);
87+
conf.setMaxRetryTimes(maxRetryTimes);
88+
}
89+
90+
public JPushClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf) {
91+
_pushClient = new PushClient(masterSecret, appKey, proxy, conf);
92+
_reportClient = new ReportClient(masterSecret, appKey, proxy, conf);
93+
_deviceClient = new DeviceClient(masterSecret, appKey, proxy, conf);
94+
_scheduleClient = new ScheduleClient(masterSecret, appKey, proxy, conf);
8895
}
8996

9097
/**
@@ -101,12 +108,33 @@ public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPr
101108
* @param apnsProduction Global APNs environment setting. It will override PushPayload Options.
102109
* @param timeToLive Global time_to_live setting. It will override PushPayload Options.
103110
*/
111+
@Deprecated
104112
public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy, ClientConfig conf,
105113
boolean apnsProduction, long timeToLive) {
106-
_pushClient = new PushClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
107-
_reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
108-
_deviceClient = new DeviceClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
109-
_scheduleClient = new ScheduleClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
114+
this(masterSecret, appKey, proxy, conf, apnsProduction, timeToLive);
115+
conf.setMaxRetryTimes(maxRetryTimes);
116+
117+
}
118+
119+
/**
120+
* Create a JPush Client by custom Client configuration with global settings.
121+
*
122+
* If you are using JPush privacy cloud, and you want different settings from default globally,
123+
* maybe this constructor is what you needed.
124+
*
125+
* @param masterSecret API access secret of the appKey.
126+
* @param appKey The KEY of one application on JPush.
127+
* @param proxy The proxy, if there is no proxy, should be null.
128+
* @param conf The client configuration. Can use ClientConfig.getInstance() as default.
129+
* @param apnsProduction Global APNs environment setting. It will override PushPayload Options.
130+
* @param timeToLive Global time_to_live setting. It will override PushPayload Options.
131+
*/
132+
public JPushClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf,
133+
boolean apnsProduction, long timeToLive) {
134+
_pushClient = new PushClient(masterSecret, appKey, proxy, conf);
135+
_reportClient = new ReportClient(masterSecret, appKey, proxy, conf);
136+
_deviceClient = new DeviceClient(masterSecret, appKey, proxy, conf);
137+
_scheduleClient = new ScheduleClient(masterSecret, appKey, proxy, conf);
110138
_pushClient.setDefaults(apnsProduction, timeToLive);
111139
}
112140

@@ -126,8 +154,6 @@ public JPushClient(String masterSecret, String appKey, boolean apnsProduction, l
126154
_deviceClient = new DeviceClient(masterSecret, appKey);
127155
_scheduleClient = new ScheduleClient(masterSecret, appKey);
128156
}
129-
130-
131157
// ----------------------------- Push API
132158

133159
/**

src/main/java/cn/jpush/api/common/ClientConfig.java

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
public class ClientConfig extends HashMap<String, Object> {
77

8-
private static ClientConfig instance = new ClientConfig();
9-
108
public static final String DEVICE_HOST_NAME = "device.host.name";
119
public static final Object DEVICE_HOST_NAME_SCHEMA = String.class;
1210

@@ -46,8 +44,26 @@ public class ClientConfig extends HashMap<String, Object> {
4644
public static final String SCHEDULE_PATH = "schedule.path";
4745
public static final Object SCHEDULE_PATH_SCHEMA = String.class;
4846

47+
public static final String SSL_VERSION = "ssl.version";
48+
public static final Object SSL_VERSION_SCHEMA = String.class;
49+
public static final String DEFAULT_SSL_VERSION = "TLS";
50+
51+
public static final String MAX_RETRY_TIMES = "max.retry.times";
52+
public static final Object MAX_RETRY_TIMES_SCHEMA = Integer.class;
53+
public static final int DEFULT_MAX_RETRY_TIMES = 3;
54+
55+
public static final String READ_TIMEOUT = "read.timeout";
56+
public static final Object READ_TIMEOUT_SCHEMA = Integer.class;
57+
public static final int DEFAULT_READ_TIMEOUT = 30 * 1000;
58+
59+
public static final String CONNECTION_TIMEOUT = "connection.timeout";
60+
public static final Object CONNECTION_TIMEOUT_SCHEMA = Integer.class;
61+
public static final int DEFAULT_CONNECTION_TIMEOUT = 5 * 1000;
62+
63+
private static ClientConfig instance = new ClientConfig();
64+
4965
private ClientConfig() {
50-
super(12);
66+
super(20);
5167
this.put(DEVICE_HOST_NAME, "https://device.jpush.cn");
5268
this.put(DEVICES_PATH, "/v3/devices");
5369
this.put(TAGS_PATH, "/v3/tags");
@@ -64,6 +80,12 @@ private ClientConfig() {
6480

6581
this.put(SCHEDULE_HOST_NAME, "https://api.jpush.cn");
6682
this.put(SCHEDULE_PATH, "/v3/schedules");
83+
84+
this.put(SSL_VERSION, DEFAULT_SSL_VERSION);
85+
this.put(MAX_RETRY_TIMES, DEFULT_MAX_RETRY_TIMES);
86+
this.put(READ_TIMEOUT, DEFAULT_READ_TIMEOUT);
87+
this.put(CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
88+
6789
}
6890

6991
public static ClientConfig getInstance() {
@@ -114,4 +136,52 @@ public void setScheduleHostName(String hostName) {
114136
setScheduleHostName(this, hostName);
115137
}
116138

139+
public void setSSLVersion(String sslVer) {
140+
setSSLVersion(this, sslVer);
141+
}
142+
143+
public static void setSSLVersion(Map conf, String sslVer) {
144+
conf.put(SSL_VERSION, sslVer);
145+
}
146+
147+
public void setMaxRetryTimes(int maxRetryTimes) {
148+
setMaxRetryTimes(this, maxRetryTimes);
149+
}
150+
151+
public static void setMaxRetryTimes(Map conf, int maxRetryTimes) {
152+
conf.put(MAX_RETRY_TIMES, maxRetryTimes);
153+
}
154+
155+
public void setReadTimeout(int readTimeout) {
156+
setReadTimeout(this, readTimeout);
157+
}
158+
159+
public static void setReadTimeout(Map conf, int readTimeout) {
160+
conf.put(READ_TIMEOUT, readTimeout);
161+
}
162+
163+
public void setConnectionTimeout(int connectionTimeout) {
164+
setConnectionTimeout(this, connectionTimeout);
165+
}
166+
167+
public static void setConnectionTimeout(Map conf, int connectionTimeout) {
168+
conf.put(CONNECTION_TIMEOUT, connectionTimeout);
169+
}
170+
171+
public String getSSLVersion() {
172+
return (String) this.get(SSL_VERSION);
173+
}
174+
175+
public Integer getMaxRetryTimes() {
176+
return (Integer) this.get(MAX_RETRY_TIMES);
177+
}
178+
179+
public Integer getReadTimeout() {
180+
return (Integer) this.get(READ_TIMEOUT);
181+
}
182+
183+
public Integer getConnectionTimeout() {
184+
return (Integer) this.get(CONNECTION_TIMEOUT);
185+
}
186+
117187
}

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

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cn.jpush.api.common.connection;
22

3+
import cn.jpush.api.common.ClientConfig;
34
import cn.jpush.api.common.resp.APIConnectionException;
45
import cn.jpush.api.common.resp.APIRequestException;
56
import cn.jpush.api.common.resp.ResponseWrapper;
@@ -14,6 +15,7 @@
1415
import java.net.*;
1516
import java.security.cert.CertificateException;
1617
import java.security.cert.X509Certificate;
18+
import java.text.MessageFormat;
1719

1820
/**
1921
* The implementation has no connection pool mechanism, used origin java connection.
@@ -28,31 +30,34 @@ public class NativeHttpClient implements IHttpClient {
2830
private static final Logger LOG = LoggerFactory.getLogger(NativeHttpClient.class);
2931
private static final String KEYWORDS_CONNECT_TIMED_OUT = "connect timed out";
3032
private static final String KEYWORDS_READ_TIMED_OUT = "Read timed out";
31-
32-
private int _maxRetryTimes = 0;
33+
34+
private final int _connectionTimeout;
35+
private final int _readTimeout;
36+
private final int _maxRetryTimes;
37+
private final String _sslVer;
38+
3339
private String _authCode;
3440
private HttpProxy _proxy;
3541

36-
/**
37-
* 默认的重连次数是 3
38-
*/
39-
public NativeHttpClient(String authCode) {
40-
this(authCode, DEFAULT_MAX_RETRY_TIMES, null);
41-
}
42-
43-
public NativeHttpClient(String authCode, int maxRetryTimes, HttpProxy proxy) {
44-
this._maxRetryTimes = maxRetryTimes;
45-
LOG.info("Created instance with _maxRetryTimes = " + _maxRetryTimes);
46-
47-
this._authCode = authCode;
48-
this._proxy = proxy;
49-
42+
public NativeHttpClient(String authCode, HttpProxy proxy, ClientConfig config ) {
43+
_maxRetryTimes = config.getMaxRetryTimes();
44+
_connectionTimeout = config.getConnectionTimeout();
45+
_readTimeout = config.getReadTimeout();
46+
_sslVer = config.getSSLVersion();
47+
48+
_authCode = authCode;
49+
_proxy = proxy;
50+
51+
String message = MessageFormat.format("Created instance with "
52+
+ "connectionTimeout {0}, readTimeout {1}, maxRetryTimes {2}, SSL Version {3}",
53+
_connectionTimeout, _readTimeout, _maxRetryTimes, _sslVer);
54+
LOG.info(message);
55+
5056
if ( null != _proxy && _proxy.isAuthenticationNeeded()) {
5157
Authenticator.setDefault(new SimpleProxyAuthenticator(
5258
_proxy.getUsername(), _proxy.getPassword()));
5359
}
54-
55-
initSSL();
60+
initSSL(_sslVer);
5661
}
5762

5863
public ResponseWrapper sendGet(String url)
@@ -133,8 +138,8 @@ private ResponseWrapper _doRequest(String url, String content,
133138
conn = (HttpURLConnection) aUrl.openConnection();
134139
}
135140

136-
conn.setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT);
137-
conn.setReadTimeout(DEFAULT_READ_TIMEOUT);
141+
conn.setConnectTimeout(_connectionTimeout);
142+
conn.setReadTimeout(_readTimeout);
138143
conn.setUseCaches(false);
139144
conn.setRequestMethod(method.name());
140145
conn.setRequestProperty("User-Agent", JPUSH_USER_AGENT);
@@ -257,11 +262,11 @@ private ResponseWrapper _doRequest(String url, String content,
257262
return wrapper;
258263
}
259264

260-
protected void initSSL() {
265+
protected void initSSL(String sslVer) {
261266
TrustManager[] tmCerts = new javax.net.ssl.TrustManager[1];
262267
tmCerts[0] = new SimpleTrustManager();
263268
try {
264-
SSLContext sslContext = SSLContext.getInstance("TLS");
269+
SSLContext sslContext = SSLContext.getInstance(sslVer);
265270
sslContext.init(null, tmCerts, null);
266271
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
267272

src/main/java/cn/jpush/api/device/DeviceClient.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import cn.jpush.api.common.ClientConfig;
44
import cn.jpush.api.common.ServiceHelper;
55
import cn.jpush.api.common.connection.HttpProxy;
6-
import cn.jpush.api.common.connection.IHttpClient;
76
import cn.jpush.api.common.connection.NativeHttpClient;
87
import cn.jpush.api.common.resp.*;
98
import cn.jpush.api.utils.Preconditions;
@@ -27,26 +26,39 @@ public class DeviceClient {
2726
private String aliasesPath;
2827

2928
public DeviceClient(String masterSecret, String appKey) {
30-
this(masterSecret, appKey, IHttpClient.DEFAULT_MAX_RETRY_TIMES);
29+
this(masterSecret, appKey, null, ClientConfig.getInstance());
3130
}
32-
31+
32+
@Deprecated
3333
public DeviceClient(String masterSecret, String appKey, int maxRetryTimes) {
3434
this(masterSecret, appKey, maxRetryTimes, null);
3535
}
36-
36+
37+
@Deprecated
3738
public DeviceClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) {
38-
this(masterSecret, appKey, maxRetryTimes, proxy, ClientConfig.getInstance());
39+
ClientConfig conf = ClientConfig.getInstance();
40+
conf.setMaxRetryTimes(maxRetryTimes);
41+
ServiceHelper.checkBasic(appKey, masterSecret);
42+
43+
hostName = (String) conf.get(ClientConfig.DEVICE_HOST_NAME);
44+
devicesPath = (String) conf.get(ClientConfig.DEVICES_PATH);
45+
tagsPath = (String) conf.get(ClientConfig.TAGS_PATH);
46+
aliasesPath = (String) conf.get(ClientConfig.ALIASES_PATH);
47+
48+
String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
49+
_httpClient = new NativeHttpClient(authCode, proxy, conf);
50+
3951
}
4052

4153
/**
54+
* Create a Device Client by client configuration.
4255
*
4356
* @param masterSecret API access secret of the appKey.
4457
* @param appKey The KEY of one application on JPush.
45-
* @param maxRetryTimes Max retry times
4658
* @param proxy The proxy, if there is no proxy, should be null.
4759
* @param conf The client configuration. Can use ClientConfig.getInstance() as default.
4860
*/
49-
public DeviceClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy, ClientConfig conf) {
61+
public DeviceClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf) {
5062
ServiceHelper.checkBasic(appKey, masterSecret);
5163

5264
hostName = (String) conf.get(ClientConfig.DEVICE_HOST_NAME);
@@ -55,7 +67,7 @@ public DeviceClient(String masterSecret, String appKey, int maxRetryTimes, HttpP
5567
aliasesPath = (String) conf.get(ClientConfig.ALIASES_PATH);
5668

5769
String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
58-
_httpClient = new NativeHttpClient(authCode, maxRetryTimes, proxy);
70+
_httpClient = new NativeHttpClient(authCode, proxy, conf);
5971
}
6072

6173
// -------------- device

0 commit comments

Comments
 (0)