⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clearspaceuserprovider.java

📁 openfire 服务器源码下载
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * @param username         the username of the user     * @param modificationDate the new modificationDate of the user     * @throws UserNotFoundException if the user could not be found     */    public void setModificationDate(String username, Date modificationDate) throws UserNotFoundException {        if (isReadOnly()) {            // Reject the operation since the provider is read-only            throw new UnsupportedOperationException();        }        // Creates the params        Element userUpdateParams = getUserUpdateParams(username);        // Modifies the attribute of the user        String[] path = new String[]{"user", "modificationDate"};        String newValue = WSUtils.formatDate(modificationDate);        WSUtils.modifyElementText(userUpdateParams, path, newValue);        // Updates the user        updateUser(userUpdateParams);    }    /**     * Creates the parameters to send in a update user request based on the information of <code>username</code>     *     * @param username the username of the user     * @return the parameters to send in a update user request     * @throws UserNotFoundException if the user could not be found     */    protected Element getUserUpdateParams(String username) throws UserNotFoundException {        // Creates the user update params element        Element userUpdateParams = DocumentHelper.createDocument().addElement("updateUser");        Element newUser = userUpdateParams.addElement("user");        // Gets the current user information        Element currentUser = getUserByUsername(username).element("return");        List<Element> userAttributes = currentUser.elements();        for (Element userAttribute : userAttributes) {            newUser.addElement(userAttribute.getName()).setText(userAttribute.getText());        }        return userUpdateParams;    }    /**     * Updates the user using the userService/users PUT service.     *     * @param userUpdateParams the request parameters     * @throws UserNotFoundException if the user could not be found     */    protected void updateUser(Element userUpdateParams) throws UserNotFoundException {        try {            String path = USER_URL_PREFIX + "users";            manager.executeRequest(PUT, path, userUpdateParams.asXML());        } catch (UserNotFoundException e) {            throw new UserNotFoundException("User not found.");        } catch (Exception e) {            // It is not supported exception, wrap it into an UnsupportedOperationException            throw new UnsupportedOperationException("Unexpected error", e);        }    }    /**     * Clearsapce can search using three fields: username, name and email.     *     * @return a list of username, name and email     * @throws UnsupportedOperationException     */    public Set<String> getSearchFields() throws UnsupportedOperationException {        return new LinkedHashSet<String>(Arrays.asList("Username", "Name", "Email"));    }    /**     * Search for the user using the userService/search POST method.     *     * @param fields the fields to search on.     * @param query  the query string.     * @return a Collection of users that match the search.     * @throws UnsupportedOperationException if the provider does not     *                                       support the operation (this is an optional operation).     */    public Collection<User> findUsers(Set<String> fields, String query) throws UnsupportedOperationException {        // Creates the XML with the data        Element paramsE = DocumentHelper.createDocument().addElement("search");        Element queryE = paramsE.addElement("query");        queryE.addElement("keywords").addText(query);        queryE.addElement("searchUsername").addText("true");        queryE.addElement("searchName").addText("true");        queryE.addElement("searchEmail").addText("true");        queryE.addElement("searchProfile").addText("false");        try {            List<String> usernames = new ArrayList<String>();            //TODO create a service on CS to get only the username field            String path = SEARCH_URL_PREFIX + "searchProfile";            Element element = manager.executeRequest(POST, path, paramsE.asXML());            List<Node> userNodes = (List<Node>) element.selectNodes("return");            for (Node userNode : userNodes) {                String username = userNode.selectSingleNode("username").getText();                usernames.add(username);            }            return new UserCollection(usernames.toArray(new String[usernames.size()]));        } catch (Exception e) {            // It is not supported exception, wrap it into an UnsupportedOperationException            throw new UnsupportedOperationException("Unexpected error", e);        }    }    /**     * Search for the user using the userService/searchBounded POST method.     *     * @param fields     the fields to search on.     * @param query      the query string.     * @param startIndex the starting index in the search result to return.     * @param numResults the number of users to return in the search result.     * @return a Collection of users that match the search.     * @throws UnsupportedOperationException if the provider does not     *                                       support the operation (this is an optional operation).     */    public Collection<User> findUsers(Set<String> fields, String query, int startIndex, int numResults) throws UnsupportedOperationException {        // Creates the XML with the data        Element paramsE = DocumentHelper.createDocument().addElement("searchBounded");        Element queryE = paramsE.addElement("query");        queryE.addElement("keywords").addText(query);        queryE.addElement("searchUsername").addText("true");        queryE.addElement("searchName").addText("true");        queryE.addElement("searchEmail").addText("true");        queryE.addElement("searchProfile").addText("false");        paramsE.addElement("startIndex").addText(String.valueOf(startIndex));        paramsE.addElement("numResults").addText(String.valueOf(numResults));        try {            List<String> usernames = new ArrayList<String>();            //TODO create a service on CS to get only the username field            String path = SEARCH_URL_PREFIX + "searchProfile";            Element element = manager.executeRequest(POST, path, paramsE.asXML());            List<Node> userNodes = (List<Node>) element.selectNodes("return");            for (Node userNode : userNodes) {                String username = userNode.selectSingleNode("username").getText();                usernames.add(username);            }            return new UserCollection(usernames.toArray(new String[usernames.size()]));        } catch (Exception e) {            // It is not supported exception, wrap it into an UnsupportedOperationException            throw new UnsupportedOperationException("Unexpected error", e);        }    }    /**     * Returns true if Clearspace is a read only user provider.     *     * @return true if Clearspace is a read only user provider     */    public boolean isReadOnly() {        if (readOnly == null) {            synchronized (this) {                if (readOnly == null) {                    loadReadOnly();                }            }        }        // If it is null returns the most restrictive answer.        return (readOnly == null ? false : readOnly);    }    /**     * In Clearspace name is optional.     *     * @return false     */    public boolean isNameRequired() {        return false;    }    /**     * In Clearspace email is required     *     * @return true     */    public boolean isEmailRequired() {        return true;    }    /**     * Tries to load the read only attribute using the userService/isReadOnly service.     */    private void loadReadOnly() {        try {            // See if the is read only            String path = USER_URL_PREFIX + "isReadOnly";            Element element = manager.executeRequest(GET, path);            readOnly = Boolean.valueOf(getReturn(element));        } catch (Exception e) {            // if there is a problem, keep it null, maybe in the next call success.        }    }    /**     * Translates a Clearspace xml user response into a Openfire User     *     * @param responseNode the Clearspace response     * @return a User instance with its information     */    private User translate(Node responseNode) {        String username = null;        String name = null;        String email = null;        Date creationDate = null;        Date modificationDate = null;        Node userNode = responseNode.selectSingleNode("return");        Node tmpNode;        // Gets the username        username = userNode.selectSingleNode("username").getText();        // Gets the name if it is visible        boolean nameVisible = Boolean.valueOf(userNode.selectSingleNode("nameVisible").getText());        // Gets the name        tmpNode = userNode.selectSingleNode("name");        if (tmpNode != null) {            name = tmpNode.getText();        }        // Gets the email if it is visible        boolean emailVisible = Boolean.valueOf(userNode.selectSingleNode("emailVisible").getText());        // Gets the email        tmpNode = userNode.selectSingleNode("email");        if (tmpNode != null) {            email = tmpNode.getText();        }        // Gets the creation date        tmpNode = userNode.selectSingleNode("creationDate");        if (tmpNode != null) {            creationDate = WSUtils.parseDate(tmpNode.getText());        }        // Gets the modification date        tmpNode = userNode.selectSingleNode("modificationDate");        if (tmpNode != null) {            modificationDate = WSUtils.parseDate(tmpNode.getText());        }        // Creates the user        User user = new User(username, name, email, creationDate, modificationDate);        user.setNameVisible(nameVisible);        user.setEmailVisible(emailVisible);        return user;    }    /**     * Gets a user using the userService/users GET service.     *     * @param username the username of the user     * @return the user xml response     * @throws UserNotFoundException if the user could not be found     */    private Element getUserByUsername(String username) throws UserNotFoundException {        try {            // Requests the user            String path = USER_URL_PREFIX + "users/" + username;            Element response = manager.executeRequest(GET, path);            // return the response            return response;        } catch (UserNotFoundException unfe) {            throw unfe;        } catch (Exception e) {            // It is not supported exception, wrap it into an UserNotFoundException            throw new UserNotFoundException("Error loading the user", e);        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -