Search in sources :

Example 6 with RemoteConnectionEndpointLookupFailure

use of org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure in project Smack by igniterealtime.

the class RemoteXmppTcpConnectionEndpointsTest method testConnectionException.

@Test
public void testConnectionException() {
    List<RemoteConnectionException<? extends RemoteConnectionEndpoint>> connectionExceptions = new ArrayList<>();
    {
        A aRr = new A("1.2.3.4");
        UInt16 port = UInt16.from(1234);
        String host = "example.org";
        IpTcpRemoteConnectionEndpoint<A> remoteConnectionEndpoint = new IpTcpRemoteConnectionEndpoint<>(host, port, aRr);
        Exception exception = new Exception("Failed for some reason");
        RemoteConnectionException<IpTcpRemoteConnectionEndpoint<A>> remoteConnectionException = RemoteConnectionException.from(remoteConnectionEndpoint, exception);
        connectionExceptions.add(remoteConnectionException);
    }
    {
        A aRr = new A("1.3.3.7");
        UInt16 port = UInt16.from(5678);
        String host = "other.example.org";
        IpTcpRemoteConnectionEndpoint<A> remoteConnectionEndpoint = new IpTcpRemoteConnectionEndpoint<>(host, port, aRr);
        Exception exception = new Exception("Failed for some other reason");
        RemoteConnectionException<IpTcpRemoteConnectionEndpoint<A>> remoteConnectionException = RemoteConnectionException.from(remoteConnectionEndpoint, exception);
        connectionExceptions.add(remoteConnectionException);
    }
    List<RemoteConnectionEndpointLookupFailure> lookupFailures = Collections.emptyList();
    SmackException.EndpointConnectionException endpointConnectionException = SmackException.EndpointConnectionException.from(lookupFailures, connectionExceptions);
    String message = endpointConnectionException.getMessage();
    assertEquals("The following addresses failed: " + "'RFC 6120 A/AAAA Endpoint + [example.org:1234] (/1.2.3.4:1234)' failed because: java.lang.Exception: Failed for some reason, " + "'RFC 6120 A/AAAA Endpoint + [other.example.org:5678] (/1.3.3.7:5678)' failed because: java.lang.Exception: Failed for some other reason", message);
}
Also used : A(org.minidns.record.A) RemoteConnectionException(org.jivesoftware.smack.util.rce.RemoteConnectionException) RemoteConnectionEndpointLookupFailure(org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure) SmackException(org.jivesoftware.smack.SmackException) ArrayList(java.util.ArrayList) SmackException(org.jivesoftware.smack.SmackException) RemoteConnectionException(org.jivesoftware.smack.util.rce.RemoteConnectionException) UInt16(org.jivesoftware.smack.datatypes.UInt16) RemoteConnectionEndpoint(org.jivesoftware.smack.util.rce.RemoteConnectionEndpoint) Test(org.junit.jupiter.api.Test)

Example 7 with RemoteConnectionEndpointLookupFailure

use of org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure in project Smack by igniterealtime.

the class WebSocketRemoteConnectionEndpointLookup method lookup.

