📄 clearspaceuserprovider.java
字号:
/** * $Revision$ * $Date$ * * Copyright (C) 2008 Jive Software. All rights reserved. * * This software is published under the terms of the GNU Public License (GPL), * a copy of which is included in this distribution, or a commercial license * agreement with Jive. */package org.jivesoftware.openfire.clearspace;import org.dom4j.Document;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.Node;import org.jivesoftware.openfire.XMPPServer;import static org.jivesoftware.openfire.clearspace.ClearspaceManager.HttpType.*;import static org.jivesoftware.openfire.clearspace.WSUtils.getReturn;import static org.jivesoftware.openfire.clearspace.WSUtils.parseStringArray;import org.jivesoftware.openfire.user.*;import org.xmpp.packet.JID;import java.util.*;/** * The ClearspaceUserProvider uses the UserService and ProfileSearchService web service inside of Clearspace * to retrieve user information and to search for users from Clearspace. * * @author Gabriel Guardincerri */public class ClearspaceUserProvider implements UserProvider { // The UserService webservice url prefix protected static final String USER_URL_PREFIX = "userService/"; // The ProfileSearchService webservice url prefix protected static final String SEARCH_URL_PREFIX = "profileSearchService/"; private ClearspaceManager manager; // Used to know it CS is a read only user provider private Boolean readOnly; public ClearspaceUserProvider() { // Gets the manager manager = ClearspaceManager.getInstance(); } /** * Loads the user using the userService/users GET service. Only loads local users. * Throws a UserNotFoundException exception if the user could not be found. * * @param username the username of the user to load * @return a user instance with the user information * @throws UserNotFoundException if the user could not be found */ public User loadUser(String username) throws UserNotFoundException { // Checks if the user is local if (username.contains("@")) { if (!XMPPServer.getInstance().isLocal(new JID(username))) { throw new UserNotFoundException("Cannot load user of remote server: " + username); } username = username.substring(0, username.lastIndexOf("@")); } // Translate the response return translate(getUserByUsername(username)); } /** * Creates user using the userService/users POST service. If Clearspace is a read only * provider throws an UnsupportedOperationException. If there is already a user with * the username throws a UserAlreadyExistsException. * * @param username the username of the user * @param password the password of the user * @param name the name of the user (optional) * @param email the email of the user * @return an instance of the created user * @throws UserAlreadyExistsException If there is already a user with the username * @throws UnsupportedOperationException If Clearspace is a read only provider */ public User createUser(String username, String password, String name, String email) throws UserAlreadyExistsException { if (isReadOnly()) { // Reject the operation since the provider is read-only throw new UnsupportedOperationException(); } try { String path = USER_URL_PREFIX + "users/"; // Creates the XML with the data Document groupDoc = DocumentHelper.createDocument(); Element rootE = groupDoc.addElement("createUserWithUser"); Element userE = rootE.addElement("user"); // adds the username Element usernameE = userE.addElement("username"); usernameE.addText(username); // adds the name if it is not empty if (name != null && !"".equals(name.trim())) { Element nameE = userE.addElement("name"); nameE.addText(name); } // adds the password Element passwordE = userE.addElement("password"); passwordE.addText(password); // adds the the email Element emailE = userE.addElement("email"); emailE.addText(email); // new user are always enabled Element enabledE = userE.addElement("enabled"); enabledE.addText("true"); Element user = manager.executeRequest(POST, path, groupDoc.asXML()); return translate(user); } catch (UserAlreadyExistsException uaee) { throw uaee; } catch (Exception e) { throw new UnsupportedOperationException("Error creating the user", e); } } /** * Creates user using the userService/users DELETE service. If the user is not found returns. * * @param username the username of the user to delete */ public void deleteUser(String username) { if (isReadOnly()) { // Reject the operation since the provider is read-only throw new UnsupportedOperationException(); } try { long userID = manager.getUserID(username); String path = USER_URL_PREFIX + "users/" + userID; manager.executeRequest(DELETE, path); } catch (UserNotFoundException gnfe) { // it is OK, the user doesn't exist "anymore" } catch (Exception e) { // It is not supported exception, wrap it into an UnsupportedOperationException throw new UnsupportedOperationException("Unexpected error", e); } } /** * Gets the user count using the userService/users/count GET service. * * @return the user count */ public int getUserCount() { try { String path = USER_URL_PREFIX + "users/count"; Element element = manager.executeRequest(GET, path); int count = Integer.valueOf(getReturn(element)); return count; } catch (Exception e) { // It is not supported exception, wrap it into an UnsupportedOperationException throw new UnsupportedOperationException("Unexpected error", e); } } /** * Gets all users using the userService/userNames GET service. * * @return a list of all users */ public Collection<User> getUsers() { Collection<String> usernames = getUsernames(); return new UserCollection(usernames.toArray(new String[usernames.size()])); } /** * Gets all usernames using the userService/userNames GET service. * * @return a list of all the usernames */ public Collection<String> getUsernames() { try { String path = USER_URL_PREFIX + "userNames"; Element element = manager.executeRequest(GET, path); return parseStringArray(element); } catch (Exception e) { // It is not supported exception, wrap it into an UnsupportedOperationException throw new UnsupportedOperationException("Unexpected error", e); } } /** * Gets a bounded list of users using the userService/userNames GET service. * * @param startIndex the start index * @param numResults the number of result * @return a bounded list of users */ public Collection<User> getUsers(int startIndex, int numResults) { String[] usernamesAll = getUsernames().toArray(new String[0]); Collection<String> usernames = new ArrayList<String>(); // Filters the user for (int i = startIndex; (i < startIndex + numResults) && (i < usernamesAll.length); i++) { usernames.add(usernamesAll[i]); } return new UserCollection(usernames.toArray(new String[usernames.size()])); } /** * Updates the name of the user using the userService/update service. * * @param username the username of the user * @param name the new name of the user * @throws UserNotFoundException if there is no user with that username */ public void setName(String username, String name) throws UserNotFoundException { if (isReadOnly()) { // Reject the operation since the provider is read-only throw new UnsupportedOperationException(); } // Creates the params Element userUpdateParams = getUserUpdateParams(username); // Modifies the attribute of the user String[] path = new String[]{"user", "name"}; WSUtils.modifyElementText(userUpdateParams, path, name); // Updates the user updateUser(userUpdateParams); } /** * Updates the email of the user using the userService/update service. * * @param username the username of the user * @param email the new email of the user * @throws UserNotFoundException if the user could not be found */ public void setEmail(String username, String email) throws UserNotFoundException { if (isReadOnly()) { // Reject the operation since the provider is read-only throw new UnsupportedOperationException(); } // Creates the params Element userUpdateParams = getUserUpdateParams(username); // Modifies the attribute of the user String[] path = new String[]{"user", "email"}; WSUtils.modifyElementText(userUpdateParams, path, email); // Updates the user updateUser(userUpdateParams); } /** * Updates the creationDate of the user using the userService/update service. * * @param username the username of the user * @param creationDate the new email of the user * @throws UserNotFoundException if the user could not be found */ public void setCreationDate(String username, Date creationDate) throws UserNotFoundException { if (isReadOnly()) { // Reject the operation since the provider is read-only throw new UnsupportedOperationException(); } // Creates the params Element userUpdateParams = getUserUpdateParams(username); // Modifies the attribute of the user String[] path = new String[]{"user", "creationDate"}; String newValue = WSUtils.formatDate(creationDate); WSUtils.modifyElementText(userUpdateParams, path, newValue); // Updates the user updateUser(userUpdateParams); } /** * Updates the modificationDate of the user using the userService/update service. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -