Java Code Examples for org.jxmpp.jid.Jid#equals()

The following examples show how to use org.jxmpp.jid.Jid#equals() . 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 File: IoTProvisioningManager.java    From Smack with Apache License 2.0 6 votes vote down vote up
private boolean isFromProvisioningService(Stanza stanza, boolean log) {
    Jid provisioningServer;
    try {
        provisioningServer = getConfiguredProvisioningServer();
    }
    catch (NotConnectedException | InterruptedException | NoResponseException | XMPPErrorException e) {
        LOGGER.log(Level.WARNING, "Could determine provisioning server", e);
        return false;
    }
    if (provisioningServer == null) {
        if (log) {
            LOGGER.warning("Ignoring request '" + stanza
                            + "' because no provisioning server configured.");
        }
        return false;
    }
    if (!provisioningServer.equals(stanza.getFrom())) {
        if (log) {
            LOGGER.warning("Ignoring  request '" + stanza
                            + "' because not from provisioning server '" + provisioningServer
                            + "'.");
        }
        return false;
    }
    return true;
}
 
Example 2
Source File: JidUtil.java    From jxmpp with Apache License 2.0 5 votes vote down vote up
/**
 * Check if two JIDs are equals. Takes <code>null</code> values into consideration. Which means that this method will return <code>true</code> if both JIDs are <code>null</code>.
 *
 * @param jidOne The first JID to compare.
 * @param jidTwo The second JID to compare.
 * @return <code>true</code> if both JIDs are equals.
 * @since 0.7.0
 */
public static boolean equals(Jid jidOne, Jid jidTwo) {
	if (jidOne != null) {
		return jidOne.equals(jidTwo);
	}
	if (jidTwo != null) {
		return jidTwo.equals(jidOne);
	}

	return jidOne == jidTwo;
}
 
Example 3
Source File: AbstractFromToMatchesFilter.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Override
public final boolean accept(final Stanza stanza) {
    Jid stanzaAddress = getAddressToCompare(stanza);

    if (stanzaAddress == null) {
        return address == null;
    }

    if (ignoreResourcepart) {
        stanzaAddress = stanzaAddress.asBareJid();
    }

    return stanzaAddress.equals(address);
}
 
Example 4
Source File: RosterUtil.java    From Smack with Apache License 2.0 5 votes vote down vote up
public static void ensureSubscribedTo(final XMPPConnection connectionOne, final XMPPConnection connectionTwo,
                final Date deadline)
                throws NotLoggedInException, NotConnectedException, InterruptedException, TimeoutException {
    final Roster rosterOne = Roster.getInstanceFor(connectionOne);
    final BareJid jidTwo = connectionTwo.getUser().asBareJid();

    if (rosterOne.iAmSubscribedTo(jidTwo))
        return;

    final BareJid jidOne = connectionOne.getUser().asBareJid();
    final SubscribeListener subscribeListener = new SubscribeListener() {
        @Override
        public SubscribeAnswer processSubscribe(Jid from, Presence subscribeRequest) {
            if (from.equals(jidOne)) {
                return SubscribeAnswer.Approve;
            }
            return null;
        }
    };
    final Roster rosterTwo = Roster.getInstanceFor(connectionTwo);

    rosterTwo.addSubscribeListener(subscribeListener);
    try {
        rosterOne.sendSubscriptionRequest(jidTwo);
        waitUntilOtherEntityIsSubscribed(rosterTwo, jidOne, deadline);
    }
    finally {
        rosterTwo.removeSubscribeListener(subscribeListener);
    }
}
 
Example 5
Source File: PingManager.java    From Smack with Apache License 2.0 5 votes vote down vote up
private boolean isValidErrorPong(Jid destinationJid, XMPPErrorException xmppErrorException) {
    // If it is an error error response and the destination was our own service, then this must mean that the
    // service responded, i.e. is up and pingable.
    if (destinationJid.equals(connection().getXMPPServiceDomain())) {
        return true;
    }

    final StanzaError xmppError = xmppErrorException.getStanzaError();

    // We may received an error response from an intermediate service returning an error like
    // 'remote-server-not-found' or 'remote-server-timeout' to us (which would fake the 'from' address,
    // see RFC 6120 § 8.3.1 2.). Or the recipient could became unavailable.

    // Sticking with the current rules of RFC 6120/6121, it is undecidable at this point whether we received an
    // error response from the pinged entity or not. This is because a service-unavailable error condition is
    // *required* (as per the RFCs) to be send back in both relevant cases:
    // 1. When the receiving entity is unaware of the IQ request type. RFC 6120 § 8.4.:
    //    "If an intended recipient receives an IQ stanza of type "get" or
    //    "set" containing a child element qualified by a namespace it does
    //    not understand, then the entity MUST return an IQ stanza of type
    //    "error" with an error condition of <service-unavailable/>.
    //  2. When the receiving resource is not available. RFC 6121 § 8.5.3.2.3.

    // Some clients don't obey the first rule and instead send back a feature-not-implement condition with type 'cancel',
    // which allows us to consider this response as valid "error response" pong.
    StanzaError.Type type = xmppError.getType();
    StanzaError.Condition condition = xmppError.getCondition();
    return type == StanzaError.Type.CANCEL && condition == StanzaError.Condition.feature_not_implemented;
}
 
Example 6
Source File: XMPPUtils.java    From mangosta-android with Apache License 2.0 4 votes vote down vote up
public static boolean isAutenticatedJid(Jid jid) {
    return jid.equals(XMPPSession.getInstance().getUser().asBareJid());
}
 
Example 7
Source File: Roster.java    From Smack with Apache License 2.0 4 votes vote down vote up
@Override
public IQ handleIQRequest(IQ iqRequest) {
    final XMPPConnection connection = connection();
    RosterPacket rosterPacket = (RosterPacket) iqRequest;

    EntityFullJid ourFullJid = connection.getUser();
    if (ourFullJid == null) {
        LOGGER.warning("Ignoring roster push " + iqRequest + " while " + connection
                        + " has no bound resource. This may be a server bug.");
        return null;
    }

    // Roster push (RFC 6121, 2.1.6)
    // A roster push with a non-empty from not matching our address MUST be ignored
    EntityBareJid ourBareJid = ourFullJid.asEntityBareJid();
    Jid from = rosterPacket.getFrom();
    if (from != null) {
        if (from.equals(ourFullJid)) {
            // Since RFC 6121 roster pushes are no longer allowed to
            // origin from the full JID as it was the case with RFC
            // 3921. Log a warning an continue processing the push.
            // See also SMACK-773.
            LOGGER.warning(
                    "Received roster push from full JID. This behavior is since RFC 6121 not longer standard compliant. "
                            + "Please ask your server vendor to fix this and comply to RFC 6121 § 2.1.6. IQ roster push stanza: "
                            + iqRequest);
        } else if (!from.equals(ourBareJid)) {
            LOGGER.warning("Ignoring roster push with a non matching 'from' ourJid='" + ourBareJid + "' from='"
                    + from + "'");
            return IQ.createErrorResponse(iqRequest, Condition.service_unavailable);
        }
    }

    // A roster push must contain exactly one entry
    Collection<Item> items = rosterPacket.getRosterItems();
    if (items.size() != 1) {
        LOGGER.warning("Ignoring roster push with not exactly one entry. size=" + items.size());
        return IQ.createErrorResponse(iqRequest, Condition.bad_request);
    }

    Collection<Jid> addedEntries = new ArrayList<>();
    Collection<Jid> updatedEntries = new ArrayList<>();
    Collection<Jid> deletedEntries = new ArrayList<>();
    Collection<Jid> unchangedEntries = new ArrayList<>();

    // We assured above that the size of items is exactly 1, therefore we are able to
    // safely retrieve this single item here.
    Item item = items.iterator().next();
    RosterEntry entry = new RosterEntry(item, Roster.this, connection);
    String version = rosterPacket.getVersion();

    if (item.getItemType().equals(RosterPacket.ItemType.remove)) {
        deleteEntry(deletedEntries, entry);
        if (rosterStore != null) {
            rosterStore.removeEntry(entry.getJid(), version);
        }
    }
    else if (hasValidSubscriptionType(item)) {
        addUpdateEntry(addedEntries, updatedEntries, unchangedEntries, item, entry);
        if (rosterStore != null) {
            rosterStore.addEntry(item, version);
        }
    }

    removeEmptyGroups();

    // Fire event for roster listeners.
    fireRosterChangedEvent(addedEntries, updatedEntries, deletedEntries);

    return IQ.createResultIQ(rosterPacket);
}
 
Example 8
Source File: IntegrationTestRosterUtil.java    From Smack with Apache License 2.0 4 votes vote down vote up
public static void ensureSubscribedTo(final XMPPConnection presenceRequestReceiverConnection, final XMPPConnection presenceRequestingConnection, long timeout) throws TimeoutException, Exception {
    final Roster presenceRequestReceiverRoster = Roster.getInstanceFor(presenceRequestReceiverConnection);
    final Roster presenceRequestingRoster = Roster.getInstanceFor(presenceRequestingConnection);

    final EntityFullJid presenceRequestReceiverAddress = presenceRequestReceiverConnection.getUser();
    final EntityFullJid presenceRequestingAddress = presenceRequestingConnection.getUser();

    if (presenceRequestReceiverRoster.isSubscribedToMyPresence(presenceRequestingAddress)) {
        return;
    }

    final SubscribeListener subscribeListener = new SubscribeListener() {
        @Override
        public SubscribeAnswer processSubscribe(Jid from, Presence subscribeRequest) {
            if (from.equals(presenceRequestingConnection.getUser().asBareJid())) {
                return SubscribeAnswer.Approve;
            }
            return SubscribeAnswer.Deny;
        }
    };
    presenceRequestReceiverRoster.addSubscribeListener(subscribeListener);

    final SimpleResultSyncPoint syncPoint = new SimpleResultSyncPoint();
    final PresenceEventListener presenceEventListener = new AbstractPresenceEventListener() {
        @Override
        public void presenceSubscribed(BareJid address, Presence subscribedPresence) {
            if (!address.equals(presenceRequestReceiverAddress.asBareJid())) {
                return;
            }
            syncPoint.signal();
        }
    };
    presenceRequestingRoster.addPresenceEventListener(presenceEventListener);

    try {
        presenceRequestingRoster.sendSubscriptionRequest(presenceRequestReceiverAddress.asBareJid());

        syncPoint.waitForResult(timeout);
    } finally {
        presenceRequestReceiverRoster.removeSubscribeListener(subscribeListener);
        presenceRequestingRoster.removePresenceEventListener(presenceEventListener);
    }
}
 
Example 9
Source File: UserInvitationPane.java    From Spark with Apache License 2.0 4 votes vote down vote up
/**
 * Removes oneself as an owner of the room.
 *
 * @param muc the <code>MultiUserChat</code> of the chat room.
 */
private void removeOwner(MultiUserChat muc) {
    if (muc.isJoined()) {
        // Try and remove myself as an owner if I am one.
        Collection<Affiliate> owners = null;
        try {
            owners = muc.getOwners();
        }
        catch (XMPPException | SmackException | InterruptedException e1) {
            return;
        }

        if (owners == null) {
            return;
        }

        Iterator<Affiliate> iter = owners.iterator();

        List<Jid> list = new ArrayList<>();
        while (iter.hasNext()) {
            Affiliate affilitate = iter.next();
            Jid jid = affilitate.getJid();
            if (!jid.equals(SparkManager.getSessionManager().getBareUserAddress())) {
                list.add(jid);
            }
        }
        if (list.size() > 0) {
            try {
                Form form = muc.getConfigurationForm().createAnswerForm();
                List<String> jidStrings = JidUtil.toStringList(list);
                form.setAnswer("muc#roomconfig_roomowners", jidStrings);

                // new DataFormDialog(groupChat, form);
                muc.sendConfigurationForm(form);
            }
            catch (XMPPException | SmackException | InterruptedException e) {
                Log.error(e);
            }
        }
    }
}
 
Example 10
Source File: InvitationPane.java    From Spark with Apache License 2.0 4 votes vote down vote up
/**
 * Removes oneself as an owner of the room.
 *
 * @param muc the <code>MultiUserChat</code> of the chat room.
 */
private void removeOwner(MultiUserChat muc) {
    if (muc.isJoined()) {
        // Try and remove myself as an owner if I am one.
        Collection owners = null;
        try {
            owners = muc.getOwners();
        }
        catch (XMPPException | SmackException | InterruptedException e1) {
            return;
        }

        if (owners == null) {
            return;
        }

        Iterator iter = owners.iterator();

        List<Jid> list = new ArrayList<>();
        while (iter.hasNext()) {
            Affiliate affilitate = (Affiliate)iter.next();
            Jid jid = affilitate.getJid();
            if (!jid.equals(SparkManager.getSessionManager().getBareUserAddress())) {
                list.add(jid);
            }
        }
        if (list.size() > 0) {
            try {
                Form form = muc.getConfigurationForm().createAnswerForm();
                List<String> jidStrings = new ArrayList<>(list.size());
                JidUtil.toStrings(list, jidStrings);
                form.setAnswer("muc#roomconfig_roomowners", jidStrings);

                // new DataFormDialog(groupChat, form);
                muc.sendConfigurationForm(form);
            }
            catch (XMPPException | SmackException | InterruptedException e) {
                Log.error(e);
            }
        }
    }
}