Skip to content

Commit 8a04227

Browse files
committed
Merge pull request #15 from Liuchy1/master
add client configuration
2 parents d2107cc + 734f95c commit 8a04227

File tree

10 files changed

+294
-66
lines changed

10 files changed

+294
-66
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<dependency>
2525
<groupId>cn.jpush.api</groupId>
2626
<artifactId>jpush-client</artifactId>
27-
<version>3.2.5</version>
27+
<version>3.2.6</version>
2828
</dependency>
2929
```
3030
### jar 包方式

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

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

3+
import cn.jpush.api.common.ClientConfig;
34
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
56

@@ -21,8 +22,8 @@ public class PushExample {
2122
protected static final Logger LOG = LoggerFactory.getLogger(PushExample.class);
2223

2324
// demo App defined in resources/jpush-api.conf
24-
private static final String appKey ="dd1066407b044738b6479275";
25-
private static final String masterSecret = "2b38ce69b1de2a7fa95706ea";
25+
private static final String appKey ="e5c0d34f58732cf09b2d4d74";
26+
private static final String masterSecret = "4cdda6d3c8b029941dbc5cb3";
2627

2728
public static final String TITLE = "Test from API example";
2829
public static final String ALERT = "Test from API Example - alert";
@@ -31,7 +32,7 @@ public class PushExample {
3132
public static final String TAG = "tag_api";
3233

3334
public static void main(String[] args) {
34-
testSendPush();
35+
testSendPushWithCustomConfig();
3536
}
3637

3738

@@ -126,7 +127,32 @@ public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {
126127
.build())
127128
.build();
128129
}
129-
130-
130+
131+
public static void testSendPushWithCustomConfig() {
132+
ClientConfig config = ClientConfig.getInstance();
133+
// Setup the custom hostname
134+
config.setPushHostName("https://api.jpush.cn");
135+
136+
JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3, null, config);
137+
138+
// For push, all you need do is to build PushPayload object.
139+
PushPayload payload = buildPushObject_all_all_alert();
140+
141+
try {
142+
PushResult result = jpushClient.sendPush(payload);
143+
LOG.info("Got result - " + result);
144+
145+
} catch (APIConnectionException e) {
146+
LOG.error("Connection error. Should retry later. ", e);
147+
148+
} catch (APIRequestException e) {
149+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
150+
LOG.info("HTTP Status: " + e.getStatus());
151+
LOG.info("Error Code: " + e.getErrorCode());
152+
LOG.info("Error Message: " + e.getErrorMessage());
153+
LOG.info("Msg ID: " + e.getMsgId());
154+
}
155+
}
156+
131157
}
132158

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>cn.jpush.api</groupId>
55
<artifactId>jpush-client</artifactId>
6-
<version>3.2.5-SNAPSHOT</version>
6+
<version>3.2.6-SNAPSHOT</version>
77
<packaging>jar</packaging>
88
<url>https://github.com/jpush/jpush-api-java-client</url>
99
<name>JPush API Java Client</name>

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.Map;
44
import java.util.Set;
55

6+
import cn.jpush.api.common.ClientConfig;
67
import cn.jpush.api.common.TimeUnit;
78
import cn.jpush.api.common.connection.HttpProxy;
89
import cn.jpush.api.common.resp.APIConnectionException;
@@ -56,6 +57,45 @@ public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPr
5657
_reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes, proxy);
5758
_deviceClient = new DeviceClient(masterSecret, appKey, maxRetryTimes, proxy);
5859
}
60+
61+
/**
62+
* Create a JPush Client by custom Client configuration.
63+
*
64+
* If you are using JPush privacy cloud, maybe this constructor is what you needed.
65+
*
66+
* @param masterSecret API access secret of the appKey.
67+
* @param appKey The KEY of one application on JPush.
68+
* @param maxRetryTimes Client request retry times.
69+
* @param proxy The proxy, if there is no proxy, should be null.
70+
* @param conf The client configuration. Can use ClientConfig.getInstance() as default.
71+
*/
72+
public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy, ClientConfig conf) {
73+
_pushClient = new PushClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
74+
_reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
75+
_deviceClient = new DeviceClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
76+
}
77+
78+
/**
79+
* Create a JPush Client by custom Client configuration with global settings.
80+
*
81+
* If you are using JPush privacy cloud, and you want different settings from default globally,
82+
* maybe this constructor is what you needed.
83+
*
84+
* @param masterSecret API access secret of the appKey.
85+
* @param appKey The KEY of one application on JPush.
86+
* @param maxRetryTimes Client request retry times.
87+
* @param proxy The proxy, if there is no proxy, should be null.
88+
* @param conf The client configuration. Can use ClientConfig.getInstance() as default.
89+
* @param apnsProduction Global APNs environment setting. It will override PushPayload Options.
90+
* @param timeToLive Global time_to_live setting. It will override PushPayload Options.
91+
*/
92+
public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy, ClientConfig conf,
93+
boolean apnsProduction, long timeToLive) {
94+
_pushClient = new PushClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
95+
_reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
96+
_deviceClient = new DeviceClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
97+
_pushClient.setDefaults(apnsProduction, timeToLive);
98+
}
5999

60100
/**
61101
* Create a JPush Client with global settings.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package cn.jpush.api.common;
2+
3+
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
public class ClientConfig extends HashMap<String, Object> {
8+
9+
private static ClientConfig instance = new ClientConfig();
10+
11+
public static final String DEVICE_HOST_NAME = "device.host.name";
12+
public static final Object DEVICE_HOST_NAME_SCHEMA = String.class;
13+
14+
public static final String DEVICES_PATH = "devices.path";
15+
public static final Object DEVICES_PATH_SCHEMA = String.class;
16+
17+
public static final String TAGS_PATH = "tags.path";
18+
public static final Object TAGS_PATH_SCHEMA = String.class;
19+
20+
public static final String ALIASES_PATH = "aliases.path";
21+
public static final Object ALIASES_PATH_SCHEMA = String.class;
22+
23+
public static final String PUSH_HOST_NAME = "push.host.name";
24+
public static final Object PUSH_HOST_NAME_SCHEMA = String.class;
25+
26+
public static final String PUSH_PATH = "push.path";
27+
public static final Object PUSH_PATH_SCHEMA = String.class;
28+
29+
public static final String PUSH_VALIDATE_PATH = "push.validate.path";
30+
public static final Object PUSH_VALIDATE_PATH_SCHMEA = String.class;
31+
32+
public static final String REPORT_HOST_NAME = "report.host.name";
33+
public static final Object REPORT_HOST_NAME_SCHEMA = String.class;
34+
35+
public static final String REPORT_RECEIVE_PATH = "report.receive.path";
36+
public static final Object REPORT_RECEIVE_PATH_SCHEMA = String.class;
37+
38+
public static final String REPORT_USER_PATH = "report.user.path";
39+
public static final Object REPORT_USER_PATH_SCHEMA = String.class;
40+
41+
public static final String REPORT_MESSAGE_PATH = "report.message.path";
42+
public static final Object REPORT_MESSAGE_PATH_SCHEMA = String.class;
43+
44+
private ClientConfig() {
45+
super();
46+
this.put(DEVICE_HOST_NAME, "https://device.jpush.cn");
47+
this.put(DEVICES_PATH, "/v3/devices");
48+
this.put(TAGS_PATH, "/v3/tags");
49+
this.put(ALIASES_PATH, "/v3/aliases");
50+
51+
this.put(PUSH_HOST_NAME, "https://api.jpush.cn");
52+
this.put(PUSH_PATH, "/v3/push");
53+
this.put(PUSH_VALIDATE_PATH, "/v3/push/validate");
54+
55+
this.put(REPORT_HOST_NAME, "https://report.jpush.cn");
56+
this.put(REPORT_RECEIVE_PATH, "/v3/received");
57+
this.put(REPORT_USER_PATH, "/v3/users");
58+
this.put(REPORT_MESSAGE_PATH, "/v3/messages");
59+
}
60+
61+
public static ClientConfig getInstance() {
62+
return instance;
63+
}
64+
65+
public static void setDeviceHostName(Map conf, String hostName) {
66+
conf.put(DEVICE_HOST_NAME, hostName);
67+
}
68+
69+
/**
70+
* Setup custom device api host name, if using the JPush privacy cloud.
71+
* @param hostName the custom api host name, default is JPush domain name
72+
*/
73+
public void setDeviceHostName(String hostName) {
74+
setDeviceHostName(this, hostName);
75+
}
76+
77+
public static void setPushHostName(Map conf, String hostName) {
78+
conf.put(PUSH_HOST_NAME, hostName);
79+
}
80+
81+
/**
82+
* Setup custom push api host name, if using the JPush privacy cloud.
83+
* @param hostName the custom api host name, default is JPush domain name
84+
*/
85+
public void setPushHostName(String hostName) {
86+
setPushHostName(this, hostName);
87+
}
88+
89+
public static void setReportHostName(Map conf, String hostName) {
90+
conf.put(REPORT_HOST_NAME, hostName);
91+
}
92+
93+
/**
94+
* Setup custom report api host name, if using the JPush privacy cloud.
95+
* @param hostName the custom api host name, default is JPush domain name
96+
*/
97+
public void setReportHostName(String hostName) {
98+
setReportHostName(this, hostName);
99+
}
100+
101+
}

src/main/java/cn/jpush/api/common/resp/APIRequestException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ public long getMsgId() {
3131

3232
public int getErrorCode() {
3333
ErrorObject eo = getErrorObject();
34-
if (null != eo) {
34+
if (null != eo && null != eo.error) {
3535
return eo.error.code;
3636
}
3737
return -1;
3838
}
3939

4040
public String getErrorMessage() {
4141
ErrorObject eo = getErrorObject();
42-
if (null != eo) {
42+
if (null != eo && null != eo.error) {
4343
return eo.error.message;
4444
}
4545
return null;

src/main/java/cn/jpush/api/common/resp/ResponseWrapper.java

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package cn.jpush.api.common.resp;
22

3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonArray;
5+
import com.google.gson.JsonElement;
6+
import com.google.gson.JsonObject;
7+
import com.google.gson.JsonParser;
38
import com.google.gson.JsonSyntaxException;
49
import org.slf4j.Logger;
510
import org.slf4j.LoggerFactory;
611

7-
import com.google.gson.Gson;
8-
912
public class ResponseWrapper {
1013
private static final Logger LOG = LoggerFactory.getLogger(ResponseWrapper.class);
1114
private static final int RESPONSE_CODE_NONE = -1;
1215

1316
private static Gson _gson = new Gson();
17+
private static JsonParser jsonParser = new JsonParser();
1418

1519
public int responseCode = RESPONSE_CODE_NONE;
1620
public String responseContent;
@@ -36,21 +40,43 @@ public void setRateLimit(String quota, String remaining, String reset) {
3640
}
3741

3842
public void setErrorObject() {
43+
error = new ErrorObject();
44+
error.error = new ErrorEntity();
3945
try {
40-
error = _gson.fromJson(responseContent, ErrorObject.class);
41-
} catch (JsonSyntaxException e) {
42-
int index = responseContent.indexOf("error");
43-
if( -1 != index ) {
44-
int from = responseContent.indexOf("{", index);
45-
int to = responseContent.indexOf("}", from);
46-
String errorStr = responseContent.substring(from, to + 1);
47-
error = new ErrorObject();
48-
try {
49-
error.error = _gson.fromJson(errorStr, ErrorEntity.class);
50-
} catch (JsonSyntaxException e1) {
51-
LOG.error("unknown response content:" + responseContent, e);
46+
JsonElement element = jsonParser.parse(responseContent);
47+
JsonObject errorObj = null;
48+
if( element instanceof JsonArray) {
49+
JsonArray array = (JsonArray) element;
50+
for(int i = 0; i < array.size(); i++) {
51+
if(array.get(i).getAsString().contains("error")) {
52+
errorObj = array.get(i).getAsJsonObject();
53+
break;
54+
}
5255
}
56+
} else if(element instanceof JsonObject) {
57+
errorObj = (JsonObject) element;
58+
} else {
59+
// nothing
5360
}
61+
if(null != errorObj) {
62+
JsonObject errorMsg = errorObj;
63+
if(errorObj.has("msg_id")) {
64+
error.msg_id = errorObj.get("msg_id").getAsLong();
65+
}
66+
if (errorObj.has("error")) {
67+
errorMsg = (JsonObject) errorObj.get("error");
68+
}
69+
if(errorMsg.has("code")) {
70+
error.error.code = errorMsg.get("code").getAsInt();
71+
}
72+
if(errorMsg.has("message")) {
73+
error.error.message = errorMsg.get("message").getAsString();
74+
}
75+
}
76+
} catch(JsonSyntaxException e) {
77+
LOG.error("Unexpected - responseContent:" + responseContent, e);
78+
} catch (Exception e) {
79+
LOG.error("Unexpected - responseContent:" + responseContent, e);
5480
}
5581
}
5682

@@ -75,9 +101,9 @@ public class ErrorEntity {
75101
public String message;
76102

77103
@Override
78-
public String toString() {
79-
return _gson.toJson(this);
80-
}
104+
public String toString() {
105+
return _gson.toJson(this);
106+
}
81107
}
82108

83109
}

0 commit comments

Comments
 (0)