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  import java.util.concurrent.ThreadFactory;
31  
32  import org.apache.hc.core5.concurrent.Cancellable;
33  import org.apache.hc.core5.concurrent.ComplexFuture;
34  import org.apache.hc.core5.concurrent.FutureCallback;
35  import org.apache.hc.core5.http.HttpHost;
36  import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
37  import org.apache.hc.core5.http.nio.AsyncPushConsumer;
38  import org.apache.hc.core5.http.nio.AsyncRequestProducer;
39  import org.apache.hc.core5.http.nio.AsyncResponseConsumer;
40  import org.apache.hc.core5.http.nio.HandlerFactory;
41  import org.apache.hc.core5.http.nio.support.BasicClientExchangeHandler;
42  import org.apache.hc.core5.http.protocol.HttpContext;
43  import org.apache.hc.core5.reactor.DefaultConnectingIOReactor;
44  
45  abstract class AbstractMinimalHttpAsyncClientBase extends AbstractHttpAsyncClientBase {
46  
47      AbstractMinimalHttpAsyncClientBase(
48              final DefaultConnectingIOReactor ioReactor,
49              final AsyncPushConsumerRegistry pushConsumerRegistry,
50              final ThreadFactory threadFactory) {
51          super(ioReactor, pushConsumerRegistry, threadFactory);
52      }
53  
54      @Override
55      protected <T> Future<T> doExecute(
56              final HttpHost httpHost,
57              final AsyncRequestProducer requestProducer,
58              final AsyncResponseConsumer<T> responseConsumer,
59              final HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
60              final HttpContext context,
61              final FutureCallback<T> callback) {
62          final ComplexFuture<T> future = new ComplexFuture<>(callback);
63          future.setDependency(execute(new BasicClientExchangeHandler<>(
64                  requestProducer,
65                  responseConsumer,
66                  new FutureCallback<T>() {
67  
68                      @Override
69                      public void completed(final T result) {
70                          future.completed(result);
71                      }
72  
73                      @Override
74                      public void failed(final Exception ex) {
75                          future.failed(ex);
76                      }
77  
78                      @Override
79                      public void cancelled() {
80                          future.cancel();
81                      }
82  
83                  }), pushHandlerFactory, context));
84          return future;
85      }
86  
87      public final Cancellable execute(final AsyncClientExchangeHandler exchangeHandler) {
88          return execute(exchangeHandler, null, null);
89      }
90  
91      public abstract Cancellable execute(
92              AsyncClientExchangeHandler exchangeHandler,
93              HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
94              HttpContext context);
95  
96  }