Search in sources :

Example 11 with DiscoverInfo

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

the class STUN method serviceAvailable.

/**
     * Check if the server support STUN Service.
     *
     * @param connection the connection
     * @return true if the server support STUN
     * @throws SmackException 
     * @throws XMPPException 
     * @throws InterruptedException 
     */
public static boolean serviceAvailable(XMPPConnection connection) throws XMPPException, SmackException, InterruptedException {
    if (!connection.isConnected()) {
        return false;
    }
    LOGGER.fine("Service listing");
    ServiceDiscoveryManager disco = ServiceDiscoveryManager.getInstanceFor(connection);
    DiscoverItems items = disco.discoverItems(connection.getXMPPServiceDomain());
    for (DiscoverItems.Item item : items.getItems()) {
        DiscoverInfo info = disco.discoverInfo(item.getEntityID());
        for (DiscoverInfo.Identity identity : info.getIdentities()) {
            if (identity.getCategory().equals("proxy") && identity.getType().equals("stun"))
                if (info.containsFeature(NAMESPACE))
                    return true;
        }
        LOGGER.fine(item.getName() + "-" + info.getType());
    }
    return false;
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems) ServiceDiscoveryManager(org.jivesoftware.smackx.disco.ServiceDiscoveryManager)

Example 12 with DiscoverInfo

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

the class Node method discoverInfo.

/**
     * Discover node information in standard {@link DiscoverInfo} format.
     * 
     * @return The discovery information about the node.
     * @throws XMPPErrorException 
     * @throws NoResponseException if there was no response from the server.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public DiscoverInfo discoverInfo() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    DiscoverInfo info = new DiscoverInfo();
    info.setTo(pubSubManager.getServiceJid());
    info.setNode(getId());
    return pubSubManager.getConnection().createStanzaCollectorAndSend(info).nextResultOrThrow();
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo)

Example 13 with DiscoverInfo

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

the class PubSubManager method getNode.

/**
     * Retrieves the requested node, if it exists.  It will throw an 
     * exception if it does not.
     * 
     * @param id - The unique id of the node
     * @return the node
     * @throws XMPPErrorException The node does not exist
     * @throws NoResponseException if there was no response from the server.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public <T extends Node> T getNode(String id) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    Node node = nodeMap.get(id);
    if (node == null) {
        DiscoverInfo info = new DiscoverInfo();
        info.setTo(pubSubService);
        info.setNode(id);
        DiscoverInfo infoReply = connection().createStanzaCollectorAndSend(info).nextResultOrThrow();
        if (infoReply.hasIdentity(PubSub.ELEMENT, "leaf")) {
            node = new LeafNode(this, id);
        } else if (infoReply.hasIdentity(PubSub.ELEMENT, "collection")) {
            node = new CollectionNode(this, id);
        } else {
            // If this is not the case, then we are dealing with an PubSub implementation that doesn't follow the specification.
            throw new AssertionError("PubSub service '" + pubSubService + "' returned disco info result for node '" + id + "', but it did not contain an Identity of type 'leaf' or 'collection' (and category 'pubsub'), which is not allowed according to XEP-60 5.3.");
        }
        nodeMap.put(id, node);
    }
    @SuppressWarnings("unchecked") T res = (T) node;
    return res;
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo)

Example 14 with DiscoverInfo

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

the class EntityCapsTest method testPreventDiscoInfo.

/**
     * Test if entity caps actually prevent a disco info request and reply.
     * 
     * @throws XMPPException
     * @throws InterruptedException 
     * @throws NotConnectedException 
     * @throws NoResponseException 
     * 
     */
@SmackIntegrationTest
public void testPreventDiscoInfo() throws XMPPException, NoResponseException, NotConnectedException, InterruptedException {
    final String dummyFeature = getNewDummyFeature();
    conOne.addPacketSendingListener(new StanzaListener() {

        @Override
        public void processStanza(Stanza stanza) {
            discoInfoSend = true;
        }
    }, new AndFilter(new StanzaTypeFilter(DiscoverInfo.class), IQTypeFilter.GET));
    // add a bogus feature so that con1 ver won't match con0's
    sdmTwo.addFeature(dummyFeature);
    dropCapsCache();
    // discover that
    DiscoverInfo info = sdmOne.discoverInfo(conTwo.getUser());
    // that discovery should cause a disco#info
    assertTrue(discoInfoSend);
    assertTrue(info.containsFeature(dummyFeature));
    discoInfoSend = false;
    // discover that
    info = sdmOne.discoverInfo(conTwo.getUser());
    // that discovery shouldn't cause a disco#info
    assertFalse(discoInfoSend);
    assertTrue(info.containsFeature(dummyFeature));
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) Stanza(org.jivesoftware.smack.packet.Stanza) StanzaListener(org.jivesoftware.smack.StanzaListener) SmackIntegrationTest(org.igniterealtime.smack.inttest.SmackIntegrationTest) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest)

Example 15 with DiscoverInfo

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

the class Socks5ByteStreamManagerTest method shouldFailIfTargetDoesNotAcceptSocks5Bytestream.

/**
     * Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid, String)} should fail if the
     * target does not accept a SOCKS5 Bytestream. See <a
     * href="http://xmpp.org/extensions/xep-0065.html#usecase-alternate">XEP-0065 Section 5.2 A2</a>
     */
@Test
public void shouldFailIfTargetDoesNotAcceptSocks5Bytestream() {
    // disable clients local SOCKS5 proxy
    Socks5Proxy.setLocalSocks5ProxyEnabled(false);
    // get Socks5ByteStreamManager for connection
    Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
    /**
         * create responses in the order they should be queried specified by the XEP-0065
         * specification
         */
    // build discover info that supports the SOCKS5 feature
    DiscoverInfo discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID);
    discoverInfo.addFeature(Bytestream.NAMESPACE);
    // return that SOCKS5 is supported if target is queried
    protocol.addResponse(discoverInfo, 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 being a SOCKS5 proxy
    DiscoverInfo proxyInfo = Socks5PacketUtils.createDiscoverInfo(proxyJID, initiatorJID);
    Identity identity = new Identity("proxy", proxyJID.toString(), "bytestreams");
    proxyInfo.addIdentity(identity);
    // return the socks5 bytestream proxy identity if proxy is queried
    protocol.addResponse(proxyInfo, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build a socks5 stream host info containing the address and the port of the
    // proxy
    Bytestream streamHostInfo = Socks5PacketUtils.createBytestreamResponse(proxyJID, initiatorJID);
    streamHostInfo.addStreamHost(proxyJID, proxyAddress, 7778);
    // return stream host info if it is queried
    protocol.addResponse(streamHostInfo, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build error packet to reject SOCKS5 Bytestream
    XMPPError.Builder builder = XMPPError.getBuilder(XMPPError.Condition.not_acceptable);
    IQ rejectPacket = new ErrorIQ(builder);
    rejectPacket.setFrom(targetJID);
    rejectPacket.setTo(initiatorJID);
    // return error packet as response to the bytestream initiation
    protocol.addResponse(rejectPacket, Verification.correspondingSenderReceiver, Verification.requestTypeSET);
    try {
        // start SOCKS5 Bytestream
        byteStreamManager.establishSession(targetJID, sessionID);
        fail("exception should be thrown");
    } catch (XMPPErrorException e) {
        protocol.verifyAll();
        assertEquals(rejectPacket.getError(), e.getXMPPError());
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) Item(org.jivesoftware.smackx.disco.packet.DiscoverItems.Item) ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) IQ(org.jivesoftware.smack.packet.IQ) DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems) XMPPError(org.jivesoftware.smack.packet.XMPPError) Identity(org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity) SmackException(org.jivesoftware.smack.SmackException) FeatureNotSupportedException(org.jivesoftware.smack.SmackException.FeatureNotSupportedException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) XMPPException(org.jivesoftware.smack.XMPPException) Test(org.junit.Test)

Aggregations

DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)53 Test (org.junit.Test)18 DiscoverItems (org.jivesoftware.smackx.disco.packet.DiscoverItems)12 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)11 Identity (org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity)11 IOException (java.io.IOException)8 SmackException (org.jivesoftware.smack.SmackException)8 XMPPException (org.jivesoftware.smack.XMPPException)8 Item (org.jivesoftware.smackx.disco.packet.DiscoverItems.Item)8 ConnectException (java.net.ConnectException)7 FeatureNotSupportedException (org.jivesoftware.smack.SmackException.FeatureNotSupportedException)7 ServiceDiscoveryManager (org.jivesoftware.smackx.disco.ServiceDiscoveryManager)7 DataForm (org.jivesoftware.smackx.xdata.packet.DataForm)7 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)7 IQ (org.jivesoftware.smack.packet.IQ)6 Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)6 FormField (org.jivesoftware.smackx.xdata.FormField)6 XMPPConnection (org.jivesoftware.smack.XMPPConnection)5 ArrayList (java.util.ArrayList)4 ThreadedDummyConnection (org.jivesoftware.smack.ThreadedDummyConnection)4