Search in sources :

Example 1 with ComponentException

use of org.xmpp.component.ComponentException in project Openfire by igniterealtime.

the class SearchPlugin method processPacket.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.xmpp.component.Component#processPacket(org.xmpp.packet.Packet)
	 */
public void processPacket(Packet p) {
    if (!(p instanceof IQ)) {
        return;
    }
    final IQ packet = (IQ) p;
    if (packet.getType().equals(IQ.Type.error) || packet.getType().equals(IQ.Type.result)) {
        return;
    }
    // Packet p is an IQ stanza of type GET or SET. Therefor, it _must_ be
    // replied to.
    final IQ replyPacket = handleIQRequest(packet);
    try {
        componentManager.sendPacket(this, replyPacket);
    } catch (ComponentException e) {
        Log.error(e.getMessage(), e);
    }
}
Also used : IQ(org.xmpp.packet.IQ) ComponentException(org.xmpp.component.ComponentException)

Example 2 with ComponentException

use of org.xmpp.component.ComponentException in project Openfire by igniterealtime.

the class MultiUserChatManager method unregisterMultiUserChatService.

/**
 * Unregisters a MultiUserChatService from the manager.
 *
 * It can be used to explicitly unregister services, and is also used internally to unregister database stored services.
 *
 * Triggers the service to shut down.
 *
 * This method has a boolean parameter that controls whether a 'service removed' event is to be sent to all other
 * cluster nodes. This generally is desirable when a pre-existing service is being removed. A reason to _not_ send
 * such an event is when this method is being invoked as a result of receiving/processing such an event that was
 * received from another cluster node, or when shutting down this instance (as the service might continue to live on
 * other cluster nodes).
 *
 * @param subdomain The subdomain of the service to be unregistered.
 * @param allNodes true if a 'service removed' event needs to be sent to other cluster nodes.
 * @see #removeMultiUserChatService(String)
 */
public void unregisterMultiUserChatService(@Nonnull final String subdomain, final boolean allNodes) {
    Log.debug("Unregistering MUC service '{}'", subdomain);
    final MultiUserChatService service = mucServices.remove(subdomain);
    if (service != null) {
        service.shutdown();
        try {
            ComponentManagerFactory.getComponentManager().removeComponent(subdomain);
        } catch (ComponentException e) {
            Log.error("Unable to remove MUC service '{}' from component manager.", subdomain, e);
            mucServices.put(subdomain, service);
        }
    }
    if (allNodes) {
        Log.trace("Sending 'service removed' event for MUC service '{}' to all other cluster nodes.", subdomain);
        CacheFactory.doClusterTask(new ServiceRemovedEvent(subdomain));
    }
}
Also used : ServiceRemovedEvent(org.jivesoftware.openfire.muc.cluster.ServiceRemovedEvent) ComponentException(org.xmpp.component.ComponentException)

Example 3 with ComponentException

use of org.xmpp.component.ComponentException in project Openfire by igniterealtime.

the class ComponentStanzaHandler method processUnknowPacket.

