📄 searchplugin.java
字号:
"advance.user.search." + fieldName.toLowerCase(), "search")); searchResults.addReportedField(field); } for (User user : users) { String username = JID.unescapeNode(user.getUsername()); ArrayList<XFormFieldImpl> items = new ArrayList<XFormFieldImpl>(); XFormFieldImpl fieldJID = new XFormFieldImpl("jid"); fieldJID.addValue(username + "@" + serverName); items.add(fieldJID); XFormFieldImpl fieldUsername = new XFormFieldImpl(LocaleUtils.getLocalizedString("advance.user.search.username", "search")); fieldUsername.addValue(username); items.add(fieldUsername); XFormFieldImpl fieldName = new XFormFieldImpl(LocaleUtils.getLocalizedString("advance.user.search.name", "search")); fieldName.addValue((user.isNameVisible() ? removeNull(user.getName()) : "")); items.add(fieldName); XFormFieldImpl fieldEmail = new XFormFieldImpl(LocaleUtils.getLocalizedString("advance.user.search.email", "search")); fieldEmail.addValue((user.isEmailVisible() ? removeNull(user.getEmail()) : "")); items.add(fieldEmail); searchResults.addItemFields(items); } IQ replyPacket = IQ.createResultIQ(packet); Element reply = replyPacket.setChildElement("query", NAMESPACE_JABBER_IQ_SEARCH); reply.add(searchResults.asXMLElement()); return replyPacket; } /** * Constructs a query that is returned as an IQ packet that contains the search results. * * @param users set of users that will be used to construct the search results * @param packet the IQ packet sent by the client * @return the iq packet that contains the search results */ private IQ replyNonDataFormResult(Collection<User> users, IQ packet) { IQ replyPacket = IQ.createResultIQ(packet); Element replyQuery = replyPacket.setChildElement("query", NAMESPACE_JABBER_IQ_SEARCH); for (User user : users) { Element item = replyQuery.addElement("item"); String username = JID.unescapeNode(user.getUsername()); item.addAttribute("jid", username + "@" + serverName); // return to the client the same fields that were submitted for (String field : reverseFieldLookup.keySet()) { if ("Username".equals(field)) { Element element = item.addElement(reverseFieldLookup .get(field)); element.addText(username); } if ("Name".equals(field)) { Element element = item.addElement(reverseFieldLookup .get(field)); element.addText(user.isNameVisible() ? removeNull(user.getName()) : ""); } if ("Email".equals(field)) { Element element = item.addElement(reverseFieldLookup .get(field)); element.addText(user.isEmailVisible() ? removeNull(user.getEmail()) : ""); } } } return replyPacket; } /** * Returns the service name of this component, which is "search" by default. * * @return the service name of this component. */ public String getServiceName() { return serviceName; } /** * Sets the service name of this component, which is "search" by default. If the name * is different than the existing name the plugin will remove itself from the ComponentManager * and then add itself back using the new name. * * @param name the service name of this component. */ public void setServiceName(String name) { changeServiceName(name); JiveGlobals.setProperty(SERVICENAME, name); } /** * Checks if the search service is enabled. * * @return true if search service is enabled. */ public boolean getServiceEnabled() { return serviceEnabled; } /** * Enables or disables the search service. When disabled, when a client tries * to do a search they will receive an XForm informing that the service is * unavailable. * * @param enabled true if group permission checking should be disabled. */ public void setServiceEnabled(boolean enabled) { serviceEnabled = enabled; JiveGlobals.setProperty(SERVICEENABLED, enabled ? "true" : "false"); } /** * Returns the collection of searchable field names that does not include the fields * listed in the EXCLUDEDFIELDS property list. * * @return collection of searchable field names. */ public Collection<String> getFilteredSearchFields() { Collection<String> searchFields; // See if the installed provider supports searching. If not, workaround // by providing our own searching. try { searchFields = new ArrayList<String>(userManager.getSearchFields()); } catch (UnsupportedOperationException uoe) { // Use a SearchPluginUserManager instead. searchFields = getSearchPluginUserManagerSearchFields(); } searchFields.removeAll(exculudedFields); return searchFields; } /** * Restricts which fields can be searched on and shown to clients. This can be used * in the case of preventing users email addresses from being revealed as part of * the search results. * * @param exculudedFields fields that can not be searched on or shown to the client */ public void setExcludedFields(Collection<String> exculudedFields) { this.exculudedFields = exculudedFields; JiveGlobals.setProperty(EXCLUDEDFIELDS, StringUtils.collectionToString(exculudedFields)); } /* * (non-Javadoc) * * @see org.jivesoftware.util.PropertyEventListener#propertySet(java.lang.String, * java.util.Map) */ public void propertySet(String property, Map<String, Object> params) { if (property.equals(SERVICEENABLED)) { this.serviceEnabled = Boolean.parseBoolean((String) params.get("value")); } else if (property.equals(SERVICENAME)) { changeServiceName((String) params.get("value")); } else if (property.equals(EXCLUDEDFIELDS)) { exculudedFields = StringUtils.stringToCollection(JiveGlobals.getProperty(EXCLUDEDFIELDS, (String) params.get("value"))); } } /* * (non-Javadoc) * * @see org.jivesoftware.util.PropertyEventListener#propertyDeleted(java.lang.String, * java.util.Map) */ public void propertyDeleted(String property, Map<String, Object> params) { if (property.equals(SERVICEENABLED)) { this.serviceEnabled = true; } else if (property.equals(SERVICENAME)) { changeServiceName("search"); } else if (property.equals(EXCLUDEDFIELDS)) { exculudedFields = new ArrayList<String>(); } } /* * (non-Javadoc) * * @see org.jivesoftware.util.PropertyEventListener#xmlPropertySet(java.lang.String, * java.util.Map) */ public void xmlPropertySet(String property, Map<String, Object> params) { // not used } /* * (non-Javadoc) * * @see org.jivesoftware.util.PropertyEventListener#xmlPropertyDeleted(java.lang.String, * java.util.Map) */ public void xmlPropertyDeleted(String property, Map<String, Object> params) { // not used } private void changeServiceName(String serviceName) { if (serviceName == null) { throw new NullPointerException("Service name cannot be null"); } if (this.serviceName.equals(serviceName)) { return; } // Re-register the service. try { componentManager.removeComponent(this.serviceName); } catch (Exception e) { componentManager.getLog().error(e); } try { componentManager.addComponent(serviceName, this); } catch (Exception e) { componentManager.getLog().error(e); } this.serviceName = serviceName; } /** * Comparator that compares String objects, ignoring capitalization. */ private class CaseInsensitiveComparator implements Comparator<String> { public int compare(String s1, String s2) { return s1.compareToIgnoreCase(s2); } } /** * Returns the trimmed argument, or an empty String object of null was * supplied as an argument. * * @param s The String to be trimmed. * @return String object that does not start or end with whitespace * characters. */ private String removeNull(String s) { if (s == null) { return ""; } return s.trim(); } /** * Returns the collection of field names that can be used to search for a * user. Typical fields are username, name, and email. These values can be * used to contruct a data form. * * @return the collection of field names that can be used to search. */ public Collection<String> getSearchPluginUserManagerSearchFields() { return Arrays.asList("Username", "Name", "Email"); } /** * Finds a user using the specified field and query string. For example, a * field name of "email" and query of "jsmith@example.com" would search for * the user with that email address. Wildcard (*) characters are allowed as * part of queries. * <p/> * A possible future improvement would be to have a third parameter that * sets the maximum number of users returned and/or the number of users * that are searched. * * @param field Field we will be searching on. * @param query Comparison to make on specified field. * @return Collection of User's matching query. */ public Collection<User> findUsers(String field, String query) { List<User> foundUsers = new ArrayList<User>(); if (!getSearchPluginUserManagerSearchFields().contains(field)) { return foundUsers; } int index = query.indexOf("*"); if (index == -1) { Collection<User> users = userManager.getUsers(); for (User user : users) { if (field.equals("Username")) { try { foundUsers.add(userManager.getUser(query)); return foundUsers; } catch (UserNotFoundException e) { Log.error("Error getting user", e); } } else if (field.equals("Name")) { if (user.isNameVisible()) { if (query.equalsIgnoreCase(user.getName())) { foundUsers.add(user); } } } else if (field.equals("Email")) { if (user.isEmailVisible() && user.getEmail() != null) { if (query.equalsIgnoreCase(user.getEmail())) { foundUsers.add(user); } } } } } else { String prefix = query.substring(0, index); Collection<User> users = userManager.getUsers(); for (User user : users) { String userInfo = ""; if (field.equals("Username")) { userInfo = user.getUsername(); } else if (field.equals("Name")) { userInfo = user.getName(); } else if (field.equals("Email")) { userInfo = user.getEmail() == null ? "" : user.getEmail(); } if (index < userInfo.length()) { if (userInfo.substring(0, index).equalsIgnoreCase(prefix)) { foundUsers.add(user); } } } } return foundUsers; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -