Skip to content

Commit

Permalink
Merge pull request #31 from xdx54321/master
Browse files Browse the repository at this point in the history
更新到1.1.8
  • Loading branch information
xdx54321 authored May 20, 2020
2 parents a9a7a3d + 592bf0d commit b5012ad
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 21 deletions.
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>jiguang-common</artifactId>
<version>1.1.8-SNAPSHOT</version>
<version>1.1.8</version>
<packaging>jar</packaging>
<url>https://github.com/jpush/jiguang-java-client-common</url>
<name>Jiguang Client Common Dependencies</name>
Expand Down Expand Up @@ -34,7 +34,7 @@
<url>https://github.com/jpush/jiguang-java-client-common</url>
<connection>scm:git:git@github.com:jpush/jiguang-java-client-common.git</connection>
<developerConnection>scm:git:git@github.com:jpush/jiguang-java-client-common.git</developerConnection>
<tag>jiguang-common-1.1.4</tag>
<tag>jiguang-common-1.1.8</tag>
</scm>

<dependencies>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/cn/jiguang/common/ClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public class ClientConfig extends HashMap<String, Object> {
public static final String GROUP_PUSH_PATH = "group.push.path";
public static final Object GROUP_PUSH_PATH_SCHEMA = String.class;

public static final String V3_FILES_PATH = "jpush.v3.files.path";
public static final Object V3_FILES_PATH_SCHEMA = String.class;

public static final String SSL_VERSION = "ssl.version";
public static final Object SSL_VERSION_SCHEMA = String.class;
public static final String DEFAULT_SSL_VERSION = "TLS";
Expand Down Expand Up @@ -144,6 +147,8 @@ private ClientConfig() {
this.put(SCHEDULE_HOST_NAME, "https://api.jpush.cn");
this.put(SCHEDULE_PATH, "/v3/schedules");

this.put(V3_FILES_PATH, "/v3/files");

this.put(SSL_VERSION, DEFAULT_SSL_VERSION);
this.put(MAX_RETRY_TIMES, DEFULT_MAX_RETRY_TIMES);
this.put(READ_TIMEOUT, DEFAULT_READ_TIMEOUT);
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/cn/jiguang/common/connection/IHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ public interface IHttpClient {
public static final String CHARSET = "UTF-8";
public static final String CONTENT_TYPE_JSON = "application/json";
public static final String CONTENT_TYPE_FORM = "application/x-www-form-urlencoded";

public static final String RATE_LIMIT_QUOTA = "X-Rate-Limit-Limit";
public static final String RATE_LIMIT_Remaining = "X-Rate-Limit-Remaining";
public static final String RATE_LIMIT_Reset = "X-Rate-Limit-Reset";
public static final String JPUSH_USER_AGENT = "JPush-API-Java-Client";

public static final int RESPONSE_OK = 200;

public enum RequestMethod {
GET,
GET,
POST,
PUT,
DELETE
}

public static final String IO_ERROR_MESSAGE = "Connection IO error. \n"
+ "Can not connect to JPush Server. "
+ "Please ensure your internet connection is ok. \n"
Expand All @@ -49,27 +49,27 @@ public enum RequestMethod {

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

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

public static final int DEFAULT_MAX_RETRY_TIMES = 3;

public ResponseWrapper sendGet(String url)
public ResponseWrapper sendGet(String url)
throws APIConnectionException, APIRequestException;

public ResponseWrapper sendGet(String url, String content)
throws APIConnectionException, APIRequestException;
public ResponseWrapper sendDelete(String url)

public ResponseWrapper sendDelete(String url)
throws APIConnectionException, APIRequestException;

public ResponseWrapper sendDelete(String url, String content)
throws APIConnectionException, APIRequestException;
public ResponseWrapper sendPost(String url, String content)

public ResponseWrapper sendPost(String url, String content)
throws APIConnectionException, APIRequestException;


public ResponseWrapper sendPut(String url, String content)
throws APIConnectionException, APIRequestException;
Expand Down
124 changes: 118 additions & 6 deletions src/main/java/cn/jiguang/common/connection/NativeHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.activation.MimetypesFileTypeMap;
import javax.net.ssl.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.*;
import java.net.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.MessageFormat;
import java.util.Iterator;
import java.util.Map;

/**
* The implementation has no connection pool mechanism, used origin java connection.
Expand Down Expand Up @@ -283,7 +283,7 @@ private ResponseWrapper _doRequest(String url, String content,
}

protected void initSSL(String sslVer) {
TrustManager[] tmCerts = new javax.net.ssl.TrustManager[1];
TrustManager[] tmCerts = new TrustManager[1];
tmCerts[0] = new SimpleTrustManager();
try {
SSLContext sslContext = SSLContext.getInstance(sslVer);
Expand All @@ -297,6 +297,118 @@ protected void initSSL(String sslVer) {
}
}

public String formUpload(String urlStr, Map<String, String> textMap,
Map<String, String> fileMap, String contentType) {
String res = "";
HttpURLConnection conn = null;
// boundary就是request头和上传文件内容的分隔符
String BOUNDARY = "---------------------------" + System.currentTimeMillis();
try {
URL url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(30000);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Authorization", _authCode);
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
OutputStream out = new DataOutputStream(conn.getOutputStream());
// text
if (textMap != null) {
StringBuffer strBuf = new StringBuffer();
Iterator iter = textMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
String inputName = (String) entry.getKey();
String inputValue = (String) entry.getValue();
if (inputValue == null) {
continue;
}
strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");
strBuf.append(inputValue);
}
out.write(strBuf.toString().getBytes());
}
// file
if (fileMap != null) {
Iterator iter = fileMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
String inputName = (String) entry.getKey();
String inputValue = (String) entry.getValue();
if (inputValue == null) {
continue;
}
File file = new File(inputValue);
String filename = file.getName();

//没有传入文件类型,同时根据文件获取不到类型,默认采用application/octet-stream
contentType = new MimetypesFileTypeMap().getContentType(file);
//contentType非空采用filename匹配默认的图片类型
if (!"".equals(contentType)) {
if (filename.endsWith(".png")) {
contentType = "image/png";
} else if (filename.endsWith(".jpg") || filename.endsWith(".jpeg") || filename.endsWith(".jpe")) {
contentType = "image/jpeg";
} else if (filename.endsWith(".gif")) {
contentType = "image/gif";
} else if (filename.endsWith(".ico")) {
contentType = "image/image/x-icon";
}
}
if (contentType == null || "".equals(contentType)) {
contentType = "application/octet-stream";
}
StringBuffer strBuf = new StringBuffer();
strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"; filename=\"" + filename + "\"\r\n");
strBuf.append("Content-Type:" + contentType + "\r\n\r\n");
out.write(strBuf.toString().getBytes());
DataInputStream in = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
in.close();
}
}
byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
out.write(endData);
out.flush();
out.close();
// 读取返回数据
StringBuffer strBuf = new StringBuffer();

InputStream is = null;
int responseCode = conn.getResponseCode();
BufferedReader reader;
if (responseCode == 200) {
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
} else {
reader = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
}
String line = null;
while ((line = reader.readLine()) != null) {
strBuf.append(line).append("\n");
}
res = strBuf.toString();
reader.close();
reader = null;
} catch (Exception e) {
LOG.error("formUpload error", e);
} finally {
if (conn != null) {
conn.disconnect();
conn = null;
}
}
return res;
}

private static class SimpleHostnameVerifier implements HostnameVerifier {

Expand All @@ -323,7 +435,7 @@ public X509Certificate[] getAcceptedIssuers() {
}
}

public static class SimpleProxyAuthenticator extends java.net.Authenticator {
public static class SimpleProxyAuthenticator extends Authenticator {
private String username;
private String password;

Expand Down

0 comments on commit b5012ad

Please sign in to comment.