Search in sources :

Example 1 with DomainBareJid

use of org.jxmpp.jid.DomainBareJid in project Smack by igniterealtime.

the class ServiceDiscoveryManager method findServicesDiscoverInfo.

/**
     * Find all services under the users service that provide a given feature.
     * 
     * @param feature the feature to search for
     * @param stopOnFirst if true, stop searching after the first service was found
     * @param useCache if true, query a cache first to avoid network I/O
     * @return a possible empty list of services providing the given feature
     * @throws NoResponseException
     * @throws XMPPErrorException
     * @throws NotConnectedException
     * @throws InterruptedException 
     */
public List<DiscoverInfo> findServicesDiscoverInfo(String feature, boolean stopOnFirst, boolean useCache) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    List<DiscoverInfo> serviceDiscoInfo = null;
    DomainBareJid serviceName = connection().getXMPPServiceDomain();
    if (useCache) {
        serviceDiscoInfo = services.lookup(feature);
        if (serviceDiscoInfo != null) {
            return serviceDiscoInfo;
        }
    }
    serviceDiscoInfo = new LinkedList<>();
    // Send the disco packet to the server itself
    DiscoverInfo info;
    try {
        info = discoverInfo(serviceName);
    } catch (XMPPErrorException e) {
        // Be extra robust here: Return the empty linked list and log this situation
        LOGGER.log(Level.WARNING, "Could not discover information about service", e);
        return serviceDiscoInfo;
    }
    // Check if the server supports the feature
    if (info.containsFeature(feature)) {
        serviceDiscoInfo.add(info);
        if (stopOnFirst) {
            if (useCache) {
                // Cache the discovered information
                services.put(feature, serviceDiscoInfo);
            }
            return serviceDiscoInfo;
        }
    }
    DiscoverItems items;
    try {
        // Get the disco items and send the disco packet to each server item
        items = discoverItems(serviceName);
    } catch (XMPPErrorException e) {
        LOGGER.log(Level.WARNING, "Could not discover items about service", e);
        return serviceDiscoInfo;
    }
    for (DiscoverItems.Item item : items.getItems()) {
        try {
            // TODO is it OK here in all cases to query without the node attribute?
            // MultipleRecipientManager queried initially also with the node attribute, but this
            // could be simply a fault instead of intentional.
            info = discoverInfo(item.getEntityID());
        } catch (XMPPErrorException | NoResponseException e) {
            // Don't throw this exceptions if one of the server's items fail
            LOGGER.log(Level.WARNING, "Exception while discovering info for feature " + feature + " of " + item.getEntityID() + " node: " + item.getNode(), e);
            continue;
        }
        if (info.containsFeature(feature)) {
            serviceDiscoInfo.add(info);
            //serviceAddresses.add(item.getEntityID().asDomainBareJid());
            if (stopOnFirst) {
                break;
            }
        }
    }
    if (useCache) {
        // Cache the discovered information
        services.put(feature, serviceDiscoInfo);
    }
    return serviceDiscoInfo;
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException) DomainBareJid(org.jxmpp.jid.DomainBareJid)

Example 2 with DomainBareJid

use of org.jxmpp.jid.DomainBareJid in project Smack by igniterealtime.

the class HttpFileUploadManager method uploadServiceFrom.

private static UploadService uploadServiceFrom(DiscoverInfo discoverInfo) {
    assert (containsHttpFileUploadNamespace(discoverInfo));
    UploadService.Version version;
    if (discoverInfo.containsFeature(NAMESPACE)) {
        version = Version.v0_3;
    } else if (discoverInfo.containsFeature(NAMESPACE_0_2)) {
        version = Version.v0_2;
    } else {
        throw new AssertionError();
    }
    DomainBareJid address = discoverInfo.getFrom().asDomainBareJid();
    DataForm dataForm = DataForm.from(discoverInfo);
    if (dataForm == null) {
        return new UploadService(address, version);
    }
    FormField field = dataForm.getField("max-file-size");
    if (field == null) {
        return new UploadService(address, version);
    }
    List<String> values = field.getValues();
    if (values.isEmpty()) {
        return new UploadService(address, version);
    }
    Long maxFileSize = Long.valueOf(values.get(0));
    return new UploadService(address, version, maxFileSize);
}
Also used : Version(org.jivesoftware.smackx.httpfileupload.UploadService.Version) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) DomainBareJid(org.jxmpp.jid.DomainBareJid) FormField(org.jivesoftware.smackx.xdata.FormField)

Example 3 with DomainBareJid

use of org.jxmpp.jid.DomainBareJid in project Smack by igniterealtime.

the class MultipleRecipientManager method send.

/**
     * Sends the specified stanza(/packet) to the collection of specified recipients using the specified
     * connection. If the server has support for XEP-33 then only one stanza(/packet) is going to be sent to
     * the server with the multiple recipient instructions. However, if XEP-33 is not supported by
     * the server then the client is going to send the stanza(/packet) to each recipient.
     * 
     * @param connection the connection to use to send the packet.
     * @param packet the stanza(/packet) to send to the list of recipients.
     * @param to the collection of JIDs to include in the TO list or <tt>null</tt> if no TO list exists.
     * @param cc the collection of JIDs to include in the CC list or <tt>null</tt> if no CC list exists.
     * @param bcc the collection of JIDs to include in the BCC list or <tt>null</tt> if no BCC list
     *        exists.
     * @param replyTo address to which all replies are requested to be sent or <tt>null</tt>
     *        indicating that they can reply to any address.
     * @param replyRoom JID of a MUC room to which responses should be sent or <tt>null</tt>
     *        indicating that they can reply to any address.
     * @param noReply true means that receivers should not reply to the message.
     * @throws XMPPErrorException if server does not support XEP-33: Extended Stanza Addressing and
     *         some XEP-33 specific features were requested.
     * @throws NoResponseException if there was no response from the server.
     * @throws FeatureNotSupportedException if special XEP-33 features where requested, but the
     *         server does not support them.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public static void send(XMPPConnection connection, Stanza packet, Collection<? extends Jid> to, Collection<? extends Jid> cc, Collection<? extends Jid> bcc, Jid replyTo, Jid replyRoom, boolean noReply) throws NoResponseException, XMPPErrorException, FeatureNotSupportedException, NotConnectedException, InterruptedException {
    // required at all and we can send it just as normal stanza without needing to add the extension element
    if (to != null && to.size() == 1 && (cc == null || cc.isEmpty()) && (bcc == null || bcc.isEmpty()) && !noReply && StringUtils.isNullOrEmpty(replyTo) && StringUtils.isNullOrEmpty(replyRoom)) {
        Jid toJid = to.iterator().next();
        packet.setTo(toJid);
        connection.sendStanza(packet);
        return;
    }
    DomainBareJid serviceAddress = getMultipleRecipienServiceAddress(connection);
    if (serviceAddress != null) {
        // Send packet to target users using multiple recipient service provided by the server
        sendThroughService(connection, packet, to, cc, bcc, replyTo, replyRoom, noReply, serviceAddress);
    } else {
        // Server does not support XEP-33 so try to send the packet to each recipient
        if (noReply || replyTo != null || replyRoom != null) {
            // the user that this features are not available
            throw new FeatureNotSupportedException("Extended Stanza Addressing");
        }
        // Send the packet to each individual recipient
        sendToIndividualRecipients(connection, packet, to, cc, bcc);
    }
}
Also used : Jid(org.jxmpp.jid.Jid) EntityFullJid(org.jxmpp.jid.EntityFullJid) DomainBareJid(org.jxmpp.jid.DomainBareJid) EntityBareJid(org.jxmpp.jid.EntityBareJid) FeatureNotSupportedException(org.jivesoftware.smack.SmackException.FeatureNotSupportedException) DomainBareJid(org.jxmpp.jid.DomainBareJid)

Example 4 with DomainBareJid

use of org.jxmpp.jid.DomainBareJid in project Smack by igniterealtime.

the class MultiUserChat method enter.

/**
     * Enter a room, as described in XEP-45 7.2.
     *
     * @param conf the configuration used to enter the room.
     * @return the returned presence by the service after the client send the initial presence in order to enter the room.
     * @throws NotConnectedException
     * @throws NoResponseException
     * @throws XMPPErrorException
     * @throws InterruptedException
     * @throws NotAMucServiceException 
     * @see <a href="http://xmpp.org/extensions/xep-0045.html#enter">XEP-45 7.2 Entering a Room</a>
     */
private Presence enter(MucEnterConfiguration conf) throws NotConnectedException, NoResponseException, XMPPErrorException, InterruptedException, NotAMucServiceException {
    final DomainBareJid mucService = room.asDomainBareJid();
    if (!KNOWN_MUC_SERVICES.containsKey(mucService)) {
        if (multiUserChatManager.providesMucService(mucService)) {
            KNOWN_MUC_SERVICES.put(mucService, null);
        } else {
            throw new NotAMucServiceException(this);
        }
    }
    // We enter a room by sending a presence packet where the "to"
    // field is in the form "roomName@service/nickname"
    Presence joinPresence = conf.getJoinPresence(this);
    // Setup the messageListeners and presenceListeners *before* the join presence is send.
    connection.addSyncStanzaListener(messageListener, fromRoomGroupchatFilter);
    connection.addSyncStanzaListener(presenceListener, new AndFilter(fromRoomFilter, StanzaTypeFilter.PRESENCE));
    connection.addSyncStanzaListener(subjectListener, new AndFilter(fromRoomFilter, MessageWithSubjectFilter.INSTANCE, new NotFilter(MessageTypeFilter.ERROR)));
    connection.addSyncStanzaListener(declinesListener, DECLINE_FILTER);
    connection.addPacketInterceptor(presenceInterceptor, new AndFilter(ToMatchesFilter.create(room), StanzaTypeFilter.PRESENCE));
    messageCollector = connection.createStanzaCollector(fromRoomGroupchatFilter);
    // Wait for a presence packet back from the server.
    // @formatter:off
    StanzaFilter responseFilter = new AndFilter(StanzaTypeFilter.PRESENCE, new OrFilter(// We use a bare JID filter for positive responses, since the MUC service/room may rewrite the nickname.
    new AndFilter(FromMatchesFilter.createBare(getRoom()), MUCUserStatusCodeFilter.STATUS_110_PRESENCE_TO_SELF), // JID we send the join presence to.
    new AndFilter(FromMatchesFilter.createFull(joinPresence.getTo()), new StanzaIdFilter(joinPresence), PresenceTypeFilter.ERROR)));
    // @formatter:on
    Presence presence;
    try {
        presence = connection.createStanzaCollectorAndSend(responseFilter, joinPresence).nextResultOrThrow(conf.getTimeout());
    } catch (NotConnectedException | InterruptedException | NoResponseException | XMPPErrorException e) {
        // Ensure that all callbacks are removed if there is an exception
        removeConnectionCallbacks();
        throw e;
    }
    // This presence must be send from a full JID. We use the resourcepart of this JID as nick, since the room may
    // performed roomnick rewriting
    this.nickname = presence.getFrom().asEntityFullJidIfPossible().getResourcepart();
    joined = true;
    // Update the list of joined rooms
    multiUserChatManager.addJoinedRoom(room);
    return presence;
}
Also used : StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) OrFilter(org.jivesoftware.smack.filter.OrFilter) NotAMucServiceException(org.jivesoftware.smackx.muc.MultiUserChatException.NotAMucServiceException) AndFilter(org.jivesoftware.smack.filter.AndFilter) StanzaIdFilter(org.jivesoftware.smack.filter.StanzaIdFilter) NotFilter(org.jivesoftware.smack.filter.NotFilter) Presence(org.jivesoftware.smack.packet.Presence) MUCInitialPresence(org.jivesoftware.smackx.muc.packet.MUCInitialPresence) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException) DomainBareJid(org.jxmpp.jid.DomainBareJid)

Example 5 with DomainBareJid

use of org.jxmpp.jid.DomainBareJid in project Smack by igniterealtime.

the class MultiUserChatLightManager method blockUsers.

/**
     * Block users.
     * 
     * @param mucLightService
     * @param usersJids
     * @throws NoResponseException
     * @throws XMPPErrorException
     * @throws NotConnectedException
     * @throws InterruptedException
     */
public void blockUsers(DomainBareJid mucLightService, List<Jid> usersJids) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    HashMap<Jid, Boolean> users = new HashMap<>();
    for (Jid jid : usersJids) {
        users.put(jid, false);
    }
    sendBlockUsers(mucLightService, users);
}
Also used : Jid(org.jxmpp.jid.Jid) DomainBareJid(org.jxmpp.jid.DomainBareJid) EntityBareJid(org.jxmpp.jid.EntityBareJid) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap)

Aggregations

DomainBareJid (org.jxmpp.jid.DomainBareJid)19 Jid (org.jxmpp.jid.Jid)11 EntityBareJid (org.jxmpp.jid.EntityBareJid)10 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 WeakHashMap (java.util.WeakHashMap)4 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)4 MUCLightBlockingIQ (org.jivesoftware.smackx.muclight.element.MUCLightBlockingIQ)3 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)2 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)2 ServiceDiscoveryManager (org.jivesoftware.smackx.disco.ServiceDiscoveryManager)2 DiscoverItems (org.jivesoftware.smackx.disco.packet.DiscoverItems)2 EntityFullJid (org.jxmpp.jid.EntityFullJid)2 CallbackHandler (javax.security.auth.callback.CallbackHandler)1 SmackIntegrationTest (org.igniterealtime.smack.inttest.SmackIntegrationTest)1 TestNotPossibleException (org.igniterealtime.smack.inttest.TestNotPossibleException)1 FeatureNotSupportedException (org.jivesoftware.smack.SmackException.FeatureNotSupportedException)1 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)1 XMPPConnection (org.jivesoftware.smack.XMPPConnection)1 XMPPException (org.jivesoftware.smack.XMPPException)1