Socks5Exception.java

  1. /**
  2.  *
  3.  * Copyright 2019 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.smackx.bytestreams.socks5;

  18. import java.util.Collections;
  19. import java.util.Iterator;
  20. import java.util.Map;

  21. import org.jivesoftware.smack.SmackException;

  22. import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost;

  23. public abstract class Socks5Exception extends SmackException {

  24.     /**
  25.      *
  26.      */
  27.     private static final long serialVersionUID = 1L;

  28.     protected Socks5Exception(String message) {
  29.         super(message);
  30.     }

  31.     public static final class NoSocks5StreamHostsProvided extends Socks5Exception {
  32.         /**
  33.          *
  34.          */
  35.         private static final long serialVersionUID = 1L;

  36.         NoSocks5StreamHostsProvided() {
  37.             super("No SOCKS5 stream hosts provided.");
  38.         }
  39.     }

  40.     public static final class CouldNotConnectToAnyProvidedSocks5Host extends Socks5Exception {

  41.         /**
  42.          *
  43.          */
  44.         private static final long serialVersionUID = 1L;

  45.         private final Map<StreamHost, Exception> streamHostsExceptions;

  46.         private CouldNotConnectToAnyProvidedSocks5Host(String message, Map<StreamHost, Exception> streamHostsExceptions) {
  47.             super(message);
  48.             this.streamHostsExceptions = Collections.unmodifiableMap(streamHostsExceptions);
  49.         }

  50.         public Map<StreamHost, Exception> getStreamHostsExceptions() {
  51.             return streamHostsExceptions;
  52.         }

  53.         static CouldNotConnectToAnyProvidedSocks5Host construct(Map<StreamHost, Exception> streamHostsExceptions) {
  54.             assert !streamHostsExceptions.isEmpty();

  55.             StringBuilder sb = new StringBuilder(256);
  56.             sb.append("Could not establish socket with any provided SOCKS5 stream host.");
  57.             Iterator<StreamHost> it = streamHostsExceptions.keySet().iterator();
  58.             while (it.hasNext()) {
  59.                 StreamHost streamHost = it.next();
  60.                 Exception exception = streamHostsExceptions.get(streamHost);

  61.                 sb.append(' ').append(streamHost).append(" Exception: '").append(exception).append('\'');
  62.                 if (it.hasNext()) {
  63.                     sb.append(',');
  64.                 }
  65.             }

  66.             String message = sb.toString();
  67.             return new CouldNotConnectToAnyProvidedSocks5Host(message, streamHostsExceptions);
  68.         }
  69.     }
  70. }