Java Code Examples for org.xmpp.packet.PacketError

The following examples show how to use org.xmpp.packet.PacketError. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Openfire   Source File: IQAdminHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Handles the IQ packet sent by an owner or admin of the room. Possible actions are:
 * <ul>
 * <li>Return the list of participants</li>
 * <li>Return the list of moderators</li>
 * <li>Return the list of members</li>
 * <li>Return the list of outcasts</li>
 * <li>Change user's affiliation to member</li>
 * <li>Change user's affiliation to outcast</li>
 * <li>Change user's affiliation to none</li>
 * <li>Change occupant's affiliation to moderator</li>
 * <li>Change occupant's affiliation to participant</li>
 * <li>Change occupant's affiliation to visitor</li>
 * <li>Kick occupants from the room</li>
 * </ul>
 *
 * @param packet the IQ packet sent by an owner or admin of the room.
 * @param role the role of the user that sent the request packet.
 * @throws ForbiddenException If the user is not allowed to perform his request.
 * @throws ConflictException If the desired room nickname is already reserved for the room or
 *                           if the room was going to lose all of its owners.
 * @throws NotAllowedException Thrown if trying to ban an owner or an administrator.
 * @throws CannotBeInvitedException If the user being invited as a result of being added to a members-only room still does not have permission
 */
public void handleIQ(IQ packet, MUCRole role) throws ForbiddenException, ConflictException,
        NotAllowedException, CannotBeInvitedException {
    IQ reply = IQ.createResultIQ(packet);
    Element element = packet.getChildElement();

    // Analyze the action to perform based on the included element
    @SuppressWarnings("unchecked")
    List<Element> itemsList = element.elements("item");
    
    if (!itemsList.isEmpty()) {
        handleItemsElement(role, itemsList, reply);
    }
    else {
        // An unknown and possibly incorrect element was included in the query
        // element so answer a BAD_REQUEST error
        reply.setChildElement(packet.getChildElement().createCopy());
        reply.setError(PacketError.Condition.bad_request);
    }
    if (reply.getTo() != null) {
        // Send a reply only if the sender of the original packet was from a real JID. (i.e. not
        // a packet generated locally)
        router.route(reply);
    }
}
 
Example 2
Source Project: Openfire   Source File: PubSubModule.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void process(Packet packet) {
    try {
        // Check if the packet is a disco request or a packet with namespace iq:register
        if (packet instanceof IQ) {
            if (engine.process(this, (IQ) packet) == null) {
                process((IQ) packet);
            }
        }
        else if (packet instanceof Presence) {
            engine.process(this, (Presence) packet);
        }
        else {
            engine.process(this, (Message) packet);
        }
    }
    catch (Exception e) {
        Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        if (packet instanceof IQ) {
            // Send internal server error
            IQ reply = IQ.createResultIQ((IQ) packet);
            reply.setError(PacketError.Condition.internal_server_error);
            send(reply);
        }
    }
}
 
Example 3
Source Project: Openfire   Source File: PresenceUpdateHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Handle presence updates that affect roster subscriptions.
 *
 * @param presence The presence presence to handle
 * @throws PacketException if the packet is null or the packet could not be routed.
 */
public void process(Presence presence) throws PacketException {
    try {
        process((Packet)presence);
    }
    catch (UnauthorizedException e) {
        try {
            LocalSession session = (LocalSession) sessionManager.getSession(presence.getFrom());
            presence = presence.createCopy();
            if (session != null) {
                presence.setFrom(new JID(null, session.getServerName(), null, true));
                presence.setTo(session.getAddress());
            }
            else {
                JID sender = presence.getFrom();
                presence.setFrom(presence.getTo());
                presence.setTo(sender);
            }
            presence.setError(PacketError.Condition.not_authorized);
            deliverer.deliver(presence);
        }
        catch (Exception err) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), err);
        }
    }
}
 
Example 4
Source Project: Openfire   Source File: IQRosterHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Checks the roster groups for error conditions described in RFC 6121 § 2.3.3.
 *
 * @param groups The groups.
 * @return An error if the specification is violated or null if everything is fine.
 */
private static PacketError checkGroups(Iterable<String> groups) {
    Set<String> set = new HashSet<>();
    for (String group : groups) {
        if (!set.add(group)) {
            // Duplicate group found.
            // RFC 6121 2.3.3.  Error Cases: 2. The <item/> element contains more than one <group/> element, but there are duplicate groups
            return new PacketError(PacketError.Condition.bad_request, PacketError.Type.modify, "Item contains duplicate groups");
        }
        if (group.isEmpty()) {
            // The server MUST return a <not-acceptable/> stanza error to the client if the roster set contains any of the following violations:
            // 2. The XML character data of the <group/> element is of zero length.
            return new PacketError(PacketError.Condition.not_acceptable, PacketError.Type.modify, "Group is of zero length");
        }
    }
    return null;
}
 
Example 5
Source Project: Openfire   Source File: IQSharedGroupHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public IQ handleIQ(IQ packet) throws UnauthorizedException {
    IQ result = IQ.createResultIQ(packet);
    String username = packet.getFrom().getNode();
    if (!serverName.equals(packet.getFrom().getDomain()) || username == null) {
        // Users of remote servers are not allowed to get their "shared groups". Users of
        // remote servers cannot have shared groups in this server.
        // Besides, anonymous users do not belong to shared groups so answer an error
        result.setChildElement(packet.getChildElement().createCopy());
        result.setError(PacketError.Condition.not_allowed);
        return result;
    }

    Collection<Group> groups = rosterManager.getSharedGroups(username);
    Element sharedGroups = result.setChildElement("sharedgroup",
            "http://www.jivesoftware.org/protocol/sharedgroup");
    for (Group sharedGroup : groups) {
        String displayName = sharedGroup.getProperties().get("sharedRoster.displayName");
        if (displayName != null) {
            sharedGroups.addElement("group").setText(displayName);
        }
    }
    return result;
}
 
Example 6
Source Project: Openfire   Source File: IQPrivacyHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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) {
            // Set the new active list for this session
            session.setActiveList(list);
        }
    }
    else {
        // List not found
        result.setError(PacketError.Condition.item_not_found);
    }
    return result;
}
 
Example 7
Source Project: Openfire   Source File: IQPrivacyHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * User has specified that there is no default list that should be used for this user.
 *
 * @param packet IQ packet declining default list for all sessions.
 * @param from sender of the IQ packet.
 * @return acknowledge of success.
 */
private IQ declineDefaultList(IQ packet, JID from) {
    IQ result = IQ.createResultIQ(packet);
    Element childElement = packet.getChildElement().createCopy();
    result.setChildElement(childElement);

    if (sessionManager.getSessionCount(from.getNode()) > 1) {
        // Current default list is being used by more than one session
        result.setError(PacketError.Condition.conflict);
    }
    else {
        // Get the user session
        ClientSession session = sessionManager.getSession(from);
        // Check if a default list was already defined
        if (session.getDefaultList() != null) {
            // Set the existing default list as non-default
            session.getDefaultList().setDefaultList(false);
            // Update the database with the new list state
            provider.updatePrivacyList(from.getNode(), session.getDefaultList());
            session.setDefaultList(null);
        }
    }
    return result;
}
 
Example 8
Source Project: Openfire   Source File: TransportHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void process(Packet packet) throws UnauthorizedException, PacketException {
    boolean handled = false;
    String host = packet.getTo().getDomain();
    for (Channel<Packet> channel : transports.values()) {
        if (channel.getName().equalsIgnoreCase(host)) {
            channel.add(packet);
            handled = true;
        }
    }
    if (!handled) {
        JID recipient = packet.getTo();
        JID sender = packet.getFrom();
        packet.setError(PacketError.Condition.remote_server_timeout);
        packet.setFrom(recipient);
        packet.setTo(sender);
        try {
            deliverer.deliver(packet);
        }
        catch (PacketException e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        }
    }
}
 
Example 9
Source Project: Openfire   Source File: MultiplexerStanzaHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void processIQ(final IQ packet) {
    if (session.getStatus() != Session.STATUS_AUTHENTICATED) {
        // Session is not authenticated so return error
        IQ reply = new IQ();
        reply.setChildElement(packet.getChildElement().createCopy());
        reply.setID(packet.getID());
        reply.setTo(packet.getFrom());
        reply.setFrom(packet.getTo());
        reply.setError(PacketError.Condition.not_authorized);
        session.process(reply);
        return;
    }
    // Process the packet
    packetHandler.handle(packet);
}
 
Example 10
Source Project: Openfire   Source File: ComponentStanzaHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void processIQ(IQ packet) throws UnauthorizedException {
    if (session.getStatus() != Session.STATUS_AUTHENTICATED) {
        // Session is not authenticated so return error
        IQ reply = new IQ();
        reply.setChildElement(packet.getChildElement().createCopy());
        reply.setID(packet.getID());
        reply.setTo(packet.getFrom());
        reply.setFrom(packet.getTo());
        reply.setError(PacketError.Condition.not_authorized);
        session.process(reply);
        return;
    }
    // Keep track of the component that sent an IQ get/set
    if (packet.getType() == IQ.Type.get || packet.getType() == IQ.Type.set) {
        // Handle subsequent bind packets
        LocalComponentSession componentSession = (LocalComponentSession) session;
        // Get the external component of this session
        LocalComponentSession.LocalExternalComponent component =
                (LocalComponentSession.LocalExternalComponent) componentSession.getExternalComponent();
        component.track(packet);
    }
    super.processIQ(packet);
}
 
Example 11
Source Project: Openfire   Source File: UserManagerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void isRegisteredUserWillReturnFalseForUnknownRemoteUsers() {

    final AtomicReference<IQResultListener> iqListener = new AtomicReference<>();
    doAnswer(invocationOnMock -> {
        final IQResultListener listener = invocationOnMock.getArgument(1);
        iqListener.set(listener);
        return null;
    }).when(iqRouter).addIQResultListener(any(), any(), anyLong());

    doAnswer(invocationOnMock -> {
        final IQ iq = invocationOnMock.getArgument(0);
        final Element childElement = iq.getChildElement();
        final IQ response = IQ.createResultIQ(iq);
        response.setChildElement(childElement.createCopy());
        response.setError(new PacketError(PacketError.Condition.item_not_found, PacketError.Condition.item_not_found.getDefaultType()));
        iqListener.get().receivedAnswer(response);
        return null;
    }).when(iqRouter).route(any());

    final boolean result = userManager.isRegisteredUser(new JID(USER_ID, REMOTE_XMPP_DOMAIN, null));

    assertThat(result, is(false));
    verify(iqRouter).route(any());
}
 
Example 12
Source Project: openfireLBS   Source File: LocationHandler.java    License: Apache License 2.0 6 votes vote down vote up
private IQ updateLocation(IQ packet) {
	IQ reply = IQ.createResultIQ(packet);

	Element iq = packet.getChildElement();
	JID from = packet.getFrom();
	String username = from.getNode();

	Element item = iq.element("item");
	Double myLon = Double.parseDouble(item.attributeValue("lon"));
	Double myLat = Double.parseDouble(item.attributeValue("lat"));

	boolean f = insertLocation(myLon,myLat,username);
	if (f){
		// reply.setChildElement(iq);
	}else{
		reply.setType(IQ.Type.error);
		reply.setError(PacketError.Condition.internal_server_error);
	}
	
	return reply;
}
 
Example 13
Source Project: Openfire   Source File: LocalMUCRoom.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void send(@Nonnull Packet packet, @Nonnull MUCRole sender) {
    if (packet instanceof Message) {
        broadcast((Message)packet, sender);
    }
    else if (packet instanceof Presence) {
        broadcastPresence((Presence)packet, false, sender);
    }
    else if (packet instanceof IQ) {
        IQ reply = IQ.createResultIQ((IQ) packet);
        reply.setChildElement(((IQ) packet).getChildElement());
        reply.setError(PacketError.Condition.bad_request);
        router.route(reply);
    }
}
 
