1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.client5.http.nio;
29
30 import java.io.IOException;
31 import java.util.concurrent.Future;
32
33 import org.apache.hc.core5.annotation.Contract;
34 import org.apache.hc.core5.annotation.ThreadingBehavior;
35 import org.apache.hc.core5.concurrent.BasicFuture;
36 import org.apache.hc.core5.concurrent.FutureCallback;
37 import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
38 import org.apache.hc.core5.http.nio.AsyncPushConsumer;
39 import org.apache.hc.core5.http.nio.AsyncRequestProducer;
40 import org.apache.hc.core5.http.nio.AsyncResponseConsumer;
41 import org.apache.hc.core5.http.nio.HandlerFactory;
42 import org.apache.hc.core5.http.nio.support.BasicClientExchangeHandler;
43 import org.apache.hc.core5.http.protocol.HttpContext;
44 import org.apache.hc.core5.http.protocol.HttpCoreContext;
45 import org.apache.hc.core5.io.CloseMode;
46 import org.apache.hc.core5.io.ModalCloseable;
47 import org.apache.hc.core5.util.Timeout;
48
49
50
51
52
53
54 @Contract(threading = ThreadingBehavior.SAFE)
55 public abstract class AsyncConnectionEndpoint implements ModalCloseable {
56
57
58
59
60
61
62
63
64
65 public abstract void execute(
66 String id,
67 AsyncClientExchangeHandler exchangeHandler,
68 HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
69 HttpContext context);
70
71
72
73
74 public abstract boolean isConnected();
75
76
77
78
79
80
81 public abstract void setSocketTimeout(Timeout timeout);
82
83 @Override
84 public final void close() throws IOException {
85 close(CloseMode.GRACEFUL);
86 }
87
88
89
90
91
92
93
94
95 public void execute(
96 final String id,
97 final AsyncClientExchangeHandler exchangeHandler,
98 final HttpContext context) {
99 execute(id, exchangeHandler, null, context);
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114 public final <T> Future<T> execute(
115 final String id,
116 final AsyncRequestProducer requestProducer,
117 final AsyncResponseConsumer<T> responseConsumer,
118 final HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
119 final HttpContext context,
120 final FutureCallback<T> callback) {
121 final BasicFuture<T> future = new BasicFuture<>(callback);
122 execute(id, new BasicClientExchangeHandler<>(requestProducer, responseConsumer,
123 new FutureCallback<T>() {
124
125 @Override
126 public void completed(final T result) {
127 future.completed(result);
128 }
129
130 @Override
131 public void failed(final Exception ex) {
132 future.failed(ex);
133 }
134
135 @Override
136 public void cancelled() {
137 future.cancel();
138 }
139
140 }),
141 pushHandlerFactory,
142 context != null ? context : HttpCoreContext.create());
143 return future;
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157 public final <T> Future<T> execute(
158 final String id,
159 final AsyncRequestProducer requestProducer,
160 final AsyncResponseConsumer<T> responseConsumer,
161 final HttpContext context,
162 final FutureCallback<T> callback) {
163 return execute(id, requestProducer, responseConsumer, null, context, callback);
164 }
165
166
167
168
169
170
171
172
173
174
175
176
177 public final <T> Future<T> execute(
178 final String id,
179 final AsyncRequestProducer requestProducer,
180 final AsyncResponseConsumer<T> responseConsumer,
181 final HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
182 final FutureCallback<T> callback) {
183 return execute(id, requestProducer, responseConsumer, pushHandlerFactory, null, callback);
184 }
185
186
187
188
189
190
191
192
193
194
195
196 public final <T> Future<T> execute(
197 final String id,
198 final AsyncRequestProducer requestProducer,
199 final AsyncResponseConsumer<T> responseConsumer,
200 final FutureCallback<T> callback) {
201 return execute(id, requestProducer, responseConsumer, null, null, callback);
202 }
203
204 }