| 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.util.ArrayList; |
| 30 |
import java.util.List; |
| 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.protocol.HttpClientContext; |
| 39 |
import org.apache.http.concurrent.FutureCallback; |
| 40 |
import org.apache.http.nio.client.HttpPipeliningClient; |
| 41 |
import org.apache.http.nio.client.methods.HttpAsyncMethods; |
| 42 |
import org.apache.http.nio.protocol.HttpAsyncRequestProducer; |
| 43 |
import org.apache.http.nio.protocol.HttpAsyncResponseConsumer; |
| 44 |
import org.apache.http.protocol.HttpContext; |
| 45 |
import org.apache.http.util.Args; |
| 46 |
|
| 47 |
/** |
| 48 |
* Base implementation of {@link org.apache.http.nio.client.HttpPipeliningClient} that also |
| 49 |
* implements {@link java.io.Closeable}. |
| 50 |
* |
| 51 |
* @since 4.1 |
| 52 |
*/ |
| 53 |
@Contract(threading = ThreadingBehavior.SAFE) |
| 54 |
public abstract class CloseableHttpPipeliningClient |
| 55 |
extends CloseableHttpAsyncClient implements HttpPipeliningClient { |
| 56 |
|
| 57 |
@Override |
| 58 |
public <T> Future<List<T>> execute( |
| 59 |
final HttpHost target, |
| 60 |
final List<? extends HttpAsyncRequestProducer> requestProducers, |
| 61 |
final List<? extends HttpAsyncResponseConsumer<T>> responseConsumers, |
| 62 |
final FutureCallback<List<T>> callback) { |
| 63 |
return execute(target, requestProducers, responseConsumers, HttpClientContext.create(), callback); |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public Future<List<HttpResponse>> execute( |
| 68 |
final HttpHost target, |
| 69 |
final List<HttpRequest> requests, |
| 70 |
final FutureCallback<List<HttpResponse>> callback) { |
| 71 |
return execute(target, requests, HttpClientContext.create(), callback); |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public Future<List<HttpResponse>> execute( |
| 76 |
final HttpHost target, |
| 77 |
final List<HttpRequest> requests, |
| 78 |
final HttpContext context, |
| 79 |
final FutureCallback<List<HttpResponse>> callback) { |
| 80 |
Args.notEmpty(requests, "HTTP request list"); |
| 81 |
final List<HttpAsyncRequestProducer> requestProducers = new ArrayList<HttpAsyncRequestProducer>( |
| 82 |
requests.size()); |
| 83 |
final List<HttpAsyncResponseConsumer<HttpResponse>> responseConsumers = new ArrayList<HttpAsyncResponseConsumer<HttpResponse>>( |
| 84 |
requests.size()); |
| 85 |
for (int i = 0; i < requests.size(); i++) { |
| 86 |
final HttpRequest request = requests.get(i); |
| 87 |
requestProducers.add(HttpAsyncMethods.create(target, request)); |
| 88 |
responseConsumers.add(HttpAsyncMethods.createConsumer()); |
| 89 |
} |
| 90 |
return execute(target, requestProducers, responseConsumers, context, callback); |
| 91 |
} |
| 92 |
|
| 93 |
} |