| 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.nio.client; |
| 28 |
|
| 29 |
import java.io.Closeable; |
| 30 |
import java.net.URI; |
| 31 |
import java.util.concurrent.Future; |
| 32 |
|
| 33 |
import org.apache.http.HttpHost; |
| 34 |
import org.apache.http.HttpRequest; |
| 35 |
import org.apache.http.HttpResponse; |
| 36 |
import org.apache.http.annotation.Contract; |
| 37 |
import org.apache.http.annotation.ThreadingBehavior; |
| 38 |
import org.apache.http.client.ClientProtocolException; |
| 39 |
import org.apache.http.client.methods.HttpUriRequest; |
| 40 |
import org.apache.http.client.protocol.HttpClientContext; |
| 41 |
import org.apache.http.client.utils.URIUtils; |
| 42 |
import org.apache.http.concurrent.BasicFuture; |
| 43 |
import org.apache.http.concurrent.FutureCallback; |
| 44 |
import org.apache.http.nio.client.HttpAsyncClient; |
| 45 |
import org.apache.http.nio.client.methods.HttpAsyncMethods; |
| 46 |
import org.apache.http.nio.protocol.HttpAsyncRequestProducer; |
| 47 |
import org.apache.http.nio.protocol.HttpAsyncResponseConsumer; |
| 48 |
import org.apache.http.protocol.HttpContext; |
| 49 |
import org.apache.http.util.Args; |
| 50 |
|
| 51 |
/** |
| 52 |
* Base implementation of {@link HttpAsyncClient} that also implements {@link Closeable}. |
| 53 |
* |
| 54 |
* @since 4.0 |
| 55 |
*/ |
| 56 |
@Contract(threading = ThreadingBehavior.SAFE) |
| 57 |
public abstract class CloseableHttpAsyncClient implements HttpAsyncClient, Closeable { |
| 58 |
|
| 59 |
public abstract boolean isRunning(); |
| 60 |
|
| 61 |
public abstract void start(); |
| 62 |
|
| 63 |
@Override |
| 64 |
public <T> Future<T> execute( |
| 65 |
final HttpAsyncRequestProducer requestProducer, |
| 66 |
final HttpAsyncResponseConsumer<T> responseConsumer, |
| 67 |
final FutureCallback<T> callback) { |
| 68 |
return execute(requestProducer, responseConsumer, HttpClientContext.create(), callback); |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public Future<HttpResponse> execute( |
| 73 |
final HttpHost target, final HttpRequest request, final HttpContext context, |
| 74 |
final FutureCallback<HttpResponse> callback) { |
| 75 |
return execute( |
| 76 |
HttpAsyncMethods.create(target, request), |
| 77 |
HttpAsyncMethods.createConsumer(), |
| 78 |
context, callback); |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public Future<HttpResponse> execute( |
| 83 |
final HttpHost target, final HttpRequest request, |
| 84 |
final FutureCallback<HttpResponse> callback) { |
| 85 |
return execute(target, request, HttpClientContext.create(), callback); |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public Future<HttpResponse> execute( |
| 90 |
final HttpUriRequest request, |
| 91 |
final FutureCallback<HttpResponse> callback) { |
| 92 |
return execute(request, HttpClientContext.create(), callback); |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public Future<HttpResponse> execute( |
| 97 |
final HttpUriRequest request, |
| 98 |
final HttpContext context, |
| 99 |
final FutureCallback<HttpResponse> callback) { |
| 100 |
final HttpHost target; |
| 101 |
try { |
| 102 |
target = determineTarget(request); |
| 103 |
} catch (final ClientProtocolException ex) { |
| 104 |
final BasicFuture<HttpResponse> future = new BasicFuture<HttpResponse>(callback); |
| 105 |
future.failed(ex); |
| 106 |
return future; |
| 107 |
} |
| 108 |
return execute(target, request, context, callback); |
| 109 |
} |
| 110 |
|
| 111 |
private HttpHost determineTarget(final HttpUriRequest request) throws ClientProtocolException { |
| 112 |
Args.notNull(request, "HTTP request"); |
| 113 |
// A null target may be acceptable if there is a default target. |
| 114 |
// Otherwise, the null target is detected in the director. |
| 115 |
HttpHost target = null; |
| 116 |
|
| 117 |
final URI requestURI = request.getURI(); |
| 118 |
if (requestURI.isAbsolute()) { |
| 119 |
target = URIUtils.extractHost(requestURI); |
| 120 |
if (target == null) { |
| 121 |
throw new ClientProtocolException( |
| 122 |
"URI does not specify a valid host name: " + requestURI); |
| 123 |
} |
| 124 |
} |
| 125 |
return target; |
| 126 |
} |
| 127 |
|
| 128 |
} |