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.http.conn.socket;
29
30 import java.io.IOException;
31 import java.net.InetSocketAddress;
32 import java.net.Socket;
33
34 import org.apache.http.HttpHost;
35 import org.apache.http.conn.ConnectTimeoutException;
36 import org.apache.http.protocol.HttpContext;
37
38 /**
39 * A factory for creating and connecting sockets.
40 *
41 * @since 4.3
42 */
43 public interface ConnectionSocketFactory {
44
45 /**
46 * Creates new, unconnected socket. The socket should subsequently be passed to
47 * {@link #connectSocket(int, Socket, HttpHost, InetSocketAddress, InetSocketAddress,
48 * HttpContext)}.
49 *
50 * @return a new socket
51 *
52 * @throws IOException if an I/O error occurs while creating the socket
53 */
54 Socket createSocket(HttpContext context) throws IOException;
55
56 /**
57 * Connects a socket to the target host with the given resolved remote address.
58 *
59 * @param connectTimeout connect timeout.
60 * @param sock the socket to connect, as obtained from {@link #createSocket(HttpContext)}.
61 * <code>null</code> indicates that a new socket should be created and connected.
62 * @param host target host as specified by the caller (end user).
63 * @param remoteAddress the resolved remote address to connect to.
64 * @param localAddress the local address to bind the socket to, or <code>null</code> for any.
65 * @param context the actual HTTP context.
66 *
67 * @return the connected socket. The returned object may be different
68 * from the <code>sock</code> argument if this factory supports
69 * a layered protocol.
70 *
71 * @throws IOException if an I/O error occurs
72 * @throws ConnectTimeoutException if the socket cannot be connected
73 * within the time limit defined in the <code>params</code>
74 */
75 Socket connectSocket(
76 int connectTimeout,
77 Socket sock,
78 HttpHost host,
79 InetSocketAddress remoteAddress,
80 InetSocketAddress localAddress,
81 HttpContext context) throws IOException, ConnectTimeoutException;
82
83 }