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