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.http.impl.client.cache;
28  
29  import org.apache.http.HttpException;
30  import org.apache.http.HttpHost;
31  import org.apache.http.HttpRequest;
32  import org.apache.http.HttpResponse;
33  import org.apache.http.client.methods.HttpRequestWrapper;
34  import org.apache.http.client.protocol.HttpClientContext;
35  import org.apache.http.concurrent.BasicFuture;
36  import org.apache.http.concurrent.FutureCallback;
37  import org.apache.http.conn.routing.HttpRoute;
38  import org.apache.http.impl.execchain.ClientExecChain;
39  import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
40  import org.apache.http.nio.conn.ClientAsyncConnectionManager;
41  import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
42  import org.apache.http.nio.protocol.HttpAsyncResponseConsumer;
43  import org.apache.http.nio.reactor.IOReactorStatus;
44  import org.apache.http.params.HttpParams;
45  import org.apache.http.protocol.HttpContext;
46  
47  import java.io.IOException;
48  import java.util.concurrent.Future;
49  
50  public class ClientExecChainAsyncClient extends CloseableHttpAsyncClient {
51  
52      private final ClientExecChain backend;
53  
54      public ClientExecChainAsyncClient(final ClientExecChain backend) {
55          super();
56          this.backend = backend;
57      }
58  
59      @Override
60      public void start() {
61          // no-op
62      }
63  
64      @Override
65      public boolean isRunning() {
66          return true;
67      }
68  
69      public void shutdown() throws InterruptedException {
70          // no-op
71      }
72  
73      public IOReactorStatus getStatus() {
74          return null;
75      }
76  
77      @SuppressWarnings("deprecation")
78      public ClientAsyncConnectionManager getConnectionManager() {
79          return null;
80      }
81  
82      @SuppressWarnings("deprecation")
83      public HttpParams getParams() {
84          return null;
85      }
86  
87      public void close() throws IOException {
88          // no-op
89      }
90  
91      public <T> Future<T> execute(
92              final HttpAsyncRequestProducer requestProducer,
93              final HttpAsyncResponseConsumer<T> responseConsumer,
94              final HttpContext context,
95              final FutureCallback<T> callback) {
96          throw new UnsupportedOperationException();
97      }
98  
99      @Override
100     public Future<HttpResponse> execute(
101             final HttpHost target,
102             final HttpRequest request,
103             final HttpContext context,
104             final FutureCallback<HttpResponse> callback) {
105         final BasicFuture<HttpResponse> future = new BasicFuture<HttpResponse>(
106                 callback);
107         try {
108             final HttpResponse result = backend.execute(new HttpRoute(target),
109                     HttpRequestWrapper.wrap(request),
110                     HttpClientContext.adapt(context), null);
111             future.completed(result);
112         } catch (IOException e) {
113             future.failed(e);
114         } catch (HttpException e) {
115             future.failed(e);
116         }
117         return future;
118     }
119 
120 }