diff --git a/pom.xml b/pom.xml index 8c78cd4..65d3a49 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ cn.jpush.api jiguang-common - 1.1.8-SNAPSHOT + 1.1.8 jar https://github.com/jpush/jiguang-java-client-common Jiguang Client Common Dependencies @@ -34,7 +34,7 @@ https://github.com/jpush/jiguang-java-client-common scm:git:git@github.com:jpush/jiguang-java-client-common.git scm:git:git@github.com:jpush/jiguang-java-client-common.git - jiguang-common-1.1.4 + jiguang-common-1.1.8 diff --git a/src/main/java/cn/jiguang/common/ClientConfig.java b/src/main/java/cn/jiguang/common/ClientConfig.java index 29e3b53..8f58d8c 100644 --- a/src/main/java/cn/jiguang/common/ClientConfig.java +++ b/src/main/java/cn/jiguang/common/ClientConfig.java @@ -61,6 +61,9 @@ public class ClientConfig extends HashMap { 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"; @@ -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); diff --git a/src/main/java/cn/jiguang/common/connection/IHttpClient.java b/src/main/java/cn/jiguang/common/connection/IHttpClient.java index 47c2498..73b1c78 100644 --- a/src/main/java/cn/jiguang/common/connection/IHttpClient.java +++ b/src/main/java/cn/jiguang/common/connection/IHttpClient.java @@ -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" @@ -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; diff --git a/src/main/java/cn/jiguang/common/connection/NativeHttpClient.java b/src/main/java/cn/jiguang/common/connection/NativeHttpClient.java index 2932d60..30b4dfe 100644 --- a/src/main/java/cn/jiguang/common/connection/NativeHttpClient.java +++ b/src/main/java/cn/jiguang/common/connection/NativeHttpClient.java @@ -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. @@ -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); @@ -297,6 +297,118 @@ protected void initSSL(String sslVer) { } } + public String formUpload(String urlStr, Map textMap, + Map 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 { @@ -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;