diff --git a/README.md b/README.md index e6aafe18..005ab260 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ cn.jpush.api jpush-client - 3.2.5 + 3.2.6 ``` ### jar 包方式 diff --git a/example/main/java/cn/jpush/api/examples/PushExample.java b/example/main/java/cn/jpush/api/examples/PushExample.java index b26c4031..77150e42 100644 --- a/example/main/java/cn/jpush/api/examples/PushExample.java +++ b/example/main/java/cn/jpush/api/examples/PushExample.java @@ -1,5 +1,6 @@ package cn.jpush.api.examples; +import cn.jpush.api.common.ClientConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -21,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 ="dd1066407b044738b6479275"; - private static final String masterSecret = "2b38ce69b1de2a7fa95706ea"; + private static final String appKey ="e5c0d34f58732cf09b2d4d74"; + private static final String masterSecret = "4cdda6d3c8b029941dbc5cb3"; public static final String TITLE = "Test from API example"; public static final String ALERT = "Test from API Example - alert"; @@ -31,7 +32,7 @@ public class PushExample { public static final String TAG = "tag_api"; public static void main(String[] args) { - testSendPush(); + testSendPushWithCustomConfig(); } @@ -126,7 +127,32 @@ public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() { .build()) .build(); } - - + + public static void testSendPushWithCustomConfig() { + ClientConfig config = ClientConfig.getInstance(); + // Setup the custom hostname + config.setPushHostName("https://api.jpush.cn"); + + JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3, null, config); + + // For push, all you need do is to build PushPayload object. + PushPayload payload = buildPushObject_all_all_alert(); + + try { + PushResult result = jpushClient.sendPush(payload); + LOG.info("Got result - " + result); + + } catch (APIConnectionException e) { + LOG.error("Connection error. Should retry later. ", e); + + } catch (APIRequestException e) { + LOG.error("Error response from JPush server. Should review and fix it. ", e); + LOG.info("HTTP Status: " + e.getStatus()); + LOG.info("Error Code: " + e.getErrorCode()); + LOG.info("Error Message: " + e.getErrorMessage()); + LOG.info("Msg ID: " + e.getMsgId()); + } + } + } diff --git a/pom.xml b/pom.xml index 1fe0a7cb..4f87e288 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ cn.jpush.api jpush-client - 3.2.5-SNAPSHOT + 3.2.6-SNAPSHOT jar https://github.com/jpush/jpush-api-java-client JPush API Java Client diff --git a/src/main/java/cn/jpush/api/JPushClient.java b/src/main/java/cn/jpush/api/JPushClient.java index e5933fb6..b1da450e 100644 --- a/src/main/java/cn/jpush/api/JPushClient.java +++ b/src/main/java/cn/jpush/api/JPushClient.java @@ -3,6 +3,7 @@ import java.util.Map; import java.util.Set; +import cn.jpush.api.common.ClientConfig; import cn.jpush.api.common.TimeUnit; import cn.jpush.api.common.connection.HttpProxy; import cn.jpush.api.common.resp.APIConnectionException; @@ -56,6 +57,45 @@ public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPr _reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes, proxy); _deviceClient = new DeviceClient(masterSecret, appKey, maxRetryTimes, proxy); } + + /** + * Create a JPush Client by custom Client configuration. + * + * If you are using JPush privacy cloud, maybe this constructor is what you needed. + * + * @param masterSecret API access secret of the appKey. + * @param appKey The KEY of one application on JPush. + * @param maxRetryTimes Client request 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 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); + } + + /** + * 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. + * + * @param masterSecret API access secret of the appKey. + * @param appKey The KEY of one application on JPush. + * @param maxRetryTimes Client request 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. + * @param apnsProduction Global APNs environment setting. It will override PushPayload Options. + * @param timeToLive Global time_to_live setting. It will override PushPayload Options. + */ + 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); + _pushClient.setDefaults(apnsProduction, timeToLive); + } /** * Create a JPush Client with global settings. diff --git a/src/main/java/cn/jpush/api/common/ClientConfig.java b/src/main/java/cn/jpush/api/common/ClientConfig.java new file mode 100644 index 00000000..fe43f3b7 --- /dev/null +++ b/src/main/java/cn/jpush/api/common/ClientConfig.java @@ -0,0 +1,101 @@ +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; + + public static final String DEVICES_PATH = "devices.path"; + public static final Object DEVICES_PATH_SCHEMA = String.class; + + public static final String TAGS_PATH = "tags.path"; + public static final Object TAGS_PATH_SCHEMA = String.class; + + public static final String ALIASES_PATH = "aliases.path"; + public static final Object ALIASES_PATH_SCHEMA = String.class; + + public static final String PUSH_HOST_NAME = "push.host.name"; + public static final Object PUSH_HOST_NAME_SCHEMA = String.class; + + public static final String PUSH_PATH = "push.path"; + public static final Object PUSH_PATH_SCHEMA = String.class; + + public static final String PUSH_VALIDATE_PATH = "push.validate.path"; + public static final Object PUSH_VALIDATE_PATH_SCHMEA = String.class; + + public static final String REPORT_HOST_NAME = "report.host.name"; + public static final Object REPORT_HOST_NAME_SCHEMA = String.class; + + public static final String REPORT_RECEIVE_PATH = "report.receive.path"; + public static final Object REPORT_RECEIVE_PATH_SCHEMA = String.class; + + public static final String REPORT_USER_PATH = "report.user.path"; + public static final Object REPORT_USER_PATH_SCHEMA = String.class; + + public static final String REPORT_MESSAGE_PATH = "report.message.path"; + public static final Object REPORT_MESSAGE_PATH_SCHEMA = String.class; + + private ClientConfig() { + super(); + this.put(DEVICE_HOST_NAME, "https://device.jpush.cn"); + this.put(DEVICES_PATH, "/v3/devices"); + this.put(TAGS_PATH, "/v3/tags"); + this.put(ALIASES_PATH, "/v3/aliases"); + + this.put(PUSH_HOST_NAME, "https://api.jpush.cn"); + this.put(PUSH_PATH, "/v3/push"); + this.put(PUSH_VALIDATE_PATH, "/v3/push/validate"); + + this.put(REPORT_HOST_NAME, "https://report.jpush.cn"); + this.put(REPORT_RECEIVE_PATH, "/v3/received"); + this.put(REPORT_USER_PATH, "/v3/users"); + this.put(REPORT_MESSAGE_PATH, "/v3/messages"); + } + + 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); + } + + /** + * Setup custom push api host name, if using the JPush privacy cloud. + * @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); + } + + /** + * Setup custom report api host name, if using the JPush privacy cloud. + * @param hostName the custom api host name, default is JPush domain name + */ + public void setReportHostName(String hostName) { + setReportHostName(this, hostName); + } + +} diff --git a/src/main/java/cn/jpush/api/common/resp/APIRequestException.java b/src/main/java/cn/jpush/api/common/resp/APIRequestException.java index cc4d0b8c..341e436b 100644 --- a/src/main/java/cn/jpush/api/common/resp/APIRequestException.java +++ b/src/main/java/cn/jpush/api/common/resp/APIRequestException.java @@ -31,7 +31,7 @@ public long getMsgId() { public int getErrorCode() { ErrorObject eo = getErrorObject(); - if (null != eo) { + if (null != eo && null != eo.error) { return eo.error.code; } return -1; @@ -39,7 +39,7 @@ public int getErrorCode() { public String getErrorMessage() { ErrorObject eo = getErrorObject(); - if (null != eo) { + if (null != eo && null != eo.error) { return eo.error.message; } return null; diff --git a/src/main/java/cn/jpush/api/common/resp/ResponseWrapper.java b/src/main/java/cn/jpush/api/common/resp/ResponseWrapper.java index bfcbdb54..81f4890b 100644 --- a/src/main/java/cn/jpush/api/common/resp/ResponseWrapper.java +++ b/src/main/java/cn/jpush/api/common/resp/ResponseWrapper.java @@ -1,16 +1,20 @@ package cn.jpush.api.common.resp; +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import com.google.gson.JsonSyntaxException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.gson.Gson; - public class ResponseWrapper { private static final Logger LOG = LoggerFactory.getLogger(ResponseWrapper.class); private static final int RESPONSE_CODE_NONE = -1; private static Gson _gson = new Gson(); + private static JsonParser jsonParser = new JsonParser(); public int responseCode = RESPONSE_CODE_NONE; public String responseContent; @@ -36,21 +40,43 @@ public void setRateLimit(String quota, String remaining, String reset) { } public void setErrorObject() { + error = new ErrorObject(); + error.error = new ErrorEntity(); try { - error = _gson.fromJson(responseContent, ErrorObject.class); - } catch (JsonSyntaxException e) { - int index = responseContent.indexOf("error"); - if( -1 != index ) { - int from = responseContent.indexOf("{", index); - int to = responseContent.indexOf("}", from); - String errorStr = responseContent.substring(from, to + 1); - error = new ErrorObject(); - try { - error.error = _gson.fromJson(errorStr, ErrorEntity.class); - } catch (JsonSyntaxException e1) { - LOG.error("unknown response content:" + responseContent, e); + JsonElement element = jsonParser.parse(responseContent); + JsonObject errorObj = null; + if( element instanceof JsonArray) { + JsonArray array = (JsonArray) element; + for(int i = 0; i < array.size(); i++) { + if(array.get(i).getAsString().contains("error")) { + errorObj = array.get(i).getAsJsonObject(); + break; + } } + } else if(element instanceof JsonObject) { + errorObj = (JsonObject) element; + } else { + // nothing } + if(null != errorObj) { + JsonObject errorMsg = errorObj; + if(errorObj.has("msg_id")) { + error.msg_id = errorObj.get("msg_id").getAsLong(); + } + if (errorObj.has("error")) { + errorMsg = (JsonObject) errorObj.get("error"); + } + if(errorMsg.has("code")) { + error.error.code = errorMsg.get("code").getAsInt(); + } + if(errorMsg.has("message")) { + error.error.message = errorMsg.get("message").getAsString(); + } + } + } catch(JsonSyntaxException e) { + LOG.error("Unexpected - responseContent:" + responseContent, e); + } catch (Exception e) { + LOG.error("Unexpected - responseContent:" + responseContent, e); } } @@ -75,9 +101,9 @@ public class ErrorEntity { public String message; @Override - public String toString() { - return _gson.toJson(this); - } + public String toString() { + return _gson.toJson(this); + } } } diff --git a/src/main/java/cn/jpush/api/device/DeviceClient.java b/src/main/java/cn/jpush/api/device/DeviceClient.java index 5d7ba057..0f4d1edf 100644 --- a/src/main/java/cn/jpush/api/device/DeviceClient.java +++ b/src/main/java/cn/jpush/api/device/DeviceClient.java @@ -2,6 +2,7 @@ import java.util.Set; +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; @@ -19,12 +20,12 @@ import com.google.gson.JsonPrimitive; public class DeviceClient { - public static final String HOST_NAME_SSL = "https://device.jpush.cn"; - public static final String DEVICES_PATH = "/v3/devices"; - public static final String TAGS_PATH = "/v3/tags"; - public static final String ALIASES_PATH = "/v3/aliases"; - + private final NativeHttpClient _httpClient; + private String hostName; + private String devicesPath; + private String tagsPath; + private String aliasesPath; public DeviceClient(String masterSecret, String appKey) { this(masterSecret, appKey, IHttpClient.DEFAULT_MAX_RETRY_TIMES); @@ -35,17 +36,33 @@ public DeviceClient(String masterSecret, String appKey, int maxRetryTimes) { } public DeviceClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) { + this(masterSecret, appKey, maxRetryTimes, proxy, ClientConfig.getInstance()); + } + + /** + * + * @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) { 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); - this._httpClient = new NativeHttpClient(authCode, maxRetryTimes, proxy); + _httpClient = new NativeHttpClient(authCode, maxRetryTimes, proxy); } - // -------------- device public TagAliasResult getDeviceTagAlias(String registrationId) throws APIConnectionException, APIRequestException { - String url = HOST_NAME_SSL + DEVICES_PATH + "/" + registrationId; + String url = hostName + devicesPath + "/" + registrationId; ResponseWrapper response = _httpClient.sendGet(url); @@ -55,7 +72,7 @@ public TagAliasResult getDeviceTagAlias(String registrationId) throws APIConnect public DefaultResult updateDeviceTagAlias(String registrationId, boolean clearAlias, boolean clearTag) throws APIConnectionException, APIRequestException { Preconditions.checkArgument(clearAlias || clearTag, "It is not meaningful to do nothing."); - String url = HOST_NAME_SSL + DEVICES_PATH + "/" + registrationId; + String url = hostName + devicesPath + "/" + registrationId; JsonObject top = new JsonObject(); if (clearAlias) { @@ -72,7 +89,7 @@ public DefaultResult updateDeviceTagAlias(String registrationId, boolean clearAl public DefaultResult updateDeviceTagAlias(String registrationId, String alias, Set tagsToAdd, Set tagsToRemove) throws APIConnectionException, APIRequestException { - String url = HOST_NAME_SSL + DEVICES_PATH + "/" + registrationId; + String url = hostName + devicesPath + "/" + registrationId; JsonObject top = new JsonObject(); if (null != alias) { @@ -102,7 +119,7 @@ public DefaultResult updateDeviceTagAlias(String registrationId, String alias, // ------------- tags public TagListResult getTagList() throws APIConnectionException, APIRequestException { - String url = HOST_NAME_SSL + TAGS_PATH + "/"; + String url = hostName + tagsPath + "/"; ResponseWrapper response = _httpClient.sendGet(url); @@ -110,14 +127,14 @@ public TagListResult getTagList() throws APIConnectionException, APIRequestExcep } public BooleanResult isDeviceInTag(String theTag, String registrationID) throws APIConnectionException, APIRequestException { - String url = HOST_NAME_SSL + TAGS_PATH + "/" + theTag + "/registration_ids/" + registrationID; + String url = hostName + tagsPath + "/" + theTag + "/registration_ids/" + registrationID; ResponseWrapper response = _httpClient.sendGet(url); return BaseResult.fromResponse(response, BooleanResult.class); } public DefaultResult addRemoveDevicesFromTag(String theTag, Set toAddUsers, Set toRemoveUsers) throws APIConnectionException, APIRequestException { - String url = HOST_NAME_SSL + TAGS_PATH + "/" + theTag; + String url = hostName + tagsPath + "/" + theTag; JsonObject top = new JsonObject(); JsonObject registrationIds = new JsonObject(); @@ -145,7 +162,7 @@ public DefaultResult addRemoveDevicesFromTag(String theTag, Set toAddUse } public DefaultResult deleteTag(String theTag, String platform) throws APIConnectionException, APIRequestException { - String url = HOST_NAME_SSL + TAGS_PATH + "/" + theTag; + String url = hostName + tagsPath + "/" + theTag; if (null != platform) { url += "?platform=" + platform; } @@ -159,7 +176,7 @@ public DefaultResult deleteTag(String theTag, String platform) throws APIConnect // ------------- alias public AliasDeviceListResult getAliasDeviceList(String alias, String platform) throws APIConnectionException, APIRequestException { - String url = HOST_NAME_SSL + ALIASES_PATH + "/" + alias; + String url = hostName + aliasesPath + "/" + alias; if (null != platform) { url += "?platform=" + platform; } @@ -170,7 +187,7 @@ public AliasDeviceListResult getAliasDeviceList(String alias, String platform) t } public DefaultResult deleteAlias(String alias, String platform) throws APIConnectionException, APIRequestException { - String url = HOST_NAME_SSL + ALIASES_PATH + "/" + alias; + String url = hostName + aliasesPath + "/" + alias; if (null != platform) { url += "?platform=" + platform; } diff --git a/src/main/java/cn/jpush/api/push/PushClient.java b/src/main/java/cn/jpush/api/push/PushClient.java index 48078ecd..f87a2252 100644 --- a/src/main/java/cn/jpush/api/push/PushClient.java +++ b/src/main/java/cn/jpush/api/push/PushClient.java @@ -1,5 +1,6 @@ package cn.jpush.api.push; +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; @@ -26,11 +27,12 @@ * Can be used directly. */ public class PushClient { - public static final String HOST_NAME_SSL = "https://api.jpush.cn"; - public static final String PUSH_PATH = "/v3/push"; - public static final String PUSH_VALIDATE_PATH = "/v3/push/validate"; - + private final NativeHttpClient _httpClient; + private String _baseUrl; + private String _pushPath; + private String _pushValidatePath; + private JsonParser _jsonParser = new JsonParser(); // If not present, true by default. @@ -40,9 +42,7 @@ public class PushClient { private long _timeToLive = 60 * 60 * 24; private boolean _globalSettingEnabled = false; - - private String _baseUrl; - + /** * Create a Push Client. * @@ -65,12 +65,20 @@ public PushClient(String masterSecret, String appKey, int maxRetryTimes) { * @param maxRetryTimes max retry times */ public PushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) { + this(masterSecret, appKey, maxRetryTimes, proxy, ClientConfig.getInstance()); + } + + public PushClient(String masterSecret, String appKey, int maxRetryTimes, 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); + String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret); - this._baseUrl = HOST_NAME_SSL; this._httpClient = new NativeHttpClient(authCode, maxRetryTimes, proxy); - } + + } /** * Create a Push Client with global settings. @@ -108,7 +116,7 @@ public PushResult sendPush(PushPayload pushPayload) throws APIConnectionExceptio pushPayload.resetOptionsApnsProduction(_apnsProduction); } - ResponseWrapper response = _httpClient.sendPost(_baseUrl + PUSH_PATH, pushPayload.toString()); + ResponseWrapper response = _httpClient.sendPost(_baseUrl + _pushPath, pushPayload.toString()); return BaseResult.fromResponse(response, PushResult.class); } @@ -121,7 +129,7 @@ public PushResult sendPushValidate(PushPayload pushPayload) throws APIConnection pushPayload.resetOptionsApnsProduction(_apnsProduction); } - ResponseWrapper response = _httpClient.sendPost(_baseUrl + PUSH_VALIDATE_PATH, pushPayload.toString()); + ResponseWrapper response = _httpClient.sendPost(_baseUrl + _pushValidatePath, pushPayload.toString()); return BaseResult.fromResponse(response, PushResult.class); } @@ -135,7 +143,7 @@ public PushResult sendPush(String payloadString) throws APIConnectionException, Preconditions.checkArgument(false, "payloadString should be a valid JSON string."); } - ResponseWrapper response = _httpClient.sendPost(_baseUrl + PUSH_PATH, payloadString); + ResponseWrapper response = _httpClient.sendPost(_baseUrl + _pushPath, payloadString); return BaseResult.fromResponse(response, PushResult.class); } @@ -149,7 +157,7 @@ public PushResult sendPushValidate(String payloadString) throws APIConnectionExc Preconditions.checkArgument(false, "payloadString should be a valid JSON string."); } - ResponseWrapper response = _httpClient.sendPost(_baseUrl + PUSH_VALIDATE_PATH, payloadString); + ResponseWrapper response = _httpClient.sendPost(_baseUrl + _pushValidatePath, payloadString); 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 92685db1..5c09b31e 100644 --- a/src/main/java/cn/jpush/api/report/ReportClient.java +++ b/src/main/java/cn/jpush/api/report/ReportClient.java @@ -3,6 +3,7 @@ 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; @@ -15,12 +16,12 @@ import cn.jpush.api.utils.StringUtils; public class ReportClient { - private static final String REPORT_HOST_NAME = "https://report.jpush.cn"; - private static final String REPORT_RECEIVE_PATH = "/v3/received"; - private static final String REPORT_USER_PATH = "/v3/users"; - private static final String REPORT_MESSAGE_PATH = "/v3/messages"; private final NativeHttpClient _httpClient; + private String _hostName; + private String _receivePath; + private String _userPath; + private String _messagePath; public ReportClient(String masterSecret, String appKey) { this(masterSecret, appKey, IHttpClient.DEFAULT_MAX_RETRY_TIMES, null); @@ -31,11 +32,20 @@ public ReportClient(String masterSecret, String appKey, int maxRetryTimes) { } public ReportClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) { + this(masterSecret, appKey, maxRetryTimes, proxy, ClientConfig.getInstance()); + } + + public ReportClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy, ClientConfig conf) { ServiceHelper.checkBasic(appKey, masterSecret); + + _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, maxRetryTimes, proxy); - } + } public ReceivedsResult getReceiveds(String[] msgIdArray) @@ -47,7 +57,7 @@ public ReceivedsResult getReceiveds(String msgIds) throws APIConnectionException, APIRequestException { checkMsgids(msgIds); - String url = REPORT_HOST_NAME + REPORT_RECEIVE_PATH + "?msg_ids=" + msgIds; + String url = _hostName + _receivePath + "?msg_ids=" + msgIds; ResponseWrapper response = _httpClient.sendGet(url); return ReceivedsResult.fromResponse(response); @@ -57,7 +67,7 @@ public MessagesResult getMessages(String msgIds) throws APIConnectionException, APIRequestException { checkMsgids(msgIds); - String url = REPORT_HOST_NAME + REPORT_MESSAGE_PATH + "?msg_ids=" + msgIds; + String url = _hostName + _messagePath + "?msg_ids=" + msgIds; ResponseWrapper response = _httpClient.sendGet(url); return MessagesResult.fromResponse(response); @@ -71,7 +81,7 @@ public UsersResult getUsers(TimeUnit timeUnit, String start, int duration) } catch (Exception e) { } - String url = REPORT_HOST_NAME + REPORT_USER_PATH + String url = _hostName + _userPath + "?time_unit=" + timeUnit.toString() + "&start=" + startEncoded + "&duration=" + duration; ResponseWrapper response = _httpClient.sendGet(url);