📄 clearspacevcardprovider.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 static org.jivesoftware.openfire.clearspace.ClearspaceManager.HttpType.GET;import static org.jivesoftware.openfire.clearspace.ClearspaceManager.HttpType.POST;import static org.jivesoftware.openfire.clearspace.ClearspaceVCardTranslator.Action.DELETE;import static org.jivesoftware.openfire.clearspace.ClearspaceVCardTranslator.Action.NO_ACTION;import static org.jivesoftware.openfire.clearspace.WSUtils.getReturn;import org.jivesoftware.openfire.user.User;import org.jivesoftware.openfire.user.UserManager;import org.jivesoftware.openfire.user.UserNotFoundException;import org.jivesoftware.openfire.vcard.VCardProvider;import org.jivesoftware.util.AlreadyExistsException;import org.jivesoftware.util.Log;import org.jivesoftware.util.NotFoundException;import java.util.List;/** * The ClearspaceLockOutProvider uses the UserService web service inside of Clearspace * to retrieve, edit and delete user information from Clearspace. With this information the provider * builds user's VCard. * * @author Gabriel Guardincerri */public class ClearspaceVCardProvider implements VCardProvider { protected static final String PROFILE_URL_PREFIX = "profileService/"; protected static final String PROFILE_FIELDS_URL_PREFIX = "profileFieldService/"; protected static final String AVATAR_URL_PREFIX = "avatarService/"; private ClearspaceManager manager; private Boolean avatarReadOnly; private boolean fieldsIDLoaded; public ClearspaceVCardProvider() { this.manager = ClearspaceManager.getInstance(); } /** * Loads the VCard with information from CS. It uses information from the user, the user profile and the avatar. * With this 3 sources of informations it builds the VCard. * * @param username username of user to load VCard of * @return the user's VCard */ public Element loadVCard(String username) { // if the fields id are not loaded if (!fieldsIDLoaded) { synchronized (this) { if (!fieldsIDLoaded) { // try to load them loadDefaultProfileFields(); // if still not loaded then the operation could no be perform if (!fieldsIDLoaded) { // It is not supported exception, wrap it into an UnsupportedOperationException throw new UnsupportedOperationException("Error loading the profiles IDs"); } } } } try { // Gets the user User user = UserManager.getInstance().getUser(username); long userID = manager.getUserID(username); // Gets the profiles information Element profiles = getProfiles(userID); // Gets the avatar information Element avatar = getAvatar(userID); // Translate the response return ClearspaceVCardTranslator.getInstance().translateClearspaceInfo(profiles, user, avatar); } catch (UnsupportedOperationException e) { throw e; } catch (Exception e) { // It is not supported exception, wrap it into an UnsupportedOperationException throw new UnsupportedOperationException("Error loading the vCard", e); } } /** * Creates the user's VCard. CS always has some information of users. So creating it is actually updating. * Throws an UnsupportedOperationException if Clearspace can't save some changes. Returns the VCard after the change. * * @param username the username * @param vCardElement the vCard to save. * @return vCard as it is after the provider has a chance to adjust it. * @throws AlreadyExistsException it's never throw by this implementation * @throws UnsupportedOperationException if the provider does not support the * operation. */ public Element createVCard(String username, Element vCardElement) throws AlreadyExistsException { return saveVCard(username, vCardElement); } /** * Updates the user vcard in Clearspace. Throws an UnsupportedOperationException if Clearspace can't * save some changes. Returns the VCard after the change. * * @param username the username. * @param vCardElement the vCard to save. * @return vCard as it is after the provider has a chance to adjust it. * @throws NotFoundException if the vCard to update does not exist. * @throws UnsupportedOperationException if the provider does not support the * operation. */ public Element updateVCard(String username, Element vCardElement) throws NotFoundException { return saveVCard(username, vCardElement); } /** * Always return false since Clearspace always support some changes. * * @return true */ public boolean isReadOnly() { // Return always false, since some changes are always allowed return false; } /** * Returns true the user can modify the Avatar of Clearspace. * * @return if the Avatar of Clearspace can be modified. */ private boolean isAvatarReadOnly() { if (avatarReadOnly == null) { synchronized (this) { if (avatarReadOnly == null) { loadAvatarReadOnly(); } } } return avatarReadOnly == null ? false : avatarReadOnly; } /** * Saves the vCard of the user. First check if the change can be made, * if not throws an UnsupportedOperationException. * The VCard information is divided into 3 parts. First the preferred * email and the user full name are stored into Clearspace user information. * Second the avatar is stored into Clearspace avatar information. If the avatar was * new or it was modified, a new avatar is created in Clearspace. If the avatar was * deleted, in Clearspace the user won't have an active avatar. * * @param username the username of the user to update the avatar info to * @param vCardElement the vCard with the new information * @return the VCard with the updated information * @throws UnsupportedOperationException if the provider does not support some changes. */ private Element saveVCard(String username, Element vCardElement) { if (Log.isDebugEnabled()) { Log.debug("Saving VCARD: " + vCardElement.asXML()); } if (!fieldsIDLoaded) { synchronized (this) { if (!fieldsIDLoaded) { // try to load them loadDefaultProfileFields(); // if still not loaded then the operation could no be perform if (!fieldsIDLoaded) { // It is not supported exception, wrap it into an UnsupportedOperationException throw new UnsupportedOperationException("Error loading the profiles IDs"); } } } } try { long userID = manager.getUserID(username); ClearspaceUserProvider userProvider = (ClearspaceUserProvider) UserManager.getUserProvider(); // Gets the user params that can be used to update it Element userUpdateParams = userProvider.getUserUpdateParams(username); // Gets the element that contains the user information Element userElement = userUpdateParams.element("user"); // Gets the profiles params that can be used to update them Element profilesUpdateParams = getProfilesUpdateParams(userID); //Element profilesElement = profilesUpdateParams.element("profiles"); // Get the avatar params that can be used to create it. It doesn't have an avatar sub element. Element avatarCreateParams = getAvatarCreateParams(userID); // Modifies the profile, user and avatar elements according to the VCard information. ClearspaceVCardTranslator.Action[] actions; actions = ClearspaceVCardTranslator.getInstance().translateVCard(vCardElement, profilesUpdateParams, userElement, avatarCreateParams); // Throws an exception if the changes implies to modify something that is read only if ((actions[1] != NO_ACTION && userProvider.isReadOnly()) || (actions[2] != NO_ACTION && isAvatarReadOnly())) { throw new UnsupportedOperationException("ClearspaceVCardProvider: Invalid vcard changes."); } // Updates the profiles if (actions[0] != NO_ACTION) { updateProfiles(profilesUpdateParams); } // Updates the user if (actions[1] != NO_ACTION) { userProvider.updateUser(userUpdateParams); } // Updates the avatar if (actions[2] != NO_ACTION) { // Set no active avatar to delete if (actions[2] == DELETE) { setActiveAvatar(userID, -1); } else { // else it was created or updated, on both cases it needs to be created and assigned as the active avatar. long avatarID = createAvatar(avatarCreateParams); setActiveAvatar(userID, avatarID); } } } catch (UnsupportedOperationException e) { throw e; } catch (Exception e) { throw new UnsupportedOperationException("Error saving the VCard", e); } return loadVCard(username); } /** * Deletes the profiles and avatar information of the user. * * @param username the username. */ public void deleteVCard(String username) { ClearspaceUserProvider userProvider = (ClearspaceUserProvider) UserManager.getUserProvider(); if (userProvider.isReadOnly() || isAvatarReadOnly()) { // Reject the operation since the provider is read-only throw new UnsupportedOperationException(); } long userID; try { userID = manager.getUserID(username); } catch (UserNotFoundException gnfe) { // it is OK, the user doesn't exist "anymore" return; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -