Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.2.6-SNAPSHOT</version>
<version>3.2.7-SNAPSHOT</version>
<packaging>jar</packaging>
<url>https://github.com/jpush/jpush-api-java-client</url>
<name>JPush API Java Client</name>
Expand Down Expand Up @@ -35,7 +35,7 @@
<url>https://github.com/jpush/jpush-api-java-client</url>
<connection>scm:git:git@github.com:jpush/jpush-api-java-client.git</connection>
<developerConnection>scm:git:git@github.com:jpush/jpush-api-java-client.git</developerConnection>
<tag>v3.1.1</tag>
<tag>v3.2.6</tag>
</scm>

<dependencies>
Expand Down
58 changes: 27 additions & 31 deletions src/main/java/cn/jpush/api/common/connection/NativeHttpClient.java
Original file line number Diff line number Diff line change
@@ -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.
*
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/cn/jpush/api/common/resp/BaseResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 extends BaseResult> T fromResponse(
Expand Down
14 changes: 6 additions & 8 deletions src/test/java/cn/jpush/api/push/remote/ExceptionTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -160,7 +158,7 @@ public void invalidParams_notification_ios() {
assertEquals(INVALID_PARAMS, e.getErrorCode());
}
}

/*
@Test
public void invalidParams_notification_winphone() {
JsonObject payload = new JsonObject();
Expand All @@ -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();
Expand Down