Skip to content

Commit

Permalink
Merge pull request #40 from jpush/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
KenChoi1992 committed Sep 11, 2017
2 parents 69ef7a2 + ed23dbc commit 5617ea0
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -27,7 +27,7 @@
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jmessage-client</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>cn.jpush.api</groupId>
Expand Down
31 changes: 31 additions & 0 deletions example/main/java/cn/jmessage/api/examples/UserExample.java
Expand Up @@ -6,6 +6,7 @@
import cn.jiguang.common.resp.ResponseWrapper;
import cn.jmessage.api.common.model.friend.FriendNote;
import cn.jmessage.api.common.model.NoDisturbPayload;
import cn.jmessage.api.user.UserStateListResult;
import cn.jmessage.api.user.UserStateResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -93,6 +94,22 @@ public static void testGetUserState() {
}
}

public static void testGetUsersState() {
JMessageClient client = new JMessageClient(appkey, masterSecret);
try {
UserStateListResult[] results = client.getUsersState("user1", "user2", "user3");
for (UserStateListResult result : results) {
LOG.info(result.toString());
}
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Message: " + e.getMessage());
}
}

public static void testUpdatePassword() {
JMessageClient client = new JMessageClient(appkey, masterSecret);

Expand Down Expand Up @@ -289,5 +306,19 @@ public static void testGetFriends() {
}
}

public static void testForbidUser() {
JMessageClient client = new JMessageClient(appkey, masterSecret);
try {
ResponseWrapper result = client.forbidUser("user1", true);
LOG.info("response code: " + result.responseCode);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Message: " + e.getMessage());
}
}

}

2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -34,7 +34,7 @@
<url>https://github.com/jpush/jmessage-api-java-client</url>
<connection>scm:git:git@github.com:jpush/jmessage-api-java-client.git</connection>
<developerConnection>scm:git:git@github.com:jpush/jmessage-api-java-client.git</developerConnection>
<tag>v1.1.1</tag>
<tag>v1.1.2</tag>
</scm>

<dependencies>
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/cn/jmessage/api/JMessageClient.java
Expand Up @@ -140,6 +140,18 @@ public UserStateResult getUserState(String username)
return _userClient.getUserState(username);
}

/**
* Get users' state
* @param users username of users
* @return {@link UserStateListResult}
* @throws APIConnectionException connect exception
* @throws APIRequestException request exception
*/
public UserStateListResult[] getUsersState(String...users)
throws APIConnectionException, APIRequestException {
return _userClient.getUsersState(users);
}

public void updateUserPassword(String username, String password)
throws APIConnectionException, APIRequestException {
_userClient.updatePassword(username, password);
Expand Down Expand Up @@ -311,6 +323,19 @@ public ResponseWrapper setGroupShield(GroupShieldPayload payload, String usernam
return _userClient.setGroupShield(payload, username);
}

/**
* Forbid or activate user
* @param username username
* @param disable true means forbid, false means activate
* @return No content
* @throws APIConnectionException connect exception
* @throws APIRequestException request exception
*/
public ResponseWrapper forbidUser(String username, boolean disable)
throws APIConnectionException, APIRequestException {
return _userClient.forbidUser(username, disable);
}

// ------------------------------- Group API

public GroupInfoResult getGroupInfo(long gid)
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/cn/jmessage/api/user/UserClient.java
Expand Up @@ -111,6 +111,23 @@ public UserStateResult getUserState(String username)
return UserStateResult.fromResponse(response, UserStateResult.class);
}

/**
* Get users' state
* @param users username of users
* @return {@link UserStateListResult}
* @throws APIConnectionException connect exception
* @throws APIRequestException request exception
*/
public UserStateListResult[] getUsersState(String...users) throws APIConnectionException, APIRequestException {
JsonArray jsonArray = new JsonArray();
for (String username : users) {
StringUtils.checkUsername(username);
jsonArray.add(new JsonPrimitive(username));
}
ResponseWrapper response = _httpClient.sendPost(_baseUrl + userPath + "/userstat", jsonArray.toString());
return _gson.fromJson(response.responseContent, UserStateListResult[].class);
}

public ResponseWrapper updatePassword( String username, String password )
throws APIConnectionException, APIRequestException {

Expand Down Expand Up @@ -334,4 +351,20 @@ public ResponseWrapper setGroupShield(GroupShieldPayload payload, String usernam
return _httpClient.sendPost(_baseUrl + userPath + "/" + username + "/groupsShield", payload.toString());
}

/**
* Forbid or activate user
* @param username username
* @param disable true means forbid, false means activate
* @return No content
* @throws APIConnectionException connect exception
* @throws APIRequestException request exception
*/
public ResponseWrapper forbidUser(String username, boolean disable)
throws APIConnectionException, APIRequestException {
StringUtils.checkUsername(username);
return _httpClient.sendPut(_baseUrl + userPath + "/" + username + "/forbidden?disable=" + disable, null);
}



}
35 changes: 35 additions & 0 deletions src/main/java/cn/jmessage/api/user/UserStateListResult.java
@@ -0,0 +1,35 @@
package cn.jmessage.api.user;

import cn.jiguang.common.resp.BaseResult;
import com.google.gson.annotations.Expose;

import java.util.ArrayList;
import java.util.List;

public class UserStateListResult extends BaseResult {

@Expose List<Device> devices = new ArrayList<Device>();
@Expose String username;

public String getUsername() {
return this.username;
}

private class Device {
@Expose boolean login;
@Expose boolean online;
@Expose String platform;

public boolean getLogin() {
return this.login;
}

public boolean getOnline() {
return this.online;
}

public String getPlatform() {
return this.platform;
}
}
}
33 changes: 32 additions & 1 deletion src/test/java/cn/jmessage/api/user/UserClientTest.java
Expand Up @@ -414,7 +414,24 @@ public void testGetUserInfo_UsernameInvalid() {
@Test
public void testGetUserState() {
try {
userClient.getUserState(JUNIT_USER);
UserStateResult result = userClient.getUserState(JUNIT_USER);
LOG.info(result.toString());
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Message: " + e.getMessage());
}
}

@Test
public void testGetUsersState() {
try {
UserStateListResult[] results = userClient.getUsersState(JUNIT_USER, JUNIT_USER1, JUNIT_USER2);
for (UserStateListResult result : results) {
LOG.info(result.toString());
}
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
Expand Down Expand Up @@ -902,4 +919,18 @@ public void testSetGroupShield() {
}
}

@Test
public void testForbidUser() {
try {
ResponseWrapper result = userClient.forbidUser(JUNIT_USER, true);
LOG.info("response code: " + result.responseCode);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Message: " + e.getMessage());
}
}

}

0 comments on commit 5617ea0

Please sign in to comment.