RemoteConnectionEndpointLookupFailure.java

  1. /**
  2.  *
  3.  * Copyright 2020 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.util.rce;

  18. import org.jivesoftware.smack.util.ToStringUtil;

  19. import org.jxmpp.jid.DomainBareJid;
  20. import org.minidns.dnsname.DnsName;

  21. public abstract class RemoteConnectionEndpointLookupFailure {

  22.     private final String description;
  23.     private final Exception exception;

  24.     public RemoteConnectionEndpointLookupFailure(String description, Exception exception) {
  25.         this.description = description;
  26.         this.exception = exception;
  27.     }

  28.     public final String getDescription() {
  29.         return description;
  30.     }

  31.     public final Exception getException() {
  32.         return exception;
  33.     }

  34.     public String getErrorMessage() {
  35.         return description + " because: " + exception;
  36.     }

  37.     private transient String toStringCache;

  38.     @Override
  39.     public String toString() {
  40.         if (toStringCache == null) {
  41.             toStringCache = ToStringUtil.builderFor(RemoteConnectionEndpointLookupFailure.class)
  42.                 .addValue("description", description)
  43.                 .addValue("exception", exception)
  44.                 .build();
  45.         }
  46.         return toStringCache;
  47.     }

  48.     public static class DnsLookupFailure extends RemoteConnectionEndpointLookupFailure {
  49.         private final DnsName dnsName;

  50.         public DnsLookupFailure(DnsName dnsName, Exception exception) {
  51.             super("DNS lookup exception for " + dnsName, exception);
  52.             this.dnsName = dnsName;
  53.         }

  54.         public DnsName getDnsName() {
  55.             return dnsName;
  56.         }
  57.     }

  58.     public static class HttpLookupFailure extends RemoteConnectionEndpointLookupFailure {
  59.         private final DomainBareJid host;

  60.         public HttpLookupFailure(DomainBareJid host, Exception exception) {
  61.             super("Http lookup exception for " + host, exception);
  62.             this.host = host;
  63.         }

  64.         public DomainBareJid getHost() {
  65.             return host;
  66.         }
  67.     }
  68. }