From a0a0e0a652794dd3f29a95653d9c1863e7cfa226 Mon Sep 17 00:00:00 2001 From: Liuchy1 Date: Fri, 28 Aug 2015 15:10:41 +0800 Subject: [PATCH 1/2] modify pom tag version --- pom.xml | 2 +- .../cn/jpush/api/push/remote/ExceptionTest.java | 14 ++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/pom.xml b/pom.xml index 5dd458ea..8fce1944 100644 --- a/pom.xml +++ b/pom.xml @@ -35,7 +35,7 @@ https://github.com/jpush/jpush-api-java-client scm:git:git@github.com:jpush/jpush-api-java-client.git scm:git:git@github.com:jpush/jpush-api-java-client.git - v3.1.1 + v3.2.6 diff --git a/src/test/java/cn/jpush/api/push/remote/ExceptionTest.java b/src/test/java/cn/jpush/api/push/remote/ExceptionTest.java index c38e0df9..e82cf092 100644 --- a/src/test/java/cn/jpush/api/push/remote/ExceptionTest.java +++ b/src/test/java/cn/jpush/api/push/remote/ExceptionTest.java @@ -1,9 +1,4 @@ package cn.jpush.api.push.remote; -import static org.junit.Assert.*; - -import org.junit.Test; -import org.junit.experimental.categories.Category; - import cn.jpush.api.JPushClient; import cn.jpush.api.SlowTests; import cn.jpush.api.common.resp.APIConnectionException; @@ -12,9 +7,12 @@ import cn.jpush.api.push.model.PushPayload; import cn.jpush.api.push.model.audience.Audience; import cn.jpush.api.push.model.notification.Notification; - import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import static org.junit.Assert.assertEquals; @Category(SlowTests.class) public class ExceptionTest extends BaseRemotePushTest { @@ -160,7 +158,7 @@ public void invalidParams_notification_ios() { assertEquals(INVALID_PARAMS, e.getErrorCode()); } } - + /* @Test public void invalidParams_notification_winphone() { JsonObject payload = new JsonObject(); @@ -181,7 +179,7 @@ public void invalidParams_notification_winphone() { assertEquals(INVALID_PARAMS, e.getErrorCode()); } } - + */ @Test public void invalidParams_notification_android_builderidNotNumber() { JsonObject payload = new JsonObject(); From 2522fe496387511dd113cc7a77f45fa1f132d8e4 Mon Sep 17 00:00:00 2001 From: Liuchy1 Date: Mon, 7 Sep 2015 21:01:39 +0800 Subject: [PATCH 2/2] modify BaseResult --- pom.xml | 2 +- .../common/connection/NativeHttpClient.java | 58 +++++++++---------- .../cn/jpush/api/common/resp/BaseResult.java | 12 +++- 3 files changed, 39 insertions(+), 33 deletions(-) diff --git a/pom.xml b/pom.xml index 8fce1944..511ca824 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ cn.jpush.api jpush-client - 3.2.6-SNAPSHOT + 3.2.7-SNAPSHOT jar https://github.com/jpush/jpush-api-java-client JPush API Java Client diff --git a/src/main/java/cn/jpush/api/common/connection/NativeHttpClient.java b/src/main/java/cn/jpush/api/common/connection/NativeHttpClient.java index eb52944d..32306176 100644 --- a/src/main/java/cn/jpush/api/common/connection/NativeHttpClient.java +++ b/src/main/java/cn/jpush/api/common/connection/NativeHttpClient.java @@ -1,31 +1,20 @@ package cn.jpush.api.common.connection; +import cn.jpush.api.common.resp.APIConnectionException; +import cn.jpush.api.common.resp.APIRequestException; +import cn.jpush.api.common.resp.ResponseWrapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.net.ssl.*; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; -import java.net.Authenticator; -import java.net.HttpURLConnection; -import java.net.PasswordAuthentication; -import java.net.SocketTimeoutException; -import java.net.URL; +import java.net.*; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; -import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.HttpsURLConnection; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLSession; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import cn.jpush.api.common.resp.APIConnectionException; -import cn.jpush.api.common.resp.APIRequestException; -import cn.jpush.api.common.resp.ResponseWrapper; - /** * The implementation has no connection pool mechanism, used origin java connection. * @@ -68,13 +57,23 @@ public NativeHttpClient(String authCode, int maxRetryTimes, HttpProxy proxy) { public ResponseWrapper sendGet(String url) throws APIConnectionException, APIRequestException { - return doRequest(url, null, RequestMethod.GET); + return sendGet(url, null); + } + + public ResponseWrapper sendGet(String url, String content) + throws APIConnectionException, APIRequestException { + return doRequest(url, content, RequestMethod.GET); } public ResponseWrapper sendDelete(String url) throws APIConnectionException, APIRequestException { - return doRequest(url, null, RequestMethod.DELETE); + return sendDelete(url, null); } + + public ResponseWrapper sendDelete(String url, String content) + throws APIConnectionException, APIRequestException { + return doRequest(url, content, RequestMethod.DELETE); + } public ResponseWrapper sendPost(String url, String content) throws APIConnectionException, APIRequestException { @@ -117,7 +116,6 @@ private ResponseWrapper _doRequest(String url, String content, if (null != content) { LOG.debug("Request Content - " + content); } - HttpURLConnection conn = null; OutputStream out = null; StringBuffer sb = new StringBuffer(); @@ -146,17 +144,15 @@ private ResponseWrapper _doRequest(String url, String content, conn.setRequestProperty("Authorization", _authCode); conn.setRequestProperty("Content-Type", CONTENT_TYPE_JSON); - if (RequestMethod.GET == method) { - conn.setDoOutput(false); - } else if (RequestMethod.DELETE == method) { - conn.setDoOutput(false); - } else if (RequestMethod.POST == method || RequestMethod.PUT == method) { - conn.setDoOutput(true); - byte[] data = content.getBytes(CHARSET); + if(null == content) { + conn.setDoOutput(false); + } else { + conn.setDoOutput(true); + byte[] data = content.getBytes(CHARSET); conn.setRequestProperty("Content-Length", String.valueOf(data.length)); - out = conn.getOutputStream(); + out = conn.getOutputStream(); out.write(data); - out.flush(); + out.flush(); } int status = conn.getResponseCode(); diff --git a/src/main/java/cn/jpush/api/common/resp/BaseResult.java b/src/main/java/cn/jpush/api/common/resp/BaseResult.java index b980fdd5..c54c67b6 100644 --- a/src/main/java/cn/jpush/api/common/resp/BaseResult.java +++ b/src/main/java/cn/jpush/api/common/resp/BaseResult.java @@ -23,9 +23,19 @@ public String getOriginalContent() { } return null; } + + public int getResponseCode() { + if(null != responseWrapper) { + return responseWrapper.responseCode; + } + return -1; + } public boolean isResultOK() { - return RESPONSE_OK == responseWrapper.responseCode; + if(null != responseWrapper) { + return ( responseWrapper.responseCode / 200 ) == 1; + } + return false; } public static T fromResponse(