diff --git a/README.md b/README.md index 02e097b3..abccdacd 100644 --- a/README.md +++ b/README.md @@ -301,3 +301,31 @@ LOG.info("Error Message: " + e.getErrorMessage()); } ``` + +### Custom Client 样例 + +> 一下片断来自项目代码里面的文件:example / cn.jpush.api.examples.ClientExample + +```Java + public static void testCustomClient() { + ClientConfig config = ClientConfig.getInstance(); + config.setMaxRetryTimes(5); + config.setConnectionTimeout(10 * 1000); // 10 seconds + config.setSSLVersion("TLSv1.1"); // JPush server supports SSLv3, TLSv1, TLSv1.1, TLSv1.2 + + JPushClient jPushClient = new JPushClient(masterSecret, appKey, null, config); + } + + public static void testCustomPushClient() { + ClientConfig config = ClientConfig.getInstance(); + config.setApnsProduction(false); // development env + config.setTimeToLive(60 * 60 * 24); // one day + + // config.setGlobalPushSetting(false, 60 * 60 * 24); // development env, one day + + JPushClient jPushClient = new JPushClient(masterSecret, appKey, null, config); // JPush client + + // PushClient pushClient = new PushClient(masterSecret, appKey, null, config); // push client only + + } +``` diff --git a/example/main/java/cn/jpush/api/examples/ClientExample.java b/example/main/java/cn/jpush/api/examples/ClientExample.java new file mode 100644 index 00000000..c397ba78 --- /dev/null +++ b/example/main/java/cn/jpush/api/examples/ClientExample.java @@ -0,0 +1,51 @@ +package cn.jpush.api.examples; + +import cn.jpush.api.JPushClient; +import cn.jpush.api.common.ClientConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ClientExample { + protected static final Logger LOG = LoggerFactory.getLogger(ClientExample.class); + + private static final String appKey = "dd1066407b044738b6479275"; + private static final String masterSecret = "e8cc9a76d5b7a580859bcfa7"; + + public static void main(String[] args) { +// testDefaultClient(); +// testCustomClient(); +// testCustomPushClient(); + } + + public static void testDefaultClient() { + JPushClient client = new JPushClient(masterSecret, appKey); + + // JPushClient client1 = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance()); + } + + public static void testCustomClient() { + ClientConfig config = ClientConfig.getInstance(); + config.setMaxRetryTimes(5); + config.setConnectionTimeout(10 * 1000); // 10 seconds + config.setSSLVersion("TLSv1.1"); // JPush server supports SSLv3, TLSv1, TLSv1.1, TLSv1.2 + + JPushClient jPushClient = new JPushClient(masterSecret, appKey, null, config); + } + + public static void testCustomPushClient() { + ClientConfig config = ClientConfig.getInstance(); + + config.setApnsProduction(false); // development env + config.setTimeToLive(60 * 60 * 24); // one day + + // config.setGlobalPushSetting(false, 60 * 60 * 24); // development env, one day + + JPushClient jPushClient = new JPushClient(masterSecret, appKey, null, config); // JPush client + + // PushClient pushClient = new PushClient(masterSecret, appKey, null, config); // push client only + + } + +} + + diff --git a/example/main/java/cn/jpush/api/examples/DevcieExample.java b/example/main/java/cn/jpush/api/examples/DeviceExample.java similarity index 94% rename from example/main/java/cn/jpush/api/examples/DevcieExample.java rename to example/main/java/cn/jpush/api/examples/DeviceExample.java index 5bcd0e56..5f3666f0 100644 --- a/example/main/java/cn/jpush/api/examples/DevcieExample.java +++ b/example/main/java/cn/jpush/api/examples/DeviceExample.java @@ -1,22 +1,21 @@ package cn.jpush.api.examples; -import cn.jpush.api.common.resp.DefaultResult; -import cn.jpush.api.device.OnlineStatus; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import cn.jpush.api.JPushClient; import cn.jpush.api.common.resp.APIConnectionException; import cn.jpush.api.common.resp.APIRequestException; +import cn.jpush.api.common.resp.DefaultResult; +import cn.jpush.api.device.OnlineStatus; import cn.jpush.api.device.TagAliasResult; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Map; -public class DevcieExample { - protected static final Logger LOG = LoggerFactory.getLogger(DevcieExample.class); +public class DeviceExample { + protected static final Logger LOG = LoggerFactory.getLogger(DeviceExample.class); private static final String appKey = "dd1066407b044738b6479275"; - private static final String masterSecret = "6b135be0037a5c1e693c3dfa"; + private static final String masterSecret = "e8cc9a76d5b7a580859bcfa7"; private static final String TAG1 = "tag1"; private static final String ALIAS1 = "alias1"; private static final String ALIAS2 = "alias2"; @@ -27,7 +26,7 @@ public class DevcieExample { public static void main(String[] args) { // testGetDeviceTagAlias(); - testGetUserOnlineStatus(); +// testGetUserOnlineStatus(); } public static void testGetDeviceTagAlias() { diff --git a/example/main/java/cn/jpush/api/examples/PushExample.java b/example/main/java/cn/jpush/api/examples/PushExample.java index 4a8f6f8a..ece51cc7 100644 --- a/example/main/java/cn/jpush/api/examples/PushExample.java +++ b/example/main/java/cn/jpush/api/examples/PushExample.java @@ -22,8 +22,8 @@ public class PushExample { protected static final Logger LOG = LoggerFactory.getLogger(PushExample.class); // demo App defined in resources/jpush-api.conf - private static final String appKey ="e5c0d34f58732cf09b2d4d74"; - private static final String masterSecret = "4cdda6d3c8b029941dbc5cb3"; + private static final String appKey ="dd1066407b044738b6479275"; + private static final String masterSecret = "e8cc9a76d5b7a580859bcfa7"; public static final String TITLE = "Test from API example"; public static final String ALERT = "Test from API Example - alert"; diff --git a/example/main/java/cn/jpush/api/examples/ReportsExample.java b/example/main/java/cn/jpush/api/examples/ReportsExample.java index 2482f7bb..37ccdab7 100644 --- a/example/main/java/cn/jpush/api/examples/ReportsExample.java +++ b/example/main/java/cn/jpush/api/examples/ReportsExample.java @@ -16,7 +16,7 @@ public class ReportsExample { // demo App defined in resources/jpush-api.conf private static final String appKey = "dd1066407b044738b6479275"; - private static final String masterSecret = "2b38ce69b1de2a7fa95706ea"; + private static final String masterSecret = "e8cc9a76d5b7a580859bcfa7"; public static void main(String[] args) { testGetReport(); diff --git a/example/main/java/cn/jpush/api/examples/ScheduleExample.java b/example/main/java/cn/jpush/api/examples/ScheduleExample.java index de4df8c4..98253799 100644 --- a/example/main/java/cn/jpush/api/examples/ScheduleExample.java +++ b/example/main/java/cn/jpush/api/examples/ScheduleExample.java @@ -17,8 +17,8 @@ public class ScheduleExample { protected static final Logger LOG = LoggerFactory.getLogger(ScheduleExample.class); - private static final String appKey ="e5c0d34f58732cf09b2d4d74"; - private static final String masterSecret = "4cdda6d3c8b029941dbc5cb3"; + private static final String appKey ="dd1066407b044738b6479275"; + private static final String masterSecret = "e8cc9a76d5b7a580859bcfa7"; public static void main(String[] args) { diff --git a/src/main/java/cn/jpush/api/JPushClient.java b/src/main/java/cn/jpush/api/JPushClient.java index 8e503551..dbdf8feb 100644 --- a/src/main/java/cn/jpush/api/JPushClient.java +++ b/src/main/java/cn/jpush/api/JPushClient.java @@ -54,14 +54,39 @@ public JPushClient(String masterSecret, String appKey) { _deviceClient = new DeviceClient(masterSecret, appKey); _scheduleClient = new ScheduleClient(masterSecret, appKey); } - + + /** + * Create a JPush Client by custom Client configuration. + * + * @param masterSecret API access secret of the appKey. + * @param appKey The KEY of one application on JPush. + * @param proxy The proxy, if there is no proxy, should be null. + * @param conf The client configuration. Can use ClientConfig.getInstance() as default. + */ + public JPushClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf) { + _pushClient = new PushClient(masterSecret, appKey, proxy, conf); + _reportClient = new ReportClient(masterSecret, appKey, proxy, conf); + _deviceClient = new DeviceClient(masterSecret, appKey, proxy, conf); + _scheduleClient = new ScheduleClient(masterSecret, appKey, proxy, conf); + } + + /** + * This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setMaxRetryTimes} instead of this constructor. + * + */ + @Deprecated public JPushClient(String masterSecret, String appKey, int maxRetryTimes) { _pushClient = new PushClient(masterSecret, appKey, maxRetryTimes); _reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes); _deviceClient = new DeviceClient(masterSecret, appKey, maxRetryTimes); _scheduleClient = new ScheduleClient(masterSecret, appKey, maxRetryTimes); } - + + /** + * This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setMaxRetryTimes} instead of this constructor. + * + */ + @Deprecated public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) { _pushClient = new PushClient(masterSecret, appKey, maxRetryTimes, proxy); _reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes, proxy); @@ -73,6 +98,7 @@ public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPr * Create a JPush Client by custom Client configuration. * * If you are using JPush privacy cloud, maybe this constructor is what you needed. + * This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setMaxRetryTimes} instead of this constructor. * * @param masterSecret API access secret of the appKey. * @param appKey The KEY of one application on JPush. @@ -80,18 +106,25 @@ public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPr * @param proxy The proxy, if there is no proxy, should be null. * @param conf The client configuration. Can use ClientConfig.getInstance() as default. */ + @Deprecated public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy, ClientConfig conf) { - _pushClient = new PushClient(masterSecret, appKey, maxRetryTimes, proxy, conf); - _reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes, proxy, conf); - _deviceClient = new DeviceClient(masterSecret, appKey, maxRetryTimes, proxy, conf); - _scheduleClient = new ScheduleClient(masterSecret, appKey, maxRetryTimes, proxy, conf); + conf.setMaxRetryTimes(maxRetryTimes); + + _pushClient = new PushClient(masterSecret, appKey, proxy, conf); + _reportClient = new ReportClient(masterSecret, appKey, proxy, conf); + _deviceClient = new DeviceClient(masterSecret, appKey, proxy, conf); + _scheduleClient = new ScheduleClient(masterSecret, appKey, proxy, conf); + } + + /** * Create a JPush Client by custom Client configuration with global settings. * * If you are using JPush privacy cloud, and you want different settings from default globally, * maybe this constructor is what you needed. + * This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setGlobalPushSetting} instead of this constructor. * * @param masterSecret API access secret of the appKey. * @param appKey The KEY of one application on JPush. @@ -101,33 +134,36 @@ public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPr * @param apnsProduction Global APNs environment setting. It will override PushPayload Options. * @param timeToLive Global time_to_live setting. It will override PushPayload Options. */ + @Deprecated public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy, ClientConfig conf, boolean apnsProduction, long timeToLive) { - _pushClient = new PushClient(masterSecret, appKey, maxRetryTimes, proxy, conf); - _reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes, proxy, conf); - _deviceClient = new DeviceClient(masterSecret, appKey, maxRetryTimes, proxy, conf); - _scheduleClient = new ScheduleClient(masterSecret, appKey, maxRetryTimes, proxy, conf); + conf.setMaxRetryTimes(maxRetryTimes); + _pushClient = new PushClient(masterSecret, appKey, proxy, conf); + _reportClient = new ReportClient(masterSecret, appKey, proxy, conf); + _deviceClient = new DeviceClient(masterSecret, appKey, proxy, conf); + _scheduleClient = new ScheduleClient(masterSecret, appKey, proxy, conf); _pushClient.setDefaults(apnsProduction, timeToLive); + } /** * Create a JPush Client with global settings. * * If you want different settings from default globally, this constructor is what you needed. - * + * This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setGlobalPushSetting} instead of this constructor. + * * @param masterSecret API access secret of the appKey. * @param appKey The KEY of one application on JPush. * @param apnsProduction Global APNs environment setting. It will override PushPayload Options. * @param timeToLive Global time_to_live setting. It will override PushPayload Options. */ + @Deprecated public JPushClient(String masterSecret, String appKey, boolean apnsProduction, long timeToLive) { _pushClient = new PushClient(masterSecret, appKey, apnsProduction, timeToLive); _reportClient = new ReportClient(masterSecret, appKey); _deviceClient = new DeviceClient(masterSecret, appKey); _scheduleClient = new ScheduleClient(masterSecret, appKey); } - - // ----------------------------- Push API /** diff --git a/src/main/java/cn/jpush/api/common/ClientConfig.java b/src/main/java/cn/jpush/api/common/ClientConfig.java index 97eaf9de..20163bf1 100644 --- a/src/main/java/cn/jpush/api/common/ClientConfig.java +++ b/src/main/java/cn/jpush/api/common/ClientConfig.java @@ -1,12 +1,9 @@ package cn.jpush.api.common; import java.util.HashMap; -import java.util.Map; public class ClientConfig extends HashMap { - private static ClientConfig instance = new ClientConfig(); - public static final String DEVICE_HOST_NAME = "device.host.name"; public static final Object DEVICE_HOST_NAME_SCHEMA = String.class; @@ -46,8 +43,48 @@ public class ClientConfig extends HashMap { public static final String SCHEDULE_PATH = "schedule.path"; public static final Object SCHEDULE_PATH_SCHEMA = String.class; + public static final String SSL_VERSION = "ssl.version"; + public static final Object SSL_VERSION_SCHEMA = String.class; + public static final String DEFAULT_SSL_VERSION = "TLS"; + + public static final String MAX_RETRY_TIMES = "max.retry.times"; + public static final Object MAX_RETRY_TIMES_SCHEMA = Integer.class; + public static final int DEFULT_MAX_RETRY_TIMES = 3; + + public static final String READ_TIMEOUT = "read.timeout"; + public static final Object READ_TIMEOUT_SCHEMA = Integer.class; + public static final int DEFAULT_READ_TIMEOUT = 30 * 1000; + + public static final String CONNECTION_TIMEOUT = "connection.timeout"; + public static final Object CONNECTION_TIMEOUT_SCHEMA = Integer.class; + public static final int DEFAULT_CONNECTION_TIMEOUT = 5 * 1000; + + /** + * Global APNs environment setting. + * Setting to -1, if you want to use PushPayload Options{@link cn.jpush.api.push.model.Options#apnsProduction}. + * Default value is -1. + * Setting to 0, if you want to use global setting as development environment. + * Setting to 1, if you want to use global setting as production environment. + * + */ + public static final String APNS_PRODUCTION = "apns.production"; + public static final Object APNS_PRODUCTION_SCHEMA = Integer.class; + public static final int DEFAULT_APNS_PRODUCTION = -1; + + /** + * Global time_to_live setting. Time unit is second. + * Setting to -1, if you want to use PushPayload Options{@link cn.jpush.api.push.model.Options#timeToLive}. + * Default value is -1. + * It will override PushPayload Options, while it is a positive integer value. + */ + public static final String TIME_TO_LIVE = "time.to.live"; + public static final Object TIME_TO_LIVE_SCHEMA = Long.class; + public static final long DEFAULT_TIME_TO_LIVE = -1; + + private static ClientConfig instance = new ClientConfig(); + private ClientConfig() { - super(12); + super(32); this.put(DEVICE_HOST_NAME, "https://device.jpush.cn"); this.put(DEVICES_PATH, "/v3/devices"); this.put(TAGS_PATH, "/v3/tags"); @@ -64,26 +101,27 @@ private ClientConfig() { this.put(SCHEDULE_HOST_NAME, "https://api.jpush.cn"); this.put(SCHEDULE_PATH, "/v3/schedules"); + + this.put(SSL_VERSION, DEFAULT_SSL_VERSION); + this.put(MAX_RETRY_TIMES, DEFULT_MAX_RETRY_TIMES); + this.put(READ_TIMEOUT, DEFAULT_READ_TIMEOUT); + this.put(CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); + + this.put(APNS_PRODUCTION, DEFAULT_APNS_PRODUCTION); + this.put(TIME_TO_LIVE, DEFAULT_TIME_TO_LIVE); + } public static ClientConfig getInstance() { return instance; } - public static void setDeviceHostName(Map conf, String hostName) { - conf.put(DEVICE_HOST_NAME, hostName); - } - /** * Setup custom device api host name, if using the JPush privacy cloud. * @param hostName the custom api host name, default is JPush domain name */ public void setDeviceHostName(String hostName) { - setDeviceHostName(this, hostName); - } - - public static void setPushHostName(Map conf, String hostName) { - conf.put(PUSH_HOST_NAME, hostName); + this.put(DEVICE_HOST_NAME, hostName); } /** @@ -91,11 +129,7 @@ public static void setPushHostName(Map conf, String hostName) { * @param hostName the custom api host name, default is JPush domain name */ public void setPushHostName(String hostName) { - setPushHostName(this, hostName); - } - - public static void setReportHostName(Map conf, String hostName) { - conf.put(REPORT_HOST_NAME, hostName); + this.put(PUSH_HOST_NAME, hostName); } /** @@ -103,15 +137,59 @@ public static void setReportHostName(Map conf, String hostName) { * @param hostName the custom api host name, default is JPush domain name */ public void setReportHostName(String hostName) { - setReportHostName(this, hostName); + this.put(REPORT_HOST_NAME, hostName); } - public static void setScheduleHostName(Map conf, String hostName) { - conf.put(SCHEDULE_HOST_NAME, hostName); + public void setScheduleHostName(String hostName) { + this.put(SCHEDULE_HOST_NAME, hostName); } - public void setScheduleHostName(String hostName) { - setScheduleHostName(this, hostName); + public void setSSLVersion(String sslVer) { + this.put(SSL_VERSION, sslVer); + } + + public void setMaxRetryTimes(int maxRetryTimes) { + this.put(MAX_RETRY_TIMES, maxRetryTimes); + } + + public void setReadTimeout(int readTimeout) { + this.put(READ_TIMEOUT, readTimeout); + } + + public void setConnectionTimeout(int connectionTimeout) { + this.put(CONNECTION_TIMEOUT, connectionTimeout); + } + + public String getSSLVersion() { + return (String) this.get(SSL_VERSION); } + public Integer getMaxRetryTimes() { + return (Integer) this.get(MAX_RETRY_TIMES); + } + + public Integer getReadTimeout() { + return (Integer) this.get(READ_TIMEOUT); + } + + public Integer getConnectionTimeout() { + return (Integer) this.get(CONNECTION_TIMEOUT); + } + + public void setApnsProduction(boolean production) { + if(production) { + this.put(APNS_PRODUCTION, 1); + } else { + this.put(APNS_PRODUCTION, 0); + } + } + + public void setTimeToLive(long timeToLive) { + this.put(TIME_TO_LIVE, timeToLive); + } + + public void setGlobalPushSetting(boolean apnsProduction, long timeToLive) { + setApnsProduction(apnsProduction); + setTimeToLive(timeToLive); + } } diff --git a/src/main/java/cn/jpush/api/common/connection/NativeHttpClient.java b/src/main/java/cn/jpush/api/common/connection/NativeHttpClient.java index ed216cd9..d822c681 100644 --- a/src/main/java/cn/jpush/api/common/connection/NativeHttpClient.java +++ b/src/main/java/cn/jpush/api/common/connection/NativeHttpClient.java @@ -1,5 +1,6 @@ package cn.jpush.api.common.connection; +import cn.jpush.api.common.ClientConfig; import cn.jpush.api.common.resp.APIConnectionException; import cn.jpush.api.common.resp.APIRequestException; import cn.jpush.api.common.resp.ResponseWrapper; @@ -14,6 +15,7 @@ import java.net.*; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; +import java.text.MessageFormat; /** * The implementation has no connection pool mechanism, used origin java connection. @@ -28,31 +30,34 @@ public class NativeHttpClient implements IHttpClient { private static final Logger LOG = LoggerFactory.getLogger(NativeHttpClient.class); private static final String KEYWORDS_CONNECT_TIMED_OUT = "connect timed out"; private static final String KEYWORDS_READ_TIMED_OUT = "Read timed out"; - - private int _maxRetryTimes = 0; + + private final int _connectionTimeout; + private final int _readTimeout; + private final int _maxRetryTimes; + private final String _sslVer; + private String _authCode; private HttpProxy _proxy; - /** - * 默认的重连次数是 3 - */ - public NativeHttpClient(String authCode) { - this(authCode, DEFAULT_MAX_RETRY_TIMES, null); - } - - public NativeHttpClient(String authCode, int maxRetryTimes, HttpProxy proxy) { - this._maxRetryTimes = maxRetryTimes; - LOG.info("Created instance with _maxRetryTimes = " + _maxRetryTimes); - - this._authCode = authCode; - this._proxy = proxy; - + public NativeHttpClient(String authCode, HttpProxy proxy, ClientConfig config ) { + _maxRetryTimes = config.getMaxRetryTimes(); + _connectionTimeout = config.getConnectionTimeout(); + _readTimeout = config.getReadTimeout(); + _sslVer = config.getSSLVersion(); + + _authCode = authCode; + _proxy = proxy; + + String message = MessageFormat.format("Created instance with " + + "connectionTimeout {0}, readTimeout {1}, maxRetryTimes {2}, SSL Version {3}", + _connectionTimeout, _readTimeout, _maxRetryTimes, _sslVer); + LOG.info(message); + if ( null != _proxy && _proxy.isAuthenticationNeeded()) { Authenticator.setDefault(new SimpleProxyAuthenticator( _proxy.getUsername(), _proxy.getPassword())); } - - initSSL(); + initSSL(_sslVer); } public ResponseWrapper sendGet(String url) @@ -133,8 +138,8 @@ private ResponseWrapper _doRequest(String url, String content, conn = (HttpURLConnection) aUrl.openConnection(); } - conn.setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT); - conn.setReadTimeout(DEFAULT_READ_TIMEOUT); + conn.setConnectTimeout(_connectionTimeout); + conn.setReadTimeout(_readTimeout); conn.setUseCaches(false); conn.setRequestMethod(method.name()); conn.setRequestProperty("User-Agent", JPUSH_USER_AGENT); @@ -257,11 +262,11 @@ private ResponseWrapper _doRequest(String url, String content, return wrapper; } - protected void initSSL() { + protected void initSSL(String sslVer) { TrustManager[] tmCerts = new javax.net.ssl.TrustManager[1]; tmCerts[0] = new SimpleTrustManager(); try { - SSLContext sslContext = SSLContext.getInstance("TLS"); + SSLContext sslContext = SSLContext.getInstance(sslVer); sslContext.init(null, tmCerts, null); HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); diff --git a/src/main/java/cn/jpush/api/device/DeviceClient.java b/src/main/java/cn/jpush/api/device/DeviceClient.java index b3e833aa..d1f21747 100644 --- a/src/main/java/cn/jpush/api/device/DeviceClient.java +++ b/src/main/java/cn/jpush/api/device/DeviceClient.java @@ -3,7 +3,6 @@ import cn.jpush.api.common.ClientConfig; import cn.jpush.api.common.ServiceHelper; import cn.jpush.api.common.connection.HttpProxy; -import cn.jpush.api.common.connection.IHttpClient; import cn.jpush.api.common.connection.NativeHttpClient; import cn.jpush.api.common.resp.*; import cn.jpush.api.utils.Preconditions; @@ -27,26 +26,47 @@ public class DeviceClient { private String aliasesPath; public DeviceClient(String masterSecret, String appKey) { - this(masterSecret, appKey, IHttpClient.DEFAULT_MAX_RETRY_TIMES); + this(masterSecret, appKey, null, ClientConfig.getInstance()); } - + + /** + * This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setMaxRetryTimes} instead of this constructor. + * + */ + @Deprecated public DeviceClient(String masterSecret, String appKey, int maxRetryTimes) { this(masterSecret, appKey, maxRetryTimes, null); } - + + /** + * This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setMaxRetryTimes} instead of this constructor. + * + */ + @Deprecated public DeviceClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) { - this(masterSecret, appKey, maxRetryTimes, proxy, ClientConfig.getInstance()); + ClientConfig conf = ClientConfig.getInstance(); + conf.setMaxRetryTimes(maxRetryTimes); + ServiceHelper.checkBasic(appKey, masterSecret); + + hostName = (String) conf.get(ClientConfig.DEVICE_HOST_NAME); + devicesPath = (String) conf.get(ClientConfig.DEVICES_PATH); + tagsPath = (String) conf.get(ClientConfig.TAGS_PATH); + aliasesPath = (String) conf.get(ClientConfig.ALIASES_PATH); + + String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret); + _httpClient = new NativeHttpClient(authCode, proxy, conf); + } /** + * Create a Device Client by client configuration. * * @param masterSecret API access secret of the appKey. * @param appKey The KEY of one application on JPush. - * @param maxRetryTimes Max retry times * @param proxy The proxy, if there is no proxy, should be null. * @param conf The client configuration. Can use ClientConfig.getInstance() as default. */ - public DeviceClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy, ClientConfig conf) { + public DeviceClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf) { ServiceHelper.checkBasic(appKey, masterSecret); hostName = (String) conf.get(ClientConfig.DEVICE_HOST_NAME); @@ -55,7 +75,7 @@ public DeviceClient(String masterSecret, String appKey, int maxRetryTimes, HttpP aliasesPath = (String) conf.get(ClientConfig.ALIASES_PATH); String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret); - _httpClient = new NativeHttpClient(authCode, maxRetryTimes, proxy); + _httpClient = new NativeHttpClient(authCode, proxy, conf); } // -------------- device diff --git a/src/main/java/cn/jpush/api/push/PushClient.java b/src/main/java/cn/jpush/api/push/PushClient.java index f87a2252..835aea67 100644 --- a/src/main/java/cn/jpush/api/push/PushClient.java +++ b/src/main/java/cn/jpush/api/push/PushClient.java @@ -3,7 +3,6 @@ import cn.jpush.api.common.ClientConfig; import cn.jpush.api.common.ServiceHelper; import cn.jpush.api.common.connection.HttpProxy; -import cn.jpush.api.common.connection.IHttpClient; import cn.jpush.api.common.connection.NativeHttpClient; import cn.jpush.api.common.resp.APIConnectionException; import cn.jpush.api.common.resp.APIRequestException; @@ -12,7 +11,6 @@ import cn.jpush.api.push.model.PushPayload; import cn.jpush.api.utils.Preconditions; import cn.jpush.api.utils.StringUtils; - import com.google.gson.JsonParseException; import com.google.gson.JsonParser; @@ -36,12 +34,10 @@ public class PushClient { private JsonParser _jsonParser = new JsonParser(); // If not present, true by default. - private boolean _apnsProduction = true; + private int _apnsProduction; // If not present, the default value is 86400(s) (one day) - private long _timeToLive = 60 * 60 * 24; - - private boolean _globalSettingEnabled = false; + private long _timeToLive; /** * Create a Push Client. @@ -50,33 +46,55 @@ public class PushClient { * @param appKey The KEY of one application on JPush. */ public PushClient(String masterSecret, String appKey) { - this(masterSecret, appKey, IHttpClient.DEFAULT_MAX_RETRY_TIMES); + this(masterSecret, appKey, null, ClientConfig.getInstance()); } - + + /** + * This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig} instead of this constructor. + */ + @Deprecated public PushClient(String masterSecret, String appKey, int maxRetryTimes) { this(masterSecret, appKey, maxRetryTimes, null); } /** * Create a Push Client with max retry times. - * + * This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig} instead of this constructor. + * * @param masterSecret API access secret of the appKey. * @param appKey The KEY of one application on JPush. * @param maxRetryTimes max retry times */ + @Deprecated public PushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) { - this(masterSecret, appKey, maxRetryTimes, proxy, ClientConfig.getInstance()); + ServiceHelper.checkBasic(appKey, masterSecret); + + ClientConfig conf = ClientConfig.getInstance(); + conf.setMaxRetryTimes(maxRetryTimes); + + this._baseUrl = (String) conf.get(ClientConfig.PUSH_HOST_NAME); + this._pushPath = (String) conf.get(ClientConfig.PUSH_PATH); + this._pushValidatePath = (String) conf.get(ClientConfig.PUSH_VALIDATE_PATH); + + this._apnsProduction = (Integer) conf.get(ClientConfig.APNS_PRODUCTION); + this._timeToLive = (Long) conf.get(ClientConfig.TIME_TO_LIVE); + + String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret); + this._httpClient = new NativeHttpClient(authCode, proxy, conf); } - public PushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy, ClientConfig conf) { + public PushClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf) { ServiceHelper.checkBasic(appKey, masterSecret); this._baseUrl = (String) conf.get(ClientConfig.PUSH_HOST_NAME); this._pushPath = (String) conf.get(ClientConfig.PUSH_PATH); this._pushValidatePath = (String) conf.get(ClientConfig.PUSH_VALIDATE_PATH); + this._apnsProduction = (Integer) conf.get(ClientConfig.APNS_PRODUCTION); + this._timeToLive = (Long) conf.get(ClientConfig.TIME_TO_LIVE); + String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret); - this._httpClient = new NativeHttpClient(authCode, maxRetryTimes, proxy); + this._httpClient = new NativeHttpClient(authCode, proxy, conf); } @@ -84,24 +102,38 @@ public PushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPro * Create a Push Client with global settings. * * If you want different settings from default globally, this constructor is what you needed. - * + * This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig} instead of this constructor. + * * @param masterSecret API access secret of the appKey. * @param appKey The KEY of one application on JPush. * @param apnsProduction Global APNs environment setting. It will override PushPayload Options. * @param timeToLive Global time_to_live setting. It will override PushPayload Options. */ + @Deprecated public PushClient(String masterSecret, String appKey, boolean apnsProduction, long timeToLive) { this(masterSecret, appKey); - - this._apnsProduction = apnsProduction; + if(apnsProduction) { + _apnsProduction = 1; + } else { + _apnsProduction = 0; + } this._timeToLive = timeToLive; - this._globalSettingEnabled = true; } - + + /** + * This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setGlobalPushSetting} instead of this method. + * + * @param apnsProduction Global APNs environment setting. It will override PushPayload Options. + * @param timeToLive Global time_to_live setting. It will override PushPayload Options. + */ + @Deprecated public void setDefaults(boolean apnsProduction, long timeToLive) { - this._apnsProduction = apnsProduction; + if(apnsProduction) { + _apnsProduction = 1; + } else { + _apnsProduction = 0; + } this._timeToLive = timeToLive; - this._globalSettingEnabled = true; } public void setBaseUrl(String baseUrl) { @@ -111,11 +143,16 @@ public void setBaseUrl(String baseUrl) { public PushResult sendPush(PushPayload pushPayload) throws APIConnectionException, APIRequestException { Preconditions.checkArgument(! (null == pushPayload), "pushPayload should not be null"); - if (_globalSettingEnabled) { + if (_apnsProduction > 0) { + pushPayload.resetOptionsApnsProduction(true); + } else if(_apnsProduction == 0) { + pushPayload.resetOptionsApnsProduction(false); + } + + if (_timeToLive >= 0) { pushPayload.resetOptionsTimeToLive(_timeToLive); - pushPayload.resetOptionsApnsProduction(_apnsProduction); } - + ResponseWrapper response = _httpClient.sendPost(_baseUrl + _pushPath, pushPayload.toString()); return BaseResult.fromResponse(response, PushResult.class); @@ -124,9 +161,14 @@ public PushResult sendPush(PushPayload pushPayload) throws APIConnectionExceptio public PushResult sendPushValidate(PushPayload pushPayload) throws APIConnectionException, APIRequestException { Preconditions.checkArgument(! (null == pushPayload), "pushPayload should not be null"); - if (_globalSettingEnabled) { + if (_apnsProduction > 0) { + pushPayload.resetOptionsApnsProduction(true); + } else if(_apnsProduction == 0) { + pushPayload.resetOptionsApnsProduction(false); + } + + if (_timeToLive >= 0) { pushPayload.resetOptionsTimeToLive(_timeToLive); - pushPayload.resetOptionsApnsProduction(_apnsProduction); } ResponseWrapper response = _httpClient.sendPost(_baseUrl + _pushValidatePath, pushPayload.toString()); @@ -162,7 +204,6 @@ public PushResult sendPushValidate(String payloadString) throws APIConnectionExc return BaseResult.fromResponse(response, PushResult.class); } - } diff --git a/src/main/java/cn/jpush/api/report/ReportClient.java b/src/main/java/cn/jpush/api/report/ReportClient.java index 5c09b31e..1ac997c3 100644 --- a/src/main/java/cn/jpush/api/report/ReportClient.java +++ b/src/main/java/cn/jpush/api/report/ReportClient.java @@ -1,13 +1,9 @@ package cn.jpush.api.report; -import java.net.URLEncoder; -import java.util.regex.Pattern; - import cn.jpush.api.common.ClientConfig; import cn.jpush.api.common.ServiceHelper; import cn.jpush.api.common.TimeUnit; import cn.jpush.api.common.connection.HttpProxy; -import cn.jpush.api.common.connection.IHttpClient; import cn.jpush.api.common.connection.NativeHttpClient; import cn.jpush.api.common.resp.APIConnectionException; import cn.jpush.api.common.resp.APIRequestException; @@ -15,6 +11,9 @@ import cn.jpush.api.common.resp.ResponseWrapper; import cn.jpush.api.utils.StringUtils; +import java.net.URLEncoder; +import java.util.regex.Pattern; + public class ReportClient { private final NativeHttpClient _httpClient; @@ -24,18 +23,39 @@ public class ReportClient { private String _messagePath; public ReportClient(String masterSecret, String appKey) { - this(masterSecret, appKey, IHttpClient.DEFAULT_MAX_RETRY_TIMES, null); + this(masterSecret, appKey, null, ClientConfig.getInstance()); } - + + /** + * This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setMaxRetryTimes} instead of this constructor. + * + */ + @Deprecated public ReportClient(String masterSecret, String appKey, int maxRetryTimes) { this(masterSecret, appKey, maxRetryTimes, null); } - + + /** + * This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setMaxRetryTimes} instead of this constructor. + * + */ + @Deprecated public ReportClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) { - this(masterSecret, appKey, maxRetryTimes, proxy, ClientConfig.getInstance()); + ServiceHelper.checkBasic(appKey, masterSecret); + + ClientConfig conf = ClientConfig.getInstance(); + conf.setMaxRetryTimes(maxRetryTimes); + + _hostName = (String) conf.get(ClientConfig.REPORT_HOST_NAME); + _receivePath = (String) conf.get(ClientConfig.REPORT_RECEIVE_PATH); + _userPath = (String) conf.get(ClientConfig.REPORT_USER_PATH); + _messagePath = (String) conf.get(ClientConfig.REPORT_MESSAGE_PATH); + + String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret); + _httpClient = new NativeHttpClient(authCode, proxy, conf); } - public ReportClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy, ClientConfig conf) { + public ReportClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf) { ServiceHelper.checkBasic(appKey, masterSecret); _hostName = (String) conf.get(ClientConfig.REPORT_HOST_NAME); @@ -44,7 +64,7 @@ public ReportClient(String masterSecret, String appKey, int maxRetryTimes, HttpP _messagePath = (String) conf.get(ClientConfig.REPORT_MESSAGE_PATH); String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret); - _httpClient = new NativeHttpClient(authCode, maxRetryTimes, proxy); + _httpClient = new NativeHttpClient(authCode, proxy, conf); } diff --git a/src/main/java/cn/jpush/api/schedule/ScheduleClient.java b/src/main/java/cn/jpush/api/schedule/ScheduleClient.java index 3219e748..fd492b14 100644 --- a/src/main/java/cn/jpush/api/schedule/ScheduleClient.java +++ b/src/main/java/cn/jpush/api/schedule/ScheduleClient.java @@ -3,7 +3,6 @@ import cn.jpush.api.common.ClientConfig; import cn.jpush.api.common.ServiceHelper; import cn.jpush.api.common.connection.HttpProxy; -import cn.jpush.api.common.connection.IHttpClient; import cn.jpush.api.common.connection.NativeHttpClient; import cn.jpush.api.common.resp.APIConnectionException; import cn.jpush.api.common.resp.APIRequestException; @@ -23,32 +22,50 @@ public class ScheduleClient { private String schedulePath; public ScheduleClient(String masterSecret, String appkey) { - this(masterSecret, appkey, IHttpClient.DEFAULT_MAX_RETRY_TIMES, null, ClientConfig.getInstance()); + this(masterSecret, appkey, null, ClientConfig.getInstance()); } + /** + * This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setMaxRetryTimes} instead of this constructor. + * + */ + @Deprecated public ScheduleClient(String masterSecret, String appKey, int maxRetryTimes) { - this(masterSecret, appKey, maxRetryTimes, null, ClientConfig.getInstance()); + this(masterSecret, appKey, maxRetryTimes, null); } + /** + * This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setMaxRetryTimes} instead of this constructor. + * + */ + @Deprecated public ScheduleClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) { - this(masterSecret, appKey, maxRetryTimes, proxy, ClientConfig.getInstance()); + ServiceHelper.checkBasic(appKey, masterSecret); + + ClientConfig conf = ClientConfig.getInstance(); + conf.setMaxRetryTimes(maxRetryTimes); + + hostName = (String) conf.get(ClientConfig.SCHEDULE_HOST_NAME); + schedulePath = (String) conf.get(ClientConfig.SCHEDULE_PATH); + + String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret); + this._httpClient = new NativeHttpClient(authCode, proxy, conf); } /** * Create a Schedule Client with custom configuration. * @param masterSecret API access secret of the appKey. * @param appKey The KEY of one application on JPush. - * @param maxRetryTimes Max retry times * @param proxy The proxy, if there is no proxy, should be null. * @param conf The client configuration. Can use ClientConfig.getInstance() as default. */ - public ScheduleClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy, ClientConfig conf) { + public ScheduleClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf) { ServiceHelper.checkBasic(appKey, masterSecret); hostName = (String) conf.get(ClientConfig.SCHEDULE_HOST_NAME); schedulePath = (String) conf.get(ClientConfig.SCHEDULE_PATH); String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret); - this._httpClient = new NativeHttpClient(authCode, maxRetryTimes, proxy); + this._httpClient = new NativeHttpClient(authCode, proxy, conf); } public ScheduleResult createSchedule(SchedulePayload payload) throws APIConnectionException, APIRequestException {