Search in sources :

Example 1 with ClientSession

use of org.jivesoftware.openfire.session.ClientSession in project Openfire by igniterealtime.

the class WebSocketPlugin method destroyPlugin.

@Override
public void destroyPlugin() {
    // terminate any active websocket sessions
    SessionManager sm = XMPPServer.getInstance().getSessionManager();
    for (ClientSession session : sm.getSessions()) {
        if (session instanceof LocalSession) {
            Object ws = ((LocalSession) session).getSessionData("ws");
            if (ws != null && (Boolean) ws) {
                session.close();
            }
        }
    }
    ContextHandlerCollection contexts = HttpBindManager.getInstance().getContexts();
    contexts.removeHandler(contextHandler);
    contextHandler = null;
    pluginClassLoader = null;
}
Also used : SessionManager(org.jivesoftware.openfire.SessionManager) ClientSession(org.jivesoftware.openfire.session.ClientSession) LocalSession(org.jivesoftware.openfire.session.LocalSession) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection)

Example 2 with ClientSession

use of org.jivesoftware.openfire.session.ClientSession in project Openfire by igniterealtime.

the class IQRouter method route.

/**
     * <p>Performs the actual packet routing.</p>
     * <p>You routing is considered 'quick' and implementations may not take
     * excessive amounts of time to complete the routing. If routing will take
     * a long amount of time, the actual routing should be done in another thread
     * so this method returns quickly.</p>
     * <h2>Warning</h2>
     * <p>Be careful to enforce concurrency DbC of concurrent by synchronizing
     * any accesses to class resources.</p>
     *
     * @param packet The packet to route
     * @throws NullPointerException If the packet is null
     */
public void route(IQ packet) {
    if (packet == null) {
        throw new NullPointerException();
    }
    JID sender = packet.getFrom();
    ClientSession session = sessionManager.getSession(sender);
    // may be null
    Element childElement = packet.getChildElement();
    try {
        // Invoke the interceptors before we process the read packet
        InterceptorManager.getInstance().invokeInterceptors(packet, session, true, false);
        JID to = packet.getTo();
        if (session != null && to != null && session.getStatus() == Session.STATUS_CONNECTED && !serverName.equals(to.toString())) {
            // User is requesting this server to authenticate for another server. Return
            // a bad-request error
            IQ reply = IQ.createResultIQ(packet);
            if (childElement != null) {
                reply.setChildElement(childElement.createCopy());
            }
            reply.setError(PacketError.Condition.bad_request);
            session.process(reply);
            Log.warn("User tried to authenticate with this server using an unknown receipient: " + packet.toXML());
        } else if (session == null || session.getStatus() == Session.STATUS_AUTHENTICATED || (childElement != null && isLocalServer(to) && ("jabber:iq:auth".equals(childElement.getNamespaceURI()) || "jabber:iq:register".equals(childElement.getNamespaceURI()) || "urn:ietf:params:xml:ns:xmpp-bind".equals(childElement.getNamespaceURI())))) {
            handle(packet);
        } else if (packet.getType() == IQ.Type.get || packet.getType() == IQ.Type.set) {
            IQ reply = IQ.createResultIQ(packet);
            if (childElement != null) {
                reply.setChildElement(childElement.createCopy());
            }
            reply.setError(PacketError.Condition.not_authorized);
            session.process(reply);
        }
        // Invoke the interceptors after we have processed the read packet
        InterceptorManager.getInstance().invokeInterceptors(packet, session, true, true);
    } catch (PacketRejectedException e) {
        if (session != null) {
            // An interceptor rejected this packet so answer a not_allowed error
            IQ reply = new IQ();
            if (childElement != null) {
                reply.setChildElement(childElement.createCopy());
            }
            reply.setID(packet.getID());
            reply.setTo(session.getAddress());
            reply.setFrom(packet.getTo());
            reply.setError(PacketError.Condition.not_allowed);
            session.process(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(session.getAddress());
                notification.setFrom(packet.getTo());
                notification.setBody(e.getRejectionMessage());
                session.process(notification);
            }
        }
    }
}
Also used : LocalClientSession(org.jivesoftware.openfire.session.LocalClientSession) ClientSession(org.jivesoftware.openfire.session.ClientSession) Element(org.dom4j.Element) PacketRejectedException(org.jivesoftware.openfire.interceptor.PacketRejectedException)

Example 3 with ClientSession

use of org.jivesoftware.openfire.session.ClientSession in project Openfire by igniterealtime.

the class MessageRouter method routingFailed.

/**
     * Notification message indicating that a packet has failed to be routed to the recipient.
     *
     * @param recipient address of the entity that failed to receive the packet.
     * @param packet    Message packet that failed to be sent to the recipient.
     */
