Java 通過 oenfire Rest API 操作使用者
阿新 • • 發佈:2018-11-20
1 : 開啟 openfire Server Console , 開啟外掛部分,找到 Rest API 並安裝
外掛 :
2 : 設定BaseAuto :
3 : 建立 Java Maven 專案:
Pom.xml 加入 openfire Client 支援 :
<dependency>
<groupId>org.igniterealtime</groupId>
<
<version>1.1.4</version>
</dependency>
4 : 通過 Java 程式操作使用者
package oepnfireRest;
import java.util.Random;
import javax.ws.rs.core.Response;
import org.igniterealtime.restclient.RestApiClient;
import org.igniterealtime.restclient.entity.AuthenticationToken;
import org.igniterealtime.restclient.entity.UserEntities;
import org.igniterealtime.restclient.entity.UserEntity;
/**
* Purpose: 通過 Rest API 操作使用者
* @author riverjin
* @since JDK 1.7
* @date 2018年4月17日
* @MaintenancePersonnel riverjin
*/
public class JavaOpenfire {
/**
* 隨機數
*/
private static Random r = new Random();
public static void main1(String[] args) {
// 金鑰為 openfire Server 伺服器端的金鑰
AuthenticationToken authenticationToken = new AuthenticationToken("zN0FsGbZxCCBEixR");
// 建立客戶端
RestApiClient restApiClient = new RestApiClient("http://192.168.93.65", 9090, authenticationToken);
// 刪除使用者
boolean deleteStatus = deleteByUserName(restApiClient, "river66");
if(deleteStatus) System.out.println("刪除成功");
else System.out.println("刪除失敗");
}
/**
* Purpose: CRU
* @author riverjin
* @param args
* @return void
*/
public static void main(String[] args) {
// Basic HTTP Authentication
// AuthenticationToken authenticationToken = new AuthenticationToken("admin", "admin");
// Shared secret key
AuthenticationToken authenticationToken = new AuthenticationToken("zN0FsGbZxCCBEixR");
RestApiClient restApiClient = new RestApiClient("http://192.168.93.65", 9090, authenticationToken);
String userName = "river"+r.nextInt(100);
// 建立使用者
boolean crateStatus = createUser(restApiClient, userName, "RiverJin", " [email protected]", "123456");
if(crateStatus) System.out.println("建立成功");
else System.out.println("建立失敗");
// 根據 username 查詢
UserEntity user = getUserByUserName(restApiClient, userName);
if(user != null) {
System.out.println("User UserName :" + user.getUsername());
System.out.println("User Name :" + user.getName());
System.out.println("User Password :" + user.getPassword());
System.out.println("User E-Mail :" + user.getEmail());
}
// 修改使用者資訊
boolean udpateStatus = udpateByUserName(restApiClient, userName, "RiverJinUpdate", " [email protected]", "123456Update");
if(udpateStatus) System.out.println("更新成功");
else System.out.println("更新失敗");
}
/**
* Purpose: 根據使用者名稱稱刪除使用者
* @author riverjin
* @param restApiClient
* @param userName
* @return
* @return boolean
*/
public static boolean deleteByUserName(RestApiClient restApiClient,String userName) {
boolean deleteSuccess = false;
try {
UserEntity userEntity = getUserByUserName(restApiClient, userName);
if(userEntity != null) {
Response response = restApiClient.deleteUser(userName);
if(response.getStatus() == 200) deleteSuccess = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return deleteSuccess;
}
/**
* Purpose: 建立使用者
* @author riverjin
* @param restApiClient
* @param username
* @param name
* @param email
* @param password
* @return boolean
*/
public static boolean createUser(RestApiClient restApiClient,String username, String name, String email, String password) {
boolean createSuccess = false;
try {
UserEntity userEntity = new UserEntity(username, name, email, password);
Response response = restApiClient.createUser(userEntity);
if(response.getStatus() == 201) createSuccess = true;
} catch (Exception e) {
e.printStackTrace();
}
return createSuccess;
}
/**
* Purpose: 修改使用者資訊
* @author riverjin
* @param restApiClient
* @param username
* @param name
* @param email
* @param password
* @return
* @return boolean
*/
public static boolean udpateByUserName(RestApiClient restApiClient,String username, String name, String email, String password) {
boolean updateSuccess = false;
try {
UserEntity userEntity = getUserByUserName(restApiClient, username);
if(userEntity != null) {
userEntity.setName(name);
userEntity.setEmail(email);
userEntity.setPassword(password);
Response response = restApiClient.updateUser(userEntity);
if(response.getStatus() == 200) updateSuccess = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return updateSuccess;
}
/**
* Purpose: 根據使用者名稱稱獲取使用者
* @author riverjin
* @param authenticationToken
* @return
* @return UserEntities
*/
public static UserEntity getUserByUserName(RestApiClient restApiClient,String userName) {
UserEntity userEntity = null;
try {
userEntity = restApiClient.getUser(userName);
} catch (Exception e) {
e.printStackTrace();
}
return userEntity;
}
/**
* Purpose: 獲取所有使用者
* @author riverjin
* @param authenticationToken
* @return
* @return UserEntities
*/
public static UserEntities getUsers(RestApiClient restApiClient) {
UserEntities userEntities = null;
try {
userEntities = restApiClient.getUsers();
} catch (Exception e) {
e.printStackTrace();
}
return userEntities;
}
}
5 : 相關幫助文件 : https://github.com/igniterealtime/REST-API-Client