Search in sources :

Example 41 with XMPPException

use of org.jivesoftware.smack.XMPPException in project Smack by igniterealtime.

the class PingManager method ping.

/**
     * Pings the given jid. This method will return false if an error occurs.  The exception 
     * to this, is a server ping, which will always return true if the server is reachable, 
     * event if there is an error on the ping itself (i.e. ping not supported).
     * <p>
     * Use {@link #isPingSupported(Jid)} to determine if XMPP Ping is supported 
     * by the entity.
     * 
     * @param jid The id of the entity the ping is being sent to
     * @param pingTimeout The time to wait for a reply in milliseconds
     * @return true if a reply was received from the entity, false otherwise.
     * @throws NoResponseException if there was no response from the jid.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public boolean ping(Jid jid, long pingTimeout) throws NotConnectedException, NoResponseException, InterruptedException {
    final XMPPConnection connection = connection();
    // otherwise the client JID will be null causing an NPE
    if (!connection.isAuthenticated()) {
        throw new NotConnectedException();
    }
    Ping ping = new Ping(jid);
    try {
        connection.createStanzaCollectorAndSend(ping).nextResultOrThrow(pingTimeout);
    } catch (XMPPException exc) {
        return jid.equals(connection.getXMPPServiceDomain());
    }
    return true;
}
Also used : NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) Ping(org.jivesoftware.smackx.ping.packet.Ping) XMPPConnection(org.jivesoftware.smack.XMPPConnection) XMPPException(org.jivesoftware.smack.XMPPException)

Example 42 with XMPPException

use of org.jivesoftware.smack.XMPPException in project Smack by igniterealtime.

the class Workgroup method isEmailAvailable.

/**
     * The workgroup service may be configured to send email. This queries the Workgroup Service
     * to see if the email service has been configured and is available.
     *
     * @return true if the email service is available, otherwise return false.
     * @throws SmackException 
     * @throws InterruptedException 
     */
public boolean isEmailAvailable() throws SmackException, InterruptedException {
    ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(connection);
    try {
        DomainBareJid workgroupService = workgroupJID.asDomainBareJid();
        DiscoverInfo infoResult = discoManager.discoverInfo(workgroupService);
        return infoResult.containsFeature("jive:email:provider");
    } catch (XMPPException e) {
        return false;
    }
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) XMPPException(org.jivesoftware.smack.XMPPException) DomainBareJid(org.jxmpp.jid.DomainBareJid) ServiceDiscoveryManager(org.jivesoftware.smackx.disco.ServiceDiscoveryManager)

Example 43 with XMPPException

use of org.jivesoftware.smack.XMPPException in project Smack by igniterealtime.

the class TlsTest method tlsTest.

public static boolean tlsTest(EntityBareJid jid, String password, String host, int port, String tlsPin, boolean shouldThrow) throws KeyManagementException, NoSuchAlgorithmException {
    XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
    // @formatter:off
    builder.setUsernameAndPassword(jid.getLocalpart(), password).setXmppDomain(JidCreate.domainBareFrom(jid.getDomain())).setHost(host).setPort(port).setSecurityMode(SecurityMode.required);
    // @formatter:on
    builder.setDebuggerEnabled(DEBUG);
    if (StringUtils.isNotEmpty(tlsPin)) {
        SSLContext sslContext = Java7Pinning.forPin(tlsPin);
        builder.setCustomSSLContext(sslContext);
    }
    XMPPTCPConnection connection = new XMPPTCPConnection(builder.build());
    connection.setReplyTimeout(20000);
    try {
        connection.connect().login();
        if (shouldThrow) {
            // Test not success, should have throwed on login().
            return false;
        }
    } catch (SecurityRequiredByClientException e) {
        if (!shouldThrow) {
            return false;
        }
    } catch (XMPPException | SmackException | IOException | InterruptedException e) {
        throw new IllegalStateException(e);
    } finally {
        connection.disconnect();
    }
    return true;
}
Also used : XMPPTCPConnection(org.jivesoftware.smack.tcp.XMPPTCPConnection) SmackException(org.jivesoftware.smack.SmackException) SecurityRequiredByClientException(org.jivesoftware.smack.SmackException.SecurityRequiredByClientException) XMPPTCPConnectionConfiguration(org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) XMPPException(org.jivesoftware.smack.XMPPException)

Example 44 with XMPPException

use of org.jivesoftware.smack.XMPPException in project Smack by igniterealtime.

the class STUNResolver method initialize.

/**
     * Initialize the resolver.
     *
     * @throws XMPPException
     */
@Override
public void initialize() throws XMPPException {
    LOGGER.fine("Initialized");
    if (!isResolving() && !isResolved()) {
        // Get the best STUN server available
        if (currentServer.isNull()) {
            loadSTUNServers();
        }
        // We should have a valid STUN server by now...
        if (!currentServer.isNull()) {
            clearCandidates();
            resolverThread = new Thread(new Runnable() {

                @Override
                public void run() {
                    // to the STUN server for our address.
                    try {
                        Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
                        String candAddress;
                        int candPort;
                        while (ifaces.hasMoreElements()) {
                            NetworkInterface iface = ifaces.nextElement();
                            Enumeration<InetAddress> iaddresses = iface.getInetAddresses();
                            while (iaddresses.hasMoreElements()) {
                                InetAddress iaddress = iaddresses.nextElement();
                                if (!iaddress.isLoopbackAddress() && !iaddress.isLinkLocalAddress()) {
                                    // Reset the candidate
                                    candAddress = null;
                                    candPort = -1;
                                    DiscoveryTest test = new DiscoveryTest(iaddress, currentServer.getHostname(), currentServer.getPort());
                                    try {
                                        // Run the tests and get the
                                        // discovery
                                        // information, where all the
                                        // info is stored...
                                        DiscoveryInfo di = test.test();
                                        candAddress = di.getPublicIP() != null ? di.getPublicIP().getHostAddress() : null;
                                        // Get a valid port
                                        if (defaultPort == 0) {
                                            candPort = getFreePort();
                                        } else {
                                            candPort = defaultPort;
                                        }
                                        // add it to the list.
                                        if (candAddress != null && candPort >= 0) {
                                            TransportCandidate candidate = new TransportCandidate.Fixed(candAddress, candPort);
                                            candidate.setLocalIp(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName());
                                            addCandidate(candidate);
                                            resolvedPublicIP = candidate.getIp();
                                            resolvedLocalIP = candidate.getLocalIp();
                                            return;
                                        }
                                    } catch (Exception e) {
                                        LOGGER.log(Level.SEVERE, "Exception", e);
                                    }
                                }
                            }
                        }
                    } catch (SocketException e) {
                        LOGGER.log(Level.SEVERE, "Exception", e);
                    } finally {
                        setInitialized();
                    }
                }
            }, "Waiting for all the transport candidates checks...");
            resolverThread.setName("STUN resolver");
            resolverThread.start();
        } else {
            throw new IllegalStateException("No valid STUN server found.");
        }
    }
}
Also used : SocketException(java.net.SocketException) NetworkInterface(java.net.NetworkInterface) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) SocketException(java.net.SocketException) XMPPException(org.jivesoftware.smack.XMPPException) DiscoveryInfo(de.javawi.jstun.test.DiscoveryInfo) DiscoveryTest(de.javawi.jstun.test.DiscoveryTest) InetAddress(java.net.InetAddress)

Example 45 with XMPPException

use of org.jivesoftware.smack.XMPPException in project Smack by igniterealtime.

the class Socks5ByteStreamManagerTest method shouldFailIfTargetDoesNotSupportSocks5.

/**
     * Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid)} should throw an exception
     * if the given target does not support SOCKS5 Bytestream.
     * @throws XMPPException 
     */
@Test
public void shouldFailIfTargetDoesNotSupportSocks5() throws XMPPException {
    Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
    try {
        // build empty discover info as reply if targets features are queried
        DiscoverInfo discoverInfo = new DiscoverInfo();
        protocol.addResponse(discoverInfo);
        // start SOCKS5 Bytestream
        byteStreamManager.establishSession(targetJID);
        fail("exception should be thrown");
    } catch (FeatureNotSupportedException e) {
        assertTrue(e.getFeature().equals("SOCKS5 Bytestream"));
        assertTrue(e.getJid().equals(targetJID));
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) FeatureNotSupportedException(org.jivesoftware.smack.SmackException.FeatureNotSupportedException) 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

XMPPException (org.jivesoftware.smack.XMPPException)61 ArrayList (java.util.ArrayList)15 JingleSessionRequestListener (org.jivesoftware.smackx.jingle.listeners.JingleSessionRequestListener)13 JingleMediaManager (org.jivesoftware.smackx.jingle.media.JingleMediaManager)13 IOException (java.io.IOException)11 SmackException (org.jivesoftware.smack.SmackException)11 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)9 XMPPConnection (org.jivesoftware.smack.XMPPConnection)9 Message (org.jivesoftware.smack.packet.Message)8 PayloadType (org.jivesoftware.smackx.jingle.media.PayloadType)6 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)5 FixedResolver (org.jivesoftware.smackx.jingle.nat.FixedResolver)5 FixedTransportManager (org.jivesoftware.smackx.jingle.nat.FixedTransportManager)5 TransportCandidate (org.jivesoftware.smackx.jingle.nat.TransportCandidate)5 Chat (org.jivesoftware.smack.Chat)4 TCPConnection (org.jivesoftware.smack.TCPConnection)4 Form (org.jivesoftware.smackx.Form)4 JingleSessionListener (org.jivesoftware.smackx.jingle.listeners.JingleSessionListener)4 JmfMediaManager (org.jivesoftware.smackx.jingle.mediaimpl.jmf.JmfMediaManager)4 ICETransportManager (org.jivesoftware.smackx.jingle.nat.ICETransportManager)4