001/**
002 *
003 * Copyright 2020 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smack.util.rce;
018
019import org.jivesoftware.smack.util.ToStringUtil;
020
021import org.jxmpp.jid.DomainBareJid;
022import org.minidns.dnsname.DnsName;
023
024public abstract class RemoteConnectionEndpointLookupFailure {
025
026    private final String description;
027    private final Exception exception;
028
029    public RemoteConnectionEndpointLookupFailure(String description, Exception exception) {
030        this.description = description;
031        this.exception = exception;
032    }
033
034    public final String getDescription() {
035        return description;
036    }
037
038    public final Exception getException() {
039        return exception;
040    }
041
042    public String getErrorMessage() {
043        return description + " because: " + exception;
044    }
045
046    private transient String toStringCache;
047
048    @Override
049    public String toString() {
050        if (toStringCache == null) {
051            toStringCache = ToStringUtil.builderFor(RemoteConnectionEndpointLookupFailure.class)
052                .addValue("description", description)
053                .addValue("exception", exception)
054                .build();
055        }
056        return toStringCache;
057    }
058
059    public static class DnsLookupFailure extends RemoteConnectionEndpointLookupFailure {
060        private final DnsName dnsName;
061
062        public DnsLookupFailure(DnsName dnsName, Exception exception) {
063            super("DNS lookup exception for " + dnsName, exception);
064            this.dnsName = dnsName;
065        }
066
067        public DnsName getDnsName() {
068            return dnsName;
069        }
070    }
071
072    public static class HttpLookupFailure extends RemoteConnectionEndpointLookupFailure {
073        private final DomainBareJid host;
074
075        public HttpLookupFailure(DomainBareJid host, Exception exception) {
076            super("Http lookup exception for " + host, exception);
077            this.host = host;
078        }
079
080        public DomainBareJid getHost() {
081            return host;
082        }
083    }
084}