Skip to content

Commit 657ec2c

Browse files
author
Javen
committed
Fix bug: response transformation.
1 parent 93af30a commit 657ec2c

File tree

10 files changed

+124
-80
lines changed

10 files changed

+124
-80
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ test-output/
1010
*.war
1111
*.ear
1212

13+
pom.xml.releaseBackup
14+
release.properties
15+

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

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,20 @@ public class BaseHttpClient {
4646
private final int DEFAULT_SOCKET_TIMEOUT = (30 * 1000); // milliseconds
4747

4848

49-
protected ResponseResult sendGet(String url, String params, String authCode) {
49+
protected ResponseWrapper sendGet(String url, String params, String authCode) {
5050
return sendRequest(url, params, "GET", authCode);
5151
}
5252

53-
protected ResponseResult sendPost(String url, String content, String authCode) {
53+
protected ResponseWrapper sendPost(String url, String content, String authCode) {
5454
return sendRequest(url, content, "POST", authCode);
5555
}
5656

57-
protected ResponseResult sendRequest(String url, String content, String method, String authCode) {
57+
protected ResponseWrapper sendRequest(String url, String content, String method, String authCode) {
5858
LOG.debug("Send request to - " + url + ", with content - " + content);
5959
HttpURLConnection conn = null;
6060
OutputStream out = null;
6161
StringBuffer sb = new StringBuffer();
62-
ResponseResult result = new ResponseResult();
62+
ResponseWrapper wrapper = new ResponseWrapper();
6363

6464
try {
6565
initSSL();
@@ -89,69 +89,75 @@ protected ResponseResult sendRequest(String url, String content, String method,
8989
conn.setDoOutput(false);
9090
}
9191

92-
InputStream in = conn.getInputStream();
92+
int status = conn.getResponseCode();
93+
InputStream in = null;
94+
if (status == 200) {
95+
in = conn.getInputStream();
96+
} else {
97+
in = conn.getErrorStream();
98+
}
9399
InputStreamReader reader = new InputStreamReader(in, CHARSET);
94100
char[] buff = new char[1024];
95101
int len;
96102
while ((len = reader.read(buff)) > 0) {
97103
sb.append(buff, 0, len);
98104
}
99105

100-
int status = conn.getResponseCode();
101106
String responseContent = sb.toString();
102-
result.responseCode = status;
103-
result.responseContent = responseContent;
107+
wrapper.responseCode = status;
108+
wrapper.responseContent = responseContent;
104109

105110
String quota = conn.getHeaderField(RATE_LIMIT_QUOTA);
106111
String remaining = conn.getHeaderField(RATE_LIMIT_Remaining);
107112
String reset = conn.getHeaderField(RATE_LIMIT_Reset);
108-
result.setRateLimit(quota, remaining, reset);
113+
wrapper.setRateLimit(quota, remaining, reset);
109114

110115
if (status == 200) {
111116
LOG.debug("Succeed to get response - 200 OK");
112117

113118
} else {
114-
LOG.warn("Got error response - responseCode:" + status + ", responseContent:" + responseContent);
119+
LOG.info("Got error response - responseCode:" + status + ", responseContent:" + responseContent);
115120

116121
switch (status) {
117122
case 400:
118-
LOG.warn("Your request params is invalid. Please check them according to docs.");
119-
result.setErrorObject();
123+
LOG.error("Your request params is invalid. Please check them according to error message.");
124+
wrapper.setErrorObject();
120125
break;
121126
case 403:
122-
LOG.warn("Request is forbidden! Maybe your appkey is listed in blacklist?");
123-
result.setErrorObject();
127+
LOG.error("Request is forbidden! Maybe your appkey is listed in blacklist?");
128+
wrapper.setErrorObject();
124129
break;
125130
case 401:
126-
LOG.warn("Authentication failed! Please check authentication params according to docs.");
127-
result.setErrorObject();
131+
LOG.error("Authentication failed! Please check authentication params according to docs.");
132+
wrapper.setErrorObject();
128133
break;
129134
case 429:
130-
LOG.warn("Too many requests! Please review your appkey's request quota.");
131-
result.setErrorObject();
135+
LOG.error("Too many requests! Please review your appkey's request quota.");
136+
wrapper.setErrorObject();
132137
break;
133138
case 500:
134-
LOG.warn("Seems encountered server error. Please retry later.");
139+
LOG.error("Seems encountered server error. Please retry later.");
135140
break;
136141
default:
142+
LOG.error("Unexpected response.");
137143
}
138-
return result;
144+
return wrapper;
139145
}
140146

141147
} catch (SocketTimeoutException e) {
142-
result.exceptionString = e.getMessage();
148+
wrapper.exceptionString = e.getMessage();
143149
LOG.error("Request timeout. Retry later.", e);
144150
} catch (ConnectException e) {
145-
result.exceptionString = e.getMessage();
146-
LOG.error("Connnect error. ", e);
151+
wrapper.exceptionString = e.getMessage();
152+
LOG.error("Connnect error. Retry later.", e);
147153
} catch (UnknownHostException e) {
148-
result.exceptionString = e.getMessage();
154+
wrapper.exceptionString = e.getMessage();
149155
LOG.error("Unknown host. Please check the DNS configuration of your server.", e);
150156
} catch (IOException e) {
151-
result.exceptionString = e.getMessage();
152-
LOG.error("IO error. ", e);
157+
wrapper.exceptionString = e.getMessage();
158+
LOG.error("IO error. Retry later.", e);
153159
} catch (Exception e) {
154-
result.exceptionString = e.getMessage();
160+
wrapper.exceptionString = e.getMessage();
155161
LOG.error("Unknown exception. ", e);
156162
} finally {
157163
if (null != out) {
@@ -166,7 +172,7 @@ protected ResponseResult sendRequest(String url, String content, String method,
166172
}
167173
}
168174

169-
return result;
175+
return wrapper;
170176
}
171177

172178
protected void initSSL() {

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

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

3-
import cn.jpush.api.common.ResponseResult.ErrorObject;
3+
import cn.jpush.api.common.ResponseWrapper.ErrorContent;
44

55
import com.google.gson.Gson;
66
import com.google.gson.GsonBuilder;
@@ -13,31 +13,43 @@ public abstract class BaseResult {
1313
protected static final int RESPONSE_OK = 200;
1414
protected static Gson _gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
1515

16-
public ResponseResult responseResult;
16+
public ResponseWrapper responseResult;
1717

18-
protected ErrorObject getErrorObject() {
18+
protected ErrorContent getErrorObject() {
1919
if (null != responseResult) {
2020
return responseResult.error;
2121
}
2222
return null;
2323
}
2424

2525
public int getErrorCode() {
26-
ErrorObject eo = getErrorObject();
26+
ErrorContent eo = getErrorObject();
2727
if (null != eo) {
28-
return eo.code;
28+
return eo.error.code;
2929
}
30-
return ERROR_CODE_OK;
30+
if (null != responseResult) {
31+
if (responseResult.responseCode == RESPONSE_OK) {
32+
return ERROR_CODE_OK;
33+
}
34+
}
35+
return ERROR_CODE_NONE;
3136
}
3237

3338
public String getErrorMessage() {
34-
ErrorObject eo = getErrorObject();
39+
ErrorContent eo = getErrorObject();
3540
if (null != eo) {
36-
return eo.message;
41+
return eo.error.message;
3742
}
3843
return ERROR_MESSAGE_NONE;
3944
}
4045

46+
public String getOriginalError() {
47+
if (null != responseResult) {
48+
return responseResult.responseContent;
49+
}
50+
return null;
51+
}
52+
4153
public boolean isResultOK() {
4254
if (responseResult.responseCode == RESPONSE_OK) return true;
4355
return false;

src/cn/jpush/api/common/ResponseResult.java renamed to src/cn/jpush/api/common/ResponseWrapper.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@
55

66
import com.google.gson.Gson;
77

8-
public class ResponseResult {
9-
private static final Logger LOG = LoggerFactory.getLogger(ResponseResult.class);
8+
public class ResponseWrapper {
9+
private static final Logger LOG = LoggerFactory.getLogger(ResponseWrapper.class);
1010
private static final int RESPONSE_CODE_NONE = -1;
1111

1212
private static Gson _gson = new Gson();
1313

1414
public int responseCode = RESPONSE_CODE_NONE;
1515
public String responseContent;
1616

17-
public ErrorObject error; // error for non-200 response, used by new API
17+
public ErrorContent error; // error for non-200 response, used by new API
1818

1919
public int rateLimitQuota;
2020
public int rateLimitRemaining;
2121
public int rateLimitReset;
2222

2323
public String exceptionString;
2424

25-
public ResponseResult() {
25+
public ResponseWrapper() {
2626
}
2727

2828
public void setRateLimit(String quota, String remaining, String reset) {
@@ -40,17 +40,26 @@ public void setRateLimit(String quota, String remaining, String reset) {
4040
}
4141

4242
public void setErrorObject() {
43-
error = _gson.fromJson(responseContent, ErrorObject.class);
43+
error = _gson.fromJson(responseContent, ErrorContent.class);
4444
}
4545

4646
@Override
4747
public String toString() {
4848
return _gson.toJson(this);
4949
}
50-
51-
public class ErrorObject {
50+
51+
public class ErrorContent {
52+
public Error error;
53+
}
54+
55+
public class Error {
5256
public int code;
5357
public String message;
58+
59+
@Override
60+
public String toString() {
61+
return _gson.toJson(this);
62+
}
5463
}
5564

5665
}

src/cn/jpush/api/examples/JPushClientExample.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ public class JPushClientExample {
1616
private static final String masterSecret = "2b38ce69b1de2a7fa95706ea";
1717

1818
public static final String TITLE = "Test from API example";
19-
public static final String CONTENT = "Test Test";
19+
public static final String CONTENT = "Test from Javen";
2020
public static final String REGISTRATION_ID = "0900e8d85ef";
2121
public static final String TAG = "tag_api";
2222

2323
public static void main(String[] args) {
2424
testSendNotification();
2525
// testSendMesasge();
26-
// testGetReport();
26+
testGetReport();
2727
}
2828

2929
private static void testSendNotification() {
@@ -32,8 +32,15 @@ private static void testSendNotification() {
3232
LOG.info("Paylaod JSON - " + payload.toString());
3333

3434
PushResult result = jpushClient.sendPush(payload);
35-
LOG.debug(result.responseResult.responseContent);
36-
LOG.debug(result.toString());
35+
if (result.isResultOK()) {
36+
LOG.debug(result.toString());
37+
} else {
38+
if (result.getErrorCode() > 0) {
39+
LOG.warn(result.getOriginalError());
40+
} else {
41+
LOG.debug("Maybe connect error. Retry laster. ");
42+
}
43+
}
3744
}
3845

3946
private static void testSendMesasge() {

src/cn/jpush/api/push/PushClient.java

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

33
import cn.jpush.api.common.BaseHttpClient;
4-
import cn.jpush.api.common.ResponseResult;
4+
import cn.jpush.api.common.ResponseWrapper;
55
import cn.jpush.api.push.model.PushPayload;
66

77
/**
@@ -58,16 +58,9 @@ public PushResult sendPush(PushPayload pushPayload) {
5858
}
5959

6060
String url = HOST_NAME_SSL + PUSH_PATH;
61-
ResponseResult response = sendPost(url, pushPayload.toString(), _authCode);
62-
PushResult pushResult = null;
63-
if (response.responseCode == RESPONSE_OK) {
64-
pushResult = _gson.fromJson(response.responseContent, PushResult.class);
65-
} else {
66-
pushResult = new PushResult();
67-
}
68-
pushResult.responseResult = response;
61+
ResponseWrapper response = sendPost(url, pushPayload.toString(), _authCode);
6962

70-
return pushResult;
63+
return PushResult.fromResponse(response);
7164
}
7265

7366
}

src/cn/jpush/api/push/PushResult.java

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

33
import cn.jpush.api.common.BaseResult;
4+
import cn.jpush.api.common.ResponseWrapper;
45

56
import com.google.gson.annotations.Expose;
67

78
public class PushResult extends BaseResult {
8-
9-
public static class Push {
10-
@Expose public long msg_id;
11-
@Expose public int sendno;
9+
10+
@Expose public long msg_id;
11+
@Expose public int sendno;
12+
13+
public static PushResult fromResponse(ResponseWrapper response) {
14+
PushResult pushResult = null;
15+
if (response.responseCode == RESPONSE_OK) {
16+
pushResult = _gson.fromJson(response.responseContent, PushResult.class);
17+
} else {
18+
pushResult = new PushResult();
19+
}
20+
pushResult.responseResult = response;
21+
return pushResult;
1222
}
1323

1424
}
Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
11
package cn.jpush.api.report;
22

3+
import java.lang.reflect.Type;
34
import java.util.ArrayList;
45
import java.util.List;
56

67
import cn.jpush.api.common.BaseResult;
8+
import cn.jpush.api.common.ResponseWrapper;
79

810
import com.google.gson.annotations.Expose;
11+
import com.google.gson.reflect.TypeToken;
912

1013
public class ReceivedsResult extends BaseResult {
11-
@Expose public List<Received> receivedList = new ArrayList<Received>();
12-
14+
private static final Type RECEIVED_TYPE = new TypeToken<List<Received>>(){}.getType();
15+
16+
@Expose public List<Received> received_list = new ArrayList<Received>();
17+
18+
1319
public static class Received {
1420
@Expose public long msg_id;
1521
@Expose public int android_received;
1622
@Expose public int ios_apns_sent;
1723
}
1824

1925
public List<Received> getReceivedList() {
20-
return this.receivedList;
26+
return this.received_list;
2127
}
2228

29+
public static ReceivedsResult fromResponse(ResponseWrapper wrapper) {
30+
ReceivedsResult receivedsResult = new ReceivedsResult();
31+
if (wrapper.responseCode == RESPONSE_OK) {
32+
receivedsResult.received_list = _gson.fromJson(wrapper.responseContent, RECEIVED_TYPE);
33+
}
34+
35+
receivedsResult.responseResult = wrapper;
36+
return receivedsResult;
37+
}
2338

2439
}

0 commit comments

Comments
 (0)