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.io; 28 29 import java.io.IOException; 30 31 import org.apache.hc.client5.http.HttpRoute; 32 import org.apache.hc.core5.http.protocol.HttpContext; 33 import org.apache.hc.core5.io.ModalCloseable; 34 import org.apache.hc.core5.util.TimeValue; 35 import org.apache.hc.core5.util.Timeout; 36 37 /** 38 * Represents a manager of persistent client connections. 39 * <p> 40 * The purpose of an HTTP connection manager is to serve as a factory for new 41 * HTTP connections, manage persistent connections and synchronize access to 42 * persistent connections making sure that only one thread of execution can 43 * have access to a connection at a time. 44 * </p> 45 * <p> 46 * Implementations of this interface must be thread-safe. Access to shared 47 * data must be synchronized as methods of this interface may be executed 48 * from multiple threads. 49 * </p> 50 * 51 * @since 4.3 52 */ 53 public interface HttpClientConnectionManager extends ModalCloseable { 54 55 /** 56 * Returns a {@link LeaseRequest} object which can be used to obtain 57 * a {@link ConnectionEndpoint} to cancel the request by calling 58 * {@link LeaseRequest#cancel()}. 59 * <p> 60 * Please note that newly allocated endpoints can be leased 61 * {@link ConnectionEndpoint#isConnected() disconnected}. The consumer of the endpoint 62 * is responsible for fully establishing the route to the endpoint target 63 * by calling {@link #connect(ConnectionEndpoint, TimeValue, HttpContext)} 64 * in order to connect directly to the target or to the first proxy hop, 65 * and optionally calling {@link #upgrade(ConnectionEndpoint, HttpContext)} method 66 * to upgrade the underlying transport to Transport Layer Security after having 67 * executed a {@code CONNECT} method to all intermediate proxy hops. 68 * 69 * @param id unique operation ID or {@code null}. 70 * @param route HTTP route of the requested connection. 71 * @param requestTimeout lease request timeout. 72 * @param state expected state of the connection or {@code null} 73 * if the connection is not expected to carry any state. 74 * @since 5.0 75 */ 76 LeaseRequest lease(String id, HttpRoute route, Timeout requestTimeout, Object state); 77 78 /** 79 * Releases the endpoint back to the manager making it potentially 80 * re-usable by other consumers. Optionally, the maximum period 81 * of how long the manager should keep the connection alive can be 82 * defined using {@code validDuration} and {@code timeUnit} 83 * parameters. 84 * 85 * @param endpoint the managed endpoint. 86 * @param newState the new connection state of {@code null} if state-less. 87 * @param validDuration the duration of time this connection is valid for reuse. 88 */ 89 void release(ConnectionEndpoint endpoint, Object newState, TimeValue validDuration); 90 91 /** 92 * Connects the endpoint to the initial hop (connection target in case 93 * of a direct route or to the first proxy hop in case of a route via a proxy 94 * or multiple proxies). 95 * 96 * @param endpoint the managed endpoint. 97 * @param connectTimeout connect timeout. 98 * @param context the actual HTTP context. 99 */ 100 void connect(ConnectionEndpoint endpoint, TimeValue connectTimeout, HttpContext context) throws IOException; 101 102 /** 103 * Upgrades transport security of the given endpoint by using the TLS security protocol. 104 * 105 * @param endpoint the managed endpoint. 106 * @param context the actual HTTP context. 107 */ 108 void upgrade(ConnectionEndpoint endpoint, HttpContext context) throws IOException; 109 110 }