1 /*
2  * ====================================================================
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  * ====================================================================
20  *
21  * This software consists of voluntary contributions made by many
22  * individuals on behalf of the Apache Software Foundation.  For more
23  * information on the Apache Software Foundation, please see
24  * <http://www.apache.org/>.
25  *
26  */
27 
28 package ch.boye.httpclientandroidlib.conn;
29 
30 import java.io.IOException;
31 import java.io.InterruptedIOException;
32 import java.net.InetAddress;
33 import java.util.Arrays;
34 
35 import ch.boye.httpclientandroidlib.HttpHost;
36 import ch.boye.httpclientandroidlib.annotation.Immutable;
37 
38 /**
39  * A timeout while connecting to an HTTP server or waiting for an
40  * available connection from an HttpConnectionManager.
41  *
42  *
43  * @since 4.0
44  */
45 @Immutable
46 public class ConnectTimeoutException extends InterruptedIOException {
47 
48     private static final long serialVersionUID = -4816682903149535989L;
49 
50     private final HttpHost host;
51 
52     /**
53      * Creates a ConnectTimeoutException with a <tt>null</tt> detail message.
54      */
ConnectTimeoutException()55     public ConnectTimeoutException() {
56         super();
57         this.host = null;
58     }
59 
60     /**
61      * Creates a ConnectTimeoutException with the specified detail message.
62      */
ConnectTimeoutException(final String message)63     public ConnectTimeoutException(final String message) {
64         super(message);
65         this.host = null;
66     }
67 
68     /**
69      * Creates a ConnectTimeoutException based on original {@link IOException}.
70      *
71      * @since 4.3
72      */
ConnectTimeoutException( final IOException cause, final HttpHost host, final InetAddress... remoteAddresses)73     public ConnectTimeoutException(
74             final IOException cause,
75             final HttpHost host,
76             final InetAddress... remoteAddresses) {
77         super("Connect to " +
78                 (host != null ? host.toHostString() : "remote host") +
79                 (remoteAddresses != null && remoteAddresses.length > 0 ?
80                         " " + Arrays.asList(remoteAddresses) : "") +
81                 ((cause != null && cause.getMessage() != null) ?
82                         " failed: " + cause.getMessage() : " timed out"));
83         this.host = host;
84         initCause(cause);
85     }
86 
87     /**
88      * @since 4.3
89      */
getHost()90     public HttpHost getHost() {
91         return host;
92     }
93 
94 }
95