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.classic;
29
30 import java.io.IOException;
31
32 import org.apache.hc.client5.http.EndpointInfo;
33 import org.apache.hc.client5.http.HttpRoute;
34 import org.apache.hc.client5.http.protocol.HttpClientContext;
35 import org.apache.hc.core5.annotation.Internal;
36 import org.apache.hc.core5.concurrent.CancellableDependency;
37 import org.apache.hc.core5.http.ClassicHttpRequest;
38 import org.apache.hc.core5.http.ClassicHttpResponse;
39 import org.apache.hc.core5.http.HttpException;
40 import org.apache.hc.core5.http.io.HttpResponseInformationCallback;
41 import org.apache.hc.core5.util.TimeValue;
42
43 /**
44 * Execution runtime that provides access to the underlying connection endpoint and helps
45 * manager its life cycle.
46 * <p>
47 * This interface is considered internal and generally ought not be used or accessed
48 * by custom request exec handlers.
49 *
50 * @since 5.0
51 */
52 @Internal
53 public interface ExecRuntime {
54
55 /**
56 * Determines of the request execution has been aborted.
57 *
58 * @return {@code true} if the request execution has been acquired,
59 * {@code false} otherwise.
60 */
61 boolean isExecutionAborted();
62
63 /**
64 * Determines of a connection endpoint has been acquired.
65 *
66 * @return {@code true} if an endpoint has been acquired, {@code false} otherwise.
67 */
68 boolean isEndpointAcquired();
69
70 /**
71 * Acquires a connection endpoint. Endpoints can leased from a pool
72 * or unconnected new endpoint can be created.
73 *
74 * @param id unique operation ID or {@code null}.
75 * @param route the connection route.
76 * @param state the expected connection state. May be {@code null} if connection
77 * can be state-less or its state is irrelevant.
78 * @param context the execution context.
79 * @throws IOException if an I/O error occurs.
80 */
81 void acquireEndpoint(
82 String id,
83 HttpRoute route,
84 Object state,
85 HttpClientContext context) throws IOException;
86
87 /**
88 * Releases the acquired endpoint potentially making it available for re-use.
89 */
90 void releaseEndpoint();
91
92 /**
93 * Shuts down and discards the acquired endpoint.
94 */
95 void discardEndpoint();
96
97 /**
98 * Determines of there the endpoint is connected to the initial hop (connection
99 * target in case of a direct route or to the first proxy hop in case of a route
100 * via a proxy or multiple proxies).
101 *
102 * @return {@code true} if the endpoint is connected, {@code false} otherwise.
103 */
104 boolean isEndpointConnected();
105
106 /**
107 * Disconnects the local endpoint from the initial hop in the connection route.
108 *
109 * @throws IOException if an I/O error occurs.
110 */
111 void disconnectEndpoint() throws IOException;
112
113 /**
114 * Connect the local endpoint to the initial hop (connection target in case
115 * of a direct route or to the first proxy hop in case of a route via a proxy
116 * or multiple proxies).
117 *
118 * @param context the execution context.
119 * @throws IOException if an I/O error occurs.
120 */
121 void connectEndpoint(HttpClientContext context) throws IOException;
122
123 /**
124 * Upgrades transport security of the active connection by using the TLS security protocol.
125 *
126 * @param context the execution context.
127 * @throws IOException if an I/O error occurs.
128 */
129 void upgradeTls(HttpClientContext context) throws IOException;
130
131 /**
132 * Gets information about the underlying endpoint when connected or {@code null} otherwise.
133 *
134 * @return an EndpointInfo, defaults to {@code null}.
135 */
136 default EndpointInfo getEndpointInfo() {
137 return null;
138 }
139
140 /**
141 * Executes HTTP request using the given context.
142 *
143 * @param id unique operation ID or {@code null}.
144 * @param request the request message.
145 * @param context the execution context.
146 * @return a ClassicHttpResponse.
147 * @throws IOException if an I/O error occurs.
148 * @throws HttpException if a protocol error occurs.
149 */
150 ClassicHttpResponse execute(
151 String id,
152 ClassicHttpRequest request,
153 HttpClientContext context) throws IOException, HttpException;
154
155 /**
156 * Executes HTTP request using the given context.
157 *
158 * @param id unique operation ID or {@code null}.
159 * @param request the request message.
160 * @param informationCallback information (1xx) response handler
161 * @param context the execution context.
162 * @return a ClassicHttpResponse.
163 * @throws IOException if an I/O error occurs.
164 * @throws HttpException if a protocol error occurs.
165 * @since 5.4
166 */
167 default ClassicHttpResponse execute(
168 String id,
169 ClassicHttpRequest request,
170 HttpResponseInformationCallback informationCallback,
171 HttpClientContext context) throws IOException, HttpException {
172 return execute(id, request, context);
173 }
174
175 /**
176 * Determines of the connection is considered re-usable.
177 *
178 * @return {@code true} if the connection is re-usable, {@code false} otherwise.
179 */
180 boolean isConnectionReusable();
181
182 /**
183 * Marks the connection as potentially re-usable for the given period of time
184 * and also marks it as stateful if the state representation is given.
185 * @param state the connection state representation or {@code null} if stateless.
186 * @param validityTime the period of time this connection is valid for.
187 */
188 void markConnectionReusable(Object state, TimeValue validityTime);
189
190 /**
191 * Marks the connection as non re-usable.
192 */
193 void markConnectionNonReusable();
194
195 /**
196 * Forks this runtime for parallel execution.
197 *
198 * @return another runtime with the same configuration.
199 */
200 ExecRuntime fork(CancellableDependency cancellableAware);
201
202 }