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 java.net.InetAddress;
020
021import org.jivesoftware.smack.util.ToStringUtil;
022
023public final class RemoteConnectionException<RCE extends RemoteConnectionEndpoint> {
024
025    private final RemoteConnectionEndpoint.InetSocketAddressCoupling<RCE> address;
026    private final Exception exception;
027
028    public RemoteConnectionException(RCE remoteConnectionEndpoint, InetAddress inetAddress,
029                    Exception exception) {
030        this(new RemoteConnectionEndpoint.InetSocketAddressCoupling<>(remoteConnectionEndpoint, inetAddress), exception);
031    }
032
033    public RemoteConnectionException(RemoteConnectionEndpoint.InetSocketAddressCoupling<RCE> address, Exception exception) {
034        this.address = address;
035        this.exception = exception;
036    }
037
038    public RemoteConnectionEndpoint.InetSocketAddressCoupling<RCE> getAddress() {
039        return address;
040    }
041
042    public Exception getException() {
043        return exception;
044    }
045
046    public String getErrorMessage() {
047        return "\'" +  address + "' failed because: " + exception;
048    }
049
050    private transient String toStringCache;
051
052    @Override
053    public String toString() {
054        if (toStringCache == null) {
055            toStringCache = ToStringUtil.builderFor(RemoteConnectionException.class)
056                .addValue("address", address)
057                .addValue("exception", exception)
058                .build();
059        }
060        return toStringCache;
061    }
062
063    public static <SARCE extends SingleAddressRemoteConnectionEndpoint> RemoteConnectionException<SARCE> from(SARCE remoteConnectionEndpoint, Exception exception) {
064        return new RemoteConnectionException<SARCE>(remoteConnectionEndpoint, remoteConnectionEndpoint.getInetAddress(), exception);
065    }
066}