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  package org.apache.hc.client5.http.impl.async;
28  
29  import java.util.concurrent.Future;
30  
31  import org.apache.hc.client5.http.async.HttpAsyncClient;
32  import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
33  import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
34  import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
35  import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
36  import org.apache.hc.client5.http.protocol.HttpClientContext;
37  import org.apache.hc.core5.annotation.Contract;
38  import org.apache.hc.core5.annotation.ThreadingBehavior;
39  import org.apache.hc.core5.concurrent.FutureCallback;
40  import org.apache.hc.core5.function.Supplier;
41  import org.apache.hc.core5.http.HttpHost;
42  import org.apache.hc.core5.http.nio.AsyncPushConsumer;
43  import org.apache.hc.core5.http.nio.AsyncRequestProducer;
44  import org.apache.hc.core5.http.nio.AsyncResponseConsumer;
45  import org.apache.hc.core5.http.nio.HandlerFactory;
46  import org.apache.hc.core5.http.protocol.HttpContext;
47  import org.apache.hc.core5.io.ModalCloseable;
48  import org.apache.hc.core5.reactor.IOReactorStatus;
49  import org.apache.hc.core5.util.Args;
50  import org.apache.hc.core5.util.TimeValue;
51  
52  /**
53   * Base implementation of {@link HttpAsyncClient} that also implements {@link ModalCloseable}.
54   *
55   * @since 5.0
56   */
57  @Contract(threading = ThreadingBehavior.STATELESS)
58  public abstract class CloseableHttpAsyncClient implements HttpAsyncClient, ModalCloseable {
59  
60      public abstract void start();
61  
62      public abstract IOReactorStatus getStatus();
63  
64      public abstract void awaitShutdown(TimeValue waitTime) throws InterruptedException;
65  
66      public abstract void initiateShutdown();
67  
68      protected abstract <T> Future<T> doExecute(
69              final HttpHost target,
70              final AsyncRequestProducer requestProducer,
71              final AsyncResponseConsumer<T> responseConsumer,
72              final HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
73              final HttpContext context,
74              final FutureCallback<T> callback);
75  
76      public final <T> Future<T> execute(
77              final HttpHost target,
78              final AsyncRequestProducer requestProducer,
79              final AsyncResponseConsumer<T> responseConsumer,
80              final HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
81              final HttpContext context,
82              final FutureCallback<T> callback) {
83          Args.notNull(requestProducer, "Request producer");
84          Args.notNull(responseConsumer, "Response consumer");
85          return doExecute(target, requestProducer, responseConsumer, pushHandlerFactory, context, callback);
86      }
87  
88      @Override
89      public final <T> Future<T> execute(
90              final AsyncRequestProducer requestProducer,
91              final AsyncResponseConsumer<T> responseConsumer,
92              final HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
93              final HttpContext context,
94              final FutureCallback<T> callback) {
95          Args.notNull(requestProducer, "Request producer");
96          Args.notNull(responseConsumer, "Response consumer");
97          return doExecute(null, requestProducer, responseConsumer, pushHandlerFactory, context, callback);
98      }
99  
100     public final <T> Future<T> execute(
101             final AsyncRequestProducer requestProducer,
102             final AsyncResponseConsumer<T> responseConsumer,
103             final HttpContext context,
104             final FutureCallback<T> callback) {
105         Args.notNull(requestProducer, "Request producer");
106         Args.notNull(responseConsumer, "Response consumer");
107         return execute(requestProducer, responseConsumer, null, context, callback);
108     }
109 
110     public final <T> Future<T> execute(
111             final AsyncRequestProducer requestProducer,
112             final AsyncResponseConsumer<T> responseConsumer,
113             final FutureCallback<T> callback) {
114         Args.notNull(requestProducer, "Request producer");
115         Args.notNull(responseConsumer, "Response consumer");
116         return execute(requestProducer, responseConsumer, HttpClientContext.create(), callback);
117     }
118 
119     public final Future<SimpleHttpResponse> execute(
120             final SimpleHttpRequest request,
121             final HttpContext context,
122             final FutureCallback<SimpleHttpResponse> callback) {
123         Args.notNull(request, "Request");
124         return execute(SimpleRequestProducer.create(request), SimpleResponseConsumer.create(), context, callback);
125     }
126 
127     public final Future<SimpleHttpResponse> execute(
128             final SimpleHttpRequest request,
129             final FutureCallback<SimpleHttpResponse> callback) {
130         return execute(request, HttpClientContext.create(), callback);
131     }
132 
133     public abstract void register(String hostname, String uriPattern, Supplier<AsyncPushConsumer> supplier);
134 
135     public final void register(final String uriPattern, final Supplier<AsyncPushConsumer> supplier) {
136         register(null, uriPattern, supplier);
137     }
138 
139 }