root / extensions / apachehc / src / main / java / org / jclouds / http / apachehc / ApacheHCHttpCommandExecutorService.java @ 35e7942d
History | View | Annotate | Download (3.1 kB)
1 |
/**
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
|
4 |
*
|
5 |
* ====================================================================
|
6 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
7 |
* you may not use this file except in compliance with the License.
|
8 |
* You may obtain a copy of the License at
|
9 |
*
|
10 |
* http://www.apache.org/licenses/LICENSE-2.0
|
11 |
*
|
12 |
* Unless required by applicable law or agreed to in writing, software
|
13 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15 |
* See the License for the specific language governing permissions and
|
16 |
* limitations under the License.
|
17 |
* ====================================================================
|
18 |
*/
|
19 |
|
20 |
package org.jclouds.http.apachehc; |
21 |
|
22 |
import java.io.IOException; |
23 |
import java.net.URI; |
24 |
import java.util.concurrent.ExecutorService; |
25 |
|
26 |
import javax.inject.Named; |
27 |
|
28 |
import org.apache.http.HttpHost; |
29 |
import org.apache.http.client.ClientProtocolException; |
30 |
import org.apache.http.client.HttpClient; |
31 |
import org.apache.http.client.methods.HttpUriRequest; |
32 |
import org.jclouds.Constants; |
33 |
import org.jclouds.http.HttpRequest; |
34 |
import org.jclouds.http.HttpResponse; |
35 |
import org.jclouds.http.handlers.DelegatingErrorHandler; |
36 |
import org.jclouds.http.handlers.DelegatingRetryHandler; |
37 |
import org.jclouds.http.internal.BaseHttpCommandExecutorService; |
38 |
import org.jclouds.http.internal.HttpWire; |
39 |
|
40 |
import com.google.inject.Inject; |
41 |
|
42 |
/**
|
43 |
* Simple implementation of a {@link HttpFutureCommandClient}, Apache Components HttpClient 4.x.
|
44 |
*
|
45 |
* @author Sam Tunnicliffe
|
46 |
* @author Adrian Cole
|
47 |
*/
|
48 |
public class ApacheHCHttpCommandExecutorService extends |
49 |
BaseHttpCommandExecutorService<HttpUriRequest> { |
50 |
private final HttpClient client; |
51 |
|
52 |
@Inject
|
53 |
ApacheHCHttpCommandExecutorService( |
54 |
@Named(Constants.PROPERTY_IO_WORKER_THREADS) ExecutorService ioWorkerExecutor, |
55 |
DelegatingRetryHandler retryHandler, DelegatingErrorHandler errorHandler, |
56 |
HttpWire wire, HttpClient client) { |
57 |
super(ioWorkerExecutor, retryHandler, errorHandler, wire);
|
58 |
this.client = client;
|
59 |
} |
60 |
|
61 |
@Override
|
62 |
protected HttpUriRequest convert(HttpRequest request) throws IOException { |
63 |
return ApacheHCUtils.convertToApacheRequest(request);
|
64 |
} |
65 |
|
66 |
@Override
|
67 |
protected HttpResponse invoke(HttpUriRequest nativeRequest) throws IOException { |
68 |
org.apache.http.HttpResponse nativeResponse = executeRequest(nativeRequest); |
69 |
return ApacheHCUtils.convertToJCloudsResponse(nativeResponse);
|
70 |
} |
71 |
|
72 |
private org.apache.http.HttpResponse executeRequest(HttpUriRequest nativeRequest)
|
73 |
throws IOException, ClientProtocolException { |
74 |
URI endpoint = URI.create(nativeRequest.getRequestLine().getUri()); |
75 |
HttpHost host = new HttpHost(endpoint.getHost(), endpoint.getPort(), endpoint.getScheme());
|
76 |
org.apache.http.HttpResponse nativeResponse = client.execute(host, nativeRequest); |
77 |
return nativeResponse;
|
78 |
} |
79 |
|
80 |
@Override
|
81 |
protected void cleanup(HttpUriRequest nativeResponse) { |
82 |
// No cleanup necessary
|
83 |
} |
84 |
} |