Search in sources :

Example 11 with DiscoverInfoBuilder

use of org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder in project Smack by igniterealtime.

the class ConfigureFormTest method getConfigFormWithTimeout.

@Test
public void getConfigFormWithTimeout() throws XMPPException, InterruptedException, SmackException, IOException {
    ThreadedDummyConnection con = ThreadedDummyConnection.newInstance();
    PubSubManager mgr = new PubSubManager(con, PubSubManagerTest.DUMMY_PUBSUB_SERVICE);
    DiscoverInfoBuilder info = DiscoverInfo.builder("disco-result").ofType(IQ.Type.result).from(PubSubManagerTest.DUMMY_PUBSUB_SERVICE);
    Identity ident = new Identity("pubsub", null, "leaf");
    info.addIdentity(ident);
    DiscoverInfo discoverInfo = info.build();
    con.addIQReply(discoverInfo);
    Node node = mgr.getNode("princely_musings");
    SmackConfiguration.setDefaultReplyTimeout(100);
    con.setTimeout();
    assertThrows(SmackException.class, () -> {
        node.getNodeConfiguration();
    });
}
Also used : ThreadedDummyConnection(org.jivesoftware.smack.ThreadedDummyConnection) DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) DiscoverInfoBuilder(org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder) Identity(org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity) Test(org.junit.jupiter.api.Test)

Example 12 with DiscoverInfoBuilder

use of org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder in project Smack by igniterealtime.

the class ServiceDiscoveryManager method renewEntityCapsVersion.

/**
 * Notify the {@link EntityCapabilitiesChangedListener} about changed capabilities.
 */
private synchronized void renewEntityCapsVersion() {
    renewEntityCapsRequested++;
    if (renewEntityCapsScheduledAction != null) {
        boolean canceled = renewEntityCapsScheduledAction.cancel();
        if (canceled) {
            scheduledRenewEntityCapsAvoided++;
        }
    }
    final XMPPConnection connection = connection();
    renewEntityCapsScheduledAction = scheduleBlocking(() -> {
        renewEntityCapsPerformed.incrementAndGet();
        DiscoverInfoBuilder discoverInfoBuilder = DiscoverInfo.builder("synthetized-disco-info-response").ofType(IQ.Type.result);
        addDiscoverInfoTo(discoverInfoBuilder);
        DiscoverInfo synthesizedDiscoveryInfo = discoverInfoBuilder.build();
        for (EntityCapabilitiesChangedListener entityCapabilitiesChangedListener : entityCapabilitiesChangedListeners) {
            entityCapabilitiesChangedListener.onEntityCapabilitiesChanged(synthesizedDiscoveryInfo);
        }
        // Re-send the last sent presence, and let the stanza interceptor
        // add a <c/> node to it.
        // See http://xmpp.org/extensions/xep-0115.html#advertise
        // We only send a presence packet if there was already one send
        // to respect ConnectionConfiguration.isSendPresence()
        final Presence presenceSend = this.presenceSend;
        if (connection.isAuthenticated() && presenceSend != null) {
            Presence presence = presenceSend.asBuilder(connection).build();
            try {
                connection.sendStanza(presence);
            } catch (InterruptedException | NotConnectedException e) {
                LOGGER.log(Level.WARNING, "Could could not update presence with caps info", e);
            }
        }
    }, RENEW_ENTITY_CAPS_DELAY_MILLIS, TimeUnit.MILLISECONDS);
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) DiscoverInfoBuilder(org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder) Presence(org.jivesoftware.smack.packet.Presence) XMPPConnection(org.jivesoftware.smack.XMPPConnection)

Example 13 with DiscoverInfoBuilder

use of org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder in project Smack by igniterealtime.

the class DiscoverInfoProvider method parse.

@Override
public DiscoverInfo parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    DiscoverInfoBuilder discoverInfoBuilder = DiscoverInfo.builder(iqData);
    String node = parser.getAttributeValue("node");
    discoverInfoBuilder.setNode(node);
    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        if (eventType == XmlPullParser.Event.START_ELEMENT) {
            final String name = parser.getName();
            final String namespace = parser.getNamespace();
            if (namespace.equals(DiscoverInfo.NAMESPACE)) {
                switch(name) {
                    case "identity":
                        String category = parser.getAttributeValue("category");
                        String identityName = parser.getAttributeValue("name");
                        String type = parser.getAttributeValue("type");
                        String lang = ParserUtils.getXmlLang(parser);
                        DiscoverInfo.Identity identity = new DiscoverInfo.Identity(category, type, identityName, lang);
                        discoverInfoBuilder.addIdentity(identity);
                        break;
                    case "feature":
                        String feature = parser.getAttributeValue("var");
                        discoverInfoBuilder.addFeature(feature);
                        break;
                }
            } else // Otherwise, it must be a packet extension.
            {
                PacketParserUtils.addExtensionElement(discoverInfoBuilder, parser, xmlEnvironment);
            }
        } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
            if (parser.getDepth() == initialDepth) {
                break outerloop;
            }
        }
    }
    DiscoverInfo discoverInfo = discoverInfoBuilder.buildWithoutValidiation();
    return discoverInfo;
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) DiscoverInfoBuilder(org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder)

Example 14 with DiscoverInfoBuilder

use of org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder in project Smack by igniterealtime.

the class Socks5ByteStreamManagerTest method shouldFailIfNoSocks5ProxyFound2.

/**
 * Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid, String)} should fail if no
 * proxy is a SOCKS5 proxy.
 *
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws SmackException if Smack detected an exceptional situation.
 * @throws XMPPException if an XMPP protocol error was received.
 * @throws IOException if an I/O error occurred.
 */
@Test
public void shouldFailIfNoSocks5ProxyFound2() throws SmackException, InterruptedException, IOException, XMPPException {
    final Protocol protocol = new Protocol();
    final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
    final String sessionID = "session_id_shouldFailIfNoSocks5ProxyFound2";
    // get Socks5ByteStreamManager for connection
    Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
    byteStreamManager.setAnnounceLocalStreamHost(false);
    /**
     * create responses in the order they should be queried specified by the XEP-0065
     * specification
     */
    // build discover info that supports the SOCKS5 feature
    DiscoverInfoBuilder discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID);
    discoverInfo.addFeature(Bytestream.NAMESPACE);
    // return that SOCKS5 is supported if target is queried
    protocol.addResponse(discoverInfo.build(), Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build discover items containing a proxy item
    DiscoverItems discoverItems = Socks5PacketUtils.createDiscoverItems(xmppServer, initiatorJID);
    Item item = new Item(proxyJID);
    discoverItems.addItem(item);
    // return the proxy item if XMPP server is queried
    protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build discover info for proxy containing information about NOT being a Socks5
    // proxy
    DiscoverInfoBuilder proxyInfo = Socks5PacketUtils.createDiscoverInfo(proxyJID, initiatorJID);
    Identity identity = new Identity("noproxy", proxyJID.toString(), "bytestreams");
    proxyInfo.addIdentity(identity);
    // return the proxy identity if proxy is queried
    protocol.addResponse(proxyInfo.build(), Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    SmackException e = assertThrows(SmackException.class, () -> {
        // start SOCKS5 Bytestream
        byteStreamManager.establishSession(targetJID, sessionID);
    });
    protocol.verifyAll();
    assertTrue(e.getMessage().contains("no SOCKS5 proxies available"));
}
Also used : Item(org.jivesoftware.smackx.disco.packet.DiscoverItems.Item) DiscoverInfoBuilder(org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder) SmackException(org.jivesoftware.smack.SmackException) DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems) XMPPConnection(org.jivesoftware.smack.XMPPConnection) Protocol(org.jivesoftware.util.Protocol) Identity(org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity) Test(org.junit.jupiter.api.Test)

Example 15 with DiscoverInfoBuilder

use of org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder in project Smack by igniterealtime.

the class Socks5ByteStreamManagerTest method shouldFailIfNoSocks5ProxyFound1.

/**
 * Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid, String)} should fail if XMPP
 * server doesn't return any proxies.
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws SmackException if Smack detected an exceptional situation.
 * @throws XMPPException if an XMPP protocol error was received.
 * @throws IOException if an I/O error occurred.
 */
@Test
public void shouldFailIfNoSocks5ProxyFound1() throws SmackException, InterruptedException, IOException, XMPPException {
    final Protocol protocol = new Protocol();
    final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
    final String sessionID = "session_id_shouldFailIfNoSocks5ProxyFound1";
    // get Socks5ByteStreamManager for connection
    Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
    byteStreamManager.setAnnounceLocalStreamHost(false);
    /**
     * create responses in the order they should be queried specified by the XEP-0065
     * specification
     */
    // build discover info that supports the SOCKS5 feature
    DiscoverInfoBuilder discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID);
    discoverInfo.addFeature(Bytestream.NAMESPACE);
    // return that SOCKS5 is supported if target is queried
    protocol.addResponse(discoverInfo.build(), Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build discover items with no proxy items
    DiscoverItems discoverItems = Socks5PacketUtils.createDiscoverItems(xmppServer, initiatorJID);
    // return the item with no proxy if XMPP server is queried
    protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    SmackException e = assertThrows(SmackException.class, () -> {
        // start SOCKS5 Bytestream
        byteStreamManager.establishSession(targetJID, sessionID);
        fail("exception should be thrown");
    });
    protocol.verifyAll();
    assertTrue(e.getMessage().contains("no SOCKS5 proxies available"));
}
Also used : DiscoverInfoBuilder(org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder) SmackException(org.jivesoftware.smack.SmackException) DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems) XMPPConnection(org.jivesoftware.smack.XMPPConnection) Protocol(org.jivesoftware.util.Protocol) Test(org.junit.jupiter.api.Test)

Aggregations

DiscoverInfoBuilder (org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder)16 XMPPConnection (org.jivesoftware.smack.XMPPConnection)10 Identity (org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity)10 Test (org.junit.jupiter.api.Test)10 DiscoverItems (org.jivesoftware.smackx.disco.packet.DiscoverItems)9 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)8 Protocol (org.jivesoftware.util.Protocol)8 Item (org.jivesoftware.smackx.disco.packet.DiscoverItems.Item)7 Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)6 SmackException (org.jivesoftware.smack.SmackException)4 LinkedList (java.util.LinkedList)3 ErrorIQ (org.jivesoftware.smack.packet.ErrorIQ)3 IQ (org.jivesoftware.smack.packet.IQ)3 StreamHost (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost)3 DataForm (org.jivesoftware.smackx.xdata.packet.DataForm)3 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 List (java.util.List)2 ThreadedDummyConnection (org.jivesoftware.smack.ThreadedDummyConnection)2 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)2