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