├── .classpath
├── .gitignore
├── .project
├── LICENSE
├── README.md
├── example
└── main
│ └── java
│ ├── cn
│ └── jmessage
│ │ └── api
│ │ └── examples
│ │ ├── ChatRoomExample.java
│ │ ├── CrossAppExample.java
│ │ ├── GroupExample.java
│ │ ├── MessageExample.java
│ │ ├── ReportExample.java
│ │ ├── ResourceExample.java
│ │ ├── SensitiveWordExample.java
│ │ ├── UserExample.java
│ │ └── package-info.java
│ └── log4j.properties
├── libs
├── gson-2.2.4.jar
├── log4j-1.2.17.jar
├── slf4j-api-1.7.7.jar
└── slf4j-log4j12-1.7.7.jar
├── pom.xml
└── src
├── main
├── java
│ └── cn
│ │ └── jmessage
│ │ └── api
│ │ ├── JMessageClient.java
│ │ ├── chatroom
│ │ ├── ChatRoomClient.java
│ │ ├── ChatRoomHistoryResult.java
│ │ ├── ChatRoomListResult.java
│ │ ├── ChatRoomMemberList.java
│ │ ├── ChatRoomResult.java
│ │ ├── CreateChatRoomResult.java
│ │ └── package-info.java
│ │ ├── common
│ │ ├── BaseClient.java
│ │ ├── JMessageConfig.java
│ │ └── model
│ │ │ ├── IModel.java
│ │ │ ├── Members.java
│ │ │ ├── NoDisturbPayload.java
│ │ │ ├── RegisterInfo.java
│ │ │ ├── RegisterPayload.java
│ │ │ ├── UserPayload.java
│ │ │ ├── chatroom
│ │ │ └── ChatRoomPayload.java
│ │ │ ├── cross
│ │ │ ├── CrossBlacklist.java
│ │ │ ├── CrossBlacklistPayload.java
│ │ │ ├── CrossFriendPayload.java
│ │ │ ├── CrossGroup.java
│ │ │ ├── CrossGroupPayload.java
│ │ │ ├── CrossNoDisturb.java
│ │ │ └── CrossNoDisturbPayload.java
│ │ │ ├── friend
│ │ │ ├── FriendNote.java
│ │ │ └── FriendNotePayload.java
│ │ │ ├── group
│ │ │ ├── GroupPayload.java
│ │ │ └── GroupShieldPayload.java
│ │ │ └── message
│ │ │ ├── MessageBody.java
│ │ │ ├── MessagePayload.java
│ │ │ └── Notification.java
│ │ ├── crossapp
│ │ ├── CrossAppClient.java
│ │ └── package-info.java
│ │ ├── group
│ │ ├── CreateGroupResult.java
│ │ ├── GroupClient.java
│ │ ├── GroupInfoResult.java
│ │ ├── GroupListResult.java
│ │ ├── MemberListResult.java
│ │ ├── MemberResult.java
│ │ └── package-info.java
│ │ ├── message
│ │ ├── MessageBodyResult.java
│ │ ├── MessageClient.java
│ │ ├── MessageListResult.java
│ │ ├── MessageResult.java
│ │ ├── MessageType.java
│ │ ├── SendMessageResult.java
│ │ └── package-info.java
│ │ ├── package-info.java
│ │ ├── reportv2
│ │ ├── GroupStatListResult.java
│ │ ├── MessageStatListResult.java
│ │ ├── ReportClient.java
│ │ ├── UserStatListResult.java
│ │ ├── UserStatResult.java
│ │ └── package-info.java
│ │ ├── resource
│ │ ├── DownloadResult.java
│ │ ├── ResourceClient.java
│ │ ├── UploadResult.java
│ │ └── package-info.java
│ │ ├── sensitiveword
│ │ ├── SensitiveWordClient.java
│ │ ├── SensitiveWordListResult.java
│ │ ├── SensitiveWordStatusResult.java
│ │ └── package-info.java
│ │ ├── user
│ │ ├── RegisterResult.java
│ │ ├── UserClient.java
│ │ ├── UserGroupsResult.java
│ │ ├── UserInfoResult.java
│ │ ├── UserListResult.java
│ │ ├── UserStateListResult.java
│ │ ├── UserStateResult.java
│ │ └── package-info.java
│ │ └── utils
│ │ └── StringUtils.java
└── resources
│ └── javadoc-overview.html
└── test
├── java
└── cn
│ └── jmessage
│ └── api
│ ├── BaseTest.java
│ ├── FastTests.java
│ ├── JUnitOrderedRunner.java
│ ├── SlowTests.java
│ ├── TestOrder.java
│ ├── chatroom
│ └── ChatRoomClientTest.java
│ ├── cross
│ └── CrossAppClientTest.java
│ ├── group
│ └── GroupClientTest.java
│ ├── message
│ └── MessageClientTest.java
│ ├── reportv2
│ └── ReportClientTest.java
│ ├── resource
│ └── ResourceClientTest.java
│ ├── sensitiveword
│ └── SensitiveWordClientTest.java
│ └── user
│ └── UserClientTest.java
└── resources
└── log4j.properties
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 | *.iml
3 | .idea/
4 | .settings/
5 | .DS_Store
6 | # Mobile Tools for Java (J2ME)
7 | .mtj.tmp/
8 |
9 | # Package Files #
10 | *.war
11 | *.ear
12 | target/
13 |
14 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
15 | hs_err_pid*
16 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | jmessage-api
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.eclipse.m2e.core.maven2Builder
15 |
16 |
17 |
18 |
19 |
20 | org.eclipse.m2e.core.maven2Nature
21 | org.eclipse.jdt.core.javanature
22 |
23 |
24 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 极光推送/IM JPush/JMesage
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://badge.fury.io/gh/jpush%2Fjmessage-api-java-client)
2 |
3 | # JMessage API Java Library
4 |
5 | ## 概述
6 |
7 | 这是 JMessage REST API 的 Java 版本封装开发包,是由极光推送官方提供的,一般支持最新的 API 功能。
8 |
9 | 对应的 REST API 文档:
10 |
11 | 版本更新:[Release页面](https://github.com/jpush/jmessage-api-java-client/releases)。下载更新请到这里。
12 |
13 | > 非常欢迎各位开发者提交代码,贡献一份力量,review过有效的代码将会合入本项目。
14 |
15 |
16 | ## 安装
17 |
18 | ### maven 方式
19 | 将下边的依赖条件放到你项目的 maven pom.xml 文件里。
20 | > 其中 slf4j 可以与 logback, log4j, commons-logging 等日志框架一起工作,可根据你的需要配置使用。
21 |
22 | ```Java
23 |
24 | cn.jpush.api
25 | jmessage-client
26 | 1.1.9
27 |
28 |
29 | cn.jpush.api
30 | jiguang-common
31 | 1.1.3
32 |
33 |
34 | com.google.code.gson
35 | gson
36 | 2.3
37 |
38 |
39 | org.slf4j
40 | slf4j-api
41 | 1.7.7
42 |
43 |
44 |
45 |
46 | org.slf4j
47 | slf4j-log4j12
48 | 1.7.7
49 |
50 |
51 | log4j
52 | log4j
53 | 1.2.17
54 |
55 | ```
56 | ### jar 包方式
57 |
58 | 请到 [Release页面](https://github.com/jpush/jmessage-api-java-client/releases)下载相应版本的发布包。
59 |
60 | ### 依赖包
61 | * [slf4j](http://www.slf4j.org/) / log4j (Logger)
62 | * [gson](https://code.google.com/p/google-gson/) (Google JSON Utils)
63 | * [jiguang-common](https://github.com/jpush/jiguang-java-client-common)
64 |
65 | > 其中 slf4j 可以与 logback, log4j, commons-logging 等日志框架一起工作,可根据你的需要配置使用。
66 |
67 | > jiguang-common 的 jar 包下载。[请点击](https://github.com/jpush/jmessage-api-java-client/releases)
68 |
69 | ## 编译源码
70 |
71 | > 如果开发者想基于本项目做一些扩展的开发,或者想了解本项目源码,可以参考此章,否则可略过此章。
72 |
73 | ### 导入本项目
74 |
75 | * 可以采用 `git clone https://github.com/jpush/jmessage-api-java-client.git jmessage-api-src` 命令下载源码
76 | * 如果不使用git,请到[Release页面](https://github.com/jpush/jmessage-api-java-client/releases)下载源码包并解压
77 | * 采用eclipse导入下载的源码工程,推荐采用maven的方式,方便依赖包的管理
78 | * 假如采用导入普通项目的方式,项目报错,检查Build Path,Libraries
79 | * 依赖jar包都在libs目录下可以找到,没有加入的请添加到Build Path,Libraries
80 | * jpush-client jar包可以[点击下载](https://github.com/jpush/jpush-api-java-client/releases)
81 | * 默认采用了log4j做日志框架,开发者可根据自己需求替换logback、commons-logging等日志框架
82 | * 极个别情况下,如果test目录报错,请手动添加test的依赖jar包mockwebserver-2.0.0.jar、okhttp-2.0.0.jar、okio-1.0.0.jar
83 | * 开发者需要注意,将本项目的编码格式设置为UTF-8
84 |
85 | ### 构建本项目
86 |
87 | 可以用 Eclipse 类 IDE 导出 jar 包。建议直接使用 maven,执行命令:
88 |
89 | maven package
90 |
91 | ### 自动化测试
92 |
93 | 在项目目录下执行命令:
94 |
95 | mvn test
96 |
97 | ## 使用样例
98 |
99 | > 以下片断来自项目代码里的文件:example / cn.jmessage.api.examples.UserExample
100 |
101 | ```Java
102 | public static void testGetUserInfo() {
103 | JMessageClient client = new JMessageClient(appkey, masterSecret);
104 | try {
105 | String res = client.getUserInfo("test_user");
106 | LOG.info(res);
107 | } catch (APIConnectionException e) {
108 | LOG.error("Connection error. Should retry later. ", e);
109 | } catch (APIRequestException e) {
110 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
111 | LOG.info("HTTP Status: " + e.getStatus());
112 | LOG.info("Error Message: " + e.getMessage());
113 | }
114 | }
115 | ```
116 |
117 | > 以下片断来自项目代码里的文件:example / cn.jmessage.api.examples.GroupExample
118 | ```Java
119 | public static void testCreateGroup() {
120 | JMessageClient client = new JMessageClient(appkey, masterSecret);
121 | try {
122 | String res = client.createGroup("test_user", "test_gname1", "description", "test_user");
123 | LOG.info(res);
124 | } catch (APIConnectionException e) {
125 | LOG.error("Connection error. Should retry later. ", e);
126 | } catch (APIRequestException e) {
127 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
128 | LOG.info("HTTP Status: " + e.getStatus());
129 | LOG.info("Error Message: " + e.getMessage());
130 | }
131 | }
132 | ```
133 |
134 | ## 贡献者列表
135 |
136 | * [@tangyikai](https://github.com/tangyikai)
137 |
--------------------------------------------------------------------------------
/example/main/java/cn/jmessage/api/examples/ChatRoomExample.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.examples;
2 |
3 | import cn.jiguang.common.resp.APIConnectionException;
4 | import cn.jiguang.common.resp.APIRequestException;
5 | import cn.jiguang.common.resp.ResponseWrapper;
6 | import cn.jmessage.api.chatroom.*;
7 | import cn.jmessage.api.common.model.Members;
8 | import cn.jmessage.api.common.model.chatroom.ChatRoomPayload;
9 | import org.slf4j.Logger;
10 | import org.slf4j.LoggerFactory;
11 |
12 | public class ChatRoomExample {
13 |
14 | private static Logger LOG = LoggerFactory.getLogger(ChatRoomExample.class);
15 | private static final String appkey = "242780bfdd7315dc1989fe2b";
16 | private static final String masterSecret = "2f5ced2bef64167950e63d13";
17 | private ChatRoomClient mClient = new ChatRoomClient(appkey, masterSecret);
18 |
19 | public static void main(String[] args) {
20 |
21 | }
22 |
23 | public void testCreateChatRoom() {
24 | try {
25 | ChatRoomPayload payload = ChatRoomPayload.newBuilder()
26 | .setName("haha")
27 | .setDesc("test")
28 | .setOwnerUsername("junit_admin")
29 | .setMembers(Members.newBuilder().addMember("junit_user1", "junit_user2").build())
30 | .build();
31 | CreateChatRoomResult result = mClient.createChatRoom(payload);
32 | LOG.info("Got result, room id:" + result.getChatroom_id());
33 | } catch (APIConnectionException e) {
34 | LOG.error("Connection error. Should retry later. ", e);
35 | } catch (APIRequestException e) {
36 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
37 | LOG.info("HTTP Status: " + e.getStatus());
38 | LOG.info("Error Message: " + e.getMessage());
39 | }
40 | }
41 |
42 | public void testGetBatchChatRoomInfo() {
43 | try {
44 | ChatRoomListResult result = mClient.getBatchChatRoomInfo(12500011, 10000072);
45 | ChatRoomResult[] rooms = result.getRooms();
46 | if (rooms.length > 0) {
47 | for (ChatRoomResult room: rooms) {
48 | LOG.info("Got result " + room.toString());
49 | }
50 | }
51 | } catch (APIConnectionException e) {
52 | LOG.error("Connection error. Should retry later. ", e);
53 | } catch (APIRequestException e) {
54 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
55 | LOG.info("HTTP Status: " + e.getStatus());
56 | LOG.info("Error Message: " + e.getMessage());
57 | }
58 | }
59 |
60 | public void testGetUserChatRoomInfo() {
61 | try {
62 | ChatRoomListResult result = mClient.getUserChatRoomInfo("junit_admin");
63 | ChatRoomResult[] rooms = result.getRooms();
64 | if (rooms.length > 0) {
65 | for (ChatRoomResult room: rooms) {
66 | LOG.info("Got result " + room.toString());
67 | }
68 | }
69 | } catch (APIConnectionException e) {
70 | LOG.error("Connection error. Should retry later. ", e);
71 | } catch (APIRequestException e) {
72 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
73 | LOG.info("HTTP Status: " + e.getStatus());
74 | LOG.info("Error Message: " + e.getMessage());
75 | }
76 | }
77 |
78 | public void testGetAppChatRoomInfo() {
79 | try {
80 | ChatRoomListResult result = mClient.getAppChatRoomInfo(0, 2);
81 | LOG.info("Got result " + result.getList().toString());
82 | } catch (APIConnectionException e) {
83 | LOG.error("Connection error. Should retry later. ", e);
84 | } catch (APIRequestException e) {
85 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
86 | LOG.info("HTTP Status: " + e.getStatus());
87 | LOG.info("Error Message: " + e.getMessage());
88 | }
89 | }
90 |
91 | public void testUpdateChatRoomInfo() {
92 | try {
93 | ResponseWrapper result = mClient.updateChatRoomInfo(10000072, "junit_user1", "new Test", "666");
94 | LOG.info("Got result " + result.toString());
95 | } catch (APIConnectionException e) {
96 | LOG.error("Connection error. Should retry later. ", e);
97 | } catch (APIRequestException e) {
98 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
99 | LOG.info("HTTP Status: " + e.getStatus());
100 | LOG.info("Error Message: " + e.getMessage());
101 | }
102 | }
103 |
104 | public void testDeleteChatRoom() {
105 | try {
106 | ResponseWrapper result = mClient.deleteChatRoom(12500013);
107 | LOG.info("Got result " + result.toString());
108 | } catch (APIConnectionException e) {
109 | LOG.error("Connection error. Should retry later. ", e);
110 | } catch (APIRequestException e) {
111 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
112 | LOG.info("HTTP Status: " + e.getStatus());
113 | LOG.info("Error Message: " + e.getMessage());
114 | }
115 | }
116 |
117 | public void testUpdateUserSpeakStatus() {
118 | try {
119 | ResponseWrapper result = mClient.updateUserSpeakStatus(12500011, "junit_user1", 1);
120 | LOG.info("Got result " + result.toString());
121 | } catch (APIConnectionException e) {
122 | LOG.error("Connection error. Should retry later. ", e);
123 | } catch (APIRequestException e) {
124 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
125 | LOG.info("HTTP Status: " + e.getStatus());
126 | LOG.info("Error Message: " + e.getMessage());
127 | }
128 | }
129 |
130 | public void testGetChatRoomMembers() {
131 | try {
132 | ChatRoomMemberList result = mClient.getChatRoomMembers(12500011, 0, 3);
133 | ChatRoomMemberList.ChatRoomMember[] members = result.getMembers();
134 | if (members.length > 0) {
135 | for (ChatRoomMemberList.ChatRoomMember member: members) {
136 | LOG.info("Member: " + member.getUsername());
137 | }
138 | }
139 | } catch (APIConnectionException e) {
140 | LOG.error("Connection error. Should retry later. ", e);
141 | } catch (APIRequestException e) {
142 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
143 | LOG.info("HTTP Status: " + e.getStatus());
144 | LOG.info("Error Message: " + e.getMessage());
145 | }
146 | }
147 |
148 | public void testAddChatRoomMember() {
149 | try {
150 | ResponseWrapper result = mClient.addChatRoomMember(12500011, "junit_user");
151 | LOG.info("Got result: " + result);
152 | } catch (APIConnectionException e) {
153 | LOG.error("Connection error. Should retry later. ", e);
154 | } catch (APIRequestException e) {
155 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
156 | LOG.info("HTTP Status: " + e.getStatus());
157 | LOG.info("Error Message: " + e.getMessage());
158 | }
159 | }
160 |
161 | public void testDeleteChatRoomMember() {
162 | try {
163 | ResponseWrapper result = mClient.removeChatRoomMembers(12500011, "junit_user");
164 | LOG.info("Got result: " + result);
165 | } catch (APIConnectionException e) {
166 | LOG.error("Connection error. Should retry later. ", e);
167 | } catch (APIRequestException e) {
168 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
169 | LOG.info("HTTP Status: " + e.getStatus());
170 | LOG.info("Error Message: " + e.getMessage());
171 | }
172 | }
173 |
174 |
175 | }
176 |
--------------------------------------------------------------------------------
/example/main/java/cn/jmessage/api/examples/CrossAppExample.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.examples;
2 |
3 | import cn.jiguang.common.resp.APIConnectionException;
4 | import cn.jiguang.common.resp.APIRequestException;
5 | import cn.jiguang.common.resp.ResponseWrapper;
6 | import cn.jmessage.api.JMessageClient;
7 | import cn.jmessage.api.common.model.cross.CrossBlacklist;
8 | import cn.jmessage.api.common.model.cross.CrossFriendPayload;
9 | import cn.jmessage.api.common.model.cross.CrossGroup;
10 | import cn.jmessage.api.common.model.cross.CrossNoDisturb;
11 | import cn.jmessage.api.group.MemberListResult;
12 | import cn.jmessage.api.group.MemberResult;
13 | import org.slf4j.Logger;
14 | import org.slf4j.LoggerFactory;
15 |
16 | import java.util.ArrayList;
17 | import java.util.List;
18 |
19 | public class CrossAppExample {
20 |
21 | private static Logger LOG = LoggerFactory.getLogger(CrossAppExample.class);
22 | private static final String appkey = "242780bfdd7315dc1989fe2b";
23 | private static final String masterSecret = "2f5ced2bef64167950e63d13";
24 |
25 | public static void main(String[] args) {
26 |
27 | }
28 |
29 | public static void testAddOrRemoveMembersFromCrossGroup() {
30 | JMessageClient client = new JMessageClient(appkey, masterSecret);
31 | try {
32 | List crossGroups = new ArrayList();
33 | CrossGroup crossGroup = new CrossGroup.Builder()
34 | .setAppKey(appkey)
35 | .setAddUsers("test_user1", "test_user2")
36 | .setRemoveUsers("test_user3")
37 | .build();
38 | crossGroups.add(crossGroup);
39 | CrossGroup[] array = new CrossGroup[crossGroups.size()];
40 | ResponseWrapper response = client.addOrRemoveCrossGroupMember(10004809, crossGroups.toArray(array));
41 | } catch (APIConnectionException e) {
42 | LOG.error("Connection error. Should retry later. ", e);
43 | } catch (APIRequestException e) {
44 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
45 | LOG.info("HTTP Status: " + e.getStatus());
46 | LOG.info("Error Message: " + e.getMessage());
47 | }
48 | }
49 |
50 | public static void testGetCrossGroupMembers() {
51 | JMessageClient client = new JMessageClient(appkey, masterSecret);
52 | try {
53 | MemberListResult result = client.getCrossGroupMembers(10004809);
54 | MemberResult[] members = result.getMembers();
55 | LOG.info("Member size " + members.length);
56 | } catch (APIConnectionException e) {
57 | LOG.error("Connection error. Should retry later. ", e);
58 | } catch (APIRequestException e) {
59 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
60 | LOG.info("HTTP Status: " + e.getStatus());
61 | LOG.info("Error Message: " + e.getMessage());
62 | }
63 | }
64 |
65 | public static void testAddCrossBlacklist() {
66 | JMessageClient client = new JMessageClient(appkey, masterSecret);
67 | try {
68 | List crossBlacklists = new ArrayList();
69 | CrossBlacklist blacklist = new CrossBlacklist.Builder()
70 | .setAppKey(appkey)
71 | .addUsers("test_user1", "test_user2")
72 | .build();
73 |
74 | crossBlacklists.add(blacklist);
75 | CrossBlacklist[] array = new CrossBlacklist[crossBlacklists.size()];
76 | ResponseWrapper response = client.addCrossBlacklist("test_user", crossBlacklists.toArray(array));
77 | } catch (APIConnectionException e) {
78 | LOG.error("Connection error. Should retry later. ", e);
79 | } catch (APIRequestException e) {
80 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
81 | LOG.info("HTTP Status: " + e.getStatus());
82 | LOG.info("Error Message: " + e.getMessage());
83 | }
84 | }
85 |
86 | public static void testDeleteCrossBlacklist() {
87 | JMessageClient client = new JMessageClient(appkey, masterSecret);
88 | try {
89 | List crossBlacklists = new ArrayList();
90 | CrossBlacklist blacklist = new CrossBlacklist.Builder()
91 | .setAppKey(appkey)
92 | .addUsers("test_user1", "test_user2")
93 | .build();
94 |
95 | crossBlacklists.add(blacklist);
96 | CrossBlacklist[] array = new CrossBlacklist[crossBlacklists.size()];
97 | ResponseWrapper response = client.deleteCrossBlacklist("test_user", crossBlacklists.toArray(array));
98 | } catch (APIConnectionException e) {
99 | LOG.error("Connection error. Should retry later. ", e);
100 | } catch (APIRequestException e) {
101 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
102 | LOG.info("HTTP Status: " + e.getStatus());
103 | LOG.info("Error Message: " + e.getMessage());
104 | }
105 | }
106 |
107 | public static void testSetCrossNoDisturb() {
108 | JMessageClient client = new JMessageClient(appkey, masterSecret);
109 | try {
110 | List list = new ArrayList();
111 | CrossNoDisturb crossNoDisturb = new CrossNoDisturb.Builder()
112 | .setAppKey(appkey)
113 | .setRemoveSingleUsers("test_user1", "test_user2")
114 | .setRemoveGroupIds(10004809L)
115 | .build();
116 | list.add(crossNoDisturb);
117 | CrossNoDisturb[] array = new CrossNoDisturb[list.size()];
118 | ResponseWrapper response = client.setCrossNoDisturb("test_user", list.toArray(array));
119 | } catch (APIConnectionException e) {
120 | LOG.error("Connection error. Should retry later. ", e);
121 | } catch (APIRequestException e) {
122 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
123 | LOG.info("HTTP Status: " + e.getStatus());
124 | LOG.info("Error Message: " + e.getMessage());
125 | }
126 | }
127 |
128 | public static void testAddCrossUsers() {
129 | JMessageClient client = new JMessageClient(appkey, masterSecret);
130 | try {
131 | CrossFriendPayload payload = new CrossFriendPayload.Builder()
132 | .setAppKey(appkey)
133 | .setUsers("test_user1", "test_user2")
134 | .build();
135 | ResponseWrapper response = client.addCrossFriends("test_user", payload);
136 | } catch (APIConnectionException e) {
137 | e.printStackTrace();
138 | LOG.error("Connection error. Should retry later. ", e);
139 | } catch (APIRequestException e) {
140 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
141 | LOG.info("HTTP Status: " + e.getStatus());
142 | LOG.info("Error Message: " + e.getMessage());
143 | }
144 | }
145 |
146 | }
147 |
--------------------------------------------------------------------------------
/example/main/java/cn/jmessage/api/examples/GroupExample.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.examples;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import cn.jiguang.common.resp.APIConnectionException;
7 | import cn.jiguang.common.resp.APIRequestException;
8 | import cn.jmessage.api.JMessageClient;
9 | import cn.jmessage.api.group.CreateGroupResult;
10 | import cn.jmessage.api.group.GroupInfoResult;
11 | import cn.jmessage.api.group.GroupListResult;
12 | import cn.jmessage.api.group.MemberListResult;
13 | import cn.jmessage.api.user.UserGroupsResult;
14 |
15 | public class GroupExample {
16 |
17 | protected static final Logger LOG = LoggerFactory.getLogger(GroupExample.class);
18 |
19 | private static final String appkey = "242780bfdd7315dc1989fe2b";
20 | private static final String masterSecret = "2f5ced2bef64167950e63d13";
21 |
22 | public static void main(String[] args) {
23 | // testGetGroupInfo();
24 | }
25 |
26 | public static void testCreateGroup() {
27 | JMessageClient client = new JMessageClient(appkey, masterSecret);
28 | try {
29 | CreateGroupResult res = client.createGroup("test_user", "test_gname1", "description", "", 0, "test_user");
30 | LOG.info(res.getName());
31 | } catch (APIConnectionException e) {
32 | LOG.error("Connection error. Should retry later. ", e);
33 | } catch (APIRequestException e) {
34 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
35 | LOG.info("HTTP Status: " + e.getStatus());
36 | LOG.info("Error Message: " + e.getMessage());
37 | }
38 | }
39 |
40 | public static void testGetGroupInfo() {
41 | JMessageClient client = new JMessageClient(appkey, masterSecret);
42 |
43 | try {
44 | GroupInfoResult res = client.getGroupInfo(10003767);
45 | LOG.info(res.getOriginalContent());
46 | } catch (APIConnectionException e) {
47 | LOG.error("Connection error. Should retry later. ", e);
48 | } catch (APIRequestException e) {
49 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
50 | LOG.info("HTTP Status: " + e.getStatus());
51 | LOG.info("Error Message: " + e.getMessage());
52 | }
53 | }
54 |
55 | public static void testGetGroupMemberList() {
56 | JMessageClient client = new JMessageClient(appkey, masterSecret);
57 |
58 | try {
59 | MemberListResult res = client.getGroupMembers(10003767);
60 | LOG.info(res.getOriginalContent());
61 | } catch (APIConnectionException e) {
62 | LOG.error("Connection error. Should retry later. ", e);
63 | } catch (APIRequestException e) {
64 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
65 | LOG.info("HTTP Status: " + e.getStatus());
66 | LOG.info("Error Message: " + e.getMessage());
67 | }
68 | }
69 |
70 | /**
71 | * Get group list by appkey, this method will invoke getGroupListByAppkey() in GroupClient, whose parameters
72 | * are list as follow:
73 | */
74 | public static void testGetGroupListByAppkey() {
75 | JMessageClient client = new JMessageClient(appkey, masterSecret);
76 |
77 | try {
78 | GroupListResult res = client.getGroupListByAppkey(0, 30);
79 | LOG.info(res.getOriginalContent());
80 | } catch (APIConnectionException e) {
81 | LOG.error("Connection error. Should retry later. ", e);
82 | } catch (APIRequestException e) {
83 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
84 | LOG.info("HTTP Status: " + e.getStatus());
85 | LOG.info("Error Message: " + e.getMessage());
86 | }
87 | }
88 |
89 | public static void testManageGroup() {
90 | JMessageClient client = new JMessageClient(appkey, masterSecret);
91 |
92 | try {
93 | String[] addList = {"baobao148"};
94 | String[] removeList = {"baobao148"};
95 | client.addOrRemoveMembers(10003767, addList, null );
96 | } catch (APIConnectionException e) {
97 | LOG.error("Connection error. Should retry later. ", e);
98 | } catch (APIRequestException e) {
99 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
100 | LOG.info("HTTP Status: " + e.getStatus());
101 | LOG.info("Error Message: " + e.getMessage());
102 | }
103 | }
104 |
105 | public static void testUpdateGroupInfo() {
106 | JMessageClient client = new JMessageClient(appkey, masterSecret);
107 |
108 | try {
109 | client.updateGroupInfo(10003767, "test_gname_new", "update desc", "media id");
110 | } catch (APIConnectionException e) {
111 | LOG.error("Connection error. Should retry later. ", e);
112 | } catch (APIRequestException e) {
113 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
114 | LOG.info("HTTP Status: " + e.getStatus());
115 | LOG.info("Error Message: " + e.getMessage());
116 | }
117 | }
118 |
119 | public static void testGetGroupsByUser() {
120 | JMessageClient client = new JMessageClient(appkey, masterSecret);
121 |
122 | try {
123 | UserGroupsResult res = client.getGroupListByUser("test_user");
124 | LOG.info(res.getOriginalContent());
125 | } catch (APIConnectionException e) {
126 | LOG.error("Connection error. Should retry later. ", e);
127 | } catch (APIRequestException e) {
128 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
129 | LOG.info("HTTP Status: " + e.getStatus());
130 | LOG.info("Error Message: " + e.getMessage());
131 | }
132 | }
133 |
134 | public static void testDeleteGroup() {
135 | JMessageClient client = new JMessageClient(appkey, masterSecret);
136 |
137 | try {
138 | client.deleteGroup(10003765);
139 | } catch (APIConnectionException e) {
140 | LOG.error("Connection error. Should retry later. ", e);
141 | } catch (APIRequestException e) {
142 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
143 | LOG.info("HTTP Status: " + e.getStatus());
144 | LOG.info("Error Message: " + e.getMessage());
145 | }
146 | }
147 |
148 | }
149 |
--------------------------------------------------------------------------------
/example/main/java/cn/jmessage/api/examples/MessageExample.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.examples;
2 |
3 | import cn.jiguang.common.resp.ResponseWrapper;
4 | import cn.jmessage.api.common.model.message.MessagePayload;
5 | import cn.jmessage.api.message.MessageListResult;
6 | import cn.jmessage.api.message.MessageResult;
7 | import cn.jmessage.api.message.MessageType;
8 | import cn.jmessage.api.utils.StringUtils;
9 | import org.slf4j.Logger;
10 | import org.slf4j.LoggerFactory;
11 |
12 | import cn.jiguang.common.resp.APIConnectionException;
13 | import cn.jiguang.common.resp.APIRequestException;
14 | import cn.jmessage.api.JMessageClient;
15 | import cn.jmessage.api.common.model.message.MessageBody;
16 | import cn.jmessage.api.message.SendMessageResult;
17 |
18 | public class MessageExample {
19 |
20 | protected static final Logger LOG = LoggerFactory.getLogger(MessageExample.class);
21 |
22 | private static final String appkey = "7b4b94cca0d185d611e53cca";
23 | private static final String masterSecret = "860803cf613ed54aa3b941a8";
24 |
25 | public static void main(String[] args) {
26 | testGetMessageList();
27 | }
28 |
29 | /**
30 | * Send single text message by admin, this method will invoke sendMessage() in JMessageClient eventually, whose
31 | * parameters are as list:
32 | */
33 | public static void testSendSingleTextByAdmin() {
34 | JMessageClient client = new JMessageClient(appkey, masterSecret);
35 |
36 | try {
37 | MessageBody body = MessageBody.text("Help me!");
38 | SendMessageResult result = client.sendSingleTextByAdmin("targetUserName", "fromUserName", body);
39 | LOG.info(String.valueOf(result.getMsg_id()));
40 | } catch (APIConnectionException e) {
41 | LOG.error("Connection error. Should retry later. ", e);
42 | } catch (APIRequestException e) {
43 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
44 | LOG.info("HTTP Status: " + e.getStatus());
45 | LOG.info("Error Message: " + e.getMessage());
46 | }
47 | }
48 |
49 | /**
50 | * Send group text message by admin
51 | */
52 | public static void testSendGroupTextByAdmin() {
53 | JMessageClient client = new JMessageClient(appkey, masterSecret);
54 |
55 | try {
56 | MessageBody body = MessageBody.text("Hello World!");
57 | SendMessageResult result = client.sendGroupTextByAdmin("targetUserName", "fromUserName", body);
58 | LOG.info(String.valueOf(result.getMsg_id()));
59 | } catch (APIConnectionException e) {
60 | LOG.error("Connection error. Should retry later. ", e);
61 | } catch (APIRequestException e) {
62 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
63 | LOG.info("HTTP Status: " + e.getStatus());
64 | LOG.info("Error Message: " + e.getMessage());
65 | }
66 | }
67 |
68 | public static void testSendImageMessage() {
69 | JMessageClient client = new JMessageClient(appkey, masterSecret);
70 | MessageBody messageBody = new MessageBody.Builder()
71 | .setMediaId("qiniu/image/r/A92D550D57464CDF5ADC0D79FBD46210")
72 | .setMediaCrc32(4258069839L)
73 | .setWidth(43)
74 | .setHeight(44)
75 | .setFormat("png")
76 | .setFsize(2670)
77 | .build();
78 |
79 | MessagePayload payload = MessagePayload.newBuilder()
80 | .setVersion(1)
81 | .setTargetType("single")
82 | .setTargetId("test_user1")
83 | .setFromType("admin")
84 | .setFromId("junit_admin")
85 | .setMessageType(MessageType.IMAGE)
86 | .setNoNotification(false)
87 | .setMessageBody(messageBody)
88 | .build();
89 |
90 | try {
91 | SendMessageResult res = client.sendMessage(payload);
92 | System.out.println(res.getMsg_id());
93 | } catch (APIConnectionException e) {
94 | LOG.error("Connection error. Should retry later. ", e);
95 | } catch (APIRequestException e) {
96 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
97 | LOG.info("HTTP Status: " + e.getStatus());
98 | LOG.info("Error Message: " + e.getMessage());
99 | }
100 |
101 | }
102 |
103 | /**
104 | * Get message list without cursor(first time), will return cursor, the later request will
105 | * use cursor to get messages.
106 | */
107 | public static void testGetMessageList() {
108 | JMessageClient client = new JMessageClient(appkey, masterSecret);
109 | try {
110 | MessageListResult result = client.getMessageList(10, "2018-06-08 10:10:10", "2018-06-15 10:10:10");
111 | String cursor = result.getCursor();
112 | if (null != cursor && StringUtils.isNotEmpty(cursor)) {
113 | MessageResult[] messages = result.getMessages();
114 | MessageListResult secondResult = client.getMessageListByCursor(cursor);
115 | MessageResult[] secondMessages = secondResult.getMessages();
116 | }
117 | } catch (APIConnectionException e) {
118 | LOG.error("Connection error. Should retry later. ", e);
119 | } catch (APIRequestException e) {
120 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
121 | LOG.info("HTTP Status: " + e.getStatus());
122 | LOG.info("Error Message: " + e.getMessage());
123 | }
124 | }
125 |
126 | public static void testGetUserMessageList() {
127 | JMessageClient client = new JMessageClient(appkey, masterSecret);
128 | try {
129 | MessageListResult result = client.getUserMessages("username", 10, "2016-09-08 10:10:10", "2016-09-15 10:10:10");
130 | String cursor = result.getCursor();
131 | MessageListResult secondResult = client.getUserMessagesByCursor("username", cursor);
132 | } catch (APIConnectionException e) {
133 | LOG.error("Connection error. Should retry later. ", e);
134 | } catch (APIRequestException e) {
135 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
136 | LOG.info("HTTP Status: " + e.getStatus());
137 | LOG.info("Error Message: " + e.getMessage());
138 | }
139 | }
140 |
141 | public static void testRetractMessage() {
142 | JMessageClient client = new JMessageClient(appkey, masterSecret);
143 | try {
144 | ResponseWrapper result = client.retractMessage("username", 12345);
145 | LOG.info(result.toString());
146 | } catch (APIConnectionException e) {
147 | LOG.error("Connection error. Should retry later. ", e);
148 | } catch (APIRequestException e) {
149 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
150 | LOG.info("HTTP Status: " + e.getStatus());
151 | LOG.info("Error Message: " + e.getMessage());
152 | }
153 | }
154 | }
155 |
--------------------------------------------------------------------------------
/example/main/java/cn/jmessage/api/examples/ReportExample.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.examples;
2 |
3 | import cn.jiguang.common.resp.APIConnectionException;
4 | import cn.jiguang.common.resp.APIRequestException;
5 | import cn.jmessage.api.reportv2.GroupStatListResult;
6 | import cn.jmessage.api.reportv2.MessageStatListResult;
7 | import cn.jmessage.api.reportv2.ReportClient;
8 | import cn.jmessage.api.reportv2.UserStatListResult;
9 | import org.slf4j.Logger;
10 | import org.slf4j.LoggerFactory;
11 |
12 | public class ReportExample {
13 |
14 | private static Logger LOG = LoggerFactory.getLogger(ReportExample.class);
15 | private static final String appkey = "242780bfdd7315dc1989fe2b";
16 | private static final String masterSecret = "2f5ced2bef64167950e63d13";
17 | private ReportClient mClient = new ReportClient(appkey, masterSecret);
18 |
19 | public static void main(String[] args) {
20 |
21 | }
22 |
23 | public void testGetUserStat() {
24 | try {
25 | UserStatListResult result = mClient.getUserStatistic("2016-11-08", 3);
26 | LOG.info("Got result: " + result);
27 | } catch (APIConnectionException e) {
28 | LOG.error("Connection error. Should retry later. ", e);
29 | } catch (APIRequestException e) {
30 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
31 | LOG.info("HTTP Status: " + e.getStatus());
32 | LOG.info("Error Message: " + e.getMessage());
33 | }
34 | }
35 |
36 | public void testGetMessageStat() {
37 | try {
38 | MessageStatListResult result = mClient.getMessageStatistic("DAY", "2016-11-08", 3);
39 | LOG.info("Got result: " + result);
40 | } catch (APIConnectionException e) {
41 | LOG.error("Connection error. Should retry later. ", e);
42 | } catch (APIRequestException e) {
43 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
44 | LOG.info("HTTP Status: " + e.getStatus());
45 | LOG.info("Error Message: " + e.getMessage());
46 | }
47 | }
48 |
49 | public void testGetGroupStat() {
50 | try {
51 | GroupStatListResult result = mClient.getGroupStatistic("2016-11-08", 3);
52 | LOG.info("Got result: " + result);
53 | } catch (APIConnectionException e) {
54 | LOG.error("Connection error. Should retry later. ", e);
55 | } catch (APIRequestException e) {
56 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
57 | LOG.info("HTTP Status: " + e.getStatus());
58 | LOG.info("Error Message: " + e.getMessage());
59 | }
60 | }
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/example/main/java/cn/jmessage/api/examples/ResourceExample.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.examples;
2 |
3 | import cn.jiguang.common.resp.APIConnectionException;
4 | import cn.jiguang.common.resp.APIRequestException;
5 | import cn.jmessage.api.resource.DownloadResult;
6 | import cn.jmessage.api.resource.ResourceClient;
7 | import cn.jmessage.api.resource.UploadResult;
8 | import org.slf4j.Logger;
9 | import org.slf4j.LoggerFactory;
10 |
11 | public class ResourceExample {
12 |
13 | protected static final Logger LOG = LoggerFactory.getLogger(ResourceExample.class);
14 |
15 | private static final String appkey = "242780bfdd7315dc1989fe2b";
16 | private static final String masterSecret = "2f5ced2bef64167950e63d13";
17 |
18 | public static void main(String[] args) {
19 |
20 | }
21 |
22 | public static void testDownloadFile() {
23 | ResourceClient client = new ResourceClient(appkey, masterSecret);
24 | try {
25 | DownloadResult result = client.downloadFile("qiniu/image/r/48449FBC073184BDB5B50964D45FC8C3");
26 | String url = result.getUrl();
27 | } catch (APIConnectionException e) {
28 | LOG.error("Connection error. Should retry later. ", e);
29 | } catch (APIRequestException e) {
30 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
31 | LOG.info("HTTP Status: " + e.getStatus());
32 | LOG.info("Error Message: " + e.getMessage());
33 | }
34 |
35 | }
36 |
37 | public static void testUploadFile() {
38 | ResourceClient client = new ResourceClient(appkey, masterSecret);
39 | try {
40 | UploadResult result = client.uploadFile("G:\\MyDownloads\\discourse-icon.png", "image");
41 | String mediaId = result.getMediaId();
42 | } catch (APIConnectionException e) {
43 | LOG.error("Connection error. Should retry later. ", e);
44 | } catch (APIRequestException e) {
45 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
46 | LOG.info("HTTP Status: " + e.getStatus());
47 | LOG.info("Error Message: " + e.getMessage());
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/example/main/java/cn/jmessage/api/examples/SensitiveWordExample.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.examples;
2 |
3 | import cn.jiguang.common.resp.APIConnectionException;
4 | import cn.jiguang.common.resp.APIRequestException;
5 | import cn.jiguang.common.resp.ResponseWrapper;
6 | import cn.jmessage.api.sensitiveword.SensitiveWordClient;
7 | import cn.jmessage.api.sensitiveword.SensitiveWordListResult;
8 | import cn.jmessage.api.sensitiveword.SensitiveWordStatusResult;
9 | import org.slf4j.Logger;
10 | import org.slf4j.LoggerFactory;
11 |
12 | public class SensitiveWordExample {
13 |
14 | protected static final Logger LOG = LoggerFactory.getLogger(SensitiveWordExample.class);
15 |
16 | private static final String appkey = "242780bfdd7315dc1989fe2b";
17 | private static final String masterSecret = "2f5ced2bef64167950e63d13";
18 | private static SensitiveWordClient client = new SensitiveWordClient(appkey, masterSecret);
19 |
20 | public static void main(String[] args) {
21 | testGetSensitiveWordStatus();
22 | }
23 |
24 | public static void testAddSensitiveWord() {
25 | try {
26 | ResponseWrapper result = client.addSensitiveWords("fuck", "FUCK");
27 | LOG.info("response code: " + result.responseCode + " content " + result.responseContent);
28 | } catch (APIConnectionException e) {
29 | LOG.error("Connection error. Should retry later. ", e);
30 | } catch (APIRequestException e) {
31 | LOG.error("Error response from server. Should review and fix it. ", e);
32 | LOG.info("HTTP Status: " + e.getStatus());
33 | LOG.info("Error Message: " + e.getMessage());
34 | }
35 | }
36 |
37 | public static void testUpdateSensitiveWord() {
38 | try {
39 | ResponseWrapper result = client.updateSensitiveWord("f**k", "fuck");
40 | LOG.info("response code: " + result.responseCode + " content " + result.responseContent);
41 | } catch (APIConnectionException e) {
42 | LOG.error("Connection error. Should retry later. ", e);
43 | } catch (APIRequestException e) {
44 | LOG.error("Error response from server. Should review and fix it. ", e);
45 | LOG.info("HTTP Status: " + e.getStatus());
46 | LOG.info("Error Message: " + e.getMessage());
47 | }
48 | }
49 |
50 | public static void testDeleteSensitiveWord() {
51 | try {
52 | ResponseWrapper result = client.deleteSensitiveWord("f**k");
53 | LOG.info("response code: " + result.responseCode + " content " + result.responseContent);
54 | } catch (APIConnectionException e) {
55 | LOG.error("Connection error. Should retry later. ", e);
56 | } catch (APIRequestException e) {
57 | LOG.error("Error response from server. Should review and fix it. ", e);
58 | LOG.info("HTTP Status: " + e.getStatus());
59 | LOG.info("Error Message: " + e.getMessage());
60 | }
61 | }
62 |
63 | public static void testGetSensitiveWordList() {
64 | try {
65 | SensitiveWordListResult result = client.getSensitiveWordList(0, 10);
66 | LOG.info(result.toString());
67 | } catch (APIConnectionException e) {
68 | LOG.error("Connection error. Should retry later. ", e);
69 | } catch (APIRequestException e) {
70 | LOG.error("Error response from server. Should review and fix it. ", e);
71 | LOG.info("HTTP Status: " + e.getStatus());
72 | LOG.info("Error Message: " + e.getMessage());
73 | }
74 | }
75 |
76 | public static void testUpdateSensitiveWordStatus() {
77 | try {
78 | ResponseWrapper result = client.updateSensitiveWordStatus(0);
79 | LOG.info("response code: " + result.responseCode + " content " + result.responseContent);
80 | } catch (APIConnectionException e) {
81 | LOG.error("Connection error. Should retry later. ", e);
82 | } catch (APIRequestException e) {
83 | LOG.error("Error response from server. Should review and fix it. ", e);
84 | LOG.info("HTTP Status: " + e.getStatus());
85 | LOG.info("Error Message: " + e.getMessage());
86 | }
87 | }
88 |
89 | public static void testGetSensitiveWordStatus() {
90 | try {
91 | SensitiveWordStatusResult result = client.getSensitiveWordStatus();
92 | LOG.info(result.toString());
93 | } catch (APIConnectionException e) {
94 | LOG.error("Connection error. Should retry later. ", e);
95 | } catch (APIRequestException e) {
96 | LOG.error("Error response from server. Should review and fix it. ", e);
97 | LOG.info("HTTP Status: " + e.getStatus());
98 | LOG.info("Error Message: " + e.getMessage());
99 | }
100 | }
101 | }
102 |
--------------------------------------------------------------------------------
/example/main/java/cn/jmessage/api/examples/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Examples for demo API usage.
3 | */
4 | package cn.jmessage.api.examples;
--------------------------------------------------------------------------------
/example/main/java/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootLogger=DEBUG,CONSOLE
2 |
3 | log4j.logger.org.eclipse.jetty=INFO
4 |
5 | log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
6 | log4j.appender.CONSOLE.Target=System.out
7 | log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
8 | log4j.appender.CONSOLE.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n
9 |
--------------------------------------------------------------------------------
/libs/gson-2.2.4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jpush/jmessage-api-java-client/ddeace6392a331264df98026e2ac3c4c6479638d/libs/gson-2.2.4.jar
--------------------------------------------------------------------------------
/libs/log4j-1.2.17.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jpush/jmessage-api-java-client/ddeace6392a331264df98026e2ac3c4c6479638d/libs/log4j-1.2.17.jar
--------------------------------------------------------------------------------
/libs/slf4j-api-1.7.7.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jpush/jmessage-api-java-client/ddeace6392a331264df98026e2ac3c4c6479638d/libs/slf4j-api-1.7.7.jar
--------------------------------------------------------------------------------
/libs/slf4j-log4j12-1.7.7.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jpush/jmessage-api-java-client/ddeace6392a331264df98026e2ac3c4c6479638d/libs/slf4j-log4j12-1.7.7.jar
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 | 4.0.0
3 |
4 | cn.jpush.api
5 | jmessage-client
6 | 1.1.11-SNAPSHOT
7 | jar
8 | https://github.com/jpush/jmessage-api-java-client
9 | JMessage API Java Client
10 | JPush's officially supported Java client library for accessing JMessage APIs.
11 |
12 |
13 |
14 | The Apache Software License, Version 2.0
15 | http://www.apache.org/licenses/LICENSE-2.0.txt
16 | repo
17 |
18 |
19 |
20 |
21 | github
22 | UTF-8
23 | 1.7
24 | 1.7
25 |
26 |
27 |
28 | org.sonatype.oss
29 | oss-parent
30 | 9
31 |
32 |
33 |
34 | https://github.com/jpush/jmessage-api-java-client
35 | scm:git:git@github.com:jpush/jmessage-api-java-client.git
36 | scm:git:git@github.com:jpush/jmessage-api-java-client.git
37 | v1.1.7
38 |
39 |
40 |
41 |
42 | cn.jpush.api
43 | jiguang-common
44 | 1.1.3
45 |
46 |
47 | com.google.code.gson
48 | gson
49 | 2.3
50 | provided
51 |
52 |
53 |
54 | org.slf4j
55 | slf4j-api
56 | 1.7.7
57 | provided
58 |
59 |
60 |
61 |
62 |
63 | org.slf4j
64 | slf4j-log4j12
65 | 1.7.7
66 | provided
67 |
68 |
69 | log4j
70 | log4j
71 | 1.2.17
72 | provided
73 |
74 |
75 |
76 | junit
77 | junit
78 | 4.13.1
79 | provided
80 |
81 |
82 | com.squareup.okhttp
83 | mockwebserver
84 | 2.0.0
85 | provided
86 |
87 |
88 |
89 |
90 |
91 |
92 | maven-compiler-plugin
93 | 3.1
94 |
95 | ${jdkVersion}
96 | ${jdkVersion}
97 | 1.5
98 | true
99 | true
100 | true
101 | true
102 |
103 | -Xlint:unchecked
104 |
105 |
106 |
107 |
108 |
109 | org.apache.maven.plugins
110 | maven-release-plugin
111 | 2.5.3
112 |
113 |
114 | org.apache.maven.plugins
115 | maven-scm-plugin
116 | 1.9.2
117 |
118 |
119 |
120 | deploy,site
121 | false
122 | true
123 | true
124 | v@{project.version}
125 |
126 |
127 |
128 |
129 | com.github.github
130 | site-maven-plugin
131 | 0.9
132 |
133 | Creating site for ${project.version}
134 | github
135 |
136 |
137 |
138 |
139 | site
140 |
141 | site
142 |
143 |
144 |
145 |
146 |
147 | org.apache.maven.plugins
148 | maven-surefire-plugin
149 | 2.17
150 |
151 | cn.jmessage.api.FastTests
152 | -Dfile.encoding=UTF-8
153 |
154 | **/mock/*Test.java
155 |
156 |
157 |
158 |
159 | org.apache.maven.surefire
160 | surefire-junit47
161 | 2.17
162 |
163 |
164 |
165 |
166 |
167 | org.apache.maven.plugins
168 | maven-failsafe-plugin
169 | 2.17
170 |
171 | UTF-8
172 | cn.jmessage.api.SlowTests
173 | -Dfile.encoding=UTF-8
174 |
175 | **/mock/*Test.java
176 |
177 |
178 |
179 |
180 |
181 | integration-test
182 | verify
183 |
184 |
185 |
186 | **/*.class
187 |
188 |
189 |
190 |
191 |
192 |
193 | org.apache.maven.surefire
194 | surefire-junit47
195 | 2.17
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 | org.apache.maven.plugins
206 | maven-project-info-reports-plugin
207 | 2.8
208 |
209 |
210 |
211 | dependencies
212 | license
213 | scm
214 |
215 |
216 |
217 |
218 |
219 | org.apache.maven.plugins
220 | maven-javadoc-plugin
221 | 2.7
222 |
223 | resources/javadoc-overview.html
224 |
225 |
226 |
227 |
228 |
229 |
230 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/chatroom/ChatRoomHistoryResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.chatroom;
2 |
3 | import cn.jiguang.common.resp.BaseResult;
4 | import com.google.gson.annotations.Expose;
5 |
6 | import java.util.Map;
7 |
8 | public class ChatRoomHistoryResult extends BaseResult {
9 |
10 | @Expose
11 | private Integer total;
12 |
13 | @Expose
14 | private String cursor;
15 |
16 | @Expose
17 | private Integer count;
18 |
19 | @Expose
20 | private ChatRoomBaseMessageResult[] messages;
21 |
22 | public Integer getTotal() {
23 | return total;
24 | }
25 |
26 | public String getCursor() {
27 | return cursor;
28 | }
29 |
30 | public Integer getCount() {
31 | return count;
32 | }
33 |
34 | public static class ChatRoomBaseMessageResult {
35 |
36 | @Expose
37 | private Integer set_from_name;
38 |
39 | @Expose
40 | private String from_platform;
41 |
42 | @Expose
43 | private String target_name;
44 |
45 | @Expose
46 | private String msg_type;
47 |
48 | @Expose
49 | private Integer version;
50 |
51 | @Expose
52 | private String target_id;
53 |
54 | @Expose
55 | private String from_appkey;
56 |
57 | @Expose
58 | private String from_name;
59 |
60 | @Expose
61 | private String from_id;
62 |
63 | @Expose
64 | private Map msg_body;
65 |
66 | @Expose
67 | private Long create_time;
68 |
69 | @Expose
70 | private String from_type;
71 |
72 | @Expose
73 | private String target_appkey;
74 |
75 | @Expose
76 | private String target_type;
77 |
78 | @Expose
79 | private Long msgid;
80 |
81 | @Expose
82 | private Long msg_ctime;
83 |
84 | @Expose
85 | private Integer msg_level;
86 |
87 | public Integer getSet_from_name() {
88 | return set_from_name;
89 | }
90 |
91 | public String getFrom_platform() {
92 | return from_platform;
93 | }
94 |
95 | public String getTarget_name() {
96 | return target_name;
97 | }
98 |
99 | public String getMsg_type() {
100 | return msg_type;
101 | }
102 |
103 | public Integer getVersion() {
104 | return version;
105 | }
106 |
107 | public String getTarget_id() {
108 | return target_id;
109 | }
110 |
111 | public String getFrom_appkey() {
112 | return from_appkey;
113 | }
114 |
115 | public String getFrom_name() {
116 | return from_name;
117 | }
118 |
119 | public String getFrom_id() {
120 | return from_id;
121 | }
122 |
123 | public Map getMsg_body() {
124 | return msg_body;
125 | }
126 |
127 | public Long getCreate_time() {
128 | return create_time;
129 | }
130 |
131 | public String getFrom_type() {
132 | return from_type;
133 | }
134 |
135 | public String getTarget_appkey() {
136 | return target_appkey;
137 | }
138 |
139 | public String getTarget_type() {
140 | return target_type;
141 | }
142 |
143 | public Long getMsgid() {
144 | return msgid;
145 | }
146 |
147 | public Long getMsg_ctime() {
148 | return msg_ctime;
149 | }
150 |
151 | public Integer getMsg_level() {
152 | return msg_level;
153 | }
154 | }
155 |
156 | private static class BaseMessageResult {
157 |
158 | @Expose
159 | private String text;
160 |
161 | @Expose
162 | private Map extras;
163 |
164 | public String getText() {
165 | return text;
166 | }
167 |
168 | public Map getExtras() {
169 | return extras;
170 | }
171 | }
172 |
173 | }
174 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/chatroom/ChatRoomListResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.chatroom;
2 |
3 | import cn.jiguang.common.resp.BaseResult;
4 | import cn.jiguang.common.resp.ResponseWrapper;
5 | import com.google.gson.annotations.Expose;
6 |
7 | import java.util.ArrayList;
8 | import java.util.List;
9 |
10 | public class ChatRoomListResult extends BaseResult {
11 |
12 | @Expose private List rooms = new ArrayList();
13 | @Expose private Integer total;
14 | @Expose private ChatRoomResult[] roomsArray;
15 | @Expose private Integer start;
16 | @Expose private Integer count;
17 |
18 | public static ChatRoomListResult fromResponse(ResponseWrapper responseWrapper) {
19 | ChatRoomListResult result = new ChatRoomListResult();
20 | if (responseWrapper.isServerResponse()) {
21 | result.roomsArray = _gson.fromJson(responseWrapper.responseContent, ChatRoomResult[].class);
22 | }
23 | result.setResponseWrapper(responseWrapper);
24 | return result;
25 | }
26 |
27 | public ChatRoomResult[] getRooms() {
28 | return this.roomsArray;
29 | }
30 |
31 | public List getList() {
32 | return this.rooms;
33 | }
34 |
35 | public Integer getTotal() {
36 | return this.total;
37 | }
38 |
39 | public Integer getStart() {
40 | return this.start;
41 | }
42 |
43 | public Integer getCount() {
44 | return this.count;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/chatroom/ChatRoomMemberList.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.chatroom;
2 |
3 |
4 | import cn.jiguang.common.resp.BaseResult;
5 | import cn.jiguang.common.resp.ResponseWrapper;
6 | import com.google.gson.annotations.Expose;
7 |
8 | public class ChatRoomMemberList extends BaseResult {
9 |
10 | @Expose private ChatRoomMember[] users;
11 | @Expose private Integer total;
12 | @Expose private Integer start;
13 | @Expose private Integer count;
14 |
15 | public static ChatRoomMemberList fromResponse(ResponseWrapper responseWrapper) {
16 | ChatRoomMemberList result = new ChatRoomMemberList();
17 | if (responseWrapper.isServerResponse()) {
18 | result.users = _gson.fromJson(responseWrapper.responseContent, ChatRoomMember[].class);
19 | } else {
20 | // nothing
21 | }
22 | result.setResponseWrapper(responseWrapper);
23 | return result;
24 | }
25 |
26 | public class ChatRoomMember {
27 | @Expose String username;
28 | @Expose Integer flag;
29 | @Expose String room_ctime;
30 | @Expose String mtime;
31 | @Expose String ctime;
32 |
33 | public String getUsername() {
34 | return username;
35 | }
36 |
37 | public Integer getFlag() {
38 | return flag;
39 | }
40 |
41 | public String getRoom_ctime() {
42 | return room_ctime;
43 | }
44 |
45 | public String getMtime() {
46 | return mtime;
47 | }
48 |
49 | public String getCtime() {
50 | return ctime;
51 | }
52 | }
53 |
54 | public ChatRoomMember[] getMembers() {
55 | return this.users;
56 | }
57 |
58 | public Integer getTotal() {
59 | return total;
60 | }
61 |
62 | public Integer getStart() {
63 | return start;
64 | }
65 |
66 | public Integer getCount() {
67 | return count;
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/chatroom/ChatRoomResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.chatroom;
2 |
3 | import cn.jiguang.common.resp.BaseResult;
4 | import com.google.gson.annotations.Expose;
5 |
6 | public class ChatRoomResult extends BaseResult {
7 |
8 | @Expose private Long id;
9 | @Expose private String owner_username;
10 | @Expose private String appkey;
11 | @Expose private Integer max_member_count;
12 | @Expose private String name;
13 | @Expose private String description;
14 | @Expose private Integer total_member_count;
15 | @Expose private String ctime;
16 |
17 | @Override
18 | public String toString() {
19 | return "room id: " + id + ", name: " + name +", owner username: " + owner_username + ", appKey: " + appkey
20 | + ", max member count: " + max_member_count + ", description: " + description
21 | + ", total member count: " + total_member_count;
22 | }
23 |
24 | public Long getId() {
25 | return this.id;
26 | }
27 |
28 | public String getOwnerUsername() {
29 | return owner_username;
30 | }
31 |
32 | public String getAppkey() {
33 | return appkey;
34 | }
35 |
36 | public Integer getMaxMemberCount() {
37 | return max_member_count;
38 | }
39 |
40 | public String getName() {
41 | return name;
42 | }
43 |
44 | public String getDescription() {
45 | return description;
46 | }
47 |
48 | public Integer getTotalMemberCount() {
49 | return total_member_count;
50 | }
51 |
52 | public String getCtime() {
53 | return ctime;
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/chatroom/CreateChatRoomResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.chatroom;
2 |
3 | import cn.jiguang.common.resp.BaseResult;
4 | import com.google.gson.annotations.Expose;
5 |
6 | public class CreateChatRoomResult extends BaseResult {
7 |
8 | @Expose private Long chatroom_id;
9 |
10 | public Long getChatroom_id() {
11 | return chatroom_id;
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/chatroom/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Chat room API features. https://docs.jiguang.cn/jmessage/server/rest_api_im/#_56
3 | *
4 | */
5 | package cn.jmessage.api.chatroom;
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/common/BaseClient.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.common;
2 |
3 | import cn.jiguang.common.ServiceHelper;
4 | import cn.jiguang.common.connection.HttpProxy;
5 | import cn.jiguang.common.connection.IHttpClient;
6 | import cn.jiguang.common.connection.NativeHttpClient;
7 | import com.google.gson.Gson;
8 |
9 | public class BaseClient {
10 |
11 | protected IHttpClient _httpClient;
12 | protected String _baseUrl;
13 | protected Gson _gson = new Gson();
14 |
15 | /**
16 | * Create a JMessage Base Client
17 | *
18 | * @param appKey The KEY of one application on JPush.
19 | * @param masterSecret API access secret of the appKey.
20 | * @param proxy The proxy, if there is no proxy, should be null.
21 | * @param config The client configuration. Can use JMessageConfig.getInstance() as default.
22 | */
23 | public BaseClient(String appKey, String masterSecret, HttpProxy proxy, JMessageConfig config) {
24 | ServiceHelper.checkBasic(appKey, masterSecret);
25 | String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
26 | this._baseUrl = (String) config.get(JMessageConfig.API_HOST_NAME);
27 | this._httpClient = new NativeHttpClient(authCode, proxy, config.getClientConfig());
28 | }
29 |
30 | public void setHttpClient(IHttpClient httpClient) {
31 | this._httpClient = httpClient;
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/common/JMessageConfig.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.common;
2 |
3 | import cn.jiguang.common.ClientConfig;
4 |
5 | public class JMessageConfig {
6 |
7 | private static ClientConfig clientConfig = ClientConfig.getInstance();
8 |
9 | private static JMessageConfig instance = new JMessageConfig();
10 |
11 | public static final String API_HOST_NAME = "im.api.host.name";
12 | public static final String API_REPORT_HOST_NAME = "im.api.report.host.name";
13 |
14 | public static final String ADMIN_PATH = "im.admin.path";
15 |
16 | public static final String USER_PATH = "im.user.path";
17 | public static final String V2_USER_PATH = "im.v2.user.path";
18 |
19 | public static final String GROUP_PATH = "im.group.path";
20 | public static final String V2_GROUP_PATH = "im.v2.group.path";
21 |
22 | public static final String MESSAGE_PATH = "im.message.path";
23 | public static final String V2_MESSAGE_PATH = "im.v2.message.path";
24 | public static final String V2_STATISTIC_PATH = "im.v2.statistic.path";
25 | public static final String V2_CHATROOM_PATH = "im.v2.chatroom.path";
26 |
27 | public static final String RESOURCE_PATH = "im.resource.path";
28 |
29 | public static final String CROSS_USER_PATH = "im.cross.user.path";
30 | public static final String CROSS_GROUP_PATH = "im.cross.group.path";
31 |
32 | public static final String SENSITIVE_WORD_PATH = "im.sensitive.word.path";
33 |
34 | public static final String CHAT_ROOM_PATH = "im.chat.room.path";
35 |
36 | public static final String MAX_RETRY_TIMES = ClientConfig.MAX_RETRY_TIMES;
37 |
38 | public static final String SEND_VERSION = "send.version";
39 | public static final Object SEND_VERSION_SCHMEA = Integer.class;
40 |
41 | private JMessageConfig() {
42 | clientConfig.put(API_HOST_NAME, "https://api.im.jpush.cn");
43 | clientConfig.put(API_REPORT_HOST_NAME, "https://report.im.jpush.cn");
44 | clientConfig.put(ADMIN_PATH, "/v1/admins");
45 | clientConfig.put(USER_PATH, "/v1/users");
46 | clientConfig.put(V2_USER_PATH, "/v2/users");
47 | clientConfig.put(GROUP_PATH, "/v1/groups");
48 | clientConfig.put(V2_GROUP_PATH, "/v2/groups");
49 | clientConfig.put(MESSAGE_PATH, "/v1/messages");
50 | clientConfig.put(V2_MESSAGE_PATH, "/v2/messages");
51 | clientConfig.put(RESOURCE_PATH, "/v1/resource");
52 | clientConfig.put(CROSS_USER_PATH, "/v1/cross/users");
53 | clientConfig.put(CROSS_GROUP_PATH, "/v1/cross/groups");
54 | clientConfig.put(SENSITIVE_WORD_PATH, "/v1/sensitiveword");
55 | clientConfig.put(CHAT_ROOM_PATH, "/v1/chatroom");
56 | clientConfig.put(V2_CHATROOM_PATH, "/v2/chatrooms");
57 | clientConfig.put(V2_STATISTIC_PATH, "/v2/statistic");
58 | clientConfig.put(MAX_RETRY_TIMES, 3);
59 | clientConfig.put(SEND_VERSION, 1);
60 | }
61 |
62 | public static JMessageConfig getInstance() {
63 | return instance;
64 | }
65 |
66 | public ClientConfig getClientConfig() {
67 | return clientConfig;
68 | }
69 |
70 | public JMessageConfig setApiHostName(String hostName) {
71 | clientConfig.put(API_HOST_NAME, hostName);
72 | return this;
73 | }
74 |
75 | public JMessageConfig setReportHostName(String hostName) {
76 | clientConfig.put(API_REPORT_HOST_NAME, hostName);
77 | return this;
78 | }
79 |
80 | public JMessageConfig setMaxRetryTimes(int maxRetryTimes) {
81 | clientConfig.setMaxRetryTimes(maxRetryTimes);
82 | return this;
83 | }
84 |
85 | public void put(String key, Object value) {
86 | clientConfig.put(key, value);
87 | }
88 |
89 | public Object get(String key) {
90 | return clientConfig.get(key);
91 | }
92 |
93 | }
94 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/common/model/IModel.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.common.model;
2 |
3 | import com.google.gson.JsonElement;
4 |
5 | public interface IModel {
6 |
7 | public JsonElement toJSON();
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/common/model/Members.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.common.model;
2 |
3 | import com.google.gson.JsonArray;
4 | import com.google.gson.JsonElement;
5 | import com.google.gson.JsonPrimitive;
6 |
7 |
8 | public class Members implements IModel {
9 |
10 | private JsonArray members;
11 |
12 | private Members(JsonArray members) {
13 | this.members = members;
14 | }
15 |
16 | public static Builder newBuilder() {
17 | return new Builder();
18 | }
19 |
20 | @Override
21 | public JsonElement toJSON() {
22 | return members;
23 | }
24 |
25 | public static class Builder {
26 |
27 | private JsonArray members = new JsonArray();
28 |
29 | public Builder addMember(String... usernames) {
30 |
31 | if( null == usernames ) {
32 | return this;
33 | }
34 |
35 | for (String username : usernames) {
36 | JsonPrimitive member = new JsonPrimitive(username);
37 | this.members.add(member);
38 | }
39 | return this;
40 | }
41 |
42 | public Members build() {
43 | return new Members(members);
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/common/model/NoDisturbPayload.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.common.model;
2 |
3 | import com.google.gson.*;
4 |
5 | public class NoDisturbPayload implements IModel{
6 |
7 | public static String SINGLE = "single";
8 | public static String ADD = "add";
9 | public static String REMOVE = "remove";
10 | public static String GROUP = "group";
11 | public static String GLOBAL = "global";
12 |
13 | private static Gson gson = new Gson();
14 |
15 | private String[] add_single_users;
16 | private String[] remove_single_users;
17 | private Long[] add_group_ids;
18 | private Long[] remove_group_ids;
19 | //Global no disturb setting, global value could be 0 or 1, 0 represents close, 1 represents open.
20 | //Default value is 0.
21 | private int global = 0;
22 |
23 | private NoDisturbPayload(String[] add_single_users, String[] remove_single_users, Long[] add_group_ids,
24 | Long[] remove_group_ids, int global) {
25 | this.add_single_users = add_single_users;
26 | this.remove_single_users = remove_single_users;
27 | this.add_group_ids = add_group_ids;
28 | this.remove_group_ids = remove_group_ids;
29 | this.global = global;
30 | }
31 |
32 |
33 | @Override
34 | public JsonElement toJSON() {
35 |
36 | JsonObject json = new JsonObject();
37 |
38 | if (null != add_single_users) {
39 | JsonObject jsonObject = new JsonObject();
40 | JsonArray jsonArray = new JsonArray();
41 | for (String username : add_single_users) {
42 | jsonArray.add(new JsonPrimitive(username));
43 | }
44 | jsonObject.add(ADD, jsonArray);
45 | if (null != remove_single_users) {
46 | JsonArray jsonArray1 = new JsonArray();
47 | for (String username : remove_single_users) {
48 | jsonArray1.add(new JsonPrimitive(username));
49 | }
50 | jsonObject.add(REMOVE, jsonArray1);
51 | }
52 | json.add(SINGLE, jsonObject);
53 | } else if (null != remove_single_users) {
54 | JsonObject jsonObject = new JsonObject();
55 | JsonArray jsonArray = new JsonArray();
56 | for (String username : remove_single_users) {
57 | jsonArray.add(new JsonPrimitive(username));
58 | }
59 | jsonObject.add(REMOVE, jsonArray);
60 | json.add(SINGLE, jsonObject);
61 | }
62 |
63 | if (null != add_group_ids) {
64 | JsonObject jsonObject = new JsonObject();
65 | JsonArray jsonArray = new JsonArray();
66 | for (Long groupId : add_group_ids) {
67 | jsonArray.add(new JsonPrimitive(groupId));
68 | }
69 | jsonObject.add(ADD, jsonArray);
70 |
71 | if (null != remove_group_ids) {
72 | JsonArray jsonArray1 = new JsonArray();
73 | for (Long groupId : remove_group_ids) {
74 | jsonArray1.add(new JsonPrimitive(groupId));
75 | }
76 | jsonObject.add(REMOVE, jsonArray1);
77 | }
78 | json.add(GROUP, jsonObject);
79 | } else if (null != remove_group_ids) {
80 | JsonObject jsonObject = new JsonObject();
81 | JsonArray jsonArray = new JsonArray();
82 | for (Long groupId : remove_group_ids) {
83 | jsonArray.add(new JsonPrimitive(groupId));
84 | }
85 | jsonObject.add(REMOVE, jsonArray);
86 | json.add(GROUP, jsonObject);
87 | }
88 |
89 | if (0 != global) {
90 | json.addProperty(GLOBAL, global);
91 | }
92 |
93 | return json;
94 | }
95 |
96 | @Override
97 | public String toString() {
98 | return gson.toJson(toJSON());
99 | }
100 |
101 | public static class Builder {
102 | private String[] add_single_users;
103 | private String[] remove_single_users;
104 | private Long[] add_group_ids;
105 | private Long[] remove_group_ids;
106 | private int global = 0;
107 |
108 | public Builder setAddSingleUsers(String...add_single_users) {
109 | this.add_single_users = add_single_users;
110 | return this;
111 | }
112 |
113 | public Builder setRemoveSingleUsers(String...remove_single_users) {
114 | this.remove_single_users = remove_single_users;
115 | return this;
116 | }
117 |
118 | public Builder setAddGroupIds(Long...add_group_ids) {
119 | this.add_group_ids = add_group_ids;
120 | return this;
121 | }
122 |
123 | public Builder setRemoveGroupIds(Long...remove_group_ids) {
124 | this.remove_group_ids = remove_group_ids;
125 | return this;
126 | }
127 |
128 | public Builder setGlobal(int global) {
129 | this.global = global;
130 | return this;
131 | }
132 |
133 | public NoDisturbPayload build() {
134 | return new NoDisturbPayload(add_single_users, remove_single_users, add_group_ids, remove_group_ids, global);
135 | }
136 | }
137 | }
138 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/common/model/RegisterPayload.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.common.model;
2 |
3 | import com.google.gson.Gson;
4 | import com.google.gson.JsonArray;
5 | import com.google.gson.JsonElement;
6 |
7 | import cn.jiguang.common.utils.Preconditions;
8 |
9 | public class RegisterPayload implements IModel {
10 |
11 |
12 | private static Gson gson = new Gson();
13 |
14 | private JsonArray array ;
15 |
16 | private RegisterPayload(JsonArray array) {
17 | this.array = array;
18 | }
19 |
20 | public static Builder newBuilder() {
21 | return new Builder();
22 | }
23 |
24 | @Override
25 | public JsonElement toJSON() {
26 | return array;
27 | }
28 |
29 | public static class Builder {
30 |
31 | private JsonArray array = new JsonArray();
32 |
33 | public Builder addUsers(RegisterInfo... users) {
34 |
35 | if( null == users ) {
36 | return this;
37 | }
38 |
39 | for ( RegisterInfo user : users) {
40 |
41 | array.add(user.toJSON());
42 | }
43 |
44 | return this;
45 | }
46 |
47 | public RegisterPayload build() {
48 |
49 | Preconditions.checkArgument(0 != array.size(), "The user list must not be empty.");
50 |
51 | return new RegisterPayload(array);
52 | }
53 | }
54 |
55 | @Override
56 | public String toString() {
57 | return gson.toJson(toJSON());
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/common/model/chatroom/ChatRoomPayload.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.common.model.chatroom;
2 |
3 | import cn.jmessage.api.common.model.IModel;
4 | import cn.jmessage.api.common.model.Members;
5 | import com.google.gson.Gson;
6 | import com.google.gson.JsonElement;
7 | import com.google.gson.JsonObject;
8 |
9 | public class ChatRoomPayload implements IModel {
10 |
11 | public static final String OWNER = "owner_username";
12 | public static final String NAME = "name";
13 | public static final String MEMBERS = "members_username";
14 | public static final String DESC = "description";
15 | public static final String FLAG = "flag";
16 |
17 | private static Gson gson = new Gson();
18 |
19 | private String owner;
20 | private String name;
21 | private Members members;
22 | private String desc;
23 | // 禁言标志,0 表示不禁言,1 表示禁言
24 | private int flag = -1;
25 |
26 | public ChatRoomPayload(String name, String ownerName, Members members, String desc, int flag) {
27 | this.name = name;
28 | this.owner = ownerName;
29 | this.members = members;
30 | this.desc = desc;
31 | this.flag = flag;
32 | }
33 |
34 | public static Builder newBuilder() {
35 | return new Builder();
36 | }
37 |
38 |
39 | @Override
40 | public JsonElement toJSON() {
41 | JsonObject jsonObject = new JsonObject();
42 | if (null != name) {
43 | jsonObject.addProperty(NAME, name);
44 | }
45 |
46 | if (null != owner) {
47 | jsonObject.addProperty(OWNER, owner);
48 | }
49 |
50 | if ( null != members ) {
51 | jsonObject.add(MEMBERS, members.toJSON());
52 | }
53 |
54 | if ( null != desc ) {
55 | jsonObject.addProperty(DESC, desc);
56 | }
57 |
58 | if (flag != -1) {
59 | jsonObject.addProperty(FLAG, flag);
60 | }
61 |
62 | return jsonObject;
63 | }
64 |
65 | public static class Builder {
66 | private String owner;
67 | private String name;
68 | private Members members;
69 | private String desc;
70 | private int flag = -1;
71 |
72 | public Builder setName(String name) {
73 | this.name = name;
74 | return this;
75 | }
76 |
77 | public Builder setOwnerUsername(String owner) {
78 | this.owner = owner;
79 | return this;
80 | }
81 |
82 | public Builder setMembers(Members members) {
83 | this.members = members;
84 | return this;
85 | }
86 |
87 | public Builder setDesc(String desc) {
88 | this.desc = desc;
89 | return this;
90 | }
91 |
92 | public Builder setFlag(int flag) {
93 | this.flag = flag;
94 | return this;
95 | }
96 |
97 | public ChatRoomPayload build() {
98 | return new ChatRoomPayload(name, owner, members, desc, flag);
99 | }
100 |
101 | }
102 |
103 | @Override
104 | public String toString() {
105 | return gson.toJson(toJSON());
106 | }
107 | }
108 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/common/model/cross/CrossBlacklist.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.common.model.cross;
2 |
3 | import cn.jiguang.common.utils.Preconditions;
4 | import cn.jmessage.api.common.model.IModel;
5 | import com.google.gson.*;
6 |
7 | public class CrossBlacklist implements IModel {
8 |
9 | private static String APP_KEY = "appkey";
10 | private static String USERNAMES = "usernames";
11 |
12 | private Gson gson = new Gson();
13 |
14 | private String appKey;
15 | private String[] users;
16 |
17 | private CrossBlacklist(String appKey, String[] users) {
18 | this.appKey = appKey;
19 | this.users = users;
20 | }
21 |
22 | public Builder newBuilder() {
23 | return new CrossBlacklist.Builder();
24 | }
25 |
26 | public static class Builder {
27 | private String appKey;
28 | private String[] users;
29 |
30 | public Builder setAppKey(String appKey) {
31 | this.appKey = appKey;
32 | return this;
33 | }
34 |
35 | public Builder addUsers(String...users) {
36 | this.users = users;
37 | return this;
38 | }
39 |
40 | public CrossBlacklist build() {
41 | Preconditions.checkArgument(null != appKey, "AppKey must not be null");
42 | Preconditions.checkArgument(null != users, "At least add one user");
43 | return new CrossBlacklist(appKey, users);
44 | }
45 | }
46 |
47 | @Override
48 | public JsonElement toJSON() {
49 | JsonObject json = new JsonObject();
50 |
51 | if (null != appKey) {
52 | json.addProperty(APP_KEY, appKey);
53 | }
54 |
55 | if (null != users) {
56 | JsonArray array = new JsonArray();
57 | for (String user : users) {
58 | array.add(new JsonPrimitive(user));
59 | }
60 | json.add(USERNAMES, array);
61 | }
62 |
63 | return json;
64 | }
65 |
66 | @Override
67 | public String toString() {
68 | return gson.toJson(toJSON());
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/common/model/cross/CrossBlacklistPayload.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.common.model.cross;
2 |
3 | import cn.jiguang.common.utils.Preconditions;
4 | import cn.jmessage.api.common.model.IModel;
5 | import com.google.gson.Gson;
6 | import com.google.gson.JsonArray;
7 | import com.google.gson.JsonElement;
8 |
9 | public class CrossBlacklistPayload implements IModel {
10 | private static Gson gson = new Gson();
11 |
12 | private JsonArray array ;
13 |
14 | private CrossBlacklistPayload(JsonArray array) {
15 | this.array = array;
16 | }
17 |
18 | public static Builder newBuilder() {
19 | return new CrossBlacklistPayload.Builder();
20 | }
21 |
22 | @Override
23 | public JsonElement toJSON() {
24 | return array;
25 | }
26 |
27 | public static class Builder {
28 |
29 | private JsonArray array = new JsonArray();
30 |
31 | public Builder setCrossBlacklists(CrossBlacklist... blacklists) {
32 |
33 | if( null == blacklists ) {
34 | return this;
35 | }
36 |
37 | for ( CrossBlacklist blacklist : blacklists) {
38 |
39 | array.add(blacklist.toJSON());
40 | }
41 |
42 | return this;
43 | }
44 |
45 | public CrossBlacklistPayload build() {
46 |
47 | Preconditions.checkArgument(0 != array.size(), "The array must not be empty.");
48 | Preconditions.checkArgument(array.size() <= 500, "The array size must not over 500");
49 |
50 | return new CrossBlacklistPayload(array);
51 | }
52 | }
53 |
54 | @Override
55 | public String toString() {
56 | return gson.toJson(toJSON());
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/common/model/cross/CrossFriendPayload.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.common.model.cross;
2 |
3 | import cn.jiguang.common.utils.Preconditions;
4 | import cn.jmessage.api.common.model.IModel;
5 | import com.google.gson.*;
6 |
7 | public class CrossFriendPayload implements IModel {
8 |
9 | private final static String APP_KEY = "appkey";
10 | private final static String USERS = "users";
11 | private Gson gson = new Gson();
12 | private String appKey;
13 | private String[] users;
14 |
15 | public CrossFriendPayload(String appKey, String[] users) {
16 | this.appKey = appKey;
17 | this.users = users;
18 | }
19 |
20 | public CrossFriendPayload newBuilder() {
21 | return new Builder().build();
22 | }
23 |
24 | public static class Builder {
25 | private String appKey;
26 | private String[] users;
27 |
28 | public Builder setAppKey(String appKey) {
29 | this.appKey = appKey;
30 | return this;
31 | }
32 |
33 | public Builder setUsers(String...users) {
34 | this.users = users;
35 | return this;
36 | }
37 |
38 | public CrossFriendPayload build() {
39 | Preconditions.checkArgument(null != appKey, "AppKey should not be null!");
40 | Preconditions.checkArgument(null != users, "Users should not be null");
41 | return new CrossFriendPayload(appKey, users);
42 | }
43 | }
44 |
45 |
46 | @Override
47 | public JsonElement toJSON() {
48 | JsonObject jsonObject = new JsonObject();
49 |
50 | if (null != appKey) {
51 | jsonObject.addProperty(APP_KEY, appKey);
52 | }
53 |
54 | if (null != users) {
55 | JsonArray array = new JsonArray();
56 | for (String user : users) {
57 | array.add(new JsonPrimitive(user));
58 | }
59 | jsonObject.add(USERS, array);
60 | }
61 |
62 | return jsonObject;
63 | }
64 |
65 | @Override
66 | public String toString() {
67 | return gson.toJson(toJSON());
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/common/model/cross/CrossGroup.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.common.model.cross;
2 |
3 | import cn.jiguang.common.utils.Preconditions;
4 | import cn.jmessage.api.common.model.IModel;
5 | import com.google.gson.*;
6 |
7 | public class CrossGroup implements IModel {
8 |
9 | private static final String APP_KEY = "appkey";
10 | private static final String ADD = "add";
11 | private static final String REMOVE = "remove";
12 |
13 | private Gson gson = new Gson();
14 |
15 | private String appKey;
16 | private String[] add_users;
17 | private String[] remove_users;
18 |
19 | private CrossGroup(String appKey, String[] add_users, String[] remove_users) {
20 | this.appKey = appKey;
21 | this.add_users = add_users;
22 | this.remove_users = remove_users;
23 | }
24 |
25 | public CrossGroup newBuilder() {
26 | return new Builder().build();
27 | }
28 |
29 | public static class Builder {
30 | private String appKey;
31 | private String[] add_users;
32 | private String[] remove_users;
33 |
34 | public Builder setAppKey(String appKey) {
35 | this.appKey = appKey;
36 | return this;
37 | }
38 |
39 | public Builder setAddUsers(String...users) {
40 | this.add_users = users;
41 | return this;
42 | }
43 |
44 | public Builder setRemoveUsers(String...users) {
45 | this.remove_users = users;
46 | return this;
47 | }
48 |
49 | public CrossGroup build() {
50 | Preconditions.checkArgument(null != appKey, "AppKey must not be null");
51 | if (null == add_users && null == remove_users) {
52 | throw new IllegalArgumentException("At least one of add array or remove array should not be null");
53 | }
54 | return new CrossGroup(appKey, add_users, remove_users);
55 | }
56 |
57 |
58 | }
59 |
60 | @Override
61 | public JsonElement toJSON() {
62 | JsonObject json = new JsonObject();
63 |
64 | if (null != appKey) {
65 | json.addProperty(APP_KEY, appKey);
66 | }
67 |
68 | if (null != add_users) {
69 | JsonArray array = new JsonArray();
70 | for (String user : add_users) {
71 | array.add(new JsonPrimitive(user));
72 | }
73 | json.add(ADD, array);
74 | }
75 |
76 | if (null != remove_users) {
77 | JsonArray array = new JsonArray();
78 | for (String user : remove_users) {
79 | array.add(new JsonPrimitive(user));
80 | }
81 | json.add(REMOVE, array);
82 | }
83 |
84 | return json;
85 | }
86 |
87 | @Override
88 | public String toString() {
89 | return gson.toJson(toJSON());
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/common/model/cross/CrossGroupPayload.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.common.model.cross;
2 |
3 | import cn.jiguang.common.utils.Preconditions;
4 | import cn.jmessage.api.common.model.IModel;
5 | import com.google.gson.Gson;
6 | import com.google.gson.JsonArray;
7 | import com.google.gson.JsonElement;
8 |
9 | public class CrossGroupPayload implements IModel {
10 | private static Gson gson = new Gson();
11 |
12 | private JsonArray array ;
13 |
14 | private CrossGroupPayload(JsonArray array) {
15 | this.array = array;
16 | }
17 |
18 | public static Builder newBuilder() {
19 | return new CrossGroupPayload.Builder();
20 | }
21 |
22 | @Override
23 | public JsonElement toJSON() {
24 | return array;
25 | }
26 |
27 | public static class Builder {
28 |
29 | private JsonArray array = new JsonArray();
30 |
31 | public Builder setCrossGroups(CrossGroup... groups) {
32 |
33 | if( null == groups ) {
34 | return this;
35 | }
36 |
37 | for ( CrossGroup group : groups) {
38 |
39 | array.add(group.toJSON());
40 | }
41 |
42 | return this;
43 | }
44 |
45 | public CrossGroupPayload build() {
46 |
47 | Preconditions.checkArgument(0 != array.size(), "The array must not be empty.");
48 | Preconditions.checkArgument(array.size() <= 500, "The array size must not over 500");
49 |
50 | return new CrossGroupPayload(array);
51 | }
52 | }
53 |
54 | @Override
55 | public String toString() {
56 | return gson.toJson(toJSON());
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/common/model/cross/CrossNoDisturb.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.common.model.cross;
2 |
3 | import cn.jiguang.common.utils.Preconditions;
4 | import cn.jmessage.api.common.model.IModel;
5 | import com.google.gson.*;
6 |
7 | public class CrossNoDisturb implements IModel {
8 |
9 | private static String APP_KEY = "appkey";
10 | private static String SINGLE = "single";
11 | private static String ADD = "add";
12 | private static String REMOVE = "remove";
13 | private static String GROUP = "group";
14 |
15 | private static Gson gson = new Gson();
16 |
17 | private String appKey;
18 | private String[] add_single_users;
19 | private String[] remove_single_users;
20 | private Long[] add_group_ids;
21 | private Long[] remove_group_ids;
22 |
23 | private CrossNoDisturb(String appKey, String[] add_single_users, String[] remove_single_users,
24 | Long[] add_group_ids, Long[] remove_group_ids) {
25 | this.appKey = appKey;
26 | this.add_single_users = add_single_users;
27 | this.remove_single_users = remove_single_users;
28 | this.add_group_ids = add_group_ids;
29 | this.remove_group_ids = remove_group_ids;
30 | }
31 |
32 |
33 | @Override
34 | public JsonElement toJSON() {
35 |
36 | JsonObject json = new JsonObject();
37 |
38 | if (null != appKey) {
39 | json.addProperty(APP_KEY, appKey);
40 | }
41 |
42 | if (null != add_single_users) {
43 | JsonObject jsonObject = new JsonObject();
44 | JsonArray jsonArray = new JsonArray();
45 | for (String username : add_single_users) {
46 | jsonArray.add(new JsonPrimitive(username));
47 | }
48 | jsonObject.add(ADD, jsonArray);
49 | if (null != remove_single_users) {
50 | JsonArray jsonArray1 = new JsonArray();
51 | for (String username : remove_single_users) {
52 | jsonArray1.add(new JsonPrimitive(username));
53 | }
54 | jsonObject.add(REMOVE, jsonArray1);
55 | }
56 | json.add(SINGLE, jsonObject);
57 | } else if (null != remove_single_users) {
58 | JsonObject jsonObject = new JsonObject();
59 | JsonArray jsonArray = new JsonArray();
60 | for (String username : remove_single_users) {
61 | jsonArray.add(new JsonPrimitive(username));
62 | }
63 | jsonObject.add(REMOVE, jsonArray);
64 | json.add(SINGLE, jsonObject);
65 | }
66 |
67 | if (null != add_group_ids) {
68 | JsonObject jsonObject = new JsonObject();
69 | JsonArray jsonArray = new JsonArray();
70 | for (Long groupId : add_group_ids) {
71 | jsonArray.add(new JsonPrimitive(groupId));
72 | }
73 | jsonObject.add(ADD, jsonArray);
74 |
75 | if (null != remove_group_ids) {
76 | JsonArray jsonArray1 = new JsonArray();
77 | for (Long groupId : remove_group_ids) {
78 | jsonArray1.add(new JsonPrimitive(groupId));
79 | }
80 | jsonObject.add(REMOVE, jsonArray1);
81 | }
82 | json.add(GROUP, jsonObject);
83 | } else if (null != remove_group_ids) {
84 | JsonObject jsonObject = new JsonObject();
85 | JsonArray jsonArray = new JsonArray();
86 | for (Long groupId : remove_group_ids) {
87 | jsonArray.add(new JsonPrimitive(groupId));
88 | }
89 | jsonObject.add(REMOVE, jsonArray);
90 | json.add(GROUP, jsonObject);
91 | }
92 |
93 | return json;
94 | }
95 |
96 | @Override
97 | public String toString() {
98 | return gson.toJson(toJSON());
99 | }
100 |
101 | public static class Builder {
102 | private String appKey;
103 | private String[] add_single_users;
104 | private String[] remove_single_users;
105 | private Long[] add_group_ids;
106 | private Long[] remove_group_ids;
107 | private int global = 0;
108 |
109 | public Builder setAppKey(String appKey) {
110 | this.appKey = appKey;
111 | return this;
112 | }
113 |
114 | public Builder setAddSingleUsers(String...add_single_users) {
115 | this.add_single_users = add_single_users;
116 | return this;
117 | }
118 |
119 | public Builder setRemoveSingleUsers(String...remove_single_users) {
120 | this.remove_single_users = remove_single_users;
121 | return this;
122 | }
123 |
124 | public Builder setAddGroupIds(Long...add_group_ids) {
125 | this.add_group_ids = add_group_ids;
126 | return this;
127 | }
128 |
129 | public Builder setRemoveGroupIds(Long...remove_group_ids) {
130 | this.remove_group_ids = remove_group_ids;
131 | return this;
132 | }
133 |
134 | public Builder setGlobal(int global) {
135 | this.global = global;
136 | return this;
137 | }
138 |
139 | public CrossNoDisturb build() {
140 | Preconditions.checkArgument(null != appKey, "appkey must not be null");
141 | return new CrossNoDisturb(appKey, add_single_users, remove_single_users, add_group_ids, remove_group_ids);
142 | }
143 | }
144 | }
145 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/common/model/cross/CrossNoDisturbPayload.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.common.model.cross;
2 |
3 | import cn.jiguang.common.utils.Preconditions;
4 | import cn.jmessage.api.common.model.IModel;
5 | import com.google.gson.Gson;
6 | import com.google.gson.JsonArray;
7 | import com.google.gson.JsonElement;
8 |
9 | public class CrossNoDisturbPayload implements IModel {
10 |
11 | private static Gson gson = new Gson();
12 |
13 | private JsonArray array ;
14 |
15 | private CrossNoDisturbPayload(JsonArray array) {
16 | this.array = array;
17 | }
18 |
19 | public static Builder newBuilder() {
20 | return new CrossNoDisturbPayload.Builder();
21 | }
22 |
23 | @Override
24 | public JsonElement toJSON() {
25 | return array;
26 | }
27 |
28 | public static class Builder {
29 |
30 | private JsonArray array = new JsonArray();
31 |
32 | public Builder setCrossNoDistrub(CrossNoDisturb... lists) {
33 |
34 | if( null == lists ) {
35 | return this;
36 | }
37 |
38 | for ( CrossNoDisturb entity : lists) {
39 |
40 | array.add(entity.toJSON());
41 | }
42 |
43 | return this;
44 | }
45 |
46 | public CrossNoDisturbPayload build() {
47 |
48 | Preconditions.checkArgument(0 != array.size(), "The array must not be empty.");
49 | Preconditions.checkArgument(array.size() <= 500, "The array size must not over 500");
50 |
51 | return new CrossNoDisturbPayload(array);
52 | }
53 | }
54 |
55 | @Override
56 | public String toString() {
57 | return gson.toJson(toJSON());
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/common/model/friend/FriendNote.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.common.model.friend;
2 |
3 | import cn.jiguang.common.utils.Preconditions;
4 | import cn.jmessage.api.common.model.IModel;
5 | import cn.jmessage.api.utils.StringUtils;
6 | import com.google.gson.Gson;
7 | import com.google.gson.JsonElement;
8 | import com.google.gson.JsonObject;
9 |
10 | import java.io.UnsupportedEncodingException;
11 |
12 | public class FriendNote implements IModel {
13 |
14 | private static final String NOTE_NAME = "note_name";
15 | private static final String OTHERS = "others";
16 | private static final String USERNAME = "username";
17 |
18 | private String note_name;
19 | private String others;
20 | private String username;
21 | private Gson gson = new Gson();
22 |
23 | private FriendNote(String note_name, String others, String username) {
24 | this.note_name = note_name;
25 | this.others = others;
26 | this.username = username;
27 | }
28 |
29 | public static Builder newBuilder() {
30 | return new Builder();
31 | }
32 |
33 | public static class Builder {
34 | private String note_name;
35 | private String others;
36 | private String username;
37 |
38 | public Builder setNoteName(String noteName) {
39 | this.note_name = noteName;
40 | return this;
41 | }
42 |
43 | public Builder setOthers(String others) {
44 | this.others = others;
45 | return this;
46 | }
47 |
48 | public Builder setUsername(String username) {
49 | this.username = username;
50 | return this;
51 | }
52 |
53 | public FriendNote builder() {
54 | StringUtils.checkUsername(username);
55 | try {
56 | Preconditions.checkArgument(note_name.trim().getBytes("UTF-8").length <= 250, "length of note name should less than 250");
57 | Preconditions.checkArgument(others.getBytes("UTF-8").length <= 250, "length of others should not larger than 250");
58 | } catch (UnsupportedEncodingException e) {
59 | e.printStackTrace();
60 | }
61 |
62 | return new FriendNote(note_name, others, username);
63 | }
64 | }
65 | @Override
66 | public JsonElement toJSON() {
67 | JsonObject json = new JsonObject();
68 | if (null != note_name) {
69 | json.addProperty(NOTE_NAME, note_name);
70 | }
71 |
72 | if (null != others) {
73 | json.addProperty(OTHERS, others);
74 | }
75 |
76 | if (null != username) {
77 | json.addProperty(USERNAME, username);
78 | }
79 | return json;
80 | }
81 |
82 | @Override
83 | public String toString() {
84 | return gson.toJson(toJSON());
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/common/model/friend/FriendNotePayload.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.common.model.friend;
2 |
3 | import cn.jiguang.common.utils.Preconditions;
4 | import cn.jmessage.api.common.model.IModel;
5 | import com.google.gson.Gson;
6 | import com.google.gson.JsonArray;
7 | import com.google.gson.JsonElement;
8 |
9 | public class FriendNotePayload implements IModel {
10 |
11 | private static Gson gson = new Gson();
12 |
13 | private JsonArray array ;
14 |
15 | private FriendNotePayload(JsonArray array) {
16 | this.array = array;
17 | }
18 |
19 | public static Builder newBuilder() {
20 | return new Builder();
21 | }
22 |
23 | @Override
24 | public JsonElement toJSON() {
25 | return array;
26 | }
27 |
28 | public static class Builder {
29 |
30 | private JsonArray array = new JsonArray();
31 |
32 | public Builder setFriendNotes(FriendNote... users) {
33 |
34 | if( null == users ) {
35 | return this;
36 | }
37 |
38 | for ( FriendNote user : users) {
39 |
40 | array.add(user.toJSON());
41 | }
42 |
43 | return this;
44 | }
45 |
46 | public FriendNotePayload build() {
47 |
48 | Preconditions.checkArgument(0 != array.size(), "The array must not be empty.");
49 | Preconditions.checkArgument(array.size() <= 500, "The array size must not over 500");
50 |
51 | return new FriendNotePayload(array);
52 | }
53 | }
54 |
55 | @Override
56 | public String toString() {
57 | return gson.toJson(toJSON());
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/common/model/group/GroupPayload.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.common.model.group;
2 |
3 |
4 | import cn.jmessage.api.common.model.IModel;
5 | import cn.jmessage.api.common.model.Members;
6 | import com.google.gson.Gson;
7 | import com.google.gson.JsonElement;
8 | import com.google.gson.JsonObject;
9 |
10 | import cn.jiguang.common.utils.Preconditions;
11 | import cn.jiguang.common.utils.StringUtils;
12 |
13 |
14 | public class GroupPayload implements IModel {
15 |
16 | public static final String OWNER = "owner_username";
17 | public static final String GROUP_NAME = "name";
18 | public static final String MEMBERS = "members_username";
19 | public static final String DESC = "desc";
20 | public static final String AVATAR = "avatar";
21 | public static final String FLAG = "flag";
22 |
23 | private static Gson gson = new Gson();
24 |
25 | private String owner;
26 | private String name;
27 | private Members members;
28 | private String desc;
29 | // 上传接口后的 media_id
30 | private String avatar;
31 | // 类型,1 为私有群,2 为公开群,默认为 1
32 | private int flag = 1;
33 |
34 | private GroupPayload(String owner, String name, Members members, String desc, String avatar, int flag) {
35 | this.owner = owner;
36 | this.name = name;
37 | this.members = members;
38 | this.desc = desc;
39 | this.avatar = avatar;
40 | this.flag = flag;
41 | }
42 |
43 | public static Builder newBuilder() {
44 | return new Builder();
45 | }
46 |
47 | @Override
48 | public JsonElement toJSON() {
49 |
50 | JsonObject json = new JsonObject();
51 |
52 | if ( null != owner ) {
53 | json.addProperty(OWNER, owner);
54 | }
55 |
56 | if ( null != name ) {
57 | json.addProperty(GROUP_NAME, name);
58 | }
59 |
60 | if ( null != members ) {
61 | json.add(MEMBERS, members.toJSON());
62 | }
63 |
64 | if ( null != desc ) {
65 | json.addProperty(DESC, desc);
66 | }
67 |
68 | if (null != avatar) {
69 | json.addProperty(AVATAR, avatar);
70 | }
71 |
72 | if (flag != 1) {
73 | json.addProperty(FLAG, flag);
74 | }
75 |
76 | return json;
77 |
78 | }
79 |
80 | @Override
81 | public String toString() {
82 | return gson.toJson(toJSON());
83 | }
84 |
85 | public static class Builder{
86 |
87 | private String owner;
88 | private String name;
89 | private Members members;
90 | private String desc;
91 | private String avatar;
92 | private int flag;
93 |
94 | public Builder setOwner(String owner) {
95 | this.owner = owner.trim();
96 | return this;
97 | }
98 |
99 | public Builder setName(String name) {
100 | this.name = name.trim();
101 | return this;
102 | }
103 |
104 | public Builder setMembers(Members members) {
105 | this.members = members;
106 | return this;
107 | }
108 |
109 | public Builder setDesc(String desc) {
110 | this.desc = desc.trim();
111 | return this;
112 | }
113 |
114 | public Builder setAvatar(String mediaId) {
115 | this.avatar = mediaId;
116 | return this;
117 | }
118 |
119 | public Builder setFlag(int flag) {
120 | Preconditions.checkArgument(flag == 1 || flag == 2, "Flag must be 1 or 2");
121 | this.flag = flag;
122 | return this;
123 | }
124 |
125 | public GroupPayload build() {
126 |
127 | Preconditions.checkArgument(StringUtils.isNotEmpty(owner), "The owner must not be empty.");
128 | Preconditions.checkArgument(StringUtils.isNotEmpty(name), "The group name must not be empty.");
129 | Preconditions.checkArgument(!StringUtils.isLineBroken(owner),
130 | "The owner name must not contain line feed character.");
131 | Preconditions.checkArgument(name.getBytes().length <= 64,
132 | "The length of group name must not more than 64 bytes.");
133 | Preconditions.checkArgument( !StringUtils.isLineBroken(name),
134 | "The group name must not contain line feed character.");
135 |
136 | if ( null != desc ) {
137 | Preconditions.checkArgument( desc.getBytes().length <= 250,
138 | "The length of group description must not more than 250 bytes.");
139 | }
140 |
141 | return new GroupPayload(owner, name, members, desc, avatar, flag);
142 | }
143 |
144 | }
145 | }
146 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/common/model/group/GroupShieldPayload.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.common.model.group;
2 |
3 | import cn.jiguang.common.utils.Preconditions;
4 | import cn.jmessage.api.common.model.IModel;
5 | import com.google.gson.*;
6 |
7 | import java.util.ArrayList;
8 | import java.util.List;
9 |
10 | public class GroupShieldPayload implements IModel {
11 |
12 | private static final String ADD = "add";
13 | private static final String REMOVE = "remove";
14 |
15 | private static final Gson gson = new Gson();
16 |
17 | private List addList;
18 | private List removeList;
19 |
20 | public GroupShieldPayload(List addList, List removeList) {
21 | this.addList = addList;
22 | this.removeList = removeList;
23 | }
24 |
25 | public static Builder newBuilder() {
26 | return new Builder();
27 | }
28 |
29 | public static class Builder {
30 | private List addList = new ArrayList();
31 | private List removeList = new ArrayList();
32 |
33 | public Builder addGroupShield(Long...gid) {
34 | for (long id : gid) {
35 | addList.add(id);
36 | }
37 | return this;
38 | }
39 |
40 | public Builder setAddGroupShield(List list) {
41 | Preconditions.checkArgument(null != list, "group id list is null");
42 | addList = list;
43 | return this;
44 | }
45 |
46 | public Builder removeGroupShield(long...ids) {
47 | for (long id : ids) {
48 | removeList.add(id);
49 | }
50 | return this;
51 | }
52 |
53 | public Builder setRemoveGroupShield(List list) {
54 | Preconditions.checkArgument(null != list, "group id list is null");
55 | removeList = list;
56 | return this;
57 | }
58 |
59 | public GroupShieldPayload build() {
60 | return new GroupShieldPayload(addList, removeList);
61 | }
62 | }
63 |
64 | @Override
65 | public JsonElement toJSON() {
66 | JsonObject jsonObject = new JsonObject();
67 | if (null != addList && addList.size() > 0) {
68 | JsonArray jsonArray = new JsonArray();
69 | for (long id : addList) {
70 | jsonArray.add(new JsonPrimitive(id));
71 | }
72 | jsonObject.add(ADD, jsonArray);
73 | }
74 |
75 | if (null != removeList && removeList.size() > 0) {
76 | JsonArray jsonArray = new JsonArray();
77 | for (long id : removeList) {
78 | jsonArray.add(new JsonPrimitive(id));
79 | }
80 | jsonObject.add(REMOVE, jsonArray);
81 | }
82 | return jsonObject;
83 | }
84 |
85 | @Override
86 | public String toString() {
87 | return gson.toJson(toJSON());
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/common/model/message/MessagePayload.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.common.model.message;
2 |
3 | import cn.jmessage.api.common.model.IModel;
4 | import cn.jmessage.api.message.MessageType;
5 | import com.google.gson.Gson;
6 | import com.google.gson.JsonElement;
7 | import com.google.gson.JsonObject;
8 |
9 | import cn.jiguang.common.utils.Preconditions;
10 | import cn.jmessage.api.utils.StringUtils;
11 |
12 | /**
13 | * MessagePayload https://docs.jiguang.cn/jmessage/server/rest_api_im/#_17
14 | */
15 | public class MessagePayload implements IModel {
16 |
17 | private static final String VERSION = "version";
18 | private static final String TARGET_TYPE = "target_type";
19 | private static final String FROM_TYPE = "from_type";
20 | private static final String MSG_TYPE = "msg_type";
21 | private static final String TARGET_ID = "target_id";
22 | private static final String FROM_ID = "from_id";
23 | private static final String TARGET_APP_KEY = "target_appkey";
24 | private static final String FROM_NAME = "from_name";
25 | private static final String TARGET_NAME = "target_name";
26 | private static final String MSG_BODY = "msg_body";
27 | private static final String NO_OFFLINE = "no_offline";
28 | private static final String NO_NOTIFICATION = "no_notification";
29 | private static final String NOTIFICATION = "notification";
30 |
31 |
32 | private static Gson gson = new Gson();
33 |
34 | private Integer mVersion;
35 | private String mTargetType;
36 | private String mTargetId;
37 | private String mFromType;
38 | private String mFromId;
39 | private String mTargetAppKey;
40 | private String mFromName;
41 | private String mTargetName;
42 | // 默认为false,表示需要离线存储
43 | private boolean mNoOffline = false;
44 | // 默认为false,表示在通知栏展示
45 | private boolean mNoNotification = false;
46 | private MessageType mMsgType;
47 | private MessageBody mMsgBody;
48 | private Notification mNotification;
49 |
50 | public MessagePayload(Integer version, String targetType, String targetId, String fromType, String fromId,
51 | String targetAppKey, String fromName, String targetName, boolean noOffline,
52 | boolean noNotification, MessageType msgType, MessageBody msgBody, Notification notification) {
53 | this.mVersion = version;
54 | this.mTargetType = targetType;
55 | this.mTargetId = targetId;
56 | this.mFromType = fromType;
57 | this.mFromId = fromId;
58 | this.mTargetAppKey = targetAppKey;
59 | this.mFromName = fromName;
60 | this.mTargetName = targetName;
61 | this.mNoOffline = noOffline;
62 | this.mNoNotification = noNotification;
63 | this.mMsgType = msgType;
64 | this.mMsgBody = msgBody;
65 | this.mNotification = notification;
66 | }
67 |
68 | public static Builder newBuilder(){
69 | return new Builder();
70 | }
71 |
72 | @Override
73 | public JsonElement toJSON() {
74 | JsonObject json = new JsonObject();
75 |
76 | if (null != mVersion) {
77 | json.addProperty(VERSION, mVersion);
78 | }
79 | if (null != mTargetType) {
80 | json.addProperty(TARGET_TYPE, mTargetType);
81 | }
82 | if (null != mTargetId) {
83 | json.addProperty(TARGET_ID, mTargetId);
84 | }
85 | if (null != mFromType) {
86 | json.addProperty(FROM_TYPE, mFromType);
87 | }
88 | if (null != mFromId) {
89 | json.addProperty(FROM_ID, mFromId);
90 | }
91 | if (null != mTargetAppKey) {
92 | json.addProperty(TARGET_APP_KEY, mTargetAppKey);
93 | }
94 | if (null != mFromName) {
95 | json.addProperty(FROM_NAME, mFromName);
96 | }
97 | if (null != mTargetName) {
98 | json.addProperty(TARGET_NAME, mTargetName);
99 | }
100 | if (mNoOffline) {
101 | json.addProperty(NO_OFFLINE, mNoOffline);
102 | }
103 | if (mNoNotification) {
104 | json.addProperty(NO_NOTIFICATION, mNoNotification);
105 | }
106 | if (null != mMsgType) {
107 | json.addProperty(MSG_TYPE, mMsgType.getValue());
108 | }
109 | if (null != mMsgBody) {
110 | json.add(MSG_BODY, mMsgBody.toJSON());
111 | }
112 |
113 | if (null != mNotification) {
114 | json.add(NOTIFICATION, mNotification.toJSON());
115 | }
116 |
117 | return json;
118 | }
119 |
120 | @Override
121 | public String toString() {
122 | return gson.toJson(toJSON());
123 | }
124 |
125 | public static class Builder {
126 | private Integer mVersion;
127 | private String mTargetType;
128 | private String mTargetId;
129 | private String mFromType;
130 | private String mFromId;
131 | private String mTargetAppKey;
132 | private String mFromName;
133 | private String mTargetName;
134 | // 默认为false,表示需要离线存储
135 | private boolean mNoOffline = false;
136 | // 默认为false,表示在通知栏展示
137 | private boolean mNoNotification = false;
138 | private MessageType mMsgType;
139 | private MessageBody mMsgBody;
140 | private Notification mNotification;
141 |
142 | public Builder setVersion(Integer version) {
143 | this.mVersion = version;
144 | return this;
145 | }
146 |
147 | public Builder setTargetType(String targetType) {
148 | this.mTargetType = targetType.trim();
149 | return this;
150 | }
151 |
152 | public Builder setTargetId(String targetId) {
153 | this.mTargetId = targetId.trim();
154 | return this;
155 | }
156 |
157 | public Builder setFromType(String fromType) {
158 | this.mFromType = fromType.trim();
159 | return this;
160 | }
161 |
162 | public Builder setFromId(String fromId) {
163 | this.mFromId = fromId.trim();
164 | return this;
165 | }
166 |
167 | public Builder setTargetAppKey(String appKey) {
168 | this.mTargetAppKey = appKey;
169 | return this;
170 | }
171 |
172 | public Builder setFromName(String name) {
173 | this.mFromName = name;
174 | return this;
175 | }
176 |
177 | public Builder setTargetName(String name) {
178 | this.mTargetName = name;
179 | return this;
180 | }
181 |
182 | public Builder setNoOffline(boolean noOffline) {
183 | this.mNoOffline = noOffline;
184 | return this;
185 | }
186 |
187 | public Builder setNoNotification(boolean noNotification) {
188 | this.mNoNotification = noNotification;
189 | return this;
190 | }
191 |
192 | public Builder setMessageType(MessageType msgType) {
193 | this.mMsgType = msgType;
194 | return this;
195 | }
196 |
197 | public Builder setMessageBody(MessageBody msgBody) {
198 | this.mMsgBody = msgBody;
199 | return this;
200 | }
201 |
202 | public Builder setNotification(Notification notification) {
203 | this.mNotification = notification;
204 | return this;
205 | }
206 |
207 | public MessagePayload build() {
208 | Preconditions.checkArgument(null != mVersion, "The version must not be empty!");
209 | Preconditions.checkArgument(StringUtils.isNotEmpty(mTargetType), "The target type must not be empty!");
210 | StringUtils.checkUsername(mTargetId);
211 | Preconditions.checkArgument(StringUtils.isNotEmpty(mFromType), "The from type must not be empty!");
212 | StringUtils.checkUsername(mFromId);
213 | Preconditions.checkArgument(mMsgType != null, "The message type must not be empty!");
214 | Preconditions.checkArgument(null != mMsgBody, "The message body must not be empty!");
215 |
216 | return new MessagePayload(mVersion, mTargetType, mTargetId, mFromType, mFromId, mTargetAppKey, mFromName,
217 | mTargetName, mNoOffline, mNoNotification, mMsgType, mMsgBody, mNotification);
218 | }
219 | }
220 | }
221 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/common/model/message/Notification.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.common.model.message;
2 |
3 | import cn.jmessage.api.common.model.IModel;
4 | import com.google.gson.Gson;
5 | import com.google.gson.JsonElement;
6 | import com.google.gson.JsonObject;
7 |
8 | public class Notification implements IModel {
9 |
10 | private static final String TITLE = "title";
11 | private static final String ALTRT = "alert";
12 |
13 | private static Gson gson = new Gson();
14 |
15 | private String title;
16 | private String alert;
17 |
18 | public Notification(String title, String alert) {
19 | this.title = title;
20 | this.alert = alert;
21 | }
22 |
23 | public static Builder newBuilder() {
24 | return new Builder();
25 | }
26 |
27 |
28 | @Override
29 | public JsonElement toJSON() {
30 | JsonObject jsonObject = new JsonObject();
31 | if (title != null) {
32 | jsonObject.addProperty(TITLE, title);
33 | }
34 |
35 | if (alert != null) {
36 | jsonObject.addProperty(ALTRT, alert);
37 | }
38 | return jsonObject;
39 | }
40 |
41 | @Override
42 | public String toString() {
43 | return gson.toJson(toJSON());
44 | }
45 |
46 | public static class Builder {
47 | private String title;
48 | private String alert;
49 |
50 | public Builder setTitle(String title) {
51 | this.title = title;
52 | return this;
53 | }
54 |
55 | public Builder setAlert(String alert) {
56 | this.alert = alert;
57 | return this;
58 | }
59 |
60 | public Notification build() {
61 | return new Notification(title, alert);
62 | }
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/crossapp/CrossAppClient.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.crossapp;
2 |
3 | import cn.jiguang.common.connection.*;
4 | import cn.jiguang.common.utils.Preconditions;
5 | import cn.jiguang.common.resp.APIConnectionException;
6 | import cn.jiguang.common.resp.APIRequestException;
7 | import cn.jiguang.common.resp.ResponseWrapper;
8 | import cn.jmessage.api.common.BaseClient;
9 | import cn.jmessage.api.common.JMessageConfig;
10 | import cn.jmessage.api.common.model.cross.*;
11 | import cn.jmessage.api.group.MemberListResult;
12 | import cn.jmessage.api.user.UserInfoResult;
13 | import cn.jmessage.api.utils.StringUtils;
14 |
15 |
16 | public class CrossAppClient extends BaseClient {
17 |
18 | private String crossUserPath;
19 | private String crossGroupPath;
20 |
21 | public CrossAppClient(String appkey, String masterSecret) {
22 | this(appkey, masterSecret, null, JMessageConfig.getInstance());
23 | }
24 |
25 | public CrossAppClient(String appKey, String masterSecret, HttpProxy proxy) {
26 | this(appKey, masterSecret, proxy, JMessageConfig.getInstance());
27 | }
28 |
29 | /**
30 | * Create a JMessage Base Client
31 | *
32 | * @param appKey The KEY of one application on JPush.
33 | * @param masterSecret API access secret of the appKey.
34 | * @param proxy The proxy, if there is no proxy, should be null.
35 | * @param config The client configuration. Can use JMessageConfig.getInstance() as default.
36 | */
37 | public CrossAppClient(String appKey, String masterSecret, HttpProxy proxy, JMessageConfig config) {
38 | super(appKey, masterSecret, proxy, config);
39 | this.crossUserPath = (String) config.get(JMessageConfig.CROSS_USER_PATH);
40 | this.crossGroupPath = (String) config.get(JMessageConfig.CROSS_GROUP_PATH);
41 | }
42 |
43 | /**
44 | * Add or remove group members from a given group id.
45 | * @param gid Necessary, target group id.
46 | * @param groups Necessary
47 | * @return No content
48 | * @throws APIConnectionException connect exception
49 | * @throws APIRequestException request exception
50 | */
51 | public ResponseWrapper addOrRemoveCrossGroupMembers(long gid, CrossGroup[] groups)
52 | throws APIConnectionException, APIRequestException {
53 | CrossGroupPayload payload = new CrossGroupPayload.Builder()
54 | .setCrossGroups(groups)
55 | .build();
56 | Preconditions.checkArgument(0 != gid, "gid should not be empty");
57 | Preconditions.checkArgument(null != payload, "CrossGroup must not be null");
58 | return _httpClient.sendPost(_baseUrl + crossGroupPath + "/" + gid + "/members", payload.toString());
59 | }
60 |
61 | /**
62 | * Get members' info from cross group
63 | * @param gid Necessary, target group id
64 | * @return Member info array
65 | * @throws APIConnectionException connect exception
66 | * @throws APIRequestException request exception
67 | */
68 | public MemberListResult getCrossGroupMembers(long gid)
69 | throws APIConnectionException, APIRequestException {
70 | Preconditions.checkArgument(0 != gid, "gid must not be empty");
71 | ResponseWrapper response = _httpClient.sendGet(_baseUrl + crossGroupPath + "/" + gid + "/members/");
72 | return MemberListResult.fromResponse(response);
73 | }
74 |
75 | /**
76 | * Add blacklist whose users belong to another app to a given user.
77 | * @param username The owner of the blacklist
78 | * @param blacklists CrossBlacklist array
79 | * @return No Content
80 | * @throws APIConnectionException connect exception
81 | * @throws APIRequestException request exception
82 | */
83 | public ResponseWrapper addCrossBlacklist(String username, CrossBlacklist[] blacklists)
84 | throws APIConnectionException, APIRequestException {
85 | StringUtils.checkUsername(username);
86 | CrossBlacklistPayload payload = new CrossBlacklistPayload.Builder()
87 | .setCrossBlacklists(blacklists)
88 | .build();
89 | return _httpClient.sendPut(_baseUrl + crossUserPath + "/" + username +"/blacklist", payload.toString());
90 | }
91 |
92 | /**
93 | * Delete blacklist whose users belong to another app from a given user.
94 | * @param username The owner of the blacklist
95 | * @param blacklists CrossBlacklist array
96 | * @return No content
97 | * @throws APIConnectionException connect exception
98 | * @throws APIRequestException request exception
99 | */
100 | public ResponseWrapper deleteCrossBlacklist(String username, CrossBlacklist[] blacklists)
101 | throws APIConnectionException, APIRequestException {
102 | StringUtils.checkUsername(username);
103 | CrossBlacklistPayload payload = new CrossBlacklistPayload.Builder()
104 | .setCrossBlacklists(blacklists)
105 | .build();
106 | return _httpClient.sendDelete(_baseUrl + crossUserPath + "/" + username + "/blacklist", payload.toString());
107 | }
108 |
109 | /**
110 | * Get all user's info(contains appkey) of user's cross app blacklist
111 | * @param username Necessary, the owner of blacklist
112 | * @return UserInfo array
113 | * @throws APIConnectionException connect exception
114 | * @throws APIRequestException request exception
115 | */
116 | public UserInfoResult[] getCrossBlacklist(String username)
117 | throws APIConnectionException, APIRequestException {
118 | StringUtils.checkUsername(username);
119 | ResponseWrapper response = _httpClient.sendGet(_baseUrl + crossUserPath + "/" + username + "/blacklist");
120 | return _gson.fromJson(response.responseContent, UserInfoResult[].class);
121 | }
122 |
123 | /**
124 | * Set cross app no disturb
125 | * https://docs.jiguang.cn/jmessage/server/rest_api_im/#api_1
126 | * @param username Necessary
127 | * @param array CrossNoDisturb array
128 | * @return No content
129 | * @throws APIConnectionException connect exception
130 | * @throws APIRequestException request exception
131 | */
132 | public ResponseWrapper setCrossNoDisturb(String username, CrossNoDisturb[] array)
133 | throws APIConnectionException, APIRequestException {
134 | StringUtils.checkUsername(username);
135 | CrossNoDisturbPayload payload = new CrossNoDisturbPayload.Builder()
136 | .setCrossNoDistrub(array)
137 | .build();
138 | return _httpClient.sendPost(_baseUrl + crossUserPath + "/" + username + "/nodisturb", payload.toString());
139 | }
140 |
141 | /**
142 | * Add users from cross app.
143 | * @param username Necessary
144 | * @param payload CrossFriendPayload
145 | * @return No content
146 | * @throws APIConnectionException connect exception
147 | * @throws APIRequestException request exception
148 | */
149 | public ResponseWrapper addCrossFriends(String username, CrossFriendPayload payload)
150 | throws APIConnectionException, APIRequestException {
151 | StringUtils.checkUsername(username);
152 | Preconditions.checkArgument(null != payload, "CrossFriendPayload should not be null");
153 | return _httpClient.sendPost(_baseUrl + crossUserPath + "/" + username + "/friends", payload.toString());
154 | }
155 |
156 | /**
157 | * Delete cross app friends
158 | * @param username Necessary
159 | * @param payload CrossFriendPayload
160 | * @return No content
161 | * @throws APIConnectionException connect exception
162 | * @throws APIRequestException request exception
163 | */
164 | public ResponseWrapper deleteCrossFriends(String username, CrossFriendPayload payload)
165 | throws APIConnectionException, APIRequestException {
166 | StringUtils.checkUsername(username);
167 | Preconditions.checkArgument(null != payload, "CrossFriendPayload should not be null");
168 | return _httpClient.sendDelete(_baseUrl + crossUserPath + "/" + username + "/friends", payload.toString());
169 | }
170 |
171 | }
172 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/crossapp/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Cross App API features.
3 | *
4 | */
5 | package cn.jmessage.api.crossapp;
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/group/CreateGroupResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.group;
2 |
3 | import com.google.gson.JsonArray;
4 | import com.google.gson.annotations.Expose;
5 |
6 | import cn.jiguang.common.resp.BaseResult;
7 |
8 | public class CreateGroupResult extends BaseResult {
9 |
10 | @Expose Long gid;
11 | @Expose String owner_username;
12 | @Expose String name;
13 | @Expose JsonArray members_username;
14 | @Expose String desc;
15 | @Expose String ctime;
16 | @Expose String mtime;
17 | @Expose String appkey;
18 | @Expose String avatar;
19 | @Expose Integer MaxMemberCount;
20 |
21 | public Long getGid() {
22 | return gid;
23 | }
24 |
25 | public String getOwner_username() {
26 | return owner_username;
27 | }
28 |
29 | public String getName() {
30 | return name;
31 | }
32 |
33 | public JsonArray getMembers_username() {
34 | return members_username;
35 | }
36 |
37 | public String getDesc() {
38 | return desc;
39 | }
40 |
41 | public Integer getMaxMemberCount() {
42 | return MaxMemberCount;
43 | }
44 |
45 | public String getAppkey() {
46 | return appkey;
47 | }
48 |
49 | public String getAvatar() {
50 | return avatar;
51 | }
52 |
53 | public String getCtime() {
54 | return ctime;
55 | }
56 |
57 | public String getMtime() {
58 | return mtime;
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/group/GroupClient.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.group;
2 |
3 | import cn.jiguang.common.connection.HttpProxy;
4 | import cn.jiguang.common.resp.APIConnectionException;
5 | import cn.jiguang.common.resp.APIRequestException;
6 | import cn.jiguang.common.resp.ResponseWrapper;
7 | import cn.jiguang.common.utils.Preconditions;
8 | import cn.jmessage.api.common.BaseClient;
9 | import cn.jmessage.api.common.JMessageConfig;
10 | import cn.jmessage.api.common.model.Members;
11 | import cn.jmessage.api.common.model.group.GroupPayload;
12 | import cn.jmessage.api.utils.StringUtils;
13 | import com.google.gson.Gson;
14 | import com.google.gson.JsonObject;
15 | import org.slf4j.Logger;
16 | import org.slf4j.LoggerFactory;
17 |
18 |
19 | public class GroupClient extends BaseClient {
20 |
21 | private static final Logger LOG = LoggerFactory.getLogger(GroupClient.class);
22 |
23 | private String groupPath;
24 |
25 | /**
26 | * Create a Group Client with default parameters.
27 | *
28 | * @param appkey The key of one application on JPush.
29 | * @param masterSecret API access secret of the appKey.
30 | */
31 | public GroupClient(String appkey, String masterSecret) {
32 | this(appkey, masterSecret, null, JMessageConfig.getInstance());
33 | }
34 |
35 | /**
36 | * Create a Group Client with a proxy.
37 | *
38 | * @param appkey The key of one application on JPush.
39 | * @param masterSecret API access secret of the appKey.
40 | * @param proxy The proxy, if there is no proxy, should be null.
41 | */
42 | public GroupClient(String appkey, String masterSecret, HttpProxy proxy) {
43 | this(appkey, masterSecret, proxy, JMessageConfig.getInstance());
44 | }
45 |
46 | /**
47 | * Create a Group Client by custom config.
48 | * If you are using JMessage privacy cloud or custom parameters, maybe this constructor is what you needed.
49 | *
50 | * @param appkey The key of one application on JPush.
51 | * @param masterSecret API access secret of the appKey.
52 | * @param proxy The proxy, if there is no proxy, should be null.
53 | * @param config The client configuration. Can use JMessageConfig.getInstance() as default.
54 | */
55 | public GroupClient(String appkey, String masterSecret, HttpProxy proxy, JMessageConfig config) {
56 | super(appkey, masterSecret, proxy, config);
57 | this.groupPath = (String) config.get(JMessageConfig.GROUP_PATH);
58 | }
59 |
60 | public GroupInfoResult getGroupInfo(long gid)
61 | throws APIConnectionException, APIRequestException {
62 | Preconditions.checkArgument(gid > 0, "gid should more than 0.");
63 |
64 | ResponseWrapper response = _httpClient.sendGet(_baseUrl + groupPath + "/" + gid);
65 |
66 | return GroupInfoResult.fromResponse(response, GroupInfoResult.class);
67 | }
68 |
69 | public MemberListResult getGroupMembers(long gid)
70 | throws APIConnectionException, APIRequestException {
71 | Preconditions.checkArgument(gid > 0, "gid should more than 0.");
72 |
73 | ResponseWrapper response = _httpClient.sendGet(_baseUrl + groupPath + "/" + gid + "/members");
74 |
75 | return MemberListResult.fromResponse(response);
76 | }
77 |
78 | public GroupListResult getGroupListByAppkey(int start, int count)
79 | throws APIConnectionException, APIRequestException {
80 | if (start < 0 || count <= 0 || count > 500) {
81 | throw new IllegalArgumentException("negative index or count must more than 0 and less than 501");
82 | }
83 | ResponseWrapper response = _httpClient.sendGet(_baseUrl + groupPath + "?start=" + start + "&count=" + count);
84 | return GroupListResult.fromResponse(response, GroupListResult.class);
85 | }
86 |
87 | public CreateGroupResult createGroup(GroupPayload payload)
88 | throws APIConnectionException, APIRequestException {
89 | Preconditions.checkArgument(!(null == payload), "group payload should not be null");
90 |
91 | ResponseWrapper response = _httpClient.sendPost(_baseUrl + groupPath, payload.toString());
92 | return CreateGroupResult.fromResponse(response, CreateGroupResult.class);
93 | }
94 |
95 | public ResponseWrapper addOrRemoveMembers(long gid, Members add, Members remove)
96 | throws APIConnectionException, APIRequestException {
97 | Preconditions.checkArgument(gid > 0, "gid should more than 0.");
98 |
99 | if (null == add && null == remove) {
100 | Preconditions.checkArgument(false, "add list or remove list should not be null at the same time.");
101 | }
102 |
103 | JsonObject json = new JsonObject();
104 |
105 | if (null != add) {
106 | json.add("add", add.toJSON());
107 | }
108 |
109 | if (null != remove) {
110 | json.add("remove", remove.toJSON());
111 | }
112 |
113 | return _httpClient.sendPost(_baseUrl + groupPath + "/" + gid + "/members", json.toString());
114 |
115 | }
116 |
117 | public ResponseWrapper deleteGroup(long gid)
118 | throws APIConnectionException, APIRequestException {
119 | Preconditions.checkArgument(gid > 0, "gid should more than 0.");
120 | return _httpClient.sendDelete(_baseUrl + groupPath + "/" + gid);
121 | }
122 |
123 | public ResponseWrapper updateGroupInfo(long gid, String groupName, String groupDesc, String avatar)
124 | throws APIConnectionException, APIRequestException {
125 | Preconditions.checkArgument(gid > 0, "gid should more than 0.");
126 |
127 | if (StringUtils.isTrimedEmpty(groupName) && StringUtils.isTrimedEmpty(groupDesc)
128 | && StringUtils.isTrimedEmpty(avatar)) {
129 | Preconditions.checkArgument(false, "group name, group description and group avatar should not be null at the same time.");
130 | }
131 |
132 | JsonObject json = new JsonObject();
133 | if (StringUtils.isNotEmpty(groupName)) {
134 | groupName = groupName.trim();
135 | Preconditions.checkArgument(groupName.getBytes().length <= 64,
136 | "The length of group name must not more than 64 bytes.");
137 | Preconditions.checkArgument(!StringUtils.isLineBroken(groupName),
138 | "The group name must not contain line feed character.");
139 | json.addProperty(GroupPayload.GROUP_NAME, groupName);
140 | }
141 |
142 | if (StringUtils.isNotEmpty(groupDesc)) {
143 | groupDesc = groupDesc.trim();
144 | Preconditions.checkArgument(groupDesc.getBytes().length <= 250,
145 | "The length of group description must not more than 250 bytes.");
146 | json.addProperty(GroupPayload.DESC, groupDesc);
147 | }
148 |
149 | if (StringUtils.isNotEmpty(avatar)) {
150 | avatar = avatar.trim();
151 | json.addProperty(GroupPayload.AVATAR, avatar);
152 | }
153 |
154 | return _httpClient.sendPut(_baseUrl + groupPath + "/" + gid, json.toString());
155 | }
156 |
157 | public ResponseWrapper changeGroupAdmin(long gid, String appKey, String username)
158 | throws APIConnectionException, APIRequestException {
159 | Preconditions.checkArgument(gid > 0, "gid should more than 0.");
160 |
161 | if (StringUtils.isTrimedEmpty(appKey) && StringUtils.isTrimedEmpty(username)) {
162 | Preconditions.checkArgument(false, "appKey and username should not be null at the same time.");
163 | }
164 |
165 | JsonObject json = new JsonObject();
166 | if (StringUtils.isNotEmpty(appKey)){
167 | appKey=appKey.trim();
168 | json.addProperty("appKey",appKey);
169 | }
170 |
171 | if (StringUtils.isNotEmpty(username)){
172 | username=username.trim();
173 | json.addProperty("username",username);
174 | }
175 |
176 | return _httpClient.sendPut(_baseUrl + groupPath + "/owner/" + gid,json.toString());
177 | }
178 |
179 | public ResponseWrapper setGroupMemberSilence(long gid, boolean status, String[] usernames)
180 | throws APIConnectionException, APIRequestException {
181 | Preconditions.checkArgument(gid > 0, "gid should more than 0.");
182 | Preconditions.checkArgument(usernames != null && usernames.length > 0, "username array is invalid");
183 | return _httpClient.sendPut(_baseUrl + groupPath + "/messages/" + gid + "/silence?status=" + (status ? "true" : "false"), new Gson().toJson(usernames));
184 | }
185 |
186 | }
187 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/group/GroupInfoResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.group;
2 |
3 | import com.google.gson.annotations.Expose;
4 |
5 | import cn.jiguang.common.resp.BaseResult;
6 |
7 | public class GroupInfoResult extends BaseResult {
8 |
9 | @Expose private Long gid;
10 | @Expose private String name;
11 | @Expose private String desc;
12 | @Expose private String appkey;
13 | @Expose private Integer level;
14 | @Expose private String ctime;
15 | @Expose private String mtime;
16 | @Expose private Integer MaxMemberCount;
17 |
18 | public Long getGid() {
19 | return gid;
20 | }
21 |
22 | public String getName() {
23 | return name;
24 | }
25 |
26 | public String getDesc() {
27 | return desc;
28 | }
29 |
30 | public String getAppkey() {
31 | return appkey;
32 | }
33 |
34 | public Integer getLevel() {
35 | return level;
36 | }
37 |
38 | public String getCtime() {
39 | return ctime;
40 | }
41 |
42 | public String getMtime() {
43 | return mtime;
44 | }
45 |
46 | public Integer getMaxMemberCount() {
47 | return MaxMemberCount;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/group/GroupListResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.group;
2 |
3 | import java.util.List;
4 |
5 | import com.google.gson.annotations.Expose;
6 |
7 | import cn.jiguang.common.resp.BaseResult;
8 |
9 | public class GroupListResult extends BaseResult {
10 |
11 | @Expose Integer total;
12 | @Expose Integer start;
13 | @Expose Integer count;
14 | @Expose List groups;
15 |
16 | public Integer getTotal() {
17 | return total;
18 | }
19 |
20 | public Integer getStart() {
21 | return start;
22 | }
23 |
24 | public Integer getCount() {
25 | return count;
26 | }
27 |
28 | public List getGroups() {
29 | return groups;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/group/MemberListResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.group;
2 |
3 | import cn.jiguang.common.resp.BaseResult;
4 | import cn.jiguang.common.resp.ResponseWrapper;
5 |
6 | public class MemberListResult extends BaseResult {
7 |
8 | private MemberResult[] members;
9 |
10 | public static MemberListResult fromResponse(ResponseWrapper responseWrapper) {
11 | MemberListResult result = new MemberListResult();
12 | if (responseWrapper.isServerResponse()) {
13 | result.members = _gson.fromJson(responseWrapper.responseContent, MemberResult[].class);
14 | } else {
15 | // nothing
16 | }
17 | result.setResponseWrapper(responseWrapper);
18 | return result;
19 | }
20 |
21 | public MemberResult[] getMembers() {
22 | return members;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/group/MemberResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.group;
2 |
3 | import com.google.gson.annotations.Expose;
4 |
5 | import cn.jiguang.common.resp.BaseResult;
6 |
7 | public class MemberResult extends BaseResult {
8 |
9 | @Expose String username;
10 | @Expose String nickname;
11 | @Expose String avatar;
12 | @Expose String birthday;
13 | @Expose Integer gender;
14 | @Expose String signature;
15 | @Expose String region;
16 | @Expose String address;
17 | @Expose Integer flag;
18 | @Expose String appkey;
19 |
20 | public String getUsername() {
21 | return username;
22 | }
23 |
24 | public String getNickname() {
25 | return nickname;
26 | }
27 |
28 | public String getAvatar() {
29 | return avatar;
30 | }
31 |
32 | public String getBirthday() {
33 | return birthday;
34 | }
35 |
36 | public Integer getGender() {
37 | return gender;
38 | }
39 |
40 | public String getSignature() {
41 | return signature;
42 | }
43 |
44 | public String getRegion() {
45 | return region;
46 | }
47 |
48 | public String getAddress() {
49 | return address;
50 | }
51 |
52 | public Integer getFlag() {
53 | return flag;
54 | }
55 |
56 | public String getAppkey() {
57 | return appkey;
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/group/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Group management API features.
3 | *
4 | * Url: https://api.im.jpush.cn/groups
5 | */
6 | package cn.jmessage.api.group;
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/message/MessageBodyResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.message;
2 |
3 | import cn.jiguang.common.resp.BaseResult;
4 | import com.google.gson.annotations.Expose;
5 |
6 | import java.util.HashMap;
7 |
8 | public class MessageBodyResult extends BaseResult {
9 |
10 | @Expose String text;
11 | @Expose HashMap extras;
12 | @Expose String media_id;
13 | @Expose Long media_crc32;
14 | @Expose Integer width;
15 | @Expose Integer height;
16 | @Expose String format;
17 | @Expose Integer fsize;
18 |
19 | public String getText() {
20 | return text;
21 | }
22 |
23 | public HashMap getExtras() {
24 | return extras;
25 | }
26 |
27 | public String getMediaId() {
28 | return media_id;
29 | }
30 |
31 | public Long getMediaCrc32() {
32 | return media_crc32;
33 | }
34 |
35 | public Integer getWidth() {
36 | return width;
37 | }
38 |
39 | public Integer getHeight() {
40 | return height;
41 | }
42 |
43 | public String getFormat() {
44 | return format;
45 | }
46 |
47 | public Integer getFsize() {
48 | return fsize;
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/message/MessageListResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.message;
2 |
3 | import cn.jiguang.common.resp.BaseResult;
4 | import cn.jiguang.common.resp.ResponseWrapper;
5 | import com.google.gson.annotations.Expose;
6 |
7 | import java.util.List;
8 |
9 | public class MessageListResult extends BaseResult {
10 |
11 | @Expose Integer total;
12 | @Expose String cursor;
13 | @Expose Integer count;
14 | @Expose MessageResult[] messages;
15 |
16 | public static MessageListResult fromResponse(ResponseWrapper responseWrapper) {
17 | MessageListResult result = new MessageListResult();
18 | if (responseWrapper.isServerResponse()) {
19 | result.messages = _gson.fromJson(responseWrapper.responseContent, MessageResult[].class);
20 | }
21 | result.setResponseWrapper(responseWrapper);
22 | return result;
23 | }
24 |
25 |
26 | public Integer getTotal() {
27 | return total;
28 | }
29 |
30 | public String getCursor() {
31 | return cursor;
32 | }
33 |
34 | public Integer getCount() {
35 | return count;
36 | }
37 |
38 | public MessageResult[] getMessages() {
39 | return messages;
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/message/MessageResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.message;
2 |
3 | import cn.jiguang.common.resp.BaseResult;
4 | import com.google.gson.annotations.Expose;
5 |
6 | public class MessageResult extends BaseResult {
7 |
8 | @Expose String target_type;
9 | @Expose String msg_type;
10 | @Expose String target_name;
11 | @Expose String target_id;
12 | @Expose String from_id;
13 | @Expose String from_name;
14 | @Expose String from_type;
15 | @Expose String from_platform;
16 | @Expose MessageBodyResult msg_body;
17 | @Expose Long create_time;
18 | @Expose Integer version;
19 | @Expose Long msgid;
20 | @Expose Integer msg_level;
21 | @Expose Long msg_ctime;
22 | @Expose Boolean no_offline;
23 | @Expose Boolean no_notification;
24 |
25 | public String getTargetType() {
26 | return target_type;
27 | }
28 |
29 | public String getMsgType() {
30 | return msg_type;
31 | }
32 |
33 | public String getTargetName() {
34 | return target_name;
35 | }
36 |
37 | public String getTargetId() {
38 | return target_id;
39 | }
40 |
41 | public String getFromId() {
42 | return from_id;
43 | }
44 |
45 | public String getFromName() {
46 | return from_name;
47 | }
48 |
49 | public String getFromType() {
50 | return from_type;
51 | }
52 |
53 | public String getFromPlatform() {
54 | return from_platform;
55 | }
56 |
57 | public MessageBodyResult getMsgBody() {
58 | return msg_body;
59 | }
60 |
61 | public Long getCreateTime() {
62 | return create_time;
63 | }
64 |
65 | public Integer getVersion() {
66 | return version;
67 | }
68 |
69 | public Long getMsgId() {
70 | return msgid;
71 | }
72 |
73 | public Integer getMsgLevel() {
74 | return msg_level;
75 | }
76 |
77 | public Long getMsgCtime() {
78 | return msg_ctime;
79 | }
80 |
81 | public Boolean getNoOffline() {
82 | return no_offline;
83 | }
84 |
85 | public Boolean getNoNotification() {
86 | return no_notification;
87 | }
88 | }
89 |
90 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/message/MessageType.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.message;
2 |
3 | public enum MessageType {
4 | TEXT("text"),
5 | IMAGE("image"),
6 | VOICE("voice"),
7 | CUSTOM("custom");
8 |
9 | private String value;
10 |
11 | private MessageType(String value) {
12 | this.value = value;
13 | }
14 |
15 | public String getValue() {
16 | return value;
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/message/SendMessageResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.message;
2 |
3 | import com.google.gson.annotations.Expose;
4 |
5 | import cn.jiguang.common.resp.BaseResult;
6 |
7 | public class SendMessageResult extends BaseResult{
8 |
9 | @Expose Long msg_id;
10 | @Expose Long msg_ctime;
11 |
12 | public Long getMsg_id() {
13 | return msg_id;
14 | }
15 |
16 | public Long getMsgCtime() {
17 | return msg_ctime;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/message/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Message management API features.
3 | *
4 | * Not yet.
5 | */
6 | package cn.jmessage.api.message;
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * JPush IM (JMessage) API features.
3 | *
4 | * Url: https://api.im.jpush.cn
5 | */
6 | package cn.jmessage.api;
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/reportv2/GroupStatListResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.reportv2;
2 |
3 | import cn.jiguang.common.resp.BaseResult;
4 | import cn.jiguang.common.resp.ResponseWrapper;
5 | import com.google.gson.annotations.Expose;
6 |
7 | public class GroupStatListResult extends BaseResult {
8 |
9 | @Expose private GroupStatResult[] array;
10 |
11 | public static GroupStatListResult fromResponse(ResponseWrapper responseWrapper) {
12 | GroupStatListResult result = new GroupStatListResult();
13 | if (responseWrapper.isServerResponse()) {
14 | result.array = _gson.fromJson(responseWrapper.responseContent, GroupStatResult[].class);
15 | }
16 | result.setResponseWrapper(responseWrapper);
17 | return result;
18 | }
19 |
20 | public GroupStatResult[] getArray() {
21 | return array;
22 | }
23 |
24 | public class GroupStatResult {
25 | @Expose private String date;
26 | @Expose private Integer active_group;
27 | @Expose private Integer total_group;
28 | @Expose private Integer new_group;
29 |
30 | public String getDate() {
31 | return date;
32 | }
33 |
34 | public Integer getActive_group() {
35 | return active_group;
36 | }
37 |
38 | public Integer getTotal_group() {
39 | return total_group;
40 | }
41 |
42 | public Integer getNew_group() {
43 | return new_group;
44 | }
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/reportv2/MessageStatListResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.reportv2;
2 |
3 | import cn.jiguang.common.resp.BaseResult;
4 | import com.google.gson.annotations.Expose;
5 |
6 | import java.util.ArrayList;
7 | import java.util.List;
8 |
9 | public class MessageStatListResult extends BaseResult {
10 |
11 | @Expose private List send_msg_stat = new ArrayList();
12 | @Expose private MsgStat group_msg_stat;
13 | @Expose private MsgStat single_msg_stat;
14 |
15 | public List getSend_msg_stat() {
16 | return send_msg_stat;
17 | }
18 |
19 | public MsgStat getGroup_msg_stat() {
20 | return group_msg_stat;
21 | }
22 |
23 | public MsgStat getSingle_msg_stat() {
24 | return single_msg_stat;
25 | }
26 |
27 | public class SendMsgStat {
28 | @Expose private String time;
29 | @Expose private Long group_send_msg;
30 | @Expose private Long single_send_msg;
31 |
32 | public String getTime() {
33 | return time;
34 | }
35 |
36 | public Long getGroup_send_msg() {
37 | return group_send_msg;
38 | }
39 |
40 | public Long getSingle_send_msg() {
41 | return single_send_msg;
42 | }
43 | }
44 |
45 | public class MsgStat {
46 | @Expose private Long txt_msg;
47 | @Expose private Long image_msg;
48 | @Expose private Long voice_msg;
49 | @Expose private Long other_msg;
50 |
51 | public Long getTxt_msg() {
52 | return txt_msg;
53 | }
54 |
55 | public Long getImage_msg() {
56 | return image_msg;
57 | }
58 |
59 | public Long getVoice_msg() {
60 | return voice_msg;
61 | }
62 |
63 | public Long getOther_msg() {
64 | return other_msg;
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/reportv2/UserStatListResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.reportv2;
2 |
3 | import cn.jiguang.common.resp.BaseResult;
4 | import cn.jiguang.common.resp.ResponseWrapper;
5 |
6 | public class UserStatListResult extends BaseResult {
7 |
8 | private UserStatResult[] array;
9 |
10 | public static UserStatListResult fromResponse(ResponseWrapper responseWrapper) {
11 | UserStatListResult result = new UserStatListResult();
12 | if (responseWrapper.isServerResponse()) {
13 | result.array = _gson.fromJson(responseWrapper.responseContent, UserStatResult[].class);
14 | }
15 | result.setResponseWrapper(responseWrapper);
16 | return result;
17 | }
18 |
19 | public UserStatResult[] getArray() {
20 | return array;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/reportv2/UserStatResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.reportv2;
2 |
3 | import cn.jiguang.common.resp.BaseResult;
4 | import com.google.gson.annotations.Expose;
5 |
6 | public class UserStatResult extends BaseResult {
7 |
8 | @Expose private Long active_users;
9 | @Expose private Long total_users;
10 | @Expose private Long send_msg_users;
11 | @Expose private Long new_users;
12 | @Expose private String date;
13 |
14 | public Long getActive_users() {
15 | return active_users;
16 | }
17 |
18 | public Long getTotal_users() {
19 | return total_users;
20 | }
21 |
22 | public Long getSend_msg_users() {
23 | return send_msg_users;
24 | }
25 |
26 | public Long getNew_users() {
27 | return new_users;
28 | }
29 |
30 | public String getDate() {
31 | return date;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/reportv2/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Rest Report V2 相关接口, 文档链接: https://docs.jiguang.cn/jmessage/server/rest_api_im_report_v2/
3 | */
4 | package cn.jmessage.api.reportv2;
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/resource/DownloadResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.resource;
2 |
3 | import cn.jiguang.common.resp.BaseResult;
4 | import com.google.gson.annotations.Expose;
5 |
6 | public class DownloadResult extends BaseResult {
7 |
8 | @Expose String url;
9 |
10 | public String getUrl() {
11 | return url;
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/resource/ResourceClient.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.resource;
2 |
3 | import cn.jiguang.common.ServiceHelper;
4 | import cn.jiguang.common.utils.Preconditions;
5 | import cn.jiguang.common.connection.HttpProxy;
6 | import cn.jiguang.common.resp.APIConnectionException;
7 | import cn.jiguang.common.resp.APIRequestException;
8 | import cn.jiguang.common.resp.ResponseWrapper;
9 | import cn.jmessage.api.common.BaseClient;
10 | import cn.jmessage.api.common.JMessageConfig;
11 | import org.slf4j.Logger;
12 | import org.slf4j.LoggerFactory;
13 |
14 | import java.io.*;
15 | import java.net.HttpURLConnection;
16 | import java.net.URL;
17 |
18 | public class ResourceClient extends BaseClient {
19 |
20 | private static Logger LOG = LoggerFactory.getLogger(ResourceClient.class);
21 |
22 | private String resourcePath;
23 | private String authCode;
24 |
25 |
26 | public ResourceClient(String appkey, String masterSecret) {
27 | this(appkey, masterSecret, null, JMessageConfig.getInstance());
28 | }
29 |
30 | public ResourceClient(String appkey, String masterSecret, HttpProxy proxy) {
31 | this(appkey, masterSecret, proxy, JMessageConfig.getInstance());
32 | }
33 |
34 | /**
35 | * Create a JMessage Base Client
36 | *
37 | * @param appkey The KEY of one application on JPush.
38 | * @param masterSecret API access secret of the appKey.
39 | * @param proxy The proxy, if there is no proxy, should be null.
40 | * @param config The client configuration. Can use JMessageConfig.getInstance() as default.
41 | */
42 | public ResourceClient(String appkey, String masterSecret, HttpProxy proxy, JMessageConfig config) {
43 | super(appkey, masterSecret, proxy, config);
44 | this.resourcePath = (String) config.get(JMessageConfig.RESOURCE_PATH);
45 | this.authCode = ServiceHelper.getBasicAuthorization(appkey, masterSecret);
46 | }
47 |
48 | /**
49 | * Download file with mediaId, will return DownloadResult which include url.
50 | * @param mediaId Necessary
51 | * @return DownloadResult
52 | * @throws APIConnectionException connect exception
53 | * @throws APIRequestException request exception
54 | */
55 | public DownloadResult downloadFile(String mediaId)
56 | throws APIConnectionException, APIRequestException {
57 | Preconditions.checkArgument(null != mediaId, "mediaId is necessary");
58 |
59 | ResponseWrapper response = _httpClient.sendGet(_baseUrl + resourcePath + "?mediaId=" + mediaId);
60 | return DownloadResult.fromResponse(response, DownloadResult.class);
61 | }
62 |
63 | /**
64 | * Upload file, only support image file(jpg, bmp, gif, png) currently,
65 | * file size should not larger than 8M.
66 | * @param path Necessary, the native path of the file you want to upload
67 | * @param fileType should be "image" or "file" or "voice"
68 | * @return UploadResult
69 | * @throws APIConnectionException connect exception
70 | * @throws APIRequestException request exception
71 | */
72 | public UploadResult uploadFile(String path, String fileType)
73 | throws APIConnectionException, APIRequestException {
74 | Preconditions.checkArgument(null != path, "filename is necessary");
75 | Preconditions.checkArgument(fileType.equals("image") || fileType.equals("file") || fileType.equals("voice"), "Illegal file type!");
76 | File file = new File(path);
77 | if (file.exists() && file.isFile()) {
78 | long fileSize = file.length();
79 | if (fileSize > 8 * 1024 * 1024) {
80 | throw new IllegalArgumentException("File size should not larger than 8M");
81 | }
82 | try {
83 | // 换行符
84 | final String newLine = "\r\n";
85 | final String boundaryPrefix = "--";
86 | // 定义数据分隔线
87 | String BOUNDARY = "========7d4a6d158c9";
88 | URL url = new URL(_baseUrl + resourcePath + "?type=" + fileType);
89 | HttpURLConnection conn = (HttpURLConnection) url.openConnection();
90 | conn.setRequestMethod("POST");
91 | conn.setDoOutput(true);
92 | conn.setDoInput(true);
93 | conn.setUseCaches(false);
94 | conn.setRequestProperty("connection", "Keep-Alive");
95 | conn.setRequestProperty("Charset", "UTF-8");
96 | conn.setRequestProperty("Authorization", this.authCode);
97 | conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
98 | OutputStream out = new DataOutputStream(conn.getOutputStream());
99 | StringBuilder sb = new StringBuilder();
100 | sb.append(boundaryPrefix);
101 | sb.append(BOUNDARY);
102 | sb.append(newLine);
103 | sb.append("Content-Disposition: form-data;name=\"" + fileType + "\";filename=\"" + path + "\"" + newLine);
104 | sb.append("Content-Type:application/octet-stream");
105 | // 参数头设置完以后需要两个换行,然后才是参数内容
106 | sb.append(newLine);
107 | sb.append(newLine);
108 | out.write(sb.toString().getBytes());
109 | DataInputStream dataInputStream = new DataInputStream(new FileInputStream(file));
110 | byte[] bufferOut = new byte[1024];
111 | int bytes = 0;
112 | while ((bytes = dataInputStream.read(bufferOut)) != -1) {
113 | out.write(bufferOut, 0, bytes);
114 | }
115 | out.write(newLine.getBytes());
116 | dataInputStream.close();
117 | // 定义最后数据分隔线,即--加上BOUNDARY再加上--。
118 | byte[] end_data = (newLine + boundaryPrefix + BOUNDARY + boundaryPrefix + newLine)
119 | .getBytes();
120 | // 写上结尾标识
121 | out.write(end_data);
122 | out.flush();
123 | out.close();
124 | LOG.debug("Send request - POST" + " " + url);
125 |
126 | int status1 = conn.getResponseCode();
127 | StringBuffer stringBuffer = new StringBuffer();
128 | InputStream in = null;
129 | if(status1 / 100 == 2) {
130 | in = conn.getInputStream();
131 | } else {
132 | in = conn.getErrorStream();
133 | }
134 | if(null != in) {
135 | InputStreamReader responseContent = new InputStreamReader(in, "UTF-8");
136 | char[] quota = new char[1024];
137 |
138 | int remaining;
139 | while((remaining = responseContent.read(quota)) > 0) {
140 | stringBuffer.append(quota, 0, remaining);
141 | }
142 | }
143 | ResponseWrapper wrapper = new ResponseWrapper();
144 | String responseContent1 = stringBuffer.toString();
145 | wrapper.responseCode = status1;
146 | wrapper.responseContent = responseContent1;
147 | String quota1 = conn.getHeaderField("X-Rate-Limit-Limit");
148 | String remaining1 = conn.getHeaderField("X-Rate-Limit-Remaining");
149 | String reset = conn.getHeaderField("X-Rate-Limit-Reset");
150 | wrapper.setRateLimit(quota1, remaining1, reset);
151 | if(status1 >= 200 && status1 < 300) {
152 | LOG.debug("Succeed to get response OK - responseCode:" + status1);
153 | LOG.debug("Response Content - " + responseContent1);
154 | } else {
155 | if(status1 < 300 || status1 >= 400) {
156 | LOG.warn("Got error response - responseCode:" + status1 + ", responseContent:" + responseContent1);
157 | wrapper.setErrorObject();
158 | throw new APIRequestException(wrapper);
159 | }
160 |
161 | LOG.warn("Normal response but unexpected - responseCode:" + status1 + ", responseContent:" + responseContent1);
162 | }
163 | return UploadResult.fromResponse(wrapper, UploadResult.class);
164 | } catch (Exception e) {
165 | e.printStackTrace();
166 | }
167 | } else {
168 | throw new IllegalArgumentException("File name is invalid, please check again");
169 | }
170 | return null;
171 | }
172 |
173 | }
174 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/resource/UploadResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.resource;
2 |
3 | import cn.jiguang.common.resp.BaseResult;
4 | import com.google.gson.annotations.Expose;
5 |
6 | public class UploadResult extends BaseResult {
7 |
8 | @Expose String media_id;
9 | @Expose Long media_crc32;
10 | @Expose Integer width;
11 | @Expose Integer height;
12 | @Expose String format;
13 | @Expose Integer fsize;
14 | @Expose String hash;
15 | @Expose String fname;
16 |
17 | public String getMediaId() {
18 | return media_id;
19 | }
20 |
21 | public Long getMediaCrc32() {
22 | return media_crc32;
23 | }
24 |
25 | public Integer getWidth() {
26 | return width;
27 | }
28 |
29 | public Integer getHeight() {
30 | return height;
31 | }
32 |
33 | public String getFormat() {
34 | return format;
35 | }
36 |
37 | public Integer getFileSize() {
38 | return fsize;
39 | }
40 |
41 | public String getHash() {
42 | return hash;
43 | }
44 |
45 | public String getFileName() {
46 | return fname;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/resource/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * User management API features.
3 | *
4 | * Url: https://api.im.jpush.cn/v1/resource
5 | */
6 | package cn.jmessage.api.resource;
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/sensitiveword/SensitiveWordClient.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.sensitiveword;
2 |
3 | import cn.jiguang.common.connection.HttpProxy;
4 | import cn.jiguang.common.resp.APIConnectionException;
5 | import cn.jiguang.common.resp.APIRequestException;
6 | import cn.jiguang.common.resp.ResponseWrapper;
7 | import cn.jiguang.common.utils.Preconditions;
8 | import cn.jmessage.api.common.BaseClient;
9 | import cn.jmessage.api.common.JMessageConfig;
10 | import com.google.gson.JsonArray;
11 | import com.google.gson.JsonObject;
12 | import com.google.gson.JsonPrimitive;
13 |
14 | import java.util.Set;
15 |
16 | public class SensitiveWordClient extends BaseClient {
17 |
18 | private String sensitiveWordPath;
19 |
20 | public SensitiveWordClient(String appKey, String masterSecret) {
21 | this(appKey, masterSecret, null, JMessageConfig.getInstance());
22 | }
23 |
24 | /**
25 | * Create a JMessage Base Client
26 | *
27 | * @param appkey The KEY of one application on JPush.
28 | * @param masterSecret API access secret of the appKey.
29 | * @param proxy The proxy, if there is no proxy, should be null.
30 | * @param config The client configuration. Can use JMessageConfig.getInstance() as default.
31 | */
32 | public SensitiveWordClient(String appkey, String masterSecret, HttpProxy proxy, JMessageConfig config) {
33 | super(appkey, masterSecret, proxy, config);
34 | this.sensitiveWordPath = (String) config.get(JMessageConfig.SENSITIVE_WORD_PATH);
35 | }
36 |
37 | /**
38 | * Add sensitive words
39 | * @param words String array
40 | * @return No content
41 | * @throws APIConnectionException connect exception
42 | * @throws APIRequestException request exception
43 | */
44 | public ResponseWrapper addSensitiveWords(String...words) throws APIConnectionException, APIRequestException {
45 | JsonArray array = new JsonArray();
46 | for (String word: words) {
47 | Preconditions.checkArgument(word.length() <= 10, "one word's max length is 10");
48 | array.add(new JsonPrimitive(word));
49 | }
50 | return _httpClient.sendPost(_baseUrl + sensitiveWordPath, array.toString());
51 | }
52 |
53 | /**
54 | * Add sensitive words
55 | * @param words String Set
56 | * @return No content
57 | * @throws APIConnectionException connect exception
58 | * @throws APIRequestException request exception
59 | */
60 | public ResponseWrapper addSensitiveWords(Set words) throws APIConnectionException, APIRequestException {
61 | JsonArray array = new JsonArray();
62 | for (String word : words) {
63 | Preconditions.checkArgument(word.length() <= 10, "one word's max length is 10");
64 | array.add(new JsonPrimitive(word));
65 | }
66 | return _httpClient.sendPost(_baseUrl + sensitiveWordPath, array.toString());
67 | }
68 |
69 | /**
70 | * Update sensitive word
71 | * @param newWord new word
72 | * @param oldWord old word
73 | * @return No content
74 | * @throws APIConnectionException connect exception
75 | * @throws APIRequestException request exception
76 | */
77 | public ResponseWrapper updateSensitiveWord(String newWord, String oldWord)
78 | throws APIConnectionException, APIRequestException {
79 | Preconditions.checkArgument(newWord.length() <= 10, "one word's max length is 10");
80 | Preconditions.checkArgument(oldWord.length() <= 10, "one word's max length is 10");
81 | JsonObject jsonObject = new JsonObject();
82 | jsonObject.addProperty("new_word", newWord);
83 | jsonObject.addProperty("old_word", oldWord);
84 | return _httpClient.sendPut(_baseUrl + sensitiveWordPath, jsonObject.toString());
85 | }
86 |
87 | /**
88 | * Delete sensitive word
89 | * @param word word to be deleted
90 | * @return No content
91 | * @throws APIConnectionException connect exception
92 | * @throws APIRequestException request exception
93 | */
94 | public ResponseWrapper deleteSensitiveWord(String word) throws APIConnectionException, APIRequestException {
95 | Preconditions.checkArgument(word.length() <= 10, "one word's max length is 10");
96 | JsonObject jsonObject = new JsonObject();
97 | jsonObject.addProperty("word", word);
98 | return _httpClient.sendDelete(_baseUrl + sensitiveWordPath, jsonObject.toString());
99 | }
100 |
101 | /**
102 | * Get sensitive word list
103 | * @param start the begin of the list
104 | * @param count the count of the list
105 | * @return SensitiveWordListResult
106 | * @throws APIConnectionException connect exception
107 | * @throws APIRequestException request exception
108 | */
109 | public SensitiveWordListResult getSensitiveWordList(int start, int count)
110 | throws APIConnectionException, APIRequestException {
111 | Preconditions.checkArgument(start >= 0, "start should not less than 0");
112 | Preconditions.checkArgument(count <= 2000, "count should not bigger than 2000");
113 | ResponseWrapper responseWrapper = _httpClient.sendGet(_baseUrl + sensitiveWordPath + "?start=" + start + "&count=" + count);
114 | return _gson.fromJson(responseWrapper.responseContent, SensitiveWordListResult.class);
115 | }
116 |
117 | /**
118 | * Update sensitive word status
119 | * @param status 1 represent turn on filtering, 0 represent turn off.
120 | * @return No content
121 | * @throws APIConnectionException connect exception
122 | * @throws APIRequestException request exception
123 | */
124 | public ResponseWrapper updateSensitiveWordStatus(int status) throws APIConnectionException, APIRequestException {
125 | Preconditions.checkArgument(status == 0 || status == 1, "status should be 0 or 1");
126 | return _httpClient.sendPut(_baseUrl + sensitiveWordPath + "/status?status=" + status, null);
127 | }
128 |
129 | /**
130 | * Get sensitive word status
131 | * @return status of sensitive word
132 | * @throws APIConnectionException connect exception
133 | * @throws APIRequestException request exception
134 | */
135 | public SensitiveWordStatusResult getSensitiveWordStatus() throws APIConnectionException, APIRequestException {
136 | ResponseWrapper responseWrapper = _httpClient.sendGet(_baseUrl + sensitiveWordPath + "/status");
137 | return _gson.fromJson(responseWrapper.responseContent, SensitiveWordStatusResult.class);
138 | }
139 | }
140 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/sensitiveword/SensitiveWordListResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.sensitiveword;
2 |
3 | import cn.jiguang.common.resp.BaseResult;
4 | import com.google.gson.annotations.Expose;
5 |
6 | import java.util.ArrayList;
7 | import java.util.List;
8 |
9 | public class SensitiveWordListResult extends BaseResult {
10 |
11 | @Expose Integer start;
12 | @Expose Integer count;
13 | @Expose Integer total;
14 | @Expose List words = new ArrayList();
15 |
16 | public static class Word {
17 | @Expose String name;
18 | @Expose String itime;
19 |
20 | public String getName() {
21 | return name;
22 | }
23 |
24 | public String getItime() {
25 | return itime;
26 | }
27 | }
28 |
29 | public Integer getStart() {
30 | return start;
31 | }
32 |
33 | public Integer getCount() {
34 | return count;
35 | }
36 |
37 | public Integer getTotal() {
38 | return total;
39 | }
40 |
41 | public List getWords() {
42 | return words;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/sensitiveword/SensitiveWordStatusResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.sensitiveword;
2 |
3 | import cn.jiguang.common.resp.BaseResult;
4 | import com.google.gson.annotations.Expose;
5 |
6 | public class SensitiveWordStatusResult extends BaseResult {
7 |
8 | @Expose Integer status;
9 |
10 | public Integer getStatus() {
11 | return status;
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/sensitiveword/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Sensitive word management API features.
3 | *
4 | * Not yet.
5 | */
6 | package cn.jmessage.api.sensitiveword;
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/user/RegisterResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.user;
2 |
3 | import java.util.List;
4 |
5 | import com.google.gson.JsonObject;
6 | import com.google.gson.annotations.Expose;
7 |
8 | import cn.jiguang.common.resp.BaseResult;
9 |
10 | public class RegisterResult extends BaseResult {
11 |
12 | @Expose List array;
13 |
14 | class RegisterEntity {
15 | @Expose String username;
16 | @Expose JsonObject error;
17 |
18 | public String getUsername() {
19 | return username;
20 | }
21 |
22 | public JsonObject getError() {
23 | return error;
24 | }
25 |
26 | public boolean hasError() {
27 | return null != error;
28 | }
29 |
30 | public String getErrorMessage() {
31 | if(null != error) {
32 | return error.get("message").getAsString();
33 | }
34 | return null;
35 | }
36 |
37 | public int getErrorCode() {
38 | if(null != error) {
39 | return error.get("code").getAsInt();
40 | }
41 | return -1;
42 | }
43 |
44 | }
45 |
46 | public List getArray() {
47 | return array;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/user/UserGroupsResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.user;
2 |
3 | import cn.jiguang.common.resp.BaseResult;
4 | import cn.jiguang.common.resp.ResponseWrapper;
5 | import cn.jmessage.api.group.GroupInfoResult;
6 |
7 | public class UserGroupsResult extends BaseResult{
8 |
9 | private GroupInfoResult[] groups = null;
10 |
11 | public static UserGroupsResult fromResponse(ResponseWrapper responseWrapper) {
12 | UserGroupsResult result = new UserGroupsResult();
13 | if (responseWrapper.isServerResponse()) {
14 | result.groups = _gson.fromJson(responseWrapper.responseContent, GroupInfoResult[].class);
15 | } else {
16 | // nothing
17 | }
18 | result.setResponseWrapper(responseWrapper);
19 | return result;
20 | }
21 |
22 | public GroupInfoResult[] getGroups() {
23 | return groups;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/user/UserInfoResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.user;
2 |
3 | import com.google.gson.JsonObject;
4 | import com.google.gson.annotations.Expose;
5 |
6 | import cn.jiguang.common.resp.BaseResult;
7 |
8 | import java.util.Map;
9 |
10 | public class UserInfoResult extends BaseResult {
11 |
12 | @Expose String username;
13 | @Expose String nickname;
14 | @Expose String avatar;
15 | @Expose String birthday;
16 | @Expose Integer gender;
17 | @Expose String signature;
18 | @Expose String region;
19 | @Expose String address;
20 | @Expose String ctime;
21 | @Expose String mtime;
22 | @Expose String appkey;
23 | @Expose Map extras;
24 |
25 | public String getUsername() {
26 | return username;
27 | }
28 |
29 | public String getNickname() {
30 | return nickname;
31 | }
32 |
33 | public String getAvatar() {
34 | return avatar;
35 | }
36 |
37 | public String getBirthday() {
38 | return birthday;
39 | }
40 |
41 | public Integer getGender() {
42 | return gender;
43 | }
44 |
45 | public String getSignature() {
46 | return signature;
47 | }
48 |
49 | public String getRegion() {
50 | return region;
51 | }
52 |
53 | public String getAddress() {
54 | return address;
55 | }
56 |
57 | public String getCtime() {
58 | return ctime;
59 | }
60 |
61 | public String getMtime() {
62 | return mtime;
63 | }
64 |
65 | public String getAppkey() {
66 | return appkey;
67 | }
68 |
69 | public Map getExtras() { return extras; }
70 | }
71 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/user/UserListResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.user;
2 |
3 | import cn.jiguang.common.resp.ResponseWrapper;
4 | import com.google.gson.annotations.Expose;
5 |
6 | import cn.jiguang.common.resp.BaseResult;
7 |
8 | public class UserListResult extends BaseResult {
9 |
10 | @Expose Integer total;
11 | @Expose Integer start;
12 | @Expose Integer count;
13 | @Expose UserInfoResult[] users;
14 |
15 | public static UserListResult fromResponse(ResponseWrapper responseWrapper) {
16 | UserListResult result = new UserListResult();
17 | if (responseWrapper.isServerResponse()) {
18 | result.users = _gson.fromJson(responseWrapper.responseContent, UserInfoResult[].class);
19 | }
20 | result.setResponseWrapper(responseWrapper);
21 | return result;
22 | }
23 |
24 | public Integer getTotal() {
25 | return total;
26 | }
27 |
28 | public Integer getStart() {
29 | return start;
30 | }
31 |
32 | public Integer getCount() {
33 | return count;
34 | }
35 |
36 | public UserInfoResult[] getUsers() {
37 | return users;
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/user/UserStateListResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.user;
2 |
3 | import cn.jiguang.common.resp.BaseResult;
4 | import com.google.gson.annotations.Expose;
5 |
6 | import java.util.ArrayList;
7 | import java.util.List;
8 |
9 | public class UserStateListResult extends BaseResult {
10 |
11 | @Expose List devices = new ArrayList();
12 | @Expose String username;
13 |
14 | public String getUsername() {
15 | return this.username;
16 | }
17 |
18 | public List getDevices(){
19 | return devices;
20 | }
21 |
22 | public class Device {
23 | @Expose boolean login;
24 | @Expose boolean online;
25 | @Expose String platform;
26 |
27 | public boolean getLogin() {
28 | return this.login;
29 | }
30 |
31 | public boolean getOnline() {
32 | return this.online;
33 | }
34 |
35 | public String getPlatform() {
36 | return this.platform;
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/user/UserStateResult.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.user;
2 |
3 | import cn.jiguang.common.resp.BaseResult;
4 | import com.google.gson.annotations.Expose;
5 |
6 | public class UserStateResult extends BaseResult {
7 | @Expose Boolean login;
8 | @Expose Boolean online;
9 |
10 | public Boolean getLogin() {
11 | return login;
12 | }
13 |
14 | public Boolean getOnline() {
15 | return online;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/user/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * User management API features.
3 | *
4 | * Url: https://api.im.jpush.cn/users
5 | */
6 | package cn.jmessage.api.user;
--------------------------------------------------------------------------------
/src/main/java/cn/jmessage/api/utils/StringUtils.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.utils;
2 |
3 | import cn.jiguang.common.ServiceHelper;
4 |
5 | public class StringUtils {
6 |
7 | public static void checkUsername(String username) {
8 | if ( null == username || username.trim().length() == 0) {
9 | throw new IllegalArgumentException("username must not be empty");
10 | }
11 | if (username.contains("\n") || username.contains("\r") || username.contains("\t")) {
12 | throw new IllegalArgumentException("username must not contain line feed character");
13 | }
14 | byte[] usernameByte = username.getBytes();
15 | if (usernameByte.length < 4 || usernameByte.length > 128) {
16 | throw new IllegalArgumentException("The length of username must between 4 and 128 bytes. Input is " + username);
17 | }
18 | if (!ServiceHelper.checkUsername(username)) {
19 | throw new IllegalArgumentException("The parameter username contains illegal character," +
20 | " a-zA-Z_0-9.、-,@。 is legally, and start with alphabet or number. Input is " + username);
21 | }
22 | }
23 |
24 | public static void checkPassword(String password) {
25 | if (null == password || password.trim().length() == 0) {
26 | throw new IllegalArgumentException("password must not be empty");
27 | }
28 |
29 | byte[] passwordByte = password.getBytes();
30 | if (passwordByte.length < 4 || passwordByte.length > 128) {
31 | throw new IllegalArgumentException("The length of password must between 4 and 128 bytes. Input is " + password);
32 | }
33 |
34 | }
35 |
36 | public static boolean isNotEmpty(String s) {
37 | return s != null && s.length() > 0;
38 | }
39 |
40 | public static boolean isTrimedEmpty(String s) {
41 | return s == null || s.trim().length() == 0;
42 | }
43 |
44 | public static boolean isLineBroken(String s) {
45 | if (s.contains("\n") || s.contains("\r")) {
46 | return true;
47 | }
48 | return false;
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/resources/javadoc-overview.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | JMessage Client
4 |
5 |
6 | JMessage API Java 客户端。
7 |
8 | 主要分为 3 个功能部分:用户管理、群组管理、发送消息。
9 |
10 |
11 |
--------------------------------------------------------------------------------
/src/test/java/cn/jmessage/api/BaseTest.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api;
2 |
3 |
4 | import com.google.gson.JsonParser;
5 |
6 | public class BaseTest {
7 |
8 | protected static final String APP_KEY ="c12bb0902ad1c069ffb67667";
9 | protected static final String CROSS_APP_KEY = "4f7aef34fb361292c566a1cd";
10 | protected static final String MASTER_SECRET = "bcf206e4dde5d40875c16a51";
11 |
12 | protected static final String MORE_THAN_64 = "a0123456789012345678901234567890123456789012345678901234567890123";
13 |
14 | protected static final String MORE_THAN_128 = "a012345678901234567890123456789012345678901234567890123456789" +
15 | "01234567890123456789012345678901234567890123456789012345678901234567";
16 |
17 | protected static final String MORE_THAN_250 =
18 | "a0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
19 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
20 | "01234567890123456789012345678901234567890123456789" ;
21 |
22 | protected static JsonParser parser = new JsonParser();
23 |
24 | protected static final String JUNIT_ADMIN = "junit_admin";
25 |
26 | protected static final String JUNIT_USER = "junit_no_delete";
27 |
28 | protected static final String JUNIT_USER1 = "junit_no_delete1";
29 |
30 | protected static final String JUNIT_USER2 = "junit_no_delete2";
31 |
32 | protected static final long JUNIT_GID1 = 23160755;
33 | protected static final long JUNIT_GID2 = 10055373;
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/src/test/java/cn/jmessage/api/FastTests.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api;
2 |
3 | public interface FastTests {
4 | }
5 |
--------------------------------------------------------------------------------
/src/test/java/cn/jmessage/api/JUnitOrderedRunner.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api;
2 |
3 | import org.junit.runners.BlockJUnit4ClassRunner;
4 | import org.junit.runners.model.FrameworkMethod;
5 | import org.junit.runners.model.InitializationError;
6 |
7 | import java.util.Collections;
8 | import java.util.Comparator;
9 | import java.util.List;
10 |
11 | public class JUnitOrderedRunner extends BlockJUnit4ClassRunner {
12 | public JUnitOrderedRunner(Class> klass) throws InitializationError {
13 | super(klass);
14 | }
15 |
16 | @Override
17 | protected List computeTestMethods() {
18 | List list = super.computeTestMethods();
19 | Collections.sort(list, new Comparator() {
20 | @Override
21 | public int compare(FrameworkMethod f1, FrameworkMethod f2) {
22 | TestOrder o1 = f1.getAnnotation(TestOrder.class);
23 | TestOrder o2 = f2.getAnnotation(TestOrder.class);
24 |
25 | if (o1 == null || o2 == null)
26 | return -1;
27 |
28 | return o1.order() - o2.order();
29 | }
30 | });
31 | return list;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/test/java/cn/jmessage/api/SlowTests.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api;
2 |
3 | public interface SlowTests {
4 | }
5 |
--------------------------------------------------------------------------------
/src/test/java/cn/jmessage/api/TestOrder.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api;
2 |
3 | import java.lang.annotation.Retention;
4 | import java.lang.annotation.RetentionPolicy;
5 |
6 | @Retention(RetentionPolicy.RUNTIME)
7 | public @interface TestOrder {
8 | public int order();
9 | }
10 |
--------------------------------------------------------------------------------
/src/test/java/cn/jmessage/api/chatroom/ChatRoomClientTest.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.chatroom;
2 |
3 | import cn.jiguang.common.resp.APIConnectionException;
4 | import cn.jiguang.common.resp.APIRequestException;
5 | import cn.jiguang.common.resp.ResponseWrapper;
6 | import cn.jmessage.api.BaseTest;
7 | import cn.jmessage.api.SlowTests;
8 | import cn.jmessage.api.common.model.Members;
9 | import cn.jmessage.api.common.model.chatroom.ChatRoomPayload;
10 | import cn.jmessage.api.user.UserListResult;
11 | import org.junit.Test;
12 | import org.junit.experimental.categories.Category;
13 | import org.slf4j.Logger;
14 | import org.slf4j.LoggerFactory;
15 |
16 | @Category(SlowTests.class)
17 | public class ChatRoomClientTest extends BaseTest {
18 |
19 | private Logger LOG = LoggerFactory.getLogger(ChatRoomClientTest.class);
20 |
21 | private ChatRoomClient mClient = new ChatRoomClient(APP_KEY, MASTER_SECRET);
22 |
23 | @Test
24 | public void testCreateChatRoom() {
25 | try {
26 | ChatRoomPayload payload = ChatRoomPayload.newBuilder()
27 | .setName("haha")
28 | .setDesc("test")
29 | .setOwnerUsername(JUNIT_ADMIN)
30 | .setMembers(Members.newBuilder().addMember(JUNIT_USER1, JUNIT_USER2).build())
31 | .build();
32 | CreateChatRoomResult result = mClient.createChatRoom(payload);
33 | LOG.info("Got result, room id:" + result.getChatroom_id());
34 | } catch (APIConnectionException e) {
35 | LOG.error("Connection error. Should retry later. ", e);
36 | } catch (APIRequestException e) {
37 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
38 | LOG.info("HTTP Status: " + e.getStatus());
39 | LOG.info("Error Message: " + e.getMessage());
40 | }
41 | }
42 |
43 | @Test
44 | public void testGetBatchChatRoomInfo() {
45 | try {
46 | ChatRoomListResult result = mClient.getBatchChatRoomInfo(12500011, 10000072);
47 | ChatRoomResult[] rooms = result.getRooms();
48 | if (rooms.length > 0) {
49 | for (ChatRoomResult room: rooms) {
50 | LOG.info("Got result " + room.toString());
51 | }
52 | }
53 | } catch (APIConnectionException e) {
54 | LOG.error("Connection error. Should retry later. ", e);
55 | } catch (APIRequestException e) {
56 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
57 | LOG.info("HTTP Status: " + e.getStatus());
58 | LOG.info("Error Message: " + e.getMessage());
59 | }
60 | }
61 |
62 | @Test
63 | public void testGetUserChatRoomInfo() {
64 | try {
65 | ChatRoomListResult result = mClient.getUserChatRoomInfo(JUNIT_ADMIN);
66 | ChatRoomResult[] rooms = result.getRooms();
67 | if (rooms.length > 0) {
68 | for (ChatRoomResult room: rooms) {
69 | LOG.info("Got result " + room.toString());
70 | }
71 | }
72 | } catch (APIConnectionException e) {
73 | LOG.error("Connection error. Should retry later. ", e);
74 | } catch (APIRequestException e) {
75 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
76 | LOG.info("HTTP Status: " + e.getStatus());
77 | LOG.info("Error Message: " + e.getMessage());
78 | }
79 | }
80 |
81 | @Test
82 | public void testGetAppChatRoomInfo() {
83 | try {
84 | ChatRoomListResult result = mClient.getAppChatRoomInfo(0, 2);
85 | LOG.info("Got result " + result.getList().toString());
86 | } catch (APIConnectionException e) {
87 | LOG.error("Connection error. Should retry later. ", e);
88 | } catch (APIRequestException e) {
89 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
90 | LOG.info("HTTP Status: " + e.getStatus());
91 | LOG.info("Error Message: " + e.getMessage());
92 | }
93 | }
94 |
95 | @Test
96 | public void testUpdateChatRoomInfo() {
97 | try {
98 | ResponseWrapper result = mClient.updateChatRoomInfo(10000072, JUNIT_USER1, "new Test", "666");
99 | LOG.info("Got result " + result.toString());
100 | } catch (APIConnectionException e) {
101 | LOG.error("Connection error. Should retry later. ", e);
102 | } catch (APIRequestException e) {
103 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
104 | LOG.info("HTTP Status: " + e.getStatus());
105 | LOG.info("Error Message: " + e.getMessage());
106 | }
107 | }
108 |
109 | @Test
110 | public void testDeleteChatRoom() {
111 | try {
112 | ResponseWrapper result = mClient.deleteChatRoom(12500013);
113 | LOG.info("Got result " + result.toString());
114 | } catch (APIConnectionException e) {
115 | LOG.error("Connection error. Should retry later. ", e);
116 | } catch (APIRequestException e) {
117 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
118 | LOG.info("HTTP Status: " + e.getStatus());
119 | LOG.info("Error Message: " + e.getMessage());
120 | }
121 | }
122 |
123 | @Test
124 | public void testUpdateUserSpeakStatus() {
125 | try {
126 | ResponseWrapper result = mClient.updateUserSpeakStatus(12500011, JUNIT_USER1, 1);
127 | LOG.info("Got result " + result.toString());
128 | } catch (APIConnectionException e) {
129 | LOG.error("Connection error. Should retry later. ", e);
130 | } catch (APIRequestException e) {
131 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
132 | LOG.info("HTTP Status: " + e.getStatus());
133 | LOG.info("Error Message: " + e.getMessage());
134 | }
135 | }
136 |
137 | @Test
138 | public void testGetChatRoomMembers() {
139 | try {
140 | ChatRoomMemberList result = mClient.getChatRoomMembers(12500011, 0, 3);
141 | ChatRoomMemberList.ChatRoomMember[] members = result.getMembers();
142 | if (members.length > 0) {
143 | for (ChatRoomMemberList.ChatRoomMember member: members) {
144 | LOG.info("Member: " + member.username);
145 | }
146 | }
147 | } catch (APIConnectionException e) {
148 | LOG.error("Connection error. Should retry later. ", e);
149 | } catch (APIRequestException e) {
150 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
151 | LOG.info("HTTP Status: " + e.getStatus());
152 | LOG.info("Error Message: " + e.getMessage());
153 | }
154 | }
155 |
156 | @Test
157 | public void testAddChatRoomMember() {
158 | try {
159 | ResponseWrapper result = mClient.addChatRoomMember(12500011, JUNIT_USER);
160 | LOG.info("Got result: " + result);
161 | } catch (APIConnectionException e) {
162 | LOG.error("Connection error. Should retry later. ", e);
163 | } catch (APIRequestException e) {
164 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
165 | LOG.info("HTTP Status: " + e.getStatus());
166 | LOG.info("Error Message: " + e.getMessage());
167 | }
168 | }
169 |
170 | @Test
171 | public void testDeleteChatRoomMember() {
172 | try {
173 | ResponseWrapper result = mClient.removeChatRoomMembers(12500011, JUNIT_USER);
174 | LOG.info("Got result: " + result);
175 | } catch (APIConnectionException e) {
176 | LOG.error("Connection error. Should retry later. ", e);
177 | } catch (APIRequestException e) {
178 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
179 | LOG.info("HTTP Status: " + e.getStatus());
180 | LOG.info("Error Message: " + e.getMessage());
181 | }
182 | }
183 | }
184 |
--------------------------------------------------------------------------------
/src/test/java/cn/jmessage/api/cross/CrossAppClientTest.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.cross;
2 |
3 | import cn.jiguang.common.resp.APIConnectionException;
4 | import cn.jiguang.common.resp.APIRequestException;
5 | import cn.jiguang.common.resp.ResponseWrapper;
6 | import cn.jmessage.api.BaseTest;
7 | import cn.jmessage.api.crossapp.CrossAppClient;
8 | import cn.jmessage.api.common.model.cross.CrossBlacklist;
9 | import cn.jmessage.api.common.model.cross.CrossFriendPayload;
10 | import cn.jmessage.api.common.model.cross.CrossGroup;
11 | import cn.jmessage.api.common.model.cross.CrossNoDisturb;
12 | import cn.jmessage.api.group.MemberListResult;
13 | import cn.jmessage.api.group.MemberResult;
14 | import cn.jmessage.api.user.UserInfoResult;
15 | import org.junit.Before;
16 | import org.junit.Test;
17 | import org.slf4j.Logger;
18 | import org.slf4j.LoggerFactory;
19 |
20 | import java.util.ArrayList;
21 | import java.util.List;
22 |
23 | import static org.junit.Assert.assertTrue;
24 |
25 | public class CrossAppClientTest extends BaseTest {
26 |
27 | private Logger LOG = LoggerFactory.getLogger(CrossAppClientTest.class);
28 | private CrossAppClient client = null;
29 |
30 | @Before
31 | public void before() throws Exception {
32 | client = new CrossAppClient(APP_KEY, MASTER_SECRET);
33 | }
34 |
35 | @Test
36 | public void testAddOrRemoveMembersFromCrossGroup() {
37 | try {
38 | List crossGroups = new ArrayList();
39 | CrossGroup crossGroup = new CrossGroup.Builder()
40 | .setAppKey(APP_KEY)
41 | .setAddUsers(JUNIT_USER1, JUNIT_USER2)
42 | .build();
43 | crossGroups.add(crossGroup);
44 | CrossGroup[] array = new CrossGroup[crossGroups.size()];
45 | ResponseWrapper response = client.addOrRemoveCrossGroupMembers(JUNIT_GID1, crossGroups.toArray(array));
46 | } catch (APIConnectionException e) {
47 | LOG.error("Connection error. Should retry later. ", e);
48 | } catch (APIRequestException e) {
49 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
50 | LOG.info("HTTP Status: " + e.getStatus());
51 | LOG.info("Error Message: " + e.getMessage());
52 | }
53 | }
54 |
55 | @Test
56 | public void testGetCrossGroupMembers() {
57 | try {
58 | MemberListResult result = client.getCrossGroupMembers(JUNIT_GID1);
59 | MemberResult[] members = result.getMembers();
60 | LOG.info("Member size " + members.length);
61 | assertTrue(result.isResultOK());
62 | } catch (APIConnectionException e) {
63 | LOG.error("Connection error. Should retry later. ", e);
64 | } catch (APIRequestException e) {
65 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
66 | LOG.info("HTTP Status: " + e.getStatus());
67 | LOG.info("Error Message: " + e.getMessage());
68 | }
69 | }
70 |
71 | @Test
72 | public void testAddCrossBlacklist() {
73 | try {
74 | List crossBlacklists = new ArrayList();
75 | CrossBlacklist blacklist = new CrossBlacklist.Builder()
76 | .setAppKey(APP_KEY)
77 | .addUsers(JUNIT_USER1, JUNIT_USER2)
78 | .build();
79 |
80 | crossBlacklists.add(blacklist);
81 | CrossBlacklist[] array = new CrossBlacklist[crossBlacklists.size()];
82 | ResponseWrapper response = client.addCrossBlacklist(JUNIT_USER, crossBlacklists.toArray(array));
83 | } catch (APIConnectionException e) {
84 | LOG.error("Connection error. Should retry later. ", e);
85 | } catch (APIRequestException e) {
86 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
87 | LOG.info("HTTP Status: " + e.getStatus());
88 | LOG.info("Error Message: " + e.getMessage());
89 | }
90 | }
91 |
92 | @Test
93 | public void testDeleteCrossBlacklist() {
94 | try {
95 | List crossBlacklists = new ArrayList();
96 | CrossBlacklist blacklist = new CrossBlacklist.Builder()
97 | .setAppKey(APP_KEY)
98 | .addUsers(JUNIT_USER1, JUNIT_USER2)
99 | .build();
100 |
101 | crossBlacklists.add(blacklist);
102 | CrossBlacklist[] array = new CrossBlacklist[crossBlacklists.size()];
103 | ResponseWrapper response = client.deleteCrossBlacklist(JUNIT_USER, crossBlacklists.toArray(array));
104 | } catch (APIConnectionException e) {
105 | LOG.error("Connection error. Should retry later. ", e);
106 | } catch (APIRequestException e) {
107 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
108 | LOG.info("HTTP Status: " + e.getStatus());
109 | LOG.info("Error Message: " + e.getMessage());
110 | }
111 | }
112 |
113 | @Test
114 | public void testGetCrossBlacklist() {
115 | try {
116 | UserInfoResult[] result = client.getCrossBlacklist(JUNIT_USER);
117 | LOG.info("Users' info size: " + result.length);
118 | } catch (APIConnectionException e) {
119 | LOG.error("Connection error. Should retry later. ", e);
120 | } catch (APIRequestException e) {
121 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
122 | LOG.info("HTTP Status: " + e.getStatus());
123 | LOG.info("Error Message: " + e.getMessage());
124 | }
125 | }
126 |
127 | @Test
128 | public void testSetCrossNoDisturb() {
129 | try {
130 | List list = new ArrayList();
131 | CrossNoDisturb crossNoDisturb = new CrossNoDisturb.Builder()
132 | .setAppKey(APP_KEY)
133 | .setRemoveSingleUsers(JUNIT_USER1, JUNIT_USER2)
134 | .setRemoveGroupIds(JUNIT_GID1)
135 | .build();
136 | list.add(crossNoDisturb);
137 | CrossNoDisturb[] array = new CrossNoDisturb[list.size()];
138 | ResponseWrapper response = client.setCrossNoDisturb(JUNIT_USER, list.toArray(array));
139 | } catch (APIConnectionException e) {
140 | LOG.error("Connection error. Should retry later. ", e);
141 | } catch (APIRequestException e) {
142 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
143 | LOG.info("HTTP Status: " + e.getStatus());
144 | LOG.info("Error Message: " + e.getMessage());
145 | }
146 | }
147 |
148 | @Test
149 | public void testAddCrossUsers() {
150 | try {
151 | CrossFriendPayload payload = new CrossFriendPayload.Builder()
152 | .setAppKey(CROSS_APP_KEY)
153 | .setUsers("0001", "0002")
154 | .build();
155 | ResponseWrapper response = client.addCrossFriends(JUNIT_USER, payload);
156 | LOG.info("Http status: " + response.responseCode);
157 | } catch (APIConnectionException e) {
158 | LOG.error("Connection error. Should retry later. ", e);
159 | } catch (APIRequestException e) {
160 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
161 | LOG.info("HTTP Status: " + e.getStatus());
162 | LOG.info("Error Message: " + e.getMessage());
163 | }
164 | }
165 |
166 | @Test
167 | public void testDeleteCrossFriends() {
168 | try {
169 | CrossFriendPayload payload = new CrossFriendPayload.Builder()
170 | .setAppKey(CROSS_APP_KEY)
171 | .setUsers("0001", "0002")
172 | .build();
173 | ResponseWrapper response = client.deleteCrossFriends(JUNIT_USER, payload);
174 | LOG.info("Http status: " + response.responseCode);
175 | } catch (APIConnectionException e) {
176 | LOG.error("Connection error. Should retry later. ", e);
177 | } catch (APIRequestException e) {
178 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
179 | LOG.info("HTTP Status: " + e.getStatus());
180 | LOG.info("Error Message: " + e.getMessage());
181 | }
182 | }
183 | }
184 |
--------------------------------------------------------------------------------
/src/test/java/cn/jmessage/api/reportv2/ReportClientTest.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.reportv2;
2 |
3 | import cn.jiguang.common.resp.APIConnectionException;
4 | import cn.jiguang.common.resp.APIRequestException;
5 | import cn.jmessage.api.BaseTest;
6 | import cn.jmessage.api.SlowTests;
7 | import org.junit.Before;
8 | import org.junit.Test;
9 | import org.junit.experimental.categories.Category;
10 | import org.slf4j.Logger;
11 | import org.slf4j.LoggerFactory;
12 |
13 | import static org.junit.Assert.assertTrue;
14 |
15 | @Category(SlowTests.class)
16 | public class ReportClientTest extends BaseTest {
17 |
18 | private static Logger LOG = LoggerFactory.getLogger(ReportClientTest.class);
19 | private ReportClient reportClient = null;
20 |
21 | @Before
22 | public void before() throws Exception {
23 | reportClient = new ReportClient(APP_KEY, MASTER_SECRET);
24 | }
25 |
26 | @Test
27 | public void testGetUserStat() {
28 | try {
29 | UserStatListResult result = reportClient.getUserStatistic("2016-11-08", 3);
30 | LOG.info("Got result: " + result);
31 | assertTrue(result.isResultOK());
32 | } catch (APIConnectionException e) {
33 | LOG.error("Connection error. Should retry later. ", e);
34 | } catch (APIRequestException e) {
35 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
36 | LOG.info("HTTP Status: " + e.getStatus());
37 | LOG.info("Error Message: " + e.getMessage());
38 | }
39 | }
40 |
41 | @Test
42 | public void testGetMessageStat() {
43 | try {
44 | MessageStatListResult result = reportClient.getMessageStatistic("DAY", "2016-11-08", 3);
45 | LOG.info("Got result: " + result);
46 | assertTrue(result.isResultOK());
47 | } catch (APIConnectionException e) {
48 | LOG.error("Connection error. Should retry later. ", e);
49 | } catch (APIRequestException e) {
50 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
51 | LOG.info("HTTP Status: " + e.getStatus());
52 | LOG.info("Error Message: " + e.getMessage());
53 | }
54 | }
55 |
56 | @Test
57 | public void testGetGroupStat() {
58 | try {
59 | GroupStatListResult result = reportClient.getGroupStatistic("2016-11-08", 3);
60 | LOG.info("Got result: " + result);
61 | assertTrue(result.isResultOK());
62 | } catch (APIConnectionException e) {
63 | LOG.error("Connection error. Should retry later. ", e);
64 | } catch (APIRequestException e) {
65 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
66 | LOG.info("HTTP Status: " + e.getStatus());
67 | LOG.info("Error Message: " + e.getMessage());
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/test/java/cn/jmessage/api/resource/ResourceClientTest.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.resource;
2 |
3 | import cn.jiguang.common.resp.APIConnectionException;
4 | import cn.jiguang.common.resp.APIRequestException;
5 | import cn.jmessage.api.BaseTest;
6 | import cn.jmessage.api.SlowTests;
7 | import org.junit.Before;
8 | import org.junit.Ignore;
9 | import org.junit.Test;
10 | import org.junit.experimental.categories.Category;
11 | import org.slf4j.Logger;
12 | import org.slf4j.LoggerFactory;
13 |
14 | import static org.junit.Assert.assertTrue;
15 |
16 | @Category(SlowTests.class)
17 | public class ResourceClientTest extends BaseTest {
18 |
19 | private static Logger LOG = LoggerFactory.getLogger(ResourceClientTest.class);
20 | private ResourceClient resourceClient = null;
21 |
22 | @Before
23 | public void before() throws Exception {
24 | resourceClient = new ResourceClient(APP_KEY, MASTER_SECRET);
25 | }
26 |
27 | @Test
28 | public void testDownloadFile() {
29 | try {
30 | DownloadResult result = resourceClient.downloadFile("qiniu/image/B8C68B55D34B3BC9");
31 | String url = result.getUrl();
32 | LOG.info("Url: " + url);
33 | assertTrue(result.isResultOK());
34 | } catch (APIConnectionException e) {
35 | LOG.error("Connection error. Should retry later. ", e);
36 | } catch (APIRequestException e) {
37 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
38 | LOG.info("HTTP Status: " + e.getStatus());
39 | LOG.info("Error Message: " + e.getMessage());
40 | }
41 | }
42 |
43 | @Ignore
44 | @Test
45 | public void testUploadFile() {
46 | try {
47 | UploadResult result = resourceClient.uploadFile("/users/caiyg/Desktop/discourse.png", "image");
48 | if (null != result) {
49 | assertTrue(result.isResultOK());
50 | LOG.info("media_id: " + result.getMediaId() + " file name: " + result.getFileName());
51 | }
52 | } catch (APIConnectionException e) {
53 | LOG.error("Connection error. Should retry later. ", e);
54 | } catch (APIRequestException e) {
55 | LOG.error("Error response from JPush server. Should review and fix it. ", e);
56 | LOG.info("HTTP Status: " + e.getStatus());
57 | LOG.info("Error Message: " + e.getMessage());
58 | }
59 | }
60 |
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/src/test/java/cn/jmessage/api/sensitiveword/SensitiveWordClientTest.java:
--------------------------------------------------------------------------------
1 | package cn.jmessage.api.sensitiveword;
2 |
3 | import cn.jiguang.common.resp.APIConnectionException;
4 | import cn.jiguang.common.resp.APIRequestException;
5 | import cn.jiguang.common.resp.ResponseWrapper;
6 | import cn.jmessage.api.BaseTest;
7 | import cn.jmessage.api.SlowTests;
8 | import org.junit.Before;
9 | import org.junit.Test;
10 | import org.junit.experimental.categories.Category;
11 | import org.slf4j.Logger;
12 | import org.slf4j.LoggerFactory;
13 |
14 | @Category(SlowTests.class)
15 | public class SensitiveWordClientTest extends BaseTest {
16 | private static Logger LOG = LoggerFactory.getLogger(SensitiveWordClient.class);
17 | private SensitiveWordClient client = null;
18 |
19 | @Before
20 | public void before() throws Exception {
21 | client = new SensitiveWordClient(APP_KEY, MASTER_SECRET);
22 | }
23 |
24 | @Test
25 | public void testAddSensitiveWord() {
26 | try {
27 | ResponseWrapper result = client.addSensitiveWords("fuck", "FUCK");
28 | LOG.info("response code: " + result.responseCode + " content " + result.responseContent);
29 | } catch (APIConnectionException e) {
30 | LOG.error("Connection error. Should retry later. ", e);
31 | } catch (APIRequestException e) {
32 | LOG.error("Error response from server. Should review and fix it. ", e);
33 | LOG.info("HTTP Status: " + e.getStatus());
34 | LOG.info("Error Message: " + e.getMessage());
35 | }
36 | }
37 |
38 | @Test
39 | public void testUpdateSensitiveWord() {
40 | try {
41 | ResponseWrapper result = client.updateSensitiveWord("f**k", "fuck");
42 | LOG.info("response code: " + result.responseCode + " content " + result.responseContent);
43 | } catch (APIConnectionException e) {
44 | LOG.error("Connection error. Should retry later. ", e);
45 | } catch (APIRequestException e) {
46 | LOG.error("Error response from server. Should review and fix it. ", e);
47 | LOG.info("HTTP Status: " + e.getStatus());
48 | LOG.info("Error Message: " + e.getMessage());
49 | }
50 | }
51 |
52 | @Test
53 | public void testDeleteSensitiveWord() {
54 | try {
55 | ResponseWrapper result = client.deleteSensitiveWord("f**k");
56 | LOG.info("response code: " + result.responseCode + " content " + result.responseContent);
57 | } catch (APIConnectionException e) {
58 | LOG.error("Connection error. Should retry later. ", e);
59 | } catch (APIRequestException e) {
60 | LOG.error("Error response from server. Should review and fix it. ", e);
61 | LOG.info("HTTP Status: " + e.getStatus());
62 | LOG.info("Error Message: " + e.getMessage());
63 | }
64 | }
65 |
66 | @Test
67 | public void testGetSensitiveWordList() {
68 | try {
69 | SensitiveWordListResult result = client.getSensitiveWordList(0, 10);
70 | LOG.info(result.toString());
71 | } catch (APIConnectionException e) {
72 | LOG.error("Connection error. Should retry later. ", e);
73 | } catch (APIRequestException e) {
74 | LOG.error("Error response from server. Should review and fix it. ", e);
75 | LOG.info("HTTP Status: " + e.getStatus());
76 | LOG.info("Error Message: " + e.getMessage());
77 | }
78 | }
79 |
80 | @Test
81 | public void testUpdateSensitiveWordStatus() {
82 | try {
83 | ResponseWrapper result = client.updateSensitiveWordStatus(0);
84 | LOG.info("response code: " + result.responseCode + " content " + result.responseContent);
85 | } catch (APIConnectionException e) {
86 | LOG.error("Connection error. Should retry later. ", e);
87 | } catch (APIRequestException e) {
88 | LOG.error("Error response from server. Should review and fix it. ", e);
89 | LOG.info("HTTP Status: " + e.getStatus());
90 | LOG.info("Error Message: " + e.getMessage());
91 | }
92 | }
93 |
94 | @Test
95 | public void testGetSensitiveWordStatus() {
96 | try {
97 | SensitiveWordStatusResult result = client.getSensitiveWordStatus();
98 | LOG.info(result.toString());
99 | } catch (APIConnectionException e) {
100 | LOG.error("Connection error. Should retry later. ", e);
101 | } catch (APIRequestException e) {
102 | LOG.error("Error response from server. Should review and fix it. ", e);
103 | LOG.info("HTTP Status: " + e.getStatus());
104 | LOG.info("Error Message: " + e.getMessage());
105 | }
106 | }
107 | }
108 |
--------------------------------------------------------------------------------
/src/test/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootLogger=DEBUG,CONSOLE
2 |
3 | log4j.logger.org.eclipse.jetty=INFO
4 |
5 | log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
6 | log4j.appender.CONSOLE.Target=System.out
7 | log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
8 | log4j.appender.CONSOLE.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n
9 |
--------------------------------------------------------------------------------