Example 14
Source Project: Openfire   Source File: MessageRouter.java    License: Apache License 2.0 5 votes vote down vote up
private void bounce(Message message) {
    // The bouncing behavior as implemented beyond this point was introduced as part
    // of OF-1852. This kill-switch allows it to be disabled again in case it
    // introduces unwanted side-effects.
    if ( !JiveGlobals.getBooleanProperty( "xmpp.message.bounce", true ) ) {
        log.trace( "Not bouncing a message stanza, as bouncing is disabled by configuration." );
        return;
    }

    // Do nothing if the packet included an error. This intends to prevent scenarios
    // where a stanza that is bounced itself gets bounced, causing a loop.
    if (message.getError() != null) {
        log.trace( "Not bouncing a stanza that included an error (to prevent never-ending loops of bounces-of-bounces)." );
        return;
    }

    // Do nothing if the sender was the server itself
    if (message.getFrom() == null || message.getFrom().toString().equals( serverName )) {
        log.trace( "Not bouncing a stanza that was sent by the server itself." );
        return;
    }
    try {
        log.trace( "Bouncing a message stanza." );

        // Generate a rejection response to the sender
        final Message errorResponse = message.createCopy();
        // return an error stanza to the sender, which SHOULD be <service-unavailable/>
        errorResponse.setError(PacketError.Condition.service_unavailable);
        errorResponse.setFrom(message.getTo());
        errorResponse.setTo(message.getFrom());
        // Send the response
        route(errorResponse);
    }
    catch (Exception e) {
        log.error("An exception occurred while trying to bounce a message.", e);
    }
}
 
Example 15
Source Project: Openfire   Source File: XmppWebSocket.java    License: Apache License 2.0 5 votes vote down vote up
private void sendPacketError(Element stanza, PacketError.Condition condition) {
    Element reply = stanza.createCopy();
    reply.addAttribute("type", "error");
    reply.addAttribute("to", stanza.attributeValue("from"));
    reply.addAttribute("from", stanza.attributeValue("to"));
    reply.add(new PacketError(condition).getElement());
    deliver(reply.asXML());
}
 
Example 16
Source Project: Openfire   Source File: IQPrivateHandler.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public IQ handleIQ(IQ packet) throws UnauthorizedException, PacketException {
    IQ replyPacket = IQ.createResultIQ(packet);

    Element child = packet.getChildElement();
    Element dataElement = child.elementIterator().next();

    if ( !XMPPServer.getInstance().isLocal( packet.getFrom()) || !UserManager.getInstance().isRegisteredUser( packet.getFrom()) ) {
        replyPacket.setChildElement(packet.getChildElement().createCopy());
        replyPacket.setError(PacketError.Condition.service_unavailable);
        replyPacket.getError().setText( "Service available only to locally registered users." );
        return replyPacket;
    }

    if (dataElement != null) {
        if (IQ.Type.get.equals(packet.getType())) {
            Element dataStored = privateStorage.get(packet.getFrom().getNode(), dataElement);
            dataStored.setParent(null);

            child.remove(dataElement);
            child.setParent(null);
            replyPacket.setChildElement(child);
            child.add(dataStored);
        }
        else {
            if (privateStorage.isEnabled()) {
                privateStorage.add(packet.getFrom().getNode(), dataElement);
            } else {
                replyPacket.setChildElement(packet.getChildElement().createCopy());
                replyPacket.setError(PacketError.Condition.service_unavailable);
            }
        }
    }
    else {
        replyPacket.setChildElement("query", "jabber:iq:private");
    }
    return replyPacket;
}
 
Example 17
Source Project: Openfire   Source File: IQPrivacyHandler.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 18
Source Project: Openfire   Source File: IQPrivacyHandler.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * User has specified a new default list that should be used for all session.
 *
 * @param packet IQ packet setting new default list for all sessions.
 * @param from sender of the IQ packet.
 * @param listName name of the new default list for all sessions.
 * @return acknowledge of success.
 */
private IQ setDefaultList(IQ packet, JID from, String listName) {
    IQ result = IQ.createResultIQ(packet);
    Element childElement = packet.getChildElement().createCopy();
    result.setChildElement(childElement);

    if (sessionManager.getSessionCount(from.getNode()) > 1) {
        // Current default list is being used by more than one session
        result.setError(PacketError.Condition.conflict);
    }
    else {
        // Get the list
        PrivacyList list = manager.getPrivacyList(from.getNode(), listName);
        if (list != null) {
            // Get the user session
            ClientSession session = sessionManager.getSession(from);
            PrivacyList oldDefaultList = session.getDefaultList();
            manager.changeDefaultList(from.getNode(), list, oldDefaultList);
            // Set the new default list for this session (the only existing session)
            session.setDefaultList(list);
        }
        else {
            // List not found
            result.setError(PacketError.Condition.item_not_found);
        }
    }
    return result;
}
 
Example 19
Source Project: Openfire   Source File: IQPrivacyHandler.java    License: Apache License 2.0 5 votes vote down vote up
private IQ deleteList(IQ packet, JID from, String listName) {
    ClientSession currentSession;
    IQ result = IQ.createResultIQ(packet);
    Element childElement = packet.getChildElement().createCopy();
    result.setChildElement(childElement);
    // Get the list to delete
    PrivacyList list = manager.getPrivacyList(from.getNode(), listName);

    if (list == null) {
        // List to delete was not found
        result.setError(PacketError.Condition.item_not_found);
        return result;
    }
    else {
        currentSession = sessionManager.getSession(from);
        // Check if the list is being used by another session
        for (ClientSession session : sessionManager.getSessions(from.getNode())) {
            if (currentSession == session) {
                // Ignore the active session for this checking
                continue;
            }
            if (list.equals(session.getDefaultList()) || list.equals(session.getActiveList())) {
                // List to delete is being used by another session so return a conflict error
                result.setError(PacketError.Condition.conflict);
                return result;
            }
        }
    }
    // Remove the list from the active session (if it was being used)
    if (list.equals(currentSession.getDefaultList())) {
        currentSession.setDefaultList(null);
    }
    if (list.equals(currentSession.getActiveList())) {
        currentSession.setActiveList(null);
    }
    manager.deletePrivacyList(from.getNode(), listName);
    return result;
}
 
Example 20
Source Project: Openfire   Source File: FileTransferProxy.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void process(Packet packet) throws UnauthorizedException, PacketException {
    // Check if the packet is a disco request or a packet with namespace iq:register
    if (packet instanceof IQ) {
        if (handleIQ((IQ) packet)) {
            // Do nothing
        }
        else {
            IQ reply = IQ.createResultIQ((IQ) packet);
            reply.setChildElement(((IQ) packet).getChildElement().createCopy());
            reply.setError(PacketError.Condition.feature_not_implemented);
            router.route(reply);
        }
    }
}
 
Example 21
Source Project: Openfire   Source File: MultiplexerPacketHandler.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Sends an IQ error with the specified condition to the sender of the original
 * IQ packet.
 *
 * @param packet     the packet to be bounced.
 * @param extraError application specific error or null if none.
 * @param error the error.
 */
private void sendErrorPacket(IQ packet, PacketError.Condition error, Element extraError) {
    IQ reply = IQ.createResultIQ(packet);
    reply.setChildElement(packet.getChildElement().createCopy());
    reply.setError(error);
    if (extraError != null) {
        // Add specific application error if available
        reply.getError().getElement().add(extraError);
    }
    deliver(reply);
}
 
Example 22
Source Project: Openfire   Source File: MultiplexerPacketHandler.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Sends an IQ error with the specified condition to the sender of the original
 * IQ packet.
 *
 * @param packet     the packet to be bounced.
 * @param extraError application specific error or null if none.
 * @param error the error.
 */
private void sendErrorPacket(Route packet, PacketError.Condition error, Element extraError) {
    Route reply = new Route(packet.getStreamID());
    reply.setID(packet.getID());
    reply.setFrom(packet.getTo());
    reply.setTo(packet.getFrom());
    reply.setError(error);
    if (extraError != null) {
        // Add specific application error if available
        reply.getError().getElement().add(extraError);
    }
    deliver(reply);
}
 
Example 23
Source Project: Openfire   Source File: IQPEPHandler.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public IQ handleIQ(IQ packet) {
    // Do nothing if server is not enabled
    if (!isEnabled()) {
        IQ reply = IQ.createResultIQ(packet);
        reply.setChildElement(packet.getChildElement().createCopy());
        reply.setError(PacketError.Condition.service_unavailable);
        return reply;
    }

    if (packet.getTo() == null || packet.getTo().equals( new JID(XMPPServer.getInstance().getServerInfo().getXMPPDomain())) )
    {
        // packet addressed to service itself (not to a node/user)
        switch ( packet.getType() )
        {
            case set:
                return handleIQSetToService(packet );
            case get:
                return handleIQGetToService(packet );
            default:
                return null; // Ignore 'error' and 'result' stanzas.
        }
    }
    else
    {
        // packet was addressed to a node.
        if ( packet.isRequest() ) {
            return handleIQRequestToUser( packet );
        } else {
            return null; // Ignore IQ packets of type 'error' or 'result'.
        }
    }
}
 
Example 24
Source Project: Openfire   Source File: MultiplexerStanzaHandler.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Process stanza sent by a client that is connected to a connection manager. The
 * original stanza is wrapped in the route element. Only a single stanza must be
 * wrapped in the route element.
 *
 * @param packet the route element.
 */
private void processRoute(final Route packet) {
    if (session.getStatus() != Session.STATUS_AUTHENTICATED) {
        // Session is not authenticated so return error
        Route reply = new Route(packet.getStreamID());
        reply.setID(packet.getID());
        reply.setTo(packet.getFrom());
        reply.setFrom(packet.getTo());
        reply.setError(PacketError.Condition.not_authorized);
        session.process(reply);
        return;
    }
    // Process the packet
    packetHandler.route(packet);
}
 
Example 25
Source Project: Openfire   Source File: ComponentStanzaHandler.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void processPresence(Presence packet) throws UnauthorizedException {
    if (session.getStatus() != Session.STATUS_AUTHENTICATED) {
        // Session is not authenticated so return error
        Presence reply = new Presence();
        reply.setID(packet.getID());
        reply.setTo(packet.getFrom());
        reply.setFrom(packet.getTo());
        reply.setError(PacketError.Condition.not_authorized);
        session.process(reply);
        return;
    }
    super.processPresence(packet);
}
 
Example 26
Source Project: Openfire   Source File: ComponentStanzaHandler.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void processMessage(Message packet) throws UnauthorizedException {
    if (session.getStatus() != Session.STATUS_AUTHENTICATED) {
        // Session is not authenticated so return error
        Message reply = new Message();
        reply.setID(packet.getID());
        reply.setTo(packet.getFrom());
        reply.setFrom(packet.getTo());
        reply.setError(PacketError.Condition.not_authorized);
        session.process(reply);
        return;
    }
    super.processMessage(packet);
}
 
Example 27
Source Project: onos   Source File: XmppPubSubError.java    License: Apache License 2.0 5 votes vote down vote up
public PacketError asPacketError() {
    PacketError packetError = new PacketError(this.baseCondition);
    if (applicationCondition != null) {
        packetError.setApplicationCondition(applicationCondition.toString().toLowerCase(),
                                            PUBSUB_ERROR_NS);
    }
    return packetError;
}
 
Example 28
Source Project: onos   Source File: DefaultXmppDevice.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void sendError(PacketError packetError) {
    Packet packet = new IQ();
    packet.setTo(this.deviceId.getJid());
    packet.setFrom(new JID(XmppConstants.SERVER_JID));
    packet.setError(packetError);
    this.session.sendPacket(packet);
}
 
Example 29
Source Project: openfireLBS   Source File: LocationHandler.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public IQ handleIQ(IQ packet) throws UnauthorizedException {
	
	System.out.println(">>>>>>>>>>>>> RECV IQ: " + packet.toXML()); // XXX
	
	// get users near me(from JID)
	if (IQ.Type.get.equals(packet.getType())) {
		return getUsersNearme(packet);
	// set from JID's location to ...
	} else if (IQ.Type.set.equals(packet.getType())) {
		JID to = packet.getTo();
		
		// send from JID's location to to JID
		if (to.getNode() != null && !to.getNode().equals("")){  
			XMPPServer.getInstance().getIQRouter().route(packet); // route to another user 
			
			return IQ.createResultIQ(packet);
		// send from JID's location to server , and update ofLocation  
		}else{ 
			return updateLocation(packet);
		}
	} else {
		IQ reply = IQ.createResultIQ(packet);
		reply.setType(IQ.Type.error);
		reply.setError(PacketError.Condition.bad_request);
		return reply;
	}
}
 
Example 30
Source Project: Openfire   Source File: MultiUserChatServiceImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void processPacket(final Packet packet) {

    Log.trace( "Routing stanza: {}", packet.toXML() );
    if (!isServiceEnabled()) {
        Log.trace( "Service is disabled. Ignoring stanza." );
        return;
    }
    // The MUC service will receive all the packets whose domain matches the domain of the MUC
    // service. This means that, for instance, a disco request should be responded by the
    // service itself instead of relying on the server to handle the request.
    try {
        // Check if the packet is a disco request or a packet with namespace iq:register
        if (packet instanceof IQ) {
            if (process((IQ)packet)) {
                Log.trace( "Done processing IQ stanza." );
                return;
            }
        } else if (packet instanceof Message) {
            final Message msg = (Message) packet;
            if (msg.getType() == Message.Type.error) {
                // Bounced message, drop user.
                removeUser(packet.getFrom());
                Log.trace( "Done processing Message stanza." );
                return;
            }
        } else if (packet instanceof Presence) {
            final Presence pres = (Presence) packet;
            if (pres.getType() == Presence.Type.error) {
                // Bounced presence, drop user.
                removeUser(packet.getFrom());
                Log.trace( "Done processing Presence stanza." );
                return;
            }
        }

        if ( packet.getTo().getNode() == null )
        {
            Log.trace( "Stanza was addressed at the service itself, which by now should have been handled." );
            if ( packet instanceof IQ && ((IQ) packet).isRequest() )
            {
                final IQ reply = IQ.createResultIQ( (IQ) packet );
                reply.setChildElement( ((IQ) packet).getChildElement().createCopy() );
                reply.setError( PacketError.Condition.feature_not_implemented );
                router.route( reply );
            }
            Log.debug( "Ignoring stanza addressed at conference service: {}", packet.toXML() );
        }
        else
        {
            Log.trace( "The stanza is a normal packet that should possibly be sent to the room." );
            final JID recipient = packet.getTo();
            final String roomName = recipient != null ? recipient.getNode() : null;
            final JID userJid = packet.getFrom();
            Log.trace( "Stanza recipient: {}, room name: {}, sender: {}", recipient, roomName, userJid );
            try (final AutoCloseableReentrantLock.AutoCloseableLock ignored = new AutoCloseableReentrantLock(MultiUserChatServiceImpl.class, userJid.toString()).lock()) {
                if ( !packet.getElement().elements(FMUCHandler.FMUC).isEmpty() ) {
                    Log.trace( "Stanza is a FMUC stanza." );
                    final MUCRoom chatRoom = getChatRoom(roomName);
                    if ( chatRoom != null ) {
                        chatRoom.getFmucHandler().process(packet);
                    } else {
                        Log.warn( "Unable to process FMUC stanza, as room it's addressed to does not exist: {}", roomName );
                        // FIXME need to send error back in case of IQ request, and FMUC join. Might want to send error back in other cases too.
                    }
                } else {
                    Log.trace( "Stanza is a regular MUC stanza." );
                    getChatUser(userJid, roomName).process(packet);
                }
            }
        }
    }
    catch (final Exception e) {
        Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
    }
}