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

📄 iqprivacyhandler.java

📁 基于Jabber协议的即时消息服务器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/** * $RCSfile$ * $Revision: $ * $Date: $ * * Copyright (C) 2006 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. */package org.jivesoftware.wildfire.handler;import org.dom4j.Element;import org.jivesoftware.wildfire.ClientSession;import org.jivesoftware.wildfire.IQHandlerInfo;import org.jivesoftware.wildfire.SessionManager;import org.jivesoftware.wildfire.XMPPServer;import org.jivesoftware.wildfire.auth.UnauthorizedException;import org.jivesoftware.wildfire.disco.ServerFeaturesProvider;import org.jivesoftware.wildfire.event.UserEventListener;import org.jivesoftware.wildfire.privacy.PrivacyList;import org.jivesoftware.wildfire.privacy.PrivacyListManager;import org.jivesoftware.wildfire.privacy.PrivacyListProvider;import org.jivesoftware.wildfire.user.User;import org.jivesoftware.wildfire.user.UserManager;import org.xmpp.packet.IQ;import org.xmpp.packet.JID;import org.xmpp.packet.PacketError;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Map;/** * IQPrivacyHandler is responsible for handling privacy lists. * * @author Gaston Dombiak */public class IQPrivacyHandler extends IQHandler        implements ServerFeaturesProvider, UserEventListener {    private IQHandlerInfo info;    private PrivacyListManager manager = PrivacyListManager.getInstance();    private PrivacyListProvider provider = new PrivacyListProvider();    private SessionManager sessionManager;    public IQPrivacyHandler() {        super("Blocking Communication Handler");        info = new IQHandlerInfo("query", "jabber:iq:privacy");    }    public IQ handleIQ(IQ packet) throws UnauthorizedException {        IQ.Type type = packet.getType();        JID from = packet.getFrom();        if (from.getNode() == null || !UserManager.getInstance().isRegisteredUser(from.getNode())) {            // Service is unavailable for anonymous users            IQ result = IQ.createResultIQ(packet);            result.setChildElement(packet.getChildElement().createCopy());            result.setError(PacketError.Condition.service_unavailable);            return result;        }        IQ result = null;        if (type.equals(IQ.Type.get)) {            // User wants to retrieve a privacy list or the list of privacy list            Element child = packet.getChildElement();            List elements = child.elements();            if (elements.isEmpty()) {                // User requested names of privacy lists                result = getPrivacyListsNames(packet, from);            }            else {                // User requested a privacy list                result = getPrivacyList(packet, from);            }        }        else if (type.equals(IQ.Type.set)) {            Element child = packet.getChildElement();            Element activeList = child.element("active");            Element defaultList = child.element("default");            if (activeList != null) {                // Active list handling                String listName = activeList.attributeValue("name");                if (listName != null) {                    // User wants to set or change the active list currently being applied by                    // the server to this session                    result = setActiveList(packet, from, listName);                }                else {                    // User wants to decline the use of any active list for this session                    result = declineActiveList(packet, from);                }            }            else if (defaultList != null) {                // Default list handling                String listName = defaultList.attributeValue("name");                if (listName != null) {                    // User wants to set or change its default list (i.e. which applies                    // to the user as a whole, not only the sending resource)                    result = setDefaultList(packet, from, listName);                }                else {                    // User wants to decline the use of a default list                    result = declineDefaultList(packet, from);                }            }            else {                // Privacy list handling (create/edit/delete)                Element list = child.element("list");                String listName = list.attributeValue("name");                List items = list.elements();                if (!items.isEmpty()) {                    // User wants to create or edit a privacy list                    result = updateOrCreateList(packet, from, list);                }                else {                    // User wants to delete a privacy list                    result = deleteList(packet, from, listName);                }            }        }        return result;    }    /**     * Returns the IQ packet containing the active and default lists and the lists     * defined by the user.     *     * @param packet IQ packet requesting the lists.     * @param from sender of the IQ packet.     * @return the IQ packet containing the active and default lists and the lists     *         defined by the user.     */    private IQ getPrivacyListsNames(IQ packet, JID from) {        IQ result = IQ.createResultIQ(packet);        Element childElement = packet.getChildElement().createCopy();        result.setChildElement(childElement);        Map<String, Boolean> privacyLists = provider.getPrivacyLists(from.getNode());        // Add the default list        for (String listName : privacyLists.keySet()) {            if (privacyLists.get(listName)) {                childElement.addElement("default").addAttribute("name", listName);            }        }        // Add the active list (only if there is an active list for the session)        ClientSession session = sessionManager.getSession(from);        if  (session != null && session.getActiveList() != null) {            childElement.addElement("active")                    .addAttribute("name", session.getActiveList().getName());        }        // Add a list element for each privacy list        for (String listName : privacyLists.keySet()) {            childElement.addElement("list").addAttribute("name", listName);        }        return result;    }    /**     * Returns the IQ packet containing the details of the specified list. If no list     * was found or the IQ request contains more than one specified list then an error will     * be returned.     *     * @param packet IQ packet requesting a given list.     * @param from sender of the IQ packet.     * @return the IQ packet containing the details of the specified list.     */    private IQ getPrivacyList(IQ packet, JID from) {        IQ result = IQ.createResultIQ(packet);        Element childElement = packet.getChildElement().createCopy();        result.setChildElement(childElement);        // Check that only one list was requested        List<Element> lists = childElement.elements("list");        if (lists.size() > 1) {            result.setError(PacketError.Condition.bad_request);        }        else {            String listName = lists.get(0).attributeValue("name");            PrivacyList list = null;            if (listName != null) {                // A list name was specified so get it                list = manager.getPrivacyList(from.getNode(), listName);            }            if (list != null) {                // Add the privacy list to the result                childElement = result.setChildElement("query", "jabber:iq:privacy");                childElement.add(list.asElement());            }            else {                // List not found                result.setError(PacketError.Condition.item_not_found);            }        }        return result;    }    /**     * User has specified a new active list that should be used for the current session.     *     * @param packet IQ packet setting new active list for the current session.     * @param from sender of the IQ packet.     * @param listName name of the new active list for the current session.     * @return acknowledge of success.     */    private IQ setActiveList(IQ packet, JID from, String listName) {        IQ result = IQ.createResultIQ(packet);        Element childElement = packet.getChildElement().createCopy();        result.setChildElement(childElement);        // Get the list        PrivacyList list = manager.getPrivacyList(from.getNode(), listName);        if (list != null) {            // Get the user session            ClientSession session = sessionManager.getSession(from);            if (session != null) {

⌨️ 快捷键说明

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