Java 通过 oenfire Rest API 操作用户

版权声明:歡迎转載 https://blog.csdn.net/jinjianghai/article/details/79977691

1 : 打开 openfire Server Console , 打开插件部分,找到 Rest API 并安装  


插件 : 


2 : 设置BaseAuto : 


3 : 创建 Java Maven 项目:

Pom.xml 加入 openfire Client 支持 :

    

<dependency>

    <groupId>org.igniterealtime</groupId>

    <artifactId>rest-api-client</artifactId>

    <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", "RiverJin@google.com", "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", "RiverJin@google.comUPdate", "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


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页