public void routingFailed(JID recipient, Packet packet) {
    log.debug("Message sent to unreachable address: " + packet.toXML());
    final Message msg = (Message) packet;
    boolean storeOffline = true;
    if (msg.getType().equals(Message.Type.chat) && serverName.equals(recipient.getDomain()) && recipient.getResource() != null) {
        // Find an existing AVAILABLE session with non-negative priority.
        for (JID address : routingTable.getRoutes(recipient.asBareJID(), packet.getFrom())) {
            ClientSession session = routingTable.getClientRoute(address);
            if (session != null && session.isInitialized()) {
                if (session.getPresence().getPriority() >= 1) {
                    storeOffline = false;
                }
            }
        }
    }
    if (!storeOffline) {
        // If message was sent to an unavailable full JID of a user then retry using the bare JID.
        routingTable.routePacket(recipient.asBareJID(), packet, false);
    } else {
        // Delegate to offline message strategy, which will either bounce or ignore the message depending on user settings.
        messageStrategy.storeOffline((Message) packet);
    }
}
Also used : Message(org.xmpp.packet.Message) JID(org.xmpp.packet.JID) LocalClientSession(org.jivesoftware.openfire.session.LocalClientSession) ClientSession(org.jivesoftware.openfire.session.ClientSession)

Example 4 with ClientSession

use of org.jivesoftware.openfire.session.ClientSession in project Openfire by igniterealtime.

the class MessageRouter method route.

/**
     * <p>Performs the actual packet routing.</p>
     * <p>You routing is considered 'quick' and implementations may not take
     * excessive amounts of time to complete the routing. If routing will take
     * a long amount of time, the actual routing should be done in another thread
     * so this method returns quickly.</p>
     * <h2>Warning</h2>
     * <p>Be careful to enforce concurrency DbC of concurrent by synchronizing
     * any accesses to class resources.</p>
     *
     * @param packet The packet to route
     * @throws NullPointerException If the packet is null
     */
public void route(Message packet) {
    if (packet == null) {
        throw new NullPointerException();
    }
    ClientSession session = sessionManager.getSession(packet.getFrom());
    try {
        // Invoke the interceptors before we process the read packet
        InterceptorManager.getInstance().invokeInterceptors(packet, session, true, false);
        if (session == null || session.getStatus() == Session.STATUS_AUTHENTICATED) {
            JID recipientJID = packet.getTo();
            // If the server receives a message stanza with no 'to' attribute, it MUST treat the message as if the 'to' address were the bare JID <localpart@domainpart> of the sending entity.
            if (recipientJID == null) {
                recipientJID = packet.getFrom().asBareJID();
            }
            // Check if the message was sent to the server hostname
            if (recipientJID.getNode() == null && recipientJID.getResource() == null && serverName.equals(recipientJID.getDomain())) {
                if (packet.getElement().element("addresses") != null) {
                    // Message includes multicast processing instructions. Ask the multicastRouter
                    // to route this packet
                    multicastRouter.route(packet);
                } else {
                    // Message was sent to the server hostname so forward it to a configurable
                    // set of JID's (probably admin users)
                    sendMessageToAdmins(packet);
                }
                return;
            }
            boolean isAcceptable = true;
            if (session instanceof LocalClientSession) {
                // Check if we could process messages from the recipient.
                // If not, return a not-acceptable error as per XEP-0016:
                // If the user attempts to send an outbound stanza to a contact and that stanza type is blocked, the user's server MUST NOT route the stanza to the contact but instead MUST return a <not-acceptable/> error
                Message dummyMessage = packet.createCopy();
                dummyMessage.setFrom(packet.getTo());
                dummyMessage.setTo(packet.getFrom());
                if (!((LocalClientSession) session).canProcess(dummyMessage)) {
                    packet.setTo(session.getAddress());
                    packet.setFrom((JID) null);
                    packet.setError(PacketError.Condition.not_acceptable);
                    session.process(packet);
                    isAcceptable = false;
                }
            }
            if (isAcceptable) {
                boolean isPrivate = packet.getElement().element(QName.get("private", "urn:xmpp:carbons:2")) != null;
                try {
                    // Deliver stanza to requested route
                    routingTable.routePacket(recipientJID, packet, false);
                } catch (Exception e) {
                    log.error("Failed to route packet: " + packet.toXML(), e);
                    routingFailed(recipientJID, packet);
                }
                // When a client sends a <message/> of type "chat"
                if (packet.getType() == Message.Type.chat && !isPrivate && session != null) {
                    // && session.isMessageCarbonsEnabled() ??? // must the own session also be carbon enabled?
                    List<JID> routes = routingTable.getRoutes(packet.getFrom().asBareJID(), null);
                    for (JID route : routes) {
                        // The sending server SHOULD NOT send a forwarded copy to the sending full JID if it is a Carbons-enabled resource.
                        if (!route.equals(session.getAddress())) {
                            ClientSession clientSession = sessionManager.getSession(route);
                            if (clientSession != null && clientSession.isMessageCarbonsEnabled()) {
                                Message message = new Message();
                                // The wrapping message SHOULD maintain the same 'type' attribute value
                                message.setType(packet.getType());
                                // the 'from' attribute MUST be the Carbons-enabled user's bare JID
                                message.setFrom(packet.getFrom().asBareJID());
                                // and the 'to' attribute SHOULD be the full JID of the resource receiving the copy
                                message.setTo(route);
                                // The content of the wrapping message MUST contain a <sent/> element qualified by the namespace "urn:xmpp:carbons:2", which itself contains a <forwarded/> qualified by the namespace "urn:xmpp:forward:0" that contains the original <message/> stanza.
                                message.addExtension(new Sent(new Forwarded(packet)));
                                clientSession.process(message);
                            }
                        }
                    }
                }
            }
        } else {
            packet.setTo(session.getAddress());
            packet.setFrom((JID) null);
            packet.setError(PacketError.Condition.not_authorized);
            session.process(packet);
        }
        // Invoke the interceptors after we have processed the read packet
        InterceptorManager.getInstance().invokeInterceptors(packet, session, true, true);
    } catch (PacketRejectedException e) {
        // An interceptor rejected this packet
        if (session != null && e.getRejectionMessage() != null && e.getRejectionMessage().trim().length() > 0) {
            // A message for the rejection will be sent to the sender of the rejected packet
            Message reply = new Message();
            reply.setID(packet.getID());
            reply.setTo(session.getAddress());
            reply.setFrom(packet.getTo());
            reply.setType(packet.getType());
            reply.setThread(packet.getThread());
            reply.setBody(e.getRejectionMessage());
            session.process(reply);
        }
    }
}
Also used : LocalClientSession(org.jivesoftware.openfire.session.LocalClientSession) JID(org.xmpp.packet.JID) Message(org.xmpp.packet.Message) LocalClientSession(org.jivesoftware.openfire.session.LocalClientSession) ClientSession(org.jivesoftware.openfire.session.ClientSession) Forwarded(org.jivesoftware.openfire.forward.Forwarded) PacketRejectedException(org.jivesoftware.openfire.interceptor.PacketRejectedException) PacketRejectedException(org.jivesoftware.openfire.interceptor.PacketRejectedException) Sent(org.jivesoftware.openfire.carbons.Sent)

