View Javadoc
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  package org.apache.hc.client5.http;
28  
29  import java.io.IOException;
30  import java.net.ConnectException;
31  import java.net.InetAddress;
32  import java.net.SocketTimeoutException;
33  import java.util.Arrays;
34  
35  import org.apache.hc.core5.annotation.Internal;
36  import org.apache.hc.core5.net.NamedEndpoint;
37  
38  /**
39   * Connect exception support methods.
40   *
41   * @since 5.0
42   */
43  @Internal
44  public final class ConnectExceptionSupport {
45  
46      public static ConnectTimeoutException createConnectTimeoutException(
47              final IOException cause, final NamedEndpoint namedEndpoint, final InetAddress... remoteAddresses) {
48          final String message = "Connect to " +
49                  (namedEndpoint != null ? namedEndpoint : "remote endpoint") +
50                  (remoteAddresses != null && remoteAddresses.length > 0 ? " " + Arrays.asList(remoteAddresses) : "") +
51                  ((cause != null && cause.getMessage() != null) ? " failed: " + cause.getMessage() : " timed out");
52          return new ConnectTimeoutException(message, namedEndpoint);
53      }
54  
55      public static HttpHostConnectException createHttpHostConnectException(
56              final IOException cause,
57              final NamedEndpoint namedEndpoint,
58              final InetAddress... remoteAddresses) {
59          final String message = "Connect to " +
60                  (namedEndpoint != null ? namedEndpoint : "remote endpoint") +
61                  (remoteAddresses != null && remoteAddresses.length > 0 ? " " + Arrays.asList(remoteAddresses) : "") +
62                  ((cause != null && cause.getMessage() != null) ? " failed: " + cause.getMessage() : " refused");
63          return new HttpHostConnectException(message, namedEndpoint);
64      }
65  
66      public static IOException enhance(
67              final IOException cause, final NamedEndpoint namedEndpoint, final InetAddress... remoteAddresses) {
68          if (cause instanceof SocketTimeoutException) {
69              final IOException ex = createConnectTimeoutException(cause, namedEndpoint, remoteAddresses);
70              ex.setStackTrace(cause.getStackTrace());
71              return ex;
72          } else if (cause instanceof ConnectException) {
73              if ("Connection timed out".equals(cause.getMessage())) {
74                  final IOException ex = createConnectTimeoutException(cause, namedEndpoint, remoteAddresses);
75                  ex.initCause(cause);
76                  return ex;
77              }
78              final IOException ex = createHttpHostConnectException(cause, namedEndpoint, remoteAddresses);
79              ex.setStackTrace(cause.getStackTrace());
80              return ex;
81          } else {
82              return cause;
83          }
84      }
85  
86  }