📄 ldapvcardprovider.java
字号:
/** * $Revision: 1217 $ * $Date: 2005-04-11 14:11:06 -0700 (Mon, 11 Apr 2005) $ * * 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.ldap;import org.dom4j.Document;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.Node;import org.jivesoftware.util.*;import org.jivesoftware.openfire.vcard.VCardManager;import org.jivesoftware.openfire.vcard.VCardProvider;import org.jivesoftware.openfire.vcard.DefaultVCardProvider;import org.xmpp.packet.JID;import javax.naming.directory.Attributes;import javax.naming.directory.DirContext;import java.util.*;/** * Read-only LDAP provider for vCards.Configuration consists of adding a provider:<p/> * * <pre> * <provider> * <vcard> * <className>org.jivesoftware.openfire.ldap.LdapVCardProvider</className> * </vcard> * </provider> * </pre><p/> * * and an xml vcard-mapping to openfire.xml.<p/> * * The vcard attributes can be configured by adding an <code>attrs="attr1,attr2"</code> * attribute to the vcard elements.<p/> * * Arbitrary text can be used for the element values as well as <code>MessageFormat</code> * style placeholders for the ldap attributes. For example, if you wanted to map the LDAP * attribute <code>displayName</code> to the vcard element <code>FN</code>, the xml * nippet would be:<br><pre><FN attrs="displayName">{0}</FN></pre><p/> * * The vCard XML must be escaped in CDATA and must also be well formed. It is the exact * XML this provider will send to a client after after stripping <code>attr</code> attributes * and populating the placeholders with the data retrieved from LDAP. This system should * be flexible enough to handle any client's vCard format. An example mapping follows.<br> * <pre> * <ldap> * <vcard-mapping> * <![CDATA[ * <vCard xmlns='vcard-temp'> * <FN attrs="displayName">{0}</FN> * <NICKNAME attrs="uid">{0}</NICKNAME> * <BDAY attrs="dob">{0}</BDAY> * <ADR> * <HOME/> * <EXTADR>Ste 500</EXTADR> * <STREET>317 SW Alder St</STREET> * <LOCALITY>Portland</LOCALITY> * <REGION>Oregon</REGION> * <PCODE>97204</PCODE> * <CTRY>USA</CTRY> * </ADR> * <TEL> * <HOME/> * <VOICE/> * <NUMBER attrs="telephoneNumber">{0}</NUMBER> * </TEL> * <EMAIL> * <INTERNET/> * <USERID attrs="mail">{0}</USERID> * </EMAIL> * <TITLE attrs="title">{0}</TITLE> * <ROLE attrs="">{0}</ROLE> * <ORG> * <ORGNAME attrs="o">{0}</ORGNAME> * <ORGUNIT attrs="">{0}</ORGUNIT> * </ORG> * <URL attrs="labeledURI">{0}</URL> * <DESC attrs="uidNumber,homeDirectory,loginShell"> * uid: {0} home: {1} shell: {2} * </DESC> * </vCard> * ]]> * </vcard-mapping> * </ldap> * </pre><p> * <p/> * An easy way to get the vcard format your client needs, assuming you've been * using the database store, is to do a <code>SELECT value FROM jivevcard WHERE * username='some_user'</code> in your favorite sql querier and paste the result * into the <code>vcard-mapping</code> (don't forget the CDATA). * * @author rkelly */public class LdapVCardProvider implements VCardProvider, PropertyEventListener { private LdapManager manager; private VCardTemplate template; private Boolean dbStorageEnabled = false; /** * The default vCard provider is used to handle the vCard in the database. vCard * fields that can be overriden are stored in the database. * * This is used/created only if we are storing avatars in the database. */ private DefaultVCardProvider defaultProvider = null; public LdapVCardProvider() { manager = LdapManager.getInstance(); initTemplate(); // Listen to property events so that the template is always up to date PropertyEventDispatcher.addListener(this); // DB vcard provider used for loading properties overwritten in the DB defaultProvider = new DefaultVCardProvider(); // Check of avatars can be overwritten (and stored in the database) dbStorageEnabled = JiveGlobals.getBooleanProperty("ldap.override.avatar", false); } /** * Initializes the VCard template as set by the administrator. */ private void initTemplate() { String property = JiveGlobals.getXMLProperty("ldap.vcard-mapping"); Log.debug("LdapVCardProvider: Found vcard mapping: '" + property); try { // Remove CDATA wrapping element if (property.startsWith("<![CDATA[")) { property = property.substring(9, property.length()-3); } Document document = DocumentHelper.parseText(property); template = new VCardTemplate(document); } catch (Exception e) { Log.error("Error loading vcard mapping: " + e.getMessage()); } Log.debug("LdapVCardProvider: attributes size==" + template.getAttributes().length); } /** * Creates a mapping of requested LDAP attributes to their values for the given user. * * @param username User we are looking up in LDAP. * @return Map of LDAP attribute to setting. */ private Map<String, String> getLdapAttributes(String username) { // Un-escape username username = JID.unescapeNode(username); Map<String, String> map = new HashMap<String, String>(); DirContext ctx = null; try { String userDN = manager.findUserDN(username); ctx = manager.getContext(manager.getUsersBaseDN(username)); Attributes attrs = ctx.getAttributes(userDN, template.getAttributes()); for (String attribute : template.getAttributes()) { javax.naming.directory.Attribute attr = attrs.get(attribute); String value; if (attr == null) { Log.debug("LdapVCardProvider: No ldap value found for attribute '" + attribute + "'"); value = ""; } else { Object ob = attrs.get(attribute).get(); Log.debug("LdapVCardProvider: Found attribute "+attribute+" of type: "+ob.getClass()); if(ob instanceof String) { value = (String)ob; } else { value = Base64.encodeBytes((byte[])ob); } } Log.debug("LdapVCardProvider: Ldap attribute '" + attribute + "'=>'" + value + "'"); map.put(attribute, value); } return map; } catch (Exception e) { Log.error(e); return Collections.emptyMap(); } finally { try { if (ctx != null) { ctx.close(); } } catch (Exception e) { // Ignore. } } } /** * Loads the avatar from LDAP, based off the vcard template. * * If enabled, will replace a blank PHOTO element with one from a DB stored vcard. * * @param username User we are loading the vcard for. * @return The loaded vcard element, or null if none found. */ public Element loadVCard(String username) { // Un-escape username. username = JID.unescapeNode(username); Map<String, String> map = getLdapAttributes(username); Log.debug("LdapVCardProvider: Getting mapped vcard for " + username); Element vcard = new VCard(template).getVCard(map); // If we have a vcard from ldap, but it doesn't have an avatar filled in, then we // may fill it with a locally stored vcard element. if (dbStorageEnabled && vcard != null && (vcard.element("PHOTO") == null || vcard.element("PHOTO").element("BINVAL") == null || vcard.element("PHOTO").element("BINVAL").getText().matches("\\s*"))) { Element avatarElement = loadAvatarFromDatabase(username); if (avatarElement != null) { Log.debug("LdapVCardProvider: Adding avatar element from local storage"); Element currentElement = vcard.element("PHOTO"); if (currentElement != null) { vcard.remove(currentElement); } vcard.add(avatarElement); } } Log.debug("LdapVCardProvider: Returning vcard"); return vcard; } /** * Returns a merged LDAP vCard combined with a PHOTO element provided in specified vCard. * * @param username User whose vCard this is. * @param mergeVCard vCard element that we are merging PHOTO element from into the LDAP vCard. * @return vCard element after merging in PHOTO element to LDAP data. */ private Element getMergedVCard(String username, Element mergeVCard) { // Un-escape username. username = JID.unescapeNode(username); Map<String, String> map = getLdapAttributes(username); Log.debug("LdapVCardProvider: Retrieving LDAP mapped vcard for " + username); Element vcard = new VCard(template).getVCard(map); if (mergeVCard == null) { // No vcard passed in? Hrm. Fine, return LDAP vcard. return vcard; } if (mergeVCard.element("PHOTO") == null) { // Merged vcard has no photo element, return LDAP vcard as is. return vcard; } Element photoElement = mergeVCard.element("PHOTO").createCopy(); if (photoElement == null || photoElement.element("BINVAL") == null || photoElement.element("BINVAL").getText().matches("\\s*")) { // We were passed something null or empty, so lets just return the LDAP based vcard. return vcard; } // Now we need to check that the LDAP vcard doesn't have a PHOTO element that's filled in. if (!((vcard.element("PHOTO") == null || vcard.element("PHOTO").element("BINVAL") == null || vcard.element("PHOTO").element("BINVAL").getText().matches("\\s*")))) { // Hrm, it does, return the original vcard; return vcard; } Log.debug("LdapVCardProvider: Merging avatar element from passed vcard"); Element currentElement = vcard.element("PHOTO");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -