Skip to content

Commit 9e88de6

Browse files
author
Javen
committed
Fix: notification can alert to all three platforms.
Refine: check basic params.
1 parent 42b3f8a commit 9e88de6

File tree

6 files changed

+68
-64
lines changed

6 files changed

+68
-64
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.net.UnknownHostException;
1212
import java.security.cert.CertificateException;
1313
import java.security.cert.X509Certificate;
14+
import java.util.regex.Pattern;
1415

1516
import javax.net.ssl.HostnameVerifier;
1617
import javax.net.ssl.HttpsURLConnection;
@@ -186,6 +187,24 @@ public static String getAuthorizationBase64(String appKey, String masterSecret)
186187
String encodeKey = appKey + ":" + masterSecret;
187188
return String.valueOf(Base64.encode(encodeKey.getBytes()));
188189
}
190+
191+
private final static Pattern PUSH_PATTERNS = Pattern.compile("[^a-zA-Z0-9]");
192+
193+
public static void checkBasic(String appKey, String masterSecret) {
194+
if (StringUtils.isEmpty(appKey)
195+
|| StringUtils.isEmpty(masterSecret)) {
196+
throw new IllegalArgumentException("appKey and masterSecret are both required.");
197+
}
198+
if (appKey.length() != 24
199+
|| masterSecret.length() != 24
200+
|| PUSH_PATTERNS.matcher(appKey).find()
201+
|| PUSH_PATTERNS.matcher(masterSecret).find()) {
202+
throw new IllegalArgumentException("appKey and masterSecret format is incorrect. "
203+
+ "They should be 24 size, and be composed with alphabet and numbers. "
204+
+ "Please confirm that they are coming from JPush Web Portal.");
205+
}
206+
}
207+
189208

190209
public class SimpleHostnameVerifier implements HostnameVerifier {
191210

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

Lines changed: 0 additions & 59 deletions
This file was deleted.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public PushClient(String masterSecret, String appKey) {
4343
this._masterSecret = masterSecret;
4444
this._appKey = appKey;
4545
this._authCode = getAuthorizationBase64(_appKey, _masterSecret);
46+
checkBasic(appKey, masterSecret);
4647
}
4748

4849
public PushClient(String masterSecret, String appKey, boolean apnsProduction, long timeToLive) {

src/cn/jpush/api/push/model/notification/Notification.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public static Builder newBuilder() {
3232
public static Notification alert(String alert) {
3333
return newBuilder()
3434
.addPlatformNotification(AndroidNotification.alert(alert))
35+
.addPlatformNotification(IosNotification.alert(alert))
36+
.addPlatformNotification(MpnsNotification.alert(alert))
3537
.setAlert(alert).build();
3638
}
3739

src/cn/jpush/api/report/ReportClient.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.lang.reflect.Type;
44
import java.util.List;
5+
import java.util.regex.Pattern;
56

67
import cn.jpush.api.common.BaseHttpClient;
78
import cn.jpush.api.common.ResponseResult;
@@ -24,6 +25,7 @@ public class ReportClient extends BaseHttpClient {
2425
public ReportClient(String masterSecret, String appKey) {
2526
this._masterSecret = masterSecret;
2627
this._appKey = appKey;
28+
checkBasic(appKey, masterSecret);
2729
}
2830

2931

@@ -52,7 +54,38 @@ public ReceivedsResult getResportReceived(String msgIds, String authCode) {
5254
return receivedsResult;
5355
}
5456

55-
}
57+
58+
private final static Pattern MSGID_PATTERNS = Pattern.compile("[^0-9, ]");
5659

60+
public static void checkMsgids(String msgIds) {
61+
if (StringUtils.isTrimedEmpty(msgIds)) {
62+
throw new IllegalArgumentException("msgIds param is required.");
63+
}
64+
65+
if (MSGID_PATTERNS.matcher(msgIds).find()) {
66+
throw new IllegalArgumentException("msgIds param format is incorrect. "
67+
+ "It should be msg_id (number) which response from JPush Push API. "
68+
+ "If there are many, use ',' as interval. ");
69+
}
70+
71+
msgIds = msgIds.trim();
72+
if (msgIds.endsWith(",")) {
73+
msgIds = msgIds.substring(0, msgIds.length() - 1);
74+
}
75+
76+
String[] splits = msgIds.split(",");
77+
try {
78+
for (String s : splits) {
79+
s = s.trim();
80+
if (!StringUtils.isEmpty(s)) {
81+
Integer.parseInt(s);
82+
}
83+
}
84+
} catch (NumberFormatException e) {
85+
throw new IllegalArgumentException("Every msg_id should be valid Integer number which splits by ','");
86+
}
87+
}
88+
89+
}
5790

5891

test/cn/jpush/api/push/model/notification/NotificationTests.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
import org.junit.Assert;
44
import org.junit.Test;
55

6-
import cn.jpush.api.push.model.notification.AndroidNotification;
7-
import cn.jpush.api.push.model.notification.Notification;
8-
96
import com.google.gson.JsonObject;
107
import com.google.gson.JsonPrimitive;
118

@@ -29,10 +26,21 @@ public void testIllegal2() {
2926
public void testAlertAll() {
3027
Notification notification = Notification.alert("alert");
3128
JsonObject json = new JsonObject();
29+
json.add("alert", new JsonPrimitive("alert"));
30+
3231
JsonObject android = new JsonObject();
3332
android.add("alert", new JsonPrimitive("alert"));
34-
json.add("alert", new JsonPrimitive("alert"));
33+
34+
JsonObject ios = new JsonObject();
35+
ios.add("alert", new JsonPrimitive("alert"));
36+
ios.add("sound", new JsonPrimitive(""));
37+
38+
JsonObject mpns = new JsonObject();
39+
mpns.add("alert", new JsonPrimitive("alert"));
40+
3541
json.add("android", android);
42+
json.add("ios", ios);
43+
json.add("mpns", mpns);
3644

3745
Assert.assertEquals("", json, notification.toJSON());
3846

0 commit comments

Comments
 (0)