Example 5 with ClientSession

use of org.jivesoftware.openfire.session.ClientSession in project Openfire by igniterealtime.

the class GetListActiveUsers method execute.

@Override
public void execute(SessionData data, Element command) {
    String max_items = data.getData().get("max_items").get(0);
    int maxItems = -1;
    if (max_items != null && !"none".equals(max_items)) {
        try {
            maxItems = Integer.parseInt(max_items);
        } catch (NumberFormatException e) {
        // Do nothing. Assume that all users are being requested
        }
    }
    DataForm form = new DataForm(DataForm.Type.result);
    FormField field = form.addField();
    field.setType(FormField.Type.hidden);
    field.setVariable("FORM_TYPE");
    field.addValue("http://jabber.org/protocol/admin");
    field = form.addField();
    field.setLabel("The list of active users");
    field.setVariable("activeuserjids");
    // Get list of users (i.e. bareJIDs) that are connected to the server
    Collection<ClientSession> sessions = SessionManager.getInstance().getSessions();
    Set<String> users = new HashSet<>(sessions.size());
    for (ClientSession session : sessions) {
        if (session.getPresence().isAvailable()) {
            users.add(session.getAddress().toBareJID());
        }
        if (maxItems > 0 && users.size() >= maxItems) {
            break;
        }
    }
    // Add users to the result
    for (String user : users) {
        field.addValue(user);
    }
    command.add(form.getElement());
}
Also used : ClientSession(org.jivesoftware.openfire.session.ClientSession) DataForm(org.xmpp.forms.DataForm) FormField(org.xmpp.forms.FormField)

Aggregations

ClientSession (org.jivesoftware.openfire.session.ClientSession)49 JID (org.xmpp.packet.JID)15 Element (org.dom4j.Element)14 LocalClientSession (org.jivesoftware.openfire.session.LocalClientSession)14 IQ (org.xmpp.packet.IQ)12 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)8 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)7 PrivacyList (org.jivesoftware.openfire.privacy.PrivacyList)7 StreamError (org.xmpp.packet.StreamError)7 DataForm (org.xmpp.forms.DataForm)6 FormField (org.xmpp.forms.FormField)6 Message (org.xmpp.packet.Message)6 Presence (org.xmpp.packet.Presence)6 PacketRejectedException (org.jivesoftware.openfire.interceptor.PacketRejectedException)5 StringprepException (gnu.inet.encoding.StringprepException)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)3 SessionEntities (org.jivesoftware.openfire.plugin.rest.entity.SessionEntities)3 SQLException (java.sql.SQLException)2