001/** 002 * 003 * Copyright 2019 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.smackx.bytestreams.socks5; 018 019import java.util.Collections; 020import java.util.Iterator; 021import java.util.Map; 022 023import org.jivesoftware.smack.SmackException; 024 025import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost; 026 027public abstract class Socks5Exception extends SmackException { 028 029 /** 030 * 031 */ 032 private static final long serialVersionUID = 1L; 033 034 protected Socks5Exception(String message) { 035 super(message); 036 } 037 038 public static final class NoSocks5StreamHostsProvided extends Socks5Exception { 039 /** 040 * 041 */ 042 private static final long serialVersionUID = 1L; 043 044 NoSocks5StreamHostsProvided() { 045 super("No SOCKS5 stream hosts provided."); 046 } 047 } 048 049 public static final class CouldNotConnectToAnyProvidedSocks5Host extends Socks5Exception { 050 051 /** 052 * 053 */ 054 private static final long serialVersionUID = 1L; 055 056 private final Map<StreamHost, Exception> streamHostsExceptions; 057 058 private CouldNotConnectToAnyProvidedSocks5Host(String message, Map<StreamHost, Exception> streamHostsExceptions) { 059 super(message); 060 this.streamHostsExceptions = Collections.unmodifiableMap(streamHostsExceptions); 061 } 062 063 public Map<StreamHost, Exception> getStreamHostsExceptions() { 064 return streamHostsExceptions; 065 } 066 067 static CouldNotConnectToAnyProvidedSocks5Host construct(Map<StreamHost, Exception> streamHostsExceptions) { 068 assert !streamHostsExceptions.isEmpty(); 069 070 StringBuilder sb = new StringBuilder(256); 071 sb.append("Could not establish socket with any provided SOCKS5 stream host."); 072 Iterator<StreamHost> it = streamHostsExceptions.keySet().iterator(); 073 while (it.hasNext()) { 074 StreamHost streamHost = it.next(); 075 Exception exception = streamHostsExceptions.get(streamHost); 076 077 sb.append(' ').append(streamHost).append(" Exception: '").append(exception).append('\''); 078 if (it.hasNext()) { 079 sb.append(','); 080 } 081 } 082 083 String message = sb.toString(); 084 return new CouldNotConnectToAnyProvidedSocks5Host(message, streamHostsExceptions); 085 } 086 } 087}