public static Result lookup(DomainBareJid domainBareJid) {
    List<RemoteConnectionEndpointLookupFailure> lookupFailures = new ArrayList<>(1);
    List<URI> rcUriList = null;
    try {
        // Look for remote connection endpoints by making use of http lookup method described inside XEP-0156.
        rcUriList = HttpLookupMethod.lookup(domainBareJid, LinkRelation.WEBSOCKET);
    } catch (IOException | XmlPullParserException | URISyntaxException e) {
        lookupFailures.add(new RemoteConnectionEndpointLookupFailure.HttpLookupFailure(domainBareJid, e));
        return new Result(lookupFailures);
    }
    List<SecureWebSocketRemoteConnectionEndpoint> discoveredSecureEndpoints = new ArrayList<>(rcUriList.size());
    List<InsecureWebSocketRemoteConnectionEndpoint> discoveredInsecureEndpoints = new ArrayList<>(rcUriList.size());
    for (URI webSocketUri : rcUriList) {
        WebSocketRemoteConnectionEndpoint wsRce = WebSocketRemoteConnectionEndpoint.from(webSocketUri);
        if (wsRce instanceof SecureWebSocketRemoteConnectionEndpoint) {
            SecureWebSocketRemoteConnectionEndpoint secureWsRce = (SecureWebSocketRemoteConnectionEndpoint) wsRce;
            discoveredSecureEndpoints.add(secureWsRce);
        } else if (wsRce instanceof InsecureWebSocketRemoteConnectionEndpoint) {
            InsecureWebSocketRemoteConnectionEndpoint insecureWsRce = (InsecureWebSocketRemoteConnectionEndpoint) wsRce;
            discoveredInsecureEndpoints.add(insecureWsRce);
        } else {
            // WebSocketRemoteConnectionEndpoint.from() must return an instance which type is one of the above.
            throw new AssertionError();
        }
    }
    return new Result(discoveredSecureEndpoints, discoveredInsecureEndpoints, lookupFailures);
}
Also used : RemoteConnectionEndpointLookupFailure(org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) XmlPullParserException(org.jivesoftware.smack.xml.XmlPullParserException)

Example 8 with RemoteConnectionEndpointLookupFailure

use of org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure in project Smack by igniterealtime.

the class DNSJavaResolver method lookupSrvRecords0.

@Override
protected List<SRV> lookupSrvRecords0(DnsName name, List<RemoteConnectionEndpointLookupFailure> lookupFailures, DnssecMode dnssecMode) {
    Lookup lookup;
    try {
        lookup = new Lookup(name.ace, Type.SRV);
    } catch (TextParseException e) {
        RemoteConnectionEndpointLookupFailure failure = new RemoteConnectionEndpointLookupFailure.DnsLookupFailure(name, e);
        lookupFailures.add(failure);
        return null;
    }
    Record[] recs = lookup.run();
    if (recs == null) {
        // TODO: When does this happen? Do we want/need to record a lookup failure?
        return null;
    }
    List<SRV> res = new ArrayList<>();
    for (Record record : recs) {
        org.xbill.DNS.SRVRecord srvRecord = (org.xbill.DNS.SRVRecord) record;
        if (srvRecord != null && srvRecord.getTarget() != null) {
            DnsName host = DnsName.from(srvRecord.getTarget().toString());
            int port = srvRecord.getPort();
            int priority = srvRecord.getPriority();
            int weight = srvRecord.getWeight();
            SRV r = new SRV(priority, weight, port, host);
            res.add(r);
        }
    }
    return res;
}
Also used : DnsName(org.minidns.dnsname.DnsName) RemoteConnectionEndpointLookupFailure(org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure) ArrayList(java.util.ArrayList) SRV(org.minidns.record.SRV) Lookup(org.xbill.DNS.Lookup) Record(org.xbill.DNS.Record) TextParseException(org.xbill.DNS.TextParseException)

Aggregations

RemoteConnectionEndpointLookupFailure (org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure)8 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)3 InetAddress (java.net.InetAddress)2 UInt16 (org.jivesoftware.smack.datatypes.UInt16)2 DnsName (org.minidns.dnsname.DnsName)2 DnssecResolverApi (org.minidns.hla.DnssecResolverApi)2 ResolverApi (org.minidns.hla.ResolverApi)2 A (org.minidns.record.A)2 SRV (org.minidns.record.SRV)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 UnknownHostException (java.net.UnknownHostException)1 List (java.util.List)1 NameNotFoundException (javax.naming.NameNotFoundException)1 NamingEnumeration (javax.naming.NamingEnumeration)1 NamingException (javax.naming.NamingException)1 Attribute (javax.naming.directory.Attribute)1 Attributes (javax.naming.directory.Attributes)1 SmackException (org.jivesoftware.smack.SmackException)1