use of org.jivesoftware.util.AlreadyExistsException in project Openfire by igniterealtime.
the class VCardManager method setVCard.
/**
* Sets the user's vCard information. The new vCard information will be persistent. Advanced
* user systems can use vCard information to link to user directory information or store
* other relevant user information.
*
* @param username The username of the user to set his new vCard.
* @param vCardElement The DOM element sent by the user as his new vCard.
* @throws Exception if an error occurred while storing the new vCard.
*/
public void setVCard(String username, Element vCardElement) throws Exception {
boolean created = false;
boolean updated = false;
if (provider.isReadOnly()) {
throw new UnsupportedOperationException("VCard provider is read-only.");
}
Element oldVCard = getOrLoadVCard(username);
Element newvCard = null;
// See if we need to update the vCard or insert a new one.
if (oldVCard != null) {
// Only update the vCard in the database if the vCard has changed.
if (!oldVCard.equals(vCardElement)) {
try {
newvCard = provider.updateVCard(username, vCardElement);
vcardCache.put(username, (DefaultElement) newvCard);
updated = true;
} catch (NotFoundException e) {
Log.warn("Tried to update a vCard that does not exist", e);
newvCard = provider.createVCard(username, vCardElement);
vcardCache.put(username, (DefaultElement) newvCard);
created = true;
}
}
} else {
try {
newvCard = provider.createVCard(username, vCardElement);
vcardCache.put(username, (DefaultElement) newvCard);
created = true;
} catch (AlreadyExistsException e) {
Log.warn("Tried to create a vCard when one already exist", e);
newvCard = provider.updateVCard(username, vCardElement);
vcardCache.put(username, (DefaultElement) newvCard);
updated = true;
}
}
// Dispatch vCard events
if (created) {
// Alert listeners that a new vCard has been created
VCardEventDispatcher.dispatchVCardCreated(username, newvCard);
} else if (updated) {
// Alert listeners that a vCard has been updated
VCardEventDispatcher.dispatchVCardUpdated(username, newvCard);
}
}
use of org.jivesoftware.util.AlreadyExistsException in project Openfire by igniterealtime.
the class DefaultVCardProvider method createVCard.
@Override
public Element createVCard(String username, Element vCardElement) throws AlreadyExistsException {
if (loadVCard(username) != null) {
// The user already has a vCard
throw new AlreadyExistsException("Username " + username + " already has a vCard");
}
if (JiveGlobals.getBooleanProperty(PhotoResizer.PROPERTY_RESIZE_ON_CREATE, PhotoResizer.PROPERTY_RESIZE_ON_CREATE_DEFAULT)) {
PhotoResizer.resizeAvatar(vCardElement);
}
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(INSERT_PROPERTY);
pstmt.setString(1, username);
pstmt.setString(2, vCardElement.asXML());
pstmt.executeUpdate();
} catch (SQLException e) {
Log.error("Error creating vCard for username: " + username, e);
} finally {
DbConnectionManager.closeConnection(pstmt, con);
}
return vCardElement;
}
use of org.jivesoftware.util.AlreadyExistsException in project Openfire by igniterealtime.
the class CrowdVCardProvider method loadVCard.
/**
* @see org.jivesoftware.openfire.vcard.DefaultVCardProvider#loadVCard(java.lang.String)
*/
@Override
public Element loadVCard(String username) {
if (LOG.isDebugEnabled()) {
LOG.debug("loadvcard:" + username);
}
if (MUTEX.containsKey(username)) {
// preventing looping
return null;
}
try {
MUTEX.put(username, username);
Element vcard = super.loadVCard(username);
if (vcard == null) {
CrowdUserProvider userProvider = (CrowdUserProvider) UserManager.getUserProvider();
try {
User user = userProvider.getCrowdUser(username);
String str = VCARD_TEMPLATE.replace("@displayname@", user.displayName).replace("@lastname@", user.lastName).replace("@firstname@", user.firstName).replace("@email@", user.email).replace("@nickname@", username);
vcard = SAXReaderUtil.readRootElement(str);
} catch (UserNotFoundException unfe) {
LOG.error("Unable to find user '{}' for loading its vcard", username, unfe);
return null;
} catch (ExecutionException e) {
LOG.error("VCard parsing error", e);
return null;
} catch (InterruptedException e) {
LOG.error("VCard parsing interrupted", e);
Thread.currentThread().interrupt();
return null;
}
if (LOG.isDebugEnabled()) {
LOG.debug(vcard != null ? vcard.asXML() : "vcard is null");
}
// store this new vcard
if (vcard != null) {
try {
createVCard(username, vcard);
} catch (AlreadyExistsException aee) {
LOG.error("Unable to create and store a new vcard for user:" + username + "; one already exists", aee);
}
}
}
return vcard;
} catch (RuntimeException re) {
LOG.error("Failure occured when loading a vcard for user:" + username, re);
throw re;
} finally {
MUTEX.remove(username);
}
}
use of org.jivesoftware.util.AlreadyExistsException in project Openfire by igniterealtime.
the class MultiUserChatManager method createMultiUserChatService.
/**
* Creates a new MUC service and registers it with the manager, and starts up the service.
*
* @param subdomain Subdomain of the MUC service.
* @param description Description of the MUC service (can be null for default description)
* @param isHidden True if the service is hidden from view in services lists.
* @return MultiUserChatService implementation that was just created.
* @throws AlreadyExistsException if the service already exists.
*/
public MultiUserChatServiceImpl createMultiUserChatService(String subdomain, String description, Boolean isHidden) throws AlreadyExistsException {
if (getMultiUserChatServiceID(subdomain) != null)
throw new AlreadyExistsException();
MultiUserChatServiceImpl muc = new MultiUserChatServiceImpl(subdomain, description, isHidden);
insertService(subdomain, description, isHidden);
registerMultiUserChatService(muc);
return muc;
}
use of org.jivesoftware.util.AlreadyExistsException in project Openfire by igniterealtime.
the class MultiUserChatManager method createMultiUserChatService.
/**
* Creates a new MUC service and registers it with the manager (which causes a cluster-wide notification to be sent)
* and starts up the service.
*
* @param subdomain Subdomain of the MUC service.
* @param description Description of the MUC service (can be null for default description)
* @param isHidden True if the service is hidden from view in services lists.
* @return MultiUserChatService implementation that was just created.
* @throws AlreadyExistsException if the service already exists.
*/
@Nonnull
public MultiUserChatServiceImpl createMultiUserChatService(@Nonnull final String subdomain, @Nullable final String description, final boolean isHidden) throws AlreadyExistsException {
if (getMultiUserChatServiceID(subdomain) != null) {
Log.info("Unable to create a service for {} as one already exists.", subdomain);
throw new AlreadyExistsException();
}
Log.info("Creating MUC service '{}'", subdomain);
final MultiUserChatServiceImpl muc = new MultiUserChatServiceImpl(subdomain, description, isHidden);
insertService(subdomain, description, isHidden);
registerMultiUserChatService(muc);
return muc;
}
Aggregations