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

📄 workgroup.java

📁 openfire 服务器源码下载
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

    /**
     * Returns the Collection of RequestQueues ordered by ID.
     *
     * @return the collection of request queues.
     */
    public Collection<RequestQueue> getRequestQueues() {
        final List<RequestQueue> queueList = new ArrayList<RequestQueue>(queues.values());

        // Sort by ID
        Collections.sort(queueList, queueComparator);
        return Collections.unmodifiableList(queueList);
    }

    public int getRequestQueueCount() {
        return queues.size();
    }

    public RequestQueue getRequestQueue(long queueID) throws NotFoundException {
        RequestQueue requestQueue = queues.get(queueID);
        if (requestQueue == null) {
            throw new NotFoundException("Queue not found for ID: " + queueID);
        }
        return requestQueue;
    }

    public RequestQueue getRequestQueue(String queueName) throws NotFoundException {
        for (RequestQueue queue : queues.values()) {
            if (queueName.equals(queue.getName())) {
                return queue;
            }
        }
        throw new NotFoundException("Queue not found for name: " + queueName);
    }

    /**
     * Retuns the UserRequest that is associated to the support session being currently serviced or
     * <tt>null</tt> if none was found.
     *
     * @param sessionID the ID of the support session.
     * @return the UserRequest that is associated to the support session being currently serviced or
     *         null if none was found.
     */
    public UserRequest getUserRequest(String sessionID) {
        return requests.get(sessionID);
    }

    /**
     * <p>Loads up a request queue from the database given the queueID.</p>
     * <p>Used by the workgroup queue loading SQL code.</p>
     *
     * @param queueID The ID of the existing queue to load from the DB
     *                <p/>
     *                <!-- DbC -->
     */
    private void loadRequestQueue(long queueID) {
        queues.put(queueID, new RequestQueue(this, queueID));
    }

    /**
     * Adds the request to a queue in the workgroup. If the workgroup is closed or the request
     * does not pass a filter then the request will be rejected and this method will return false.
     *
     * @param request the request to add to a queue of this wokrgroup.
     * @return true if the request was added to a queue.
     */
    public boolean queueRequest(UserRequest request) {
        // Retrieve routing manager
        RoutingManager routingManager = RoutingManager.getInstance();

        // Check if they require referer validation.
        boolean contains = containsValidReferer(request);
        if (!contains) {
            return false;
        }

        if (getStatus() != Workgroup.Status.OPEN) {
            return false;
        }

        // Check if the request may be accepted by the workgroup
        PacketError.Condition error = requestFilterFactory.getFilter().filter(request);
        if (error == null) {
            synchronized (routingManager) {
                // Add the request to the best queue of the workgroup
                routingManager.routeRequest(this, request);
                return true;
            }
        }
        return false;
    }

    public void send(Packet packet) {
        InterceptorManager interceptorManager = WorkgroupInterceptorManager.getInstance();
        try {
            interceptorManager.invokeInterceptors(getJID().toBareJID(), packet, false, false);
            WorkgroupManager.getInstance().send(packet);
            interceptorManager.invokeInterceptors(getJID().toBareJID(), packet, false, true);
        }
        catch (PacketRejectedException e) {
            ComponentManagerFactory.getComponentManager().getLog().warn("Packet was not sent " +
                "due to interceptor REJECTION: " + packet.toXML(), e);
        }
    }

    // ##############################################################################
    // Packet handler methods - We pass through to specific packet handling classes
    // ##############################################################################
    public void process(Presence packet) {
        workgroupPresenceHandler.process(packet);
    }

    public void process(IQ packet) {
        workgroupIqHandler.process(packet);
    }

    public void process(Message packet) {
        messageHandler.process(packet);
    }

    public void process(Packet packet) {
        InterceptorManager interceptorManager = WorkgroupInterceptorManager.getInstance();
        try {
            interceptorManager.invokeInterceptors(getJID().toBareJID(), packet, true, false);

            String mucDomain = WorkgroupManager.getInstance().getMUCServiceName();
            if (mucDomain.equals(packet.getFrom().getDomain())) {
                roomActivity(packet);
            }
            else if (packet instanceof Message) {
                process((Message)packet);
            }
            else if (packet instanceof Presence) {
                process((Presence)packet);
            }
            else if (packet instanceof IQ) {
                process((IQ)packet);
            }
            interceptorManager.invokeInterceptors(getJID().toBareJID(), packet, true, true);
        }
        catch (PacketRejectedException e) {
            rejectPacket(packet, e);
        }
    }

    public void rejectPacket(Packet packet, PacketRejectedException e) {
        if (packet instanceof IQ) {
            IQ reply = new IQ();
            reply.setChildElement(((IQ)packet).getChildElement().createCopy());
            reply.setID(packet.getID());
            reply.setTo(packet.getFrom());
            reply.setFrom(packet.getTo());
            reply.setError(PacketError.Condition.not_allowed);
            send(reply);
        }
        else if (packet instanceof Presence) {
            Presence reply = new Presence();
            reply.setID(packet.getID());
            reply.setTo(packet.getFrom());
            reply.setFrom(packet.getTo());
            reply.setError(PacketError.Condition.not_allowed);
            send(reply);
        }
        // Check if a message notifying the rejection should be sent
        if (e.getRejectionMessage() != null && e.getRejectionMessage().trim().length() > 0) {
            // A message for the rejection will be sent to the sender of the rejected packet
            Message notification = new Message();
            notification.setTo(packet.getFrom());
            notification.setFrom(packet.getTo());
            notification.setBody(e.getRejectionMessage());
            send(notification);
        }
        ComponentManagerFactory.getComponentManager().getLog().warn("Packet was REJECTED " +
            "by interceptor: " + packet.toXML(), e);
    }

    // ###############################################################################
    // MUC related packets
    // ###############################################################################

    /**
     * Notification message indicating that there has been new activity in a room. This implies
     * that we need to update the conversation transcript of the group chat room and possibly
     * update the number of occupants within the room.<p>
     * <p/>
     * If only the workgroup is present in the room then leave the room (i.e. destroying room) and
     * proceed to save the room conversation transcript to the database.<p>
     *
     * @param packet the packet that was sent to the group chat room.
     */
    private void roomActivity(Packet packet) {
        // Skip packet sent from this workgroup in the room
        if (packet.getFrom().toBareJID().equals(getGroupChatRoomName())) {
            return;
        }
        RoomInterceptorManager interceptorManager = RoomInterceptorManager.getInstance();
        String roomID = packet.getFrom().getNode();
        // Get the sessionID
        String sessionID = packet.getFrom().getNode();
        synchronized (sessionID.intern()) {
            if (packet instanceof Presence) {
                Presence presence = (Presence)packet;
                if (Presence.Type.error == presence.getType()) {
                    // A configuration must be wrong (eg. workgroup is not allowed to create rooms).
                    // Log the error presence
                    String warnMessage = "Possible server misconfiguration. Received error " +
                        "presence:" + presence.toXML();
                    ComponentManagerFactory.getComponentManager().getLog().warn(warnMessage);
                    return;
                }
                // Get the JID of the presence's user
                Element mucUser = presence.getChildElement("x", "http://jabber.org/protocol/muc#user");
                // Skip this presence if no extended info was included in the presence
                if (mucUser == null) {
                    return;
                }
                Element item = mucUser.element("item");
                // Skip this presence if no item was included in the presence
                if (item == null) {
                    return;
                }
                // Skip this presence if it's the presence of this workgroup in the room
                if (workgroupName.equals(packet.getFrom().getResource())) {
                    return;
                }
                JID presenceFullJID = new JID(item.attributeValue("jid"));
                String presenceJID = presenceFullJID.toBareJID();
                // Invoke the room interceptor before processing the presence
                interceptorManager.invokeInterceptors(getJID().toBareJID(), packet, false, false);
                // Get the userID associated to this sessionID
                UserRequest initialRequest = requests.get(sessionID);
                // Add the new presence to the list of sent packets
                Map<Packet, java.util.Date> messageList = transcripts.get(roomID);
                if (messageList == null) {
                    messageList = new LinkedHashMap<Packet, java.util.Date>();
                    transcripts.put(roomID, messageList);
                    // Trigger the event that a chat support has started
                    WorkgroupEventDispatcher.chatSupportStarted(this, sessionID);
                }
                messageList.put(packet.createCopy(), new java.util.Date());

                // Update the number of occupants in the room.
                boolean occupantAdded = false;
                Set<String> set = occupantsCounter.get(roomID);
                if (set == null) {
                    set = new HashSet<String>();
                    occupantsCounter.put(roomID, set);
                }
                if (presence.isAvailable()) {
                    occupantAdded = set.add(presenceJID);
                }
                else {
                    String xpath = "/presence/*[name()='x']/*[name()='status']";
                    Element status = (Element)presence.getElement().selectSingleNode(xpath);
                    if (status == null || !"303".equals(status.attributeValue("code"))) {
                        // Remove the occupant unless the occupant is changing his nickname
                        set.remove(presenceJID);
                    }
                }
                // If the presence belongs to an Agent then create/update a track
                // Look for an agent whose JID matches the presence's JID
                String agentJID = null;
                for (Agent agent : getAgents()) {
                    if (agent.getAgentJID().toBareJID().equals(presenceJID)) {
                        agentJID = agent.getAgentJID().toBareJID();
                    }
                }
                if (agentJID != null) {
                    AgentSession agentSession;
                    // Update the current chats that the agent is having
                    try {
                        agentSession = agentManager.getAgentSession(presenceFullJID);
                        if (agentSession != null) {
                            if (presence.isAvailable()) {
                                if (occupantAdded) {
                                    agentSession.addChatInfo(this, sessionID, initialRequest, new java.util.Date());
                                    // Trigger the event that an agent has joined a chat session
                                    WorkgroupEventDispatcher.agentJoinedChatSupport(this, sessionID, agentSession);
                                }
                            }
                            else {
                                agentSession.removeChatInfo(this, sessionID);
                                // Trigger the event that an agent has left a chat session
                                WorkgroupEventDispatcher.agentLeftChatSupport(this, sessionID, agentSession);
                            }
                        }
                    }
                    catch (AgentNotFoundException e) {
                        // Do nothing since the AgentSession was not found
                    }
                    if (presence.isAvailable()) {
                        if (occupantAdded) {
                            // Store in the DB that an agent has joined a room
                            DbWorkgroup.updateJoinedSession(sessionID, agentJID, true);
                        }
                    }
                    else {
                        // Store in the DB that an agent has left a room
                        DbWorkgroup.updateJoinedSession(sessionID, agentJID, false);
                    }
                }
                else {
                    if (occupantAdded) {
                        // Notify the request that the user has joined a support session
                        initialRequest.supportStarted(roomID);
                    }
                }
                if (occupantAdded) {
                    initialRequest.userJoinedRoom(new JID(packet.getFrom().toBareJID()), presenceFullJID);
                }

⌨️ 快捷键说明

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