Skip to content

Commit 010474e

Browse files
author
Javen
committed
Refactoring: raise Exception now for connection error and service error.
1 parent 39f8334 commit 010474e

22 files changed

+531
-299
lines changed

src/cn/jpush/api/JPushClient.java

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

3+
import cn.jpush.api.common.APIConnectionException;
4+
import cn.jpush.api.common.APIRequestException;
35
import cn.jpush.api.common.TimeUnit;
46
import cn.jpush.api.push.PushClient;
57
import cn.jpush.api.push.PushResult;
@@ -48,29 +50,29 @@ public JPushClient(String masterSecret, String appKey, boolean apnsProduction, l
4850
* @param pushPayload payload of a push.
4951
* @return PushResult. Can be printed to a JSON.
5052
*/
51-
public PushResult sendPush(PushPayload pushPayload) {
53+
public PushResult sendPush(PushPayload pushPayload) throws APIConnectionException, APIRequestException {
5254
return _pushClient.sendPush(pushPayload);
5355
}
5456

55-
public PushResult sendPush(String payloadString) {
57+
public PushResult sendPush(String payloadString) throws APIConnectionException, APIRequestException {
5658
return _pushClient.sendPush(payloadString);
5759
}
58-
60+
5961
/**
6062
* Get received report.
6163
*
6264
* @param msgIds 100 msgids to batch getting is supported.
6365
* @return ReceivedResult. Can be printed to JSON.
6466
*/
65-
public ReceivedsResult getReportReceiveds(String msgIds) {
67+
public ReceivedsResult getReportReceiveds(String msgIds) throws APIConnectionException, APIRequestException {
6668
return _reportClient.getReceiveds(msgIds);
6769
}
6870

69-
public UsersResult getReportUsers(TimeUnit timeUnit, String start, int duration) {
71+
public UsersResult getReportUsers(TimeUnit timeUnit, String start, int duration) throws APIConnectionException, APIRequestException {
7072
return _reportClient.getUsers(timeUnit, start, duration);
7173
}
7274

73-
public MessagesResult getReportMessages(String msgIds) {
75+
public MessagesResult getReportMessages(String msgIds) throws APIConnectionException, APIRequestException {
7476
return _reportClient.getMessages(msgIds);
7577
}
7678

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.jpush.api.common;
2+
3+
/**
4+
* Should retry for encountering this exception
5+
*/
6+
public class APIConnectionException extends Exception {
7+
private static final long serialVersionUID = -2615370590441195647L;
8+
9+
public APIConnectionException(String message) {
10+
super(message);
11+
}
12+
13+
public APIConnectionException(String message, Throwable e) {
14+
super(message, e);
15+
}
16+
17+
}
18+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package cn.jpush.api.common;
2+
3+
import cn.jpush.api.common.ResponseWrapper.ErrorObject;
4+
5+
import com.google.gson.Gson;
6+
import com.google.gson.GsonBuilder;
7+
8+
public class APIRequestException extends Exception implements IRateLimiting {
9+
private static final long serialVersionUID = -3921022835186996212L;
10+
11+
protected static Gson _gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
12+
13+
private final ResponseWrapper responseWrapper;
14+
15+
public APIRequestException(ResponseWrapper responseWrapper) {
16+
super(responseWrapper.responseContent);
17+
this.responseWrapper = responseWrapper;
18+
}
19+
20+
public int getStatus() {
21+
return this.responseWrapper.responseCode;
22+
}
23+
24+
public int getErrorCode() {
25+
ErrorObject eo = getErrorObject();
26+
if (null != eo) {
27+
return eo.error.code;
28+
}
29+
return -1;
30+
}
31+
32+
public String getErrorMessage() {
33+
ErrorObject eo = getErrorObject();
34+
if (null != eo) {
35+
return eo.error.message;
36+
}
37+
return null;
38+
}
39+
40+
@Override
41+
public String toString() {
42+
return _gson.toJson(this);
43+
}
44+
45+
private ErrorObject getErrorObject() {
46+
return this.responseWrapper.error;
47+
}
48+
49+
50+
@Override
51+
public int getRateLimitQuota() {
52+
return responseWrapper.rateLimitQuota;
53+
}
54+
55+
@Override
56+
public int getRateLimitRemaining() {
57+
return responseWrapper.rateLimitRemaining;
58+
}
59+
60+
@Override
61+
public int getRateLimitReset() {
62+
return responseWrapper.rateLimitReset;
63+
}
64+
65+
}
66+

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

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

3-
import cn.jpush.api.common.ResponseWrapper.ErrorObject;
4-
53
import com.google.gson.Gson;
64
import com.google.gson.GsonBuilder;
75

8-
public abstract class BaseResult {
6+
public abstract class BaseResult implements IRateLimiting {
97
public static final int ERROR_CODE_NONE = -1;
108
public static final int ERROR_CODE_OK = 0;
119
public static final String ERROR_MESSAGE_NONE = "None error message.";
@@ -18,34 +16,6 @@ public abstract class BaseResult {
1816
public void setResponseWrapper(ResponseWrapper responseWrapper) {
1917
this.responseWrapper = responseWrapper;
2018
}
21-
22-
protected ErrorObject getErrorObject() {
23-
if (null != responseWrapper) {
24-
return responseWrapper.error;
25-
}
26-
return null;
27-
}
28-
29-
public int getErrorCode() {
30-
ErrorObject eo = getErrorObject();
31-
if (null != eo) {
32-
return eo.error.code;
33-
}
34-
if (null != responseWrapper) {
35-
if (responseWrapper.responseCode == RESPONSE_OK) {
36-
return ERROR_CODE_OK;
37-
}
38-
}
39-
return ERROR_CODE_NONE;
40-
}
41-
42-
public String getErrorMessage() {
43-
ErrorObject eo = getErrorObject();
44-
if (null != eo) {
45-
return eo.error.message;
46-
}
47-
return ERROR_MESSAGE_NONE;
48-
}
4919

5020
public String getOriginalContent() {
5121
if (null != responseWrapper) {
@@ -54,32 +24,27 @@ public String getOriginalContent() {
5424
return null;
5525
}
5626

57-
public String getExceptionString() {
58-
if (null != responseWrapper) {
59-
return responseWrapper.exceptionString;
60-
}
61-
return null;
62-
}
63-
6427
public boolean isResultOK() {
65-
if (responseWrapper.responseCode == RESPONSE_OK) return true;
66-
return false;
28+
return RESPONSE_OK == responseWrapper.responseCode;
6729
}
68-
30+
31+
@Override
6932
public int getRateLimitQuota() {
7033
if (null != responseWrapper) {
7134
return responseWrapper.rateLimitQuota;
7235
}
7336
return 0;
7437
}
7538

39+
@Override
7640
public int getRateLimitRemaining() {
7741
if (null != responseWrapper) {
7842
return responseWrapper.rateLimitRemaining;
7943
}
8044
return 0;
8145
}
8246

47+
@Override
8348
public int getRateLimitReset() {
8449
if (null != responseWrapper) {
8550
return responseWrapper.rateLimitReset;

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

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

3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
36
public interface IHttpClient {
47

58
public static final String CHARSET = "UTF-8";
@@ -12,18 +15,32 @@ public interface IHttpClient {
1215
public static final String JPUSH_USER_AGENT = "JPush-API-Java-Client";
1316

1417
public static final int RESPONSE_OK = 200;
15-
public static final String METHOD_POST = "POST";
16-
public static final String METHOD_GET = "GET";
18+
19+
public enum RequestMethod {
20+
GET,
21+
POST,
22+
DELETE
23+
}
24+
25+
public static final String IO_ERROR_MESSAGE = "Conneciton IO error. \n"
26+
+ "Chould not connect to JPush Server. "
27+
+ "Please ensure your internet connection is ok. \n"
28+
+ "If the problem persists, please let us know at support@jpush.cn.";
29+
30+
public static Gson _gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
31+
1732

1833
//设置连接超时时间
1934
public final int DEFAULT_CONNECTION_TIMEOUT = (5 * 1000); // milliseconds
2035

2136
//设置读取超时时间
2237
public final int DEFAULT_READ_TIMEOUT = (30 * 1000); // milliseconds
2338

24-
public ResponseWrapper sendGet(String url, String params, String authCode);
39+
public ResponseWrapper sendGet(String url, String params,
40+
String authCode) throws APIConnectionException, APIRequestException;
2541

26-
public ResponseWrapper sendPost(String url, String content, String authCode);
42+
public ResponseWrapper sendPost(String url, String content,
43+
String authCode) throws APIConnectionException, APIRequestException;
2744

2845

2946
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package cn.jpush.api.common;
2+
3+
public interface IRateLimiting {
4+
5+
public int getRateLimitQuota();
6+
7+
public int getRateLimitRemaining();
8+
9+
public int getRateLimitReset();
10+
11+
}
12+

0 commit comments

Comments
 (0)