From e5d0edc3a0250f8fc528d7520c980f999b05ae86 Mon Sep 17 00:00:00 2001 From: rfeng Date: Fri, 2 Mar 2012 18:49:59 +0000 Subject: Use Apache HTTP client for REST reference binding git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1296382 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/binding/rest/provider/RESTBindingInvoker.java | 10 ++++++---- .../binding/rest/provider/RESTReferenceBindingProvider.java | 13 ++++++++++++- 2 files changed, 18 insertions(+), 5 deletions(-) (limited to 'sca-java-2.x/trunk/modules/binding-rest-runtime/src/main') diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingInvoker.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingInvoker.java index e5de008fa5..b6c598996f 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingInvoker.java +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingInvoker.java @@ -52,6 +52,7 @@ import javax.ws.rs.core.Cookie; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.UriBuilder; +import org.apache.http.client.HttpClient; import org.apache.tuscany.sca.assembly.WireFormat; import org.apache.tuscany.sca.binding.rest.RESTBinding; import org.apache.tuscany.sca.binding.rest.wireformat.json.JSONWireFormat; @@ -63,6 +64,7 @@ import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.interfacedef.java.JavaOperation; import org.apache.tuscany.sca.invocation.Invoker; import org.apache.tuscany.sca.invocation.Message; +import org.apache.wink.client.ApacheHttpClientConfig; import org.apache.wink.client.ClientConfig; import org.apache.wink.client.Resource; import org.apache.wink.client.RestClient; @@ -79,12 +81,12 @@ public class RESTBindingInvoker implements Invoker { private String httpMethod; private Class responseType; - public RESTBindingInvoker(ExtensionPointRegistry registry, RESTBinding binding, Operation operation) { + public RESTBindingInvoker(ExtensionPointRegistry registry, RESTBinding binding, Operation operation, HttpClient httpClient) { super(); this.registry = registry; this.binding = binding; this.operation = operation; - this.restClient = createRestClient(); + this.restClient = createRestClient(httpClient); } private static Map, String> mapping = new HashMap, String>(); @@ -106,8 +108,8 @@ public class RESTBindingInvoker implements Invoker { return null; } - private RestClient createRestClient() { - ClientConfig config = new ClientConfig(); + private RestClient createRestClient(HttpClient httpClient) { + ClientConfig config = new ApacheHttpClientConfig(httpClient); // configureBasicAuth(config, userName, password); diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTReferenceBindingProvider.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTReferenceBindingProvider.java index e218f32573..71d7aa79ba 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTReferenceBindingProvider.java +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTReferenceBindingProvider.java @@ -19,8 +19,10 @@ package org.apache.tuscany.sca.binding.rest.provider; +import org.apache.http.client.HttpClient; import org.apache.tuscany.sca.binding.rest.RESTBinding; import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.host.http.client.HttpClientFactory; import org.apache.tuscany.sca.interfacedef.InterfaceContract; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.invocation.Invoker; @@ -33,18 +35,22 @@ import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; public class RESTReferenceBindingProvider implements EndpointReferenceProvider { private ExtensionPointRegistry registry; private RuntimeEndpointReference endpointReference; + + private HttpClientFactory httpClientFactory; + private HttpClient httpClient; public RESTReferenceBindingProvider(ExtensionPointRegistry registry, RuntimeEndpointReference endpointReference) { super(); this.registry = registry; this.endpointReference = endpointReference; + this.httpClientFactory = HttpClientFactory.getInstance(registry); } public void configure() { } public Invoker createInvoker(Operation operation) { - return new RESTBindingInvoker(registry, (RESTBinding)endpointReference.getBinding(), operation); + return new RESTBindingInvoker(registry, (RESTBinding)endpointReference.getBinding(), operation, httpClient); } public InterfaceContract getBindingInterfaceContract() { @@ -56,9 +62,14 @@ public class RESTReferenceBindingProvider implements EndpointReferenceProvider { } public void start() { + // Create an HTTP client + httpClient = httpClientFactory.createHttpClient(); } public void stop() { + if (httpClient != null) { + httpClient.getConnectionManager().shutdown(); + } } } -- cgit v1.2.3