From 579af5465f218514507f767b92a8b791170f9ae8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E4=B8=B9=E4=BE=A0?= Date: Fri, 6 Dec 2019 10:17:15 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 463c9c25..ff26a476 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ cn.jpush.api jpush-client - 3.4.3 + 3.4.3-SNAPSHOT jar https://github.com/jpush/jpush-api-java-client JPush API Java Client From 17e5167f38a7c8789a728b576e5a46fa4aff8a0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E4=B8=B9=E4=BE=A0?= Date: Fri, 6 Dec 2019 10:18:23 +0800 Subject: [PATCH 2/4] [maven-release-plugin] prepare release v3.4.3 --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index ff26a476..7ac672c5 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ cn.jpush.api jpush-client - 3.4.3-SNAPSHOT + 3.4.3 jar https://github.com/jpush/jpush-api-java-client JPush API Java Client @@ -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.3.12 + v3.4.3 From ddc133bc93f19a6e682953ecbc62279c8897497e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E4=B8=B9=E4=BE=A0?= Date: Fri, 6 Dec 2019 10:18:23 +0800 Subject: [PATCH 3/4] [maven-release-plugin] prepare for next development iteration --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 7ac672c5..94b41d9c 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ cn.jpush.api jpush-client - 3.4.3 + 3.4.4-SNAPSHOT jar https://github.com/jpush/jpush-api-java-client JPush API Java Client @@ -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.4.3 + v3.3.12 From 4c7ee466473716a65472fccab75af04931c43d47 Mon Sep 17 00:00:00 2001 From: xudx Date: Fri, 27 Mar 2020 10:15:40 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=95=B4=E5=90=88jpush=20file=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 4 +- .../java/cn/jpush/api/file/FileClient.java | 92 ++++++++++++++++++ .../cn/jpush/api/file/model/FileModel.java | 21 +++++ .../jpush/api/file/model/FileModelPage.java | 21 +++++ .../cn/jpush/api/file/model/FileType.java | 21 +++++ .../api/file/model/FileUploadResult.java | 16 ++++ .../jpush/api/report/MessageDetailResult.java | 2 +- .../cn/jpush/api/files/FileClientTest.java | 94 +++++++++++++++++++ 8 files changed, 268 insertions(+), 3 deletions(-) create mode 100644 src/main/java/cn/jpush/api/file/FileClient.java create mode 100644 src/main/java/cn/jpush/api/file/model/FileModel.java create mode 100644 src/main/java/cn/jpush/api/file/model/FileModelPage.java create mode 100644 src/main/java/cn/jpush/api/file/model/FileType.java create mode 100644 src/main/java/cn/jpush/api/file/model/FileUploadResult.java create mode 100644 src/test/java/cn/jpush/api/files/FileClientTest.java diff --git a/pom.xml b/pom.xml index 94b41d9c..420d01b6 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ cn.jpush.api jpush-client - 3.4.4-SNAPSHOT + 3.4.4 jar https://github.com/jpush/jpush-api-java-client JPush API Java Client @@ -51,7 +51,7 @@ cn.jpush.api jiguang-common - 1.1.7 + 1.1.8 org.apache.httpcomponents diff --git a/src/main/java/cn/jpush/api/file/FileClient.java b/src/main/java/cn/jpush/api/file/FileClient.java new file mode 100644 index 00000000..601805cb --- /dev/null +++ b/src/main/java/cn/jpush/api/file/FileClient.java @@ -0,0 +1,92 @@ +package cn.jpush.api.file; + +import cn.jiguang.common.ClientConfig; +import cn.jiguang.common.ServiceHelper; +import cn.jiguang.common.connection.ApacheHttpClient; +import cn.jiguang.common.connection.HttpProxy; +import cn.jiguang.common.connection.IHttpClient; +import cn.jiguang.common.connection.NativeHttpClient; +import cn.jiguang.common.resp.APIConnectionException; +import cn.jiguang.common.resp.APIRequestException; +import cn.jiguang.common.resp.ResponseWrapper; +import cn.jiguang.common.utils.Preconditions; +import cn.jiguang.common.utils.StringUtils; +import cn.jpush.api.file.model.FileModel; +import cn.jpush.api.file.model.FileModelPage; +import cn.jpush.api.file.model.FileType; +import cn.jpush.api.file.model.FileUploadResult; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author daixuan + * @version 2020/2/23 19:38 + */ +public class FileClient { + + protected static final Logger LOG = LoggerFactory.getLogger(FileClient.class); + + private IHttpClient _httpClient; + private String _baseUrl; + private String _filesPath; + private Gson _gson = new Gson(); + + public FileClient(String masterSecret, String appKey) { + this(masterSecret, appKey, null, ClientConfig.getInstance()); + } + + public FileClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf) { + _baseUrl = (String) conf.get(ClientConfig.PUSH_HOST_NAME); + _filesPath = (String) conf.get(ClientConfig.V3_FILES_PATH); + String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret); + this._httpClient = new NativeHttpClient(authCode, proxy, conf); + } + + public FileUploadResult uploadFile(FileType type, String filename) + throws APIConnectionException, APIRequestException { + Preconditions.checkArgument(type != null, "type should not be null"); + Preconditions.checkArgument(StringUtils.isNotEmpty(filename), "filename should not be null"); + NativeHttpClient client = (NativeHttpClient) _httpClient; + String typeStr = type.value(); + String url = _baseUrl + _filesPath + "/" + typeStr; + Map fileMap = new HashMap<>(); + fileMap.put("filename", filename); + String response = client.formUpload(url, null, fileMap, null); + LOG.info("uploadFile:{}", response); + return _gson.fromJson(response, + new TypeToken() { + }.getType()); + } + + public FileModelPage queryEffectFiles() throws APIConnectionException, APIRequestException { + String url = _baseUrl + _filesPath + "/"; + ResponseWrapper response = _httpClient.sendGet(url); + LOG.info("queryEffFiles:{}", response); + return _gson.fromJson(response.responseContent, + new TypeToken() { + }.getType()); + } + + public FileModel queryFile(String fileId) throws APIConnectionException, APIRequestException { + Preconditions.checkArgument(StringUtils.isNotEmpty(fileId), "fileId should not be null"); + String url = _baseUrl + _filesPath + "/" + fileId; + ResponseWrapper response = _httpClient.sendGet(url); + LOG.info("queryFile:{}", response); + return _gson.fromJson(response.responseContent, + new TypeToken() { + }.getType()); + } + + public ResponseWrapper deleteFile(String fileId) throws APIConnectionException, APIRequestException { + Preconditions.checkArgument(StringUtils.isNotEmpty(fileId), "fileId should not be null"); + String url = _baseUrl + _filesPath + "/" + fileId; + ResponseWrapper response = _httpClient.sendDelete(url); + LOG.info("deleteFile:{}", response); + return response; + } +} diff --git a/src/main/java/cn/jpush/api/file/model/FileModel.java b/src/main/java/cn/jpush/api/file/model/FileModel.java new file mode 100644 index 00000000..024a167f --- /dev/null +++ b/src/main/java/cn/jpush/api/file/model/FileModel.java @@ -0,0 +1,21 @@ +package cn.jpush.api.file.model; + +import com.google.gson.annotations.Expose; +import lombok.Data; + +import java.util.Date; + +/** + * @author daixuan + * @version 2020/2/23 20:18 + */ +@Data +public class FileModel { + @Expose + private String file_id; + @Expose + private String type; + @Expose + private Date create_time; + +} diff --git a/src/main/java/cn/jpush/api/file/model/FileModelPage.java b/src/main/java/cn/jpush/api/file/model/FileModelPage.java new file mode 100644 index 00000000..a8030adf --- /dev/null +++ b/src/main/java/cn/jpush/api/file/model/FileModelPage.java @@ -0,0 +1,21 @@ +package cn.jpush.api.file.model; + +import com.google.gson.annotations.Expose; +import lombok.Data; + +import java.util.Date; +import java.util.List; + +/** + * @author daixuan + * @version 2020/2/23 20:18 + */ +@Data +public class FileModelPage { + + @Expose + private int total_count; + @Expose + private List files; + +} diff --git a/src/main/java/cn/jpush/api/file/model/FileType.java b/src/main/java/cn/jpush/api/file/model/FileType.java new file mode 100644 index 00000000..5a3a0142 --- /dev/null +++ b/src/main/java/cn/jpush/api/file/model/FileType.java @@ -0,0 +1,21 @@ +package cn.jpush.api.file.model; + +/** + * @author daixuan + * @version 2020/2/23 20:14 + */ +public enum FileType { + + ALIAS("alias"), + REGISTRATION_ID("registration_id"); + + private final String value; + + private FileType(final String value) { + this.value = value; + } + + public String value() { + return this.value; + } +} diff --git a/src/main/java/cn/jpush/api/file/model/FileUploadResult.java b/src/main/java/cn/jpush/api/file/model/FileUploadResult.java new file mode 100644 index 00000000..00713367 --- /dev/null +++ b/src/main/java/cn/jpush/api/file/model/FileUploadResult.java @@ -0,0 +1,16 @@ +package cn.jpush.api.file.model; + +import lombok.Data; + +@Data +public class FileUploadResult { + private String file_id; + private Error error; + + @Data + public static class Error { + private String message; + private int code; + + } +} \ No newline at end of file diff --git a/src/main/java/cn/jpush/api/report/MessageDetailResult.java b/src/main/java/cn/jpush/api/report/MessageDetailResult.java index df06f65e..942bfef8 100644 --- a/src/main/java/cn/jpush/api/report/MessageDetailResult.java +++ b/src/main/java/cn/jpush/api/report/MessageDetailResult.java @@ -13,7 +13,7 @@ public class MessageDetailResult extends BaseResult { - private static final Type RECEIVED_TYPE = new TypeToken>() {}.getType(); + private static final Type RECEIVED_TYPE = new TypeToken>() {}.getType(); private static final long serialVersionUID = 156439166846147394L; @Expose diff --git a/src/test/java/cn/jpush/api/files/FileClientTest.java b/src/test/java/cn/jpush/api/files/FileClientTest.java new file mode 100644 index 00000000..3de8fb0d --- /dev/null +++ b/src/test/java/cn/jpush/api/files/FileClientTest.java @@ -0,0 +1,94 @@ +package cn.jpush.api.files; + +import cn.jiguang.common.resp.APIConnectionException; +import cn.jiguang.common.resp.APIRequestException; +import cn.jpush.api.BaseTest; +import cn.jpush.api.file.FileClient; +import cn.jpush.api.file.model.FileModel; +import cn.jpush.api.file.model.FileModelPage; +import cn.jpush.api.file.model.FileType; +import cn.jpush.api.file.model.FileUploadResult; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author daixuan + * @version 2020/2/24 14:01 + */ +public class FileClientTest extends BaseTest { + + protected static final Logger LOG = LoggerFactory.getLogger(FileClientTest.class); + + String fileId = "d4ee2375846bc30fa51334f5-69653861-1408-4d0a-abef-117808632b23"; + + @Test + public void testUploadFile() { + FileClient fileClient = new FileClient(MASTER_SECRET, APP_KEY); + try { + FileUploadResult result = fileClient.uploadFile(FileType.ALIAS, "README.md"); + LOG.info("uploadFile:{}", result); + } catch (APIConnectionException e) { + LOG.error("Connection error. Should retry later. ", e); + } catch (APIRequestException e) { + LOG.error("Error response from JPush server. Should review and fix it. ", e); + LOG.info("HTTP Status: " + e.getStatus()); + LOG.info("Error Code: " + e.getErrorCode()); + LOG.info("Error Message: " + e.getErrorMessage()); + LOG.info("Msg ID: " + e.getMsgId()); + } + } + + @Test + public void testQueryEffFiles() { + FileClient fileClient = new FileClient(MASTER_SECRET, APP_KEY); + try { + FileModelPage result = fileClient.queryEffectFiles(); + LOG.info("queryEffFiles:{}", result); + } catch (APIConnectionException e) { + LOG.error("Connection error. Should retry later. ", e); + } catch (APIRequestException e) { + LOG.error("Error response from JPush server. Should review and fix it. ", e); + LOG.info("HTTP Status: " + e.getStatus()); + LOG.info("Error Code: " + e.getErrorCode()); + LOG.info("Error Message: " + e.getErrorMessage()); + LOG.info("Msg ID: " + e.getMsgId()); + } + } + + @Test + public void testQueryFile() { + FileClient fileClient = new FileClient(MASTER_SECRET, APP_KEY); + try { + FileModel fileModel = fileClient.queryFile(fileId); + LOG.info("fileModel:{}", fileModel); + } catch (APIConnectionException e) { + LOG.error("Connection error. Should retry later. ", e); + } catch (APIRequestException e) { + LOG.error("Error response from JPush server. Should review and fix it. ", e); + LOG.info("HTTP Status: " + e.getStatus()); + LOG.info("Error Code: " + e.getErrorCode()); + LOG.info("Error Message: " + e.getErrorMessage()); + LOG.info("Msg ID: " + e.getMsgId()); + } + } + + @Test + public void testDeleteFile() { + FileClient fileClient = new FileClient(MASTER_SECRET, APP_KEY); + try { + fileClient.deleteFile(fileId); + } catch (APIConnectionException e) { + LOG.error("Connection error. Should retry later. ", e); + } catch (APIRequestException e) { + LOG.error("Error response from JPush server. Should review and fix it. ", e); + LOG.info("HTTP Status: " + e.getStatus()); + LOG.info("Error Code: " + e.getErrorCode()); + LOG.info("Error Message: " + e.getErrorMessage()); + LOG.info("Msg ID: " + e.getMsgId()); + } + } +}