@Override
boolean processUnknowPacket(Element doc) throws UnauthorizedException {
    String tag = doc.getName();
    if ("handshake".equals(tag)) {
        // External component is trying to authenticate
        if (!((LocalComponentSession) session).authenticate(doc.getStringValue())) {
            Log.debug("Closing session that failed to authenticate: {}", session);
            session.close();
        }
        return true;
    } else if ("error".equals(tag) && "stream".equals(doc.getNamespacePrefix())) {
        Log.debug("Closing session because of received stream error {}. Affected session: {}", doc.asXML(), session);
        session.close();
        return true;
    } else if ("bind".equals(tag)) {
        // Handle subsequent bind packets
        LocalComponentSession componentSession = (LocalComponentSession) session;
        // Get the external component of this session
        ComponentSession.ExternalComponent component = componentSession.getExternalComponent();
        String initialDomain = component.getInitialSubdomain();
        String extraDomain = doc.attributeValue("name");
        String allowMultiple = doc.attributeValue("allowMultiple");
        if (extraDomain == null || "".equals(extraDomain)) {
            // No new bind domain was specified so return a bad_request error
            Element reply = doc.createCopy();
            reply.add(new PacketError(PacketError.Condition.bad_request).getElement());
            connection.deliverRawText(reply.asXML());
        } else if (extraDomain.equals(initialDomain)) {
            // Component is binding initial domain that is already registered
            // Send confirmation that the new domain has been registered
            connection.deliverRawText("<bind/>");
        } else if (extraDomain.endsWith(initialDomain)) {
            // Only accept subdomains under the initial registered domain
            if (allowMultiple != null && component.getSubdomains().contains(extraDomain)) {
                // Domain already in use so return a conflict error
                Element reply = doc.createCopy();
                reply.add(new PacketError(PacketError.Condition.conflict).getElement());
                connection.deliverRawText(reply.asXML());
            } else {
                try {
                    // Get the requested subdomain
                    final String subdomain;
                    int index = extraDomain.indexOf(XMPPServer.getInstance().getServerInfo().getXMPPDomain());
                    if (index > -1) {
                        subdomain = extraDomain.substring(0, index - 1);
                    } else {
                        subdomain = extraDomain;
                    }
                    InternalComponentManager.getInstance().addComponent(subdomain, component);
                    session.getConnection().registerCloseListener(handback -> InternalComponentManager.getInstance().removeComponent(subdomain, (ComponentSession.ExternalComponent) handback), component);
                    // Send confirmation that the new domain has been registered
                    connection.deliverRawText("<bind/>");
                } catch (ComponentException e) {
                    Log.error("Error binding extra domain: " + extraDomain + " to component: " + component, e);
                    // Return internal server error
                    Element reply = doc.createCopy();
                    reply.add(new PacketError(PacketError.Condition.internal_server_error).getElement());
                    connection.deliverRawText(reply.asXML());
                }
            }
        } else {
            // Return forbidden error since we only allow subdomains of the intial domain
            // to be used by the same external component
            Element reply = doc.createCopy();
            reply.add(new PacketError(PacketError.Condition.forbidden).getElement());
            connection.deliverRawText(reply.asXML());
        }
        return true;
    }
    return false;
}
Also used : Element(org.dom4j.Element) ComponentException(org.xmpp.component.ComponentException) LocalComponentSession(org.jivesoftware.openfire.session.LocalComponentSession) ComponentSession(org.jivesoftware.openfire.session.ComponentSession) PacketError(org.xmpp.packet.PacketError) LocalComponentSession(org.jivesoftware.openfire.session.LocalComponentSession)

Example 4 with ComponentException

use of org.xmpp.component.ComponentException in project Openfire by igniterealtime.

the class LocalComponentSession method authenticate.

/**
 * Authenticate the external component using a digest method. The digest includes the
 * stream ID and the secret key of the main domain of the external component. A component
 * needs to authenticate just once but it may bind several domains.
 *
 * @param digest the digest sent in the handshake.
 * @return true if the authentication was successful.
 */
public boolean authenticate(String digest) {
    // Perform authentication. Wait for the handshake (with the secret key)
    String secretKey = ExternalComponentManager.getSecretForComponent(defaultSubdomain);
    String anticipatedDigest = AuthFactory.createDigest(getStreamID().getID(), secretKey);
    // Check that the provided handshake (secret key + sessionID) is correct
    if (!anticipatedDigest.equalsIgnoreCase(digest)) {
        Log.debug("LocalComponentSession: [ExComp] Incorrect handshake for component with domain: " + defaultSubdomain);
        // The credentials supplied by the initiator are not valid (answer an error
        // and close the connection)
        conn.deliverRawText(new StreamError(StreamError.Condition.not_authorized).toXML());
        // Close the underlying connection
        conn.close();
        return false;
    } else {
        // Component has authenticated fine
        setStatus(STATUS_AUTHENTICATED);
        // Send empty handshake element to acknowledge success
        conn.deliverRawText("<handshake></handshake>");
        // Bind the domain to this component
        ExternalComponent component = getExternalComponent();
        try {
            InternalComponentManager.getInstance().addComponent(defaultSubdomain, component);
            conn.registerCloseListener(handback -> InternalComponentManager.getInstance().removeComponent(defaultSubdomain, (ExternalComponent) handback), component);
            Log.debug("LocalComponentSession: [ExComp] External component was registered SUCCESSFULLY with domain: " + defaultSubdomain);
            return true;
        } catch (ComponentException e) {
            Log.debug("LocalComponentSession: [ExComp] Another component is already using domain: " + defaultSubdomain);
            // The credentials supplied by the initiator are not valid (answer an error
            // and close the connection)
            conn.deliverRawText(new StreamError(StreamError.Condition.conflict).toXML());
            // Close the underlying connection
            conn.close();
            return false;
        }
    }
}
Also used : StreamError(org.xmpp.packet.StreamError) ComponentException(org.xmpp.component.ComponentException)

Example 5 with ComponentException

use of org.xmpp.component.ComponentException in project Openfire by igniterealtime.

the class BroadcastPlugin method processPresence.

private void processPresence(boolean canProceed, Presence presence) {
    try {
        if (Presence.Type.subscribe == presence.getType()) {
            // Accept all presence requests if user has permissions
            // Reply that the subscription request was approved or rejected
            Presence reply = new Presence();
            reply.setTo(presence.getFrom());
            reply.setFrom(presence.getTo());
            reply.setType(canProceed ? Presence.Type.subscribed : Presence.Type.unsubscribed);
            componentManager.sendPacket(this, reply);
        } else if (Presence.Type.unsubscribe == presence.getType()) {
            // Send confirmation of unsubscription
            Presence reply = new Presence();
            reply.setTo(presence.getFrom());
            reply.setFrom(presence.getTo());
            reply.setType(Presence.Type.unsubscribed);
            componentManager.sendPacket(this, reply);
            if (!canProceed) {
                // Send unavailable presence of the service
                reply = new Presence();
                reply.setTo(presence.getFrom());
                reply.setFrom(presence.getTo());
                reply.setType(Presence.Type.unavailable);
                componentManager.sendPacket(this, reply);
            }
        } else if (Presence.Type.probe == presence.getType()) {
            // Send that the service is available
            Presence reply = new Presence();
            reply.setTo(presence.getFrom());
            reply.setFrom(presence.getTo());
            if (!canProceed) {
                // Send forbidden error since user is not allowed
                reply.setError(PacketError.Condition.forbidden);
            }
            componentManager.sendPacket(this, reply);
        }
    } catch (ComponentException e) {
        Log.error(e.getMessage(), e);
    }
}
Also used : ComponentException(org.xmpp.component.ComponentException) Presence(org.xmpp.packet.Presence)

Aggregations

ComponentException (org.xmpp.component.ComponentException)7 Element (org.dom4j.Element)1 ServiceAddedEvent (org.jivesoftware.openfire.muc.cluster.ServiceAddedEvent)1 ServiceRemovedEvent (org.jivesoftware.openfire.muc.cluster.ServiceRemovedEvent)1 ComponentSession (org.jivesoftware.openfire.session.ComponentSession)1 LocalComponentSession (org.jivesoftware.openfire.session.LocalComponentSession)1 UserNameProvider (org.jivesoftware.openfire.user.UserNameProvider)1 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)1 Workgroup (org.jivesoftware.xmpp.workgroup.Workgroup)1 IQ (org.xmpp.packet.IQ)1 JID (org.xmpp.packet.JID)1 PacketError (org.xmpp.packet.PacketError)1 Presence (org.xmpp.packet.Presence)1 StreamError (org.xmpp.packet.StreamError)1