| 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 |
@Override |
| 88 |
public void close() throws IOException { |
| 89 |
// no-op |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public <T> Future<T> execute( |
| 94 |
final HttpAsyncRequestProducer requestProducer, |
| 95 |
final HttpAsyncResponseConsumer<T> responseConsumer, |
| 96 |
final HttpContext context, |
| 97 |
final FutureCallback<T> callback) { |
| 98 |
throw new UnsupportedOperationException(); |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public Future<HttpResponse> execute( |
| 103 |
final HttpHost target, |
| 104 |
final HttpRequest request, |
| 105 |
final HttpContext context, |
| 106 |
final FutureCallback<HttpResponse> callback) { |
| 107 |
final BasicFuture<HttpResponse> future = new BasicFuture<HttpResponse>( |
| 108 |
callback); |
| 109 |
try { |
| 110 |
final HttpResponse result = backend.execute(new HttpRoute(target), |
| 111 |
HttpRequestWrapper.wrap(request), |
| 112 |
HttpClientContext.adapt(context), null); |
| 113 |
future.completed(result); |
| 114 |
} catch (final IOException e) { |
| 115 |
future.failed(e); |
| 116 |
} catch (final HttpException e) { |
| 117 |
future.failed(e); |
| 118 |
} |
| 119 |
return future; |
| 120 |
} |
| 121 |
|
| 122 |
} |