List of usage examples for org.dom4j Element getTextTrim
String getTextTrim();
From source file:org.jivesoftware.multiplexer.ConnectionWorkerThread.java
License:Open Source License
private boolean compressConnection(XMPPPacketReader reader, StringBuilder openingStream) throws Exception { // Check if we can use stream compression String policyName = JiveGlobals.getXMLProperty("xmpp.server.compression.policy", Connection.CompressionPolicy.disabled.toString()); Connection.CompressionPolicy compressionPolicy = Connection.CompressionPolicy.valueOf(policyName); // Check if stream compression is enabled in the Connection Manager if (Connection.CompressionPolicy.optional == compressionPolicy) { Element compression = features.element("compression"); boolean zlibSupported = false; Iterator it = compression.elementIterator("method"); while (it.hasNext()) { Element method = (Element) it.next(); if ("zlib".equals(method.getTextTrim())) { zlibSupported = true;//from w ww . j av a2 s. c om } } if (zlibSupported) { MXParser xpp = reader.getXPPParser(); // Request Stream Compression connection.deliverRawText( "<compress xmlns='http://jabber.org/protocol/compress'><method>zlib</method></compress>"); // Check if we are good to start compression Element answer = reader.parseDocument().getRootElement(); if ("compressed".equals(answer.getName())) { // Server confirmed that we can use zlib compression connection.startCompression(); Log.debug("CM - Stream compression was successful with " + serverName); // Stream compression was successful so initiate a new stream connection.deliverRawText(openingStream.toString()); // Reset the parser to use stream compression over TLS ZInputStream in = new ZInputStream(connection.getTLSStreamHandler().getInputStream()); in.setFlushMode(JZlib.Z_PARTIAL_FLUSH); xpp.setInput(new InputStreamReader(in, CHARSET)); // Skip the opening stream sent by the server for (int eventType = xpp.getEventType(); eventType != XmlPullParser.START_TAG;) { eventType = xpp.next(); } // Get new stream features features = reader.parseDocument().getRootElement(); return true; } else { Log.debug("CM - Stream compression was rejected by " + serverName); } } else { Log.debug("CM - Stream compression found but zlib method is not supported by" + serverName); } return false; } return true; }
From source file:org.jivesoftware.openfire.clearspace.ClearspaceVCardTranslator.java
License:Open Source License
/** * Updates the values of the profiles with the values of the vCard * * @param profiles the profiles element to update * @param vCardValues the vCard values/*from w w w . ja v a 2 s . c o m*/ * @return the action performed */ private Action updateProfilesValues(Element profiles, Map<VCardField, String> vCardValues) { Action action = Action.NO_ACTION; List<Element> profilesList = profiles.elements("profiles"); // Modify or delete current profiles for (Element profile : profilesList) { int fieldID = Integer.valueOf(profile.elementText("fieldID")); ClearspaceField field = ClearspaceField.valueOf(fieldID); // If the field is unknown, then continue with the next one if (field == null) { continue; } // Gets the field value, it could have "value" of "values" Element value = profile.element("value"); if (value == null) { value = profile.element("values"); // It's OK if the value still null. It could need to be modified } // Modify or delete each field type. If newValue is null it will empty the field. String newValue; String oldValue; switch (field) { case TITLE: if (modifyProfileValue(vCardValues, value, VCardField.TITLE)) { action = Action.MODIFY; } break; case DEPARTMENT: if (modifyProfileValue(vCardValues, value, VCardField.ORG_ORGUNIT)) { action = Action.MODIFY; } break; case ADDRESS: if (modifyProfileValue(vCardValues, value, VCardField.ADR_WORK)) { action = Action.MODIFY; } break; case HOME_ADDRESS: if (modifyProfileValue(vCardValues, value, VCardField.ADR_HOME)) { action = Action.MODIFY; } break; case TIME_ZONE: if (modifyProfileValue(vCardValues, value, VCardField.TZ)) { action = Action.MODIFY; } break; case URL: if (modifyProfileValue(vCardValues, value, VCardField.URL)) { action = Action.MODIFY; } break; case ALT_EMAIL: // Get the new value newValue = vCardValues.get(VCardField.EMAIL_USERID); // Get the old value oldValue = value.getTextTrim(); // Get the mail type, i.e. HOME or WORK String mailType = getFieldType(oldValue); // Adds the mail type to the new value newValue = addFieldType(newValue, mailType); // Now the old and new values can be compared if (!oldValue.equalsIgnoreCase(newValue)) { value.setText(newValue == null ? "" : newValue); action = Action.MODIFY; } // Removes the value from the map to mark that is was used vCardValues.remove(VCardField.EMAIL_USERID); break; case PHONE: // Get all the phones numbers String newHomePhone = vCardValues.get(VCardField.PHONE_HOME); String newWorkPhone = vCardValues.get(VCardField.PHONE_WORK); String newWorkFax = vCardValues.get(VCardField.FAX_WORK); String newWorkMobile = vCardValues.get(VCardField.MOBILE_WORK); String newWorkPager = vCardValues.get(VCardField.PAGER_WORK); newValue = null; oldValue = value.getTextTrim(); String oldType = getFieldType(oldValue); // Modifies the phone field that is of the same type if ("work".equalsIgnoreCase(oldType)) { newValue = addFieldType(newWorkPhone, oldType); } else if ("home".equalsIgnoreCase(oldType)) { newValue = addFieldType(newHomePhone, oldType); } else if ("fax".equalsIgnoreCase(oldType)) { newValue = addFieldType(newWorkFax, oldType); } else if ("mobile".equalsIgnoreCase(oldType)) { newValue = addFieldType(newWorkMobile, oldType); } else if ("pager".equalsIgnoreCase(oldType)) { newValue = addFieldType(newWorkPager, oldType); } else if ("other".equalsIgnoreCase(oldType)) { // No phone to update // Removes the values from the map to mark that is was used vCardValues.remove(VCardField.PHONE_HOME); vCardValues.remove(VCardField.PHONE_WORK); vCardValues.remove(VCardField.FAX_WORK); vCardValues.remove(VCardField.MOBILE_WORK); vCardValues.remove(VCardField.PAGER_WORK); break; } // If newValue and oldValue are different the update the field if (!oldValue.equals(newValue)) { value.setText(newValue == null ? "" : newValue); action = Action.MODIFY; } // Removes the values from the map to mark that is was used vCardValues.remove(VCardField.PHONE_HOME); vCardValues.remove(VCardField.PHONE_WORK); vCardValues.remove(VCardField.FAX_WORK); vCardValues.remove(VCardField.MOBILE_WORK); vCardValues.remove(VCardField.PAGER_WORK); break; } } // Add new profiles that remains in the vCardValues, those are new profiles. if (vCardValues.containsKey(VCardField.TITLE)) { String newValue = vCardValues.get(VCardField.TITLE); if (addProfile(profiles, ClearspaceField.TITLE, newValue)) { action = Action.MODIFY; } } if (vCardValues.containsKey(VCardField.ORG_ORGUNIT)) { String newValue = vCardValues.get(VCardField.ORG_ORGUNIT); if (addProfile(profiles, ClearspaceField.DEPARTMENT, newValue)) { action = Action.MODIFY; } } if (vCardValues.containsKey(VCardField.ADR_WORK)) { String newValue = vCardValues.get(VCardField.ADR_WORK); if (addProfile(profiles, ClearspaceField.ADDRESS, newValue)) { action = Action.MODIFY; } } if (vCardValues.containsKey(VCardField.ADR_HOME)) { String newValue = vCardValues.get(VCardField.ADR_HOME); if (addProfile(profiles, ClearspaceField.HOME_ADDRESS, newValue)) { action = Action.MODIFY; } } if (vCardValues.containsKey(VCardField.TZ)) { String newValue = vCardValues.get(VCardField.TZ); if (addProfile(profiles, ClearspaceField.TIME_ZONE, newValue)) { action = Action.MODIFY; } } if (vCardValues.containsKey(VCardField.URL)) { String newValue = vCardValues.get(VCardField.URL); if (addProfile(profiles, ClearspaceField.URL, newValue)) { action = Action.MODIFY; } } if (vCardValues.containsKey(VCardField.EMAIL_USERID)) { String newValue = vCardValues.get(VCardField.EMAIL_USERID); newValue = addFieldType(newValue, "work"); if (addProfile(profiles, ClearspaceField.ALT_EMAIL, newValue)) { action = Action.MODIFY; } } // Adds just one phone number, the first one. Clearspace doesn't support more than one. if (vCardValues.containsKey(VCardField.PHONE_WORK)) { String newValue = vCardValues.get(VCardField.PHONE_WORK); newValue = addFieldType(newValue, "work"); if (addProfile(profiles, ClearspaceField.PHONE, newValue)) { action = Action.MODIFY; } } else if (vCardValues.containsKey(VCardField.PHONE_HOME)) { String newValue = vCardValues.get(VCardField.PHONE_HOME); newValue = addFieldType(newValue, "home"); if (addProfile(profiles, ClearspaceField.PHONE, newValue)) { action = Action.MODIFY; } } else if (vCardValues.containsKey(VCardField.FAX_WORK)) { String newValue = vCardValues.get(VCardField.FAX_WORK); newValue = addFieldType(newValue, "fax"); if (addProfile(profiles, ClearspaceField.PHONE, newValue)) { action = Action.MODIFY; } } else if (vCardValues.containsKey(VCardField.MOBILE_WORK)) { String newValue = vCardValues.get(VCardField.MOBILE_WORK); newValue = addFieldType(newValue, "mobile"); if (addProfile(profiles, ClearspaceField.PHONE, newValue)) { action = Action.MODIFY; } } else if (vCardValues.containsKey(VCardField.PAGER_WORK)) { String newValue = vCardValues.get(VCardField.PAGER_WORK); newValue = addFieldType(newValue, "pager"); if (addProfile(profiles, ClearspaceField.PHONE, newValue)) { action = Action.MODIFY; } } return action; }
From source file:org.jivesoftware.openfire.clearspace.ClearspaceVCardTranslator.java
License:Open Source License
/** * Modifies the value of a profile if it is different from the original one. * * @param vCardValues the vCard values with the new values * @param value the current value of the profile * @param vCardField the vCard field//from w ww.j a va 2 s.c o m * @return true if the field was modified */ private boolean modifyProfileValue(Map<VCardField, String> vCardValues, Element value, VCardField vCardField) { boolean modified = false; String newValue = vCardValues.get(vCardField); // Modifies or deletes the value if (!value.getTextTrim().equals(newValue)) { value.setText(newValue == null ? "" : newValue); modified = true; } // Remove the vCard value to mark that it was used vCardValues.remove(vCardField); return modified; }
From source file:org.jivesoftware.openfire.container.PluginManager.java
License:Open Source License
/** * Loads a plug-in module into the container. Loading consists of the * following steps:<ul>// ww w . jav a2 s . c o m * <p/> * <li>Add all jars in the <tt>lib</tt> dir (if it exists) to the class loader</li> * <li>Add all files in <tt>classes</tt> dir (if it exists) to the class loader</li> * <li>Locate and load <tt>module.xml</tt> into the context</li> * <li>For each jive.module entry, load the given class as a module and start it</li> * <p/> * </ul> * * @param pluginDir the plugin directory. */ private void loadPlugin(File pluginDir) { // Only load the admin plugin during setup mode. if (XMPPServer.getInstance().isSetupMode() && !(pluginDir.getName().equals("admin"))) { return; } Log.debug("PluginManager: Loading plugin " + pluginDir.getName()); Plugin plugin; try { File pluginConfig = new File(pluginDir, "plugin.xml"); if (pluginConfig.exists()) { SAXReader saxReader = new SAXReader(); saxReader.setEncoding("UTF-8"); Document pluginXML = saxReader.read(pluginConfig); // See if the plugin specifies a version of Openfire // required to run. Element minServerVersion = (Element) pluginXML.selectSingleNode("/plugin/minServerVersion"); if (minServerVersion != null) { Version requiredVersion = new Version(minServerVersion.getTextTrim()); Version currentVersion = XMPPServer.getInstance().getServerInfo().getVersion(); if (requiredVersion.isNewerThan(currentVersion)) { String msg = "Ignoring plugin " + pluginDir.getName() + ": requires " + "server version " + requiredVersion; Log.warn(msg); System.out.println(msg); return; } } PluginClassLoader pluginLoader; // Check to see if this is a child plugin of another plugin. If it is, we // re-use the parent plugin's class loader so that the plugins can interact. Element parentPluginNode = (Element) pluginXML.selectSingleNode("/plugin/parentPlugin"); String pluginName = pluginDir.getName(); String webRootKey = pluginName + ".webRoot"; String classesDirKey = pluginName + ".classes"; String webRoot = System.getProperty(webRootKey); String classesDir = System.getProperty(classesDirKey); if (webRoot != null) { final File compilationClassesDir = new File(pluginDir, "classes"); if (!compilationClassesDir.exists()) { compilationClassesDir.mkdir(); } compilationClassesDir.deleteOnExit(); } if (parentPluginNode != null) { String parentPlugin = parentPluginNode.getTextTrim(); // See if the parent is already loaded. if (plugins.containsKey(parentPlugin)) { pluginLoader = classloaders.get(getPlugin(parentPlugin)); pluginLoader.addDirectory(pluginDir, classesDir != null); } else { // See if the parent plugin exists but just hasn't been loaded yet. // This can only be the case if this plugin name is alphabetically before // the parent. if (pluginName.compareTo(parentPlugin) < 0) { // See if the parent exists. File file = new File(pluginDir.getParentFile(), parentPlugin + ".jar"); if (file.exists()) { // Silently return. The child plugin will get loaded up on the next // plugin load run after the parent. return; } else { file = new File(pluginDir.getParentFile(), parentPlugin + ".war"); if (file.exists()) { // Silently return. The child plugin will get loaded up on the next // plugin load run after the parent. return; } else { String msg = "Ignoring plugin " + pluginName + ": parent plugin " + parentPlugin + " not present."; Log.warn(msg); System.out.println(msg); return; } } } else { String msg = "Ignoring plugin " + pluginName + ": parent plugin " + parentPlugin + " not present."; Log.warn(msg); System.out.println(msg); return; } } } // This is not a child plugin, so create a new class loader. else { pluginLoader = new PluginClassLoader(); pluginLoader.addDirectory(pluginDir, classesDir != null); } // Check to see if development mode is turned on for the plugin. If it is, // configure dev mode. PluginDevEnvironment dev = null; if (webRoot != null || classesDir != null) { dev = new PluginDevEnvironment(); System.out.println("Plugin " + pluginName + " is running in development mode."); Log.info("Plugin " + pluginName + " is running in development mode."); if (webRoot != null) { File webRootDir = new File(webRoot); if (!webRootDir.exists()) { // Ok, let's try it relative from this plugin dir? webRootDir = new File(pluginDir, webRoot); } if (webRootDir.exists()) { dev.setWebRoot(webRootDir); } } if (classesDir != null) { File classes = new File(classesDir); if (!classes.exists()) { // ok, let's try it relative from this plugin dir? classes = new File(pluginDir, classesDir); } if (classes.exists()) { dev.setClassesDir(classes); pluginLoader.addURLFile(classes.getAbsoluteFile().toURI().toURL()); } } } String className = pluginXML.selectSingleNode("/plugin/class").getText().trim(); plugin = (Plugin) pluginLoader.loadClass(className).newInstance(); if (parentPluginNode != null) { String parentPlugin = parentPluginNode.getTextTrim(); // See if the parent is already loaded. if (plugins.containsKey(parentPlugin)) { pluginLoader = classloaders.get(getPlugin(parentPlugin)); classloaders.put(plugin, pluginLoader); } } plugins.put(pluginName, plugin); pluginDirs.put(plugin, pluginDir); // If this is a child plugin, register it as such. if (parentPluginNode != null) { String parentPlugin = parentPluginNode.getTextTrim(); List<String> childrenPlugins = parentPluginMap.get(plugins.get(parentPlugin)); if (childrenPlugins == null) { childrenPlugins = new ArrayList<String>(); parentPluginMap.put(plugins.get(parentPlugin), childrenPlugins); } childrenPlugins.add(pluginName); // Also register child to parent relationship. childPluginMap.put(plugin, parentPlugin); } else { // Only register the class loader in the case of this not being // a child plugin. classloaders.put(plugin, pluginLoader); } // Check the plugin's database schema (if it requires one). if (!DbConnectionManager.getSchemaManager().checkPluginSchema(plugin)) { // The schema was not there and auto-upgrade failed. Log.error(pluginName + " - " + LocaleUtils.getLocalizedString("upgrade.database.failure")); System.out.println( pluginName + " - " + LocaleUtils.getLocalizedString("upgrade.database.failure")); } // Load any JSP's defined by the plugin. File webXML = new File(pluginDir, "web" + File.separator + "WEB-INF" + File.separator + "web.xml"); if (webXML.exists()) { PluginServlet.registerServlets(this, plugin, webXML); } // Load any custom-defined servlets. File customWebXML = new File(pluginDir, "web" + File.separator + "WEB-INF" + File.separator + "web-custom.xml"); if (customWebXML.exists()) { PluginServlet.registerServlets(this, plugin, customWebXML); } if (dev != null) { pluginDevelopment.put(plugin, dev); } // Configure caches of the plugin configureCaches(pluginDir, pluginName); // Init the plugin. ClassLoader oldLoader = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(pluginLoader); plugin.initializePlugin(this, pluginDir); Thread.currentThread().setContextClassLoader(oldLoader); // If there a <adminconsole> section defined, register it. Element adminElement = (Element) pluginXML.selectSingleNode("/plugin/adminconsole"); if (adminElement != null) { Element appName = (Element) adminElement .selectSingleNode("/plugin/adminconsole/global/appname"); if (appName != null) { // Set the plugin name so that the proper i18n String can be loaded. appName.addAttribute("plugin", pluginName); } // If global images are specified, override their URL. Element imageEl = (Element) adminElement .selectSingleNode("/plugin/adminconsole/global/logo-image"); if (imageEl != null) { imageEl.setText("plugins/" + pluginName + "/" + imageEl.getText()); // Set the plugin name so that the proper i18n String can be loaded. imageEl.addAttribute("plugin", pluginName); } imageEl = (Element) adminElement.selectSingleNode("/plugin/adminconsole/global/login-image"); if (imageEl != null) { imageEl.setText("plugins/" + pluginName + "/" + imageEl.getText()); // Set the plugin name so that the proper i18n String can be loaded. imageEl.addAttribute("plugin", pluginName); } // Modify all the URL's in the XML so that they are passed through // the plugin servlet correctly. List urls = adminElement.selectNodes("//@url"); for (Object url : urls) { Attribute attr = (Attribute) url; attr.setValue("plugins/" + pluginName + "/" + attr.getValue()); } // In order to internationalize the names and descriptions in the model, // we add a "plugin" attribute to each tab, sidebar, and item so that // the the renderer knows where to load the i18n Strings from. String[] elementNames = new String[] { "tab", "sidebar", "item" }; for (String elementName : elementNames) { List values = adminElement.selectNodes("//" + elementName); for (Object value : values) { Element element = (Element) value; // Make sure there's a name or description. Otherwise, no need to // override i18n settings. if (element.attribute("name") != null || element.attribute("value") != null) { element.addAttribute("plugin", pluginName); } } } AdminConsole.addModel(pluginName, adminElement); } firePluginCreatedEvent(pluginName, plugin); } else { Log.warn("Plugin " + pluginDir + " could not be loaded: no plugin.xml file found"); } } catch (Throwable e) { Log.error("Error loading plugin: " + pluginDir, e); } }
From source file:org.jivesoftware.openfire.container.PluginManager.java
License:Open Source License
/** * Returns the value of an element selected via an xpath expression from * a Plugin's plugin.xml file.//from w ww. ja v a 2 s.c o m * * @param plugin the plugin. * @param xpath the xpath expression. * @return the value of the element selected by the xpath expression. */ private String getElementValue(Plugin plugin, String xpath) { File pluginDir = pluginDirs.get(plugin); if (pluginDir == null) { return null; } try { File pluginConfig = new File(pluginDir, "plugin.xml"); if (pluginConfig.exists()) { SAXReader saxReader = new SAXReader(); saxReader.setEncoding("UTF-8"); Document pluginXML = saxReader.read(pluginConfig); Element element = (Element) pluginXML.selectSingleNode(xpath); if (element != null) { return element.getTextTrim(); } } } catch (Exception e) { Log.error(e.getMessage(), e); } return null; }
From source file:org.jivesoftware.openfire.container.PluginMetadataHelper.java
License:Apache License
/** * Returns the value of an element selected via an xpath expression from * a Plugin's plugin.xml file.//w w w .ja v a2s. c o m * * @param pluginDir the path of the plugin directory. * @param xpath the xpath expression. * @return the value of the element selected by the xpath expression. */ static String getElementValue(Path pluginDir, String xpath) { if (pluginDir == null) { return null; } try { final Path pluginConfig = pluginDir.resolve("plugin.xml"); if (Files.exists(pluginConfig)) { final SAXReader saxReader = new SAXReader(); saxReader.setEncoding("UTF-8"); final Document pluginXML = saxReader.read(pluginConfig.toFile()); final Element element = (Element) pluginXML.selectSingleNode(xpath); if (element != null) { return element.getTextTrim(); } } } catch (Exception e) { Log.error("Unable to get element value '{}' from plugin.xml of plugin in '{}':", xpath, pluginDir, e); } return null; }
From source file:org.jivesoftware.openfire.fastpath.providers.ChatNotes.java
License:Open Source License
public void executeSet(IQ packet, Workgroup workgroup) { IQ reply;/*from w w w . ja va2s. co m*/ Element iq = packet.getChildElement(); try { // Verify that an agent is requesting this information. AgentSession agentSession = workgroup.getAgentManager().getAgentSession(packet.getFrom()); if (agentSession != null) { String sessionID = iq.element("sessionID").getTextTrim(); Element notes = iq.element("notes"); String noteText = notes.getTextTrim(); appendNote(sessionID, noteText); reply = IQ.createResultIQ(packet); } else { reply = IQ.createResultIQ(packet); reply.setChildElement(packet.getChildElement().createCopy()); reply.setError(new PacketError(PacketError.Condition.item_not_found)); } } catch (AgentNotFoundException e) { reply = IQ.createResultIQ(packet); reply.setChildElement(packet.getChildElement().createCopy()); reply.setError(new PacketError(PacketError.Condition.item_not_found)); } workgroup.send(reply); }
From source file:org.jivesoftware.openfire.forms.spi.XDataFormImpl.java
License:Open Source License
public void parse(Element formElement) { type = formElement.attributeValue("type"); Element titleElement = formElement.element("title"); if (titleElement != null) { setTitle(titleElement.getTextTrim()); }//from www .j a v a 2 s. c om Iterator instructionElements = formElement.elementIterator("instructions"); while (instructionElements.hasNext()) { addInstruction(((Element) instructionElements.next()).getTextTrim()); } Iterator fieldElements = formElement.elementIterator("field"); while (fieldElements.hasNext()) { XFormFieldImpl field = new XFormFieldImpl(); field.parse((Element) fieldElements.next()); addField(field); } Element reportedElement = formElement.element("reported"); if (reportedElement != null) { Iterator reportedFieldElements = reportedElement.elementIterator("field"); while (reportedFieldElements.hasNext()) { XFormFieldImpl field = new XFormFieldImpl(); field.parse((Element) reportedFieldElements.next()); addReportedField(field); } } Iterator itemElements = formElement.elementIterator("item"); while (itemElements.hasNext()) { Element itemElement = (Element) itemElements.next(); Iterator itemFieldElements = itemElement.elementIterator("field"); ArrayList itemFields = new ArrayList(); while (itemFieldElements.hasNext()) { XFormFieldImpl field = new XFormFieldImpl(); field.parse((Element) itemFieldElements.next()); itemFields.add(field); } addItemFields(itemFields); } }
From source file:org.jivesoftware.openfire.forms.spi.XFormFieldImpl.java
License:Open Source License
public void parse(Element formElement) { variable = formElement.attributeValue("var"); setLabel(formElement.attributeValue("label")); setType(formElement.attributeValue("type")); Element descElement = formElement.element("desc"); if (descElement != null) { setDescription(descElement.getTextTrim()); }/* ww w . j a v a 2s. c o m*/ if (formElement.element("required") != null) { setRequired(true); } Iterator valueElements = formElement.elementIterator("value"); while (valueElements.hasNext()) { addValue(((Element) valueElements.next()).getTextTrim()); } Iterator optionElements = formElement.elementIterator("option"); Element optionElement; while (optionElements.hasNext()) { optionElement = (Element) optionElements.next(); addOption(optionElement.attributeValue("label"), optionElement.elementTextTrim("value")); } }
From source file:org.jivesoftware.openfire.ldap.LdapVCardProvider.java
License:Open Source License
/** * Returns true or false if the change to the existing vcard is valid (only to PHOTO element) * * @param username User who's LDAP-based vcard we will compare with. * @param newvCard New vCard Element we will compare against. * @return True or false if the changes made were valid (only to PHOTO element) *///from ww w . java 2s . c o m private Boolean isValidVCardChange(String username, Element newvCard) { if (newvCard == null) { // Well if there's nothing to change, of course it's valid. Log.debug("LdapVCardProvider: No new vcard provided (no changes), accepting."); return true; } // Un-escape username. username = JID.unescapeNode(username); Map<String, String> map = getLdapAttributes(username); // Retrieve LDAP created vcard for comparison Element ldapvCard = new VCard(template).getVCard(map); if (ldapvCard == null) { // This person has no vcard at all, may not change it! Log.debug("LdapVCardProvider: User has no LDAP vcard, nothing they can change, rejecting."); return false; } // If the LDAP vcard has a non-empty PHOTO element set, then there is literally no way this will be accepted. Element ldapPhotoElem = ldapvCard.element("PHOTO"); if (ldapPhotoElem != null) { Element ldapBinvalElem = ldapPhotoElem.element("BINVAL"); if (ldapBinvalElem != null && !ldapBinvalElem.getTextTrim().matches("\\s*")) { // LDAP is providing a valid PHOTO element, byebye! Log.debug("LdapVCardProvider: LDAP has a PHOTO element set, no way to override, rejecting."); return false; } } // Retrieve database vcard, if it exists Element dbvCard = defaultProvider.loadVCard(username); if (dbvCard != null) { Element dbPhotoElem = dbvCard.element("PHOTO"); if (dbPhotoElem == null) { // DB has no photo, lets accept what we got. Log.debug("LdapVCardProvider: Database has no PHOTO element, accepting update."); return true; } else { Element newPhotoElem = newvCard.element("PHOTO"); if (newPhotoElem == null) { Log.debug("LdapVCardProvider: Photo element was removed, accepting update."); return true; } // Note: NodeComparator never seems to consider these equal, even if they are? if (!dbPhotoElem.asXML().equals(newPhotoElem.asXML())) { // Photo element was changed. Ignore all other changes and accept this. Log.debug("LdapVCardProvider: PHOTO element changed, accepting update."); return true; } } } else { // No vcard exists in database Log.debug("LdapVCardProvider: Database has no vCard stored, accepting update."); return true; } // Ok, either something bad changed or nothing changed. Either way, user either: // 1. should not have tried to change something 'readonly' // 2. shouldn't have bothered submitting no changes // So we'll consider this a bad return. Log.debug("LdapVCardProvider: PHOTO element didn't change, no reason to accept this, rejecting."); return false; }