CrowdVCardProvider.java 5.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*
 * Copyright (C) 2012 Issa Gorissen <issa-gorissen@usa.net>. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jivesoftware.openfire.crowd;

import java.io.StringReader;
import java.util.concurrent.ConcurrentHashMap;

import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jivesoftware.openfire.crowd.jaxb.User;
import org.jivesoftware.openfire.user.UserManager;
import org.jivesoftware.openfire.user.UserNotFoundException;
import org.jivesoftware.openfire.vcard.DefaultVCardProvider;
import org.jivesoftware.util.AlreadyExistsException;
import org.jivesoftware.util.NotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * VCard provider for Crowd.
 * 
 * <p>The name, email will be provided by crowd. All other information
 * can be managed by the XMPP client
 */
public class CrowdVCardProvider extends DefaultVCardProvider {
	private static final Logger LOG = LoggerFactory.getLogger(CrowdVCardProvider.class);
	
	private static final String VCARD_TEMPLATE
		= "<vCard xmlns=\"vcard-temp\"><FN>@displayname@</FN><N><FAMILY>@lastname@</FAMILY><GIVEN>@firstname@</GIVEN></N><NICKNAME>@nickname@</NICKNAME><EMAIL><USERID>@email@</USERID></EMAIL></vCard>";
44
	private static final ConcurrentHashMap<String, Object> MUTEX = new ConcurrentHashMap<>();
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

	/**
	 * @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);
					
					SAXReader xmlReader = new SAXReader();
					xmlReader.setEncoding("UTF-8");
					
					vcard = xmlReader.read(new StringReader(str)).getRootElement();
					
				} catch (UserNotFoundException unfe) {
					LOG.error("Unable to find user:" + String.valueOf(username) + " for loading its vcard", unfe);
					return null;
				} catch (DocumentException de) {
					LOG.error("vcard parsing error", de);
					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);
		}
	}

	/**
	 * @see org.jivesoftware.openfire.vcard.DefaultVCardProvider#createVCard(java.lang.String, org.dom4j.Element)
	 */
	@Override
	public Element createVCard(String username, Element vCardElement) throws AlreadyExistsException {
		if (LOG.isDebugEnabled()) {
			LOG.debug("createvcard:" + vCardElement.asXML());
		}
		return super.createVCard(username, vCardElement);
	}

	/**
	 * @see org.jivesoftware.openfire.vcard.DefaultVCardProvider#updateVCard(java.lang.String, org.dom4j.Element)
	 */
	@Override
	public Element updateVCard(String username, Element vCard) throws NotFoundException {
		// make sure some properties have not been overridden
		Element nickNameNode = vCard.element("NICKNAME");
		Element displayNameNode = vCard.element("FN");
		
		Element nameNode = vCard.element("N");
		Element lastNameNode = nameNode.element("FAMILY");
		Element firstnameNode = nameNode.element("GIVEN");
		
		Element emailNode = vCard.element("EMAIL").element("USERID");
		
		CrowdUserProvider userProvider = (CrowdUserProvider) UserManager.getUserProvider();
		try {
			User user = userProvider.getCrowdUser(username);
			
			nickNameNode.setText(username);
			displayNameNode.setText(user.displayName);
			lastNameNode.setText(user.lastName);
			firstnameNode.setText(user.firstName);
			emailNode.setText(user.email);
			
		} catch (UserNotFoundException unfe) {
			LOG.error("Unable to find user:" + String.valueOf(username) + " for updating its vcard", unfe);
		}

		if (LOG.isDebugEnabled()) {
			LOG.debug("updatevcard:" + vCard.asXML());
		}

		return super.updateVCard(username, vCard);
	}
	
}