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.async;
29  
30  import org.apache.hc.client5.http.HttpRoute;
31  import org.apache.hc.client5.http.protocol.HttpClientContext;
32  import org.apache.hc.core5.annotation.Internal;
33  import org.apache.hc.core5.concurrent.Cancellable;
34  import org.apache.hc.core5.concurrent.FutureCallback;
35  import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
36  import org.apache.hc.core5.util.TimeValue;
37  
38  /**
39   * Execution runtime that provides access to the underlying connection endpoint and helps
40   * manager its life cycle.
41   * <p>
42   * This interface is considered internal and generally ought not be used or accessed
43   * by custom request exec handlers.
44   * </p>
45   *
46   * @since 5.0
47   */
48  @Internal
49  public interface AsyncExecRuntime {
50  
51      /**
52       * Determines of a connection endpoint has been acquired.
53       *
54       * @return {@code true} if an endpoint has been acquired, {@code false} otherwise.
55       */
56      boolean isEndpointAcquired();
57  
58      /**
59       * Initiates operation to acquire a connection endpoint. Endpoints can leased from a pool
60       * or unconnected new endpoint can be created.
61       *
62       * @param id unique operation ID or {@code null}.
63       * @param route the connection route.
64       * @param state the expected connection state. May be {@code null} if connection
65       *              can be state-less or its state is irrelevant.
66       * @param context the execution context.
67       * @param callback the result callback.
68       * @return handle that can be used to cancel the operation.
69       */
70      Cancellable acquireEndpoint(
71              String id,
72              HttpRoute route,
73              Object state,
74              HttpClientContext context,
75              FutureCallback<AsyncExecRuntime> callback);
76  
77      /**
78       * Releases the acquired endpoint potentially making it available for re-use.
79       */
80      void releaseEndpoint();
81  
82      /**
83       * Shuts down and discards the acquired endpoint.
84       */
85      void discardEndpoint();
86  
87      /**
88       * Determines of there the endpoint is connected to the initial hop (connection target
89       * in case of a direct route or to the first proxy hop in case of a route via a proxy
90       * or multiple proxies).
91       *
92       * @return {@code true} if the endpoint is connected, {@code false} otherwise.
93       */
94      boolean isEndpointConnected();
95  
96      /**
97       * Initiates operation to connect the local endpoint to the initial hop (connection
98       * target in case of a direct route or to the first proxy hop in case of a route
99       * via a proxy or multiple proxies).
100      *
101      * @param context the execution context.
102      * @param callback the result callback.
103      * @return handle that can be used to cancel the operation.
104      */
105     Cancellable connectEndpoint(
106             HttpClientContext context,
107             FutureCallback<AsyncExecRuntime> callback);
108 
109     /**
110      * Disconnects the local endpoint from the initial hop in the connection route.
111      */
112     void disconnectEndpoint();
113 
114     /**
115      * Upgrades transport security of the active connection by using the TLS security protocol.
116      *
117      * @param context the execution context.
118      */
119     void upgradeTls(HttpClientContext context);
120 
121     /**
122      * Upgrades transport security of the active connection by using the TLS security protocol.
123      *
124      * @param context the execution context.
125      *
126      * @since 5.2
127      */
128     default void upgradeTls(HttpClientContext context,
129                             FutureCallback<AsyncExecRuntime> callback) {
130         upgradeTls(context);
131         if (callback != null) {
132             callback.completed(this);
133         }
134     }
135 
136     /**
137      * Validates the connection making sure it can be used to execute requests.
138      *
139      * @return {@code true} if the connection is valid, {@code false}.
140      */
141     boolean validateConnection();
142 
143     /**
144      * Initiates a message exchange using the given handler.
145      *
146      * @param id unique operation ID or {@code null}.
147      * @param exchangeHandler the client message handler.
148      * @param context the execution context.
149      */
150     Cancellable execute(
151             String id,
152             AsyncClientExchangeHandler exchangeHandler,
153             HttpClientContext context);
154 
155     /**
156      * Marks the connection as potentially re-usable for the given period of time
157      * and also marks it as stateful if the state representation is given.
158      * @param state the connection state representation or {@code null} if stateless.
159      * @param validityTime the period of time this connection is valid for.
160      */
161     void markConnectionReusable(Object state, TimeValue validityTime);
162 
163     /**
164      * Marks the connection as non re-usable.
165      */
166     void markConnectionNonReusable();
167 
168     /**
169      * Forks this runtime for parallel execution.
170      *
171      * @return another runtime with the same configuration.
172      */
173     AsyncExecRuntime fork();
174 
175 }