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  
28  package org.apache.hc.client5.http.socket;
29  
30  import java.io.IOException;
31  import java.net.InetSocketAddress;
32  import java.net.Socket;
33  
34  import org.apache.hc.core5.annotation.Contract;
35  import org.apache.hc.core5.annotation.ThreadingBehavior;
36  import org.apache.hc.core5.http.HttpHost;
37  import org.apache.hc.core5.http.protocol.HttpContext;
38  import org.apache.hc.core5.util.TimeValue;
39  import org.apache.hc.core5.util.Timeout;
40  
41  /**
42   * A factory for creating and connecting connection sockets.
43   *
44   * @since 4.3
45   */
46  @Contract(threading = ThreadingBehavior.STATELESS)
47  public interface ConnectionSocketFactory {
48  
49      /**
50       * Creates new, unconnected socket. The socket should subsequently be passed to
51       * {@link #connectSocket(TimeValue, Socket, HttpHost, InetSocketAddress, InetSocketAddress,
52       *    HttpContext) connectSocket} method.
53       *
54       * @return  a new socket
55       *
56       * @throws IOException if an I/O error occurs while creating the socket
57       */
58      Socket createSocket(HttpContext context) throws IOException;
59  
60      /**
61       * Connects the socket to the target host with the given resolved remote address.
62       *
63       * @param connectTimeout connect timeout.
64       * @param socket the socket to connect, as obtained from {@link #createSocket(HttpContext)}.
65       * {@code null} indicates that a new socket should be created and connected.
66       * @param host target host as specified by the caller (end user).
67       * @param remoteAddress the resolved remote address to connect to.
68       * @param localAddress the local address to bind the socket to, or {@code null} for any.
69       * @param context the actual HTTP context.
70       *
71       * @return  the connected socket. The returned object may be different
72       *          from the {@code sock} argument if this factory supports
73       *          a layered protocol.
74       *
75       * @throws IOException if an I/O error occurs
76       */
77      Socket connectSocket(
78          TimeValue connectTimeout,
79          Socket socket,
80          HttpHost host,
81          InetSocketAddress remoteAddress,
82          InetSocketAddress localAddress,
83          HttpContext context) throws IOException;
84  
85      /**
86       * Connects the socket to the target host with the given resolved remote address.
87       *
88       * @param socket the socket to connect, as obtained from {@link #createSocket(HttpContext)}.
89       * {@code null} indicates that a new socket should be created and connected.
90       * @param host target host as specified by the caller (end user).
91       * @param remoteAddress the resolved remote address to connect to.
92       * @param localAddress the local address to bind the socket to, or {@code null} for any.
93       * @param connectTimeout connect timeout.
94       * @param attachment connect request attachment.
95       * @param context the actual HTTP context.
96       *
97       * @return  the connected socket. The returned object may be different
98       *          from the {@code sock} argument if this factory supports
99       *          a layered protocol.
100      *
101      * @throws IOException if an I/O error occurs
102      *
103      * @since 5.2
104      */
105     default Socket connectSocket(
106             Socket socket,
107             HttpHost host,
108             InetSocketAddress remoteAddress,
109             InetSocketAddress localAddress,
110             Timeout connectTimeout,
111             Object attachment,
112             HttpContext context) throws IOException {
113         return connectSocket(connectTimeout, socket, host, remoteAddress, localAddress, context);
114     }
115 
116 }