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.testing.extension.async;
29  
30  import java.io.IOException;
31  import java.util.concurrent.Future;
32  
33  import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
34  import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
35  import org.apache.hc.core5.concurrent.FutureCallback;
36  import org.apache.hc.core5.function.Supplier;
37  import org.apache.hc.core5.http.HttpHost;
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.protocol.HttpContext;
43  import org.apache.hc.core5.io.CloseMode;
44  import org.apache.hc.core5.reactor.IOReactorStatus;
45  import org.apache.hc.core5.util.Args;
46  import org.apache.hc.core5.util.Asserts;
47  import org.apache.hc.core5.util.TimeValue;
48  
49  public class TestAsyncClient extends CloseableHttpAsyncClient {
50  
51      private final CloseableHttpAsyncClient client;
52      private final PoolingAsyncClientConnectionManager connectionManager;
53  
54      public TestAsyncClient(final CloseableHttpAsyncClient client,
55                             final PoolingAsyncClientConnectionManager connectionManager) {
56          this.client = Args.notNull(client, "Client");
57          this.connectionManager = connectionManager;
58      }
59  
60      @Override
61      public void close(final CloseMode closeMode) {
62          client.close(closeMode);
63      }
64  
65      @Override
66      public void close() throws IOException {
67          client.close();
68      }
69  
70      @Override
71      public void start() {
72          client.start();
73      }
74  
75      @Override
76      public IOReactorStatus getStatus() {
77          return client.getStatus();
78      }
79  
80      @Override
81      public void awaitShutdown(final TimeValue waitTime) throws InterruptedException {
82          client.awaitShutdown(waitTime);
83      }
84  
85      @Override
86      public void initiateShutdown() {
87          client.initiateShutdown();
88      }
89  
90      @Override
91      protected <T> Future<T> doExecute(final HttpHost target,
92                                        final AsyncRequestProducer requestProducer,
93                                        final AsyncResponseConsumer<T> responseConsumer,
94                                        final HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
95                                        final HttpContext context,
96                                        final FutureCallback<T> callback) {
97          return client.execute(target, requestProducer, responseConsumer, pushHandlerFactory, context, callback);
98      }
99  
100     /**
101      * @deprecated Do not use.
102      */
103     @Deprecated
104     @Override
105     public void register(final String hostname,
106                          final String uriPattern,
107                          final Supplier<AsyncPushConsumer> supplier) {
108         client.register(hostname, uriPattern, supplier);
109     }
110 
111     @SuppressWarnings("unchecked")
112     public <T extends CloseableHttpAsyncClient> T getImplementation() {
113         return (T) client;
114     }
115 
116     public PoolingAsyncClientConnectionManager getConnectionManager() {
117         Asserts.check(connectionManager != null, "Connection manager is not available");
118         return connectionManager;
119     }
120 
121 }