diff options
Diffstat (limited to 'sca-java-2.x/trunk/modules/binding-atom-runtime/src')
5 files changed, 289 insertions, 212 deletions
diff --git a/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingInvoker.java b/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingInvoker.java index e92bfa07e4..9ad69d58a3 100644 --- a/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingInvoker.java +++ b/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingInvoker.java @@ -21,6 +21,7 @@ package org.apache.tuscany.sca.binding.atom.provider; import static org.apache.tuscany.sca.binding.atom.provider.AtomBindingUtil.entry; import static org.apache.tuscany.sca.binding.atom.provider.AtomBindingUtil.feedEntry; +import java.io.IOException; import java.io.StringWriter; import java.util.ArrayList; import java.util.List; @@ -30,12 +31,15 @@ import org.apache.abdera.factory.Factory; import org.apache.abdera.model.Document; import org.apache.abdera.model.Feed; import org.apache.abdera.parser.Parser; -import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.methods.DeleteMethod; -import org.apache.commons.httpclient.methods.GetMethod; -import org.apache.commons.httpclient.methods.PostMethod; -import org.apache.commons.httpclient.methods.PutMethod; -import org.apache.commons.httpclient.methods.StringRequestEntity; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.entity.StringEntity; import org.apache.tuscany.sca.binding.atom.collection.NotFoundException; import org.apache.tuscany.sca.data.collection.Entry; import org.apache.tuscany.sca.interfacedef.Operation; @@ -49,7 +53,7 @@ import org.oasisopen.sca.ServiceRuntimeException; * @version $Rev$ $Date$ */ class AtomBindingInvoker implements Invoker { - + private static final Factory abderaFactory = Abdera.getNewFactory(); private static final Parser abderaParser = Abdera.getNewParser(); @@ -58,15 +62,19 @@ class AtomBindingInvoker implements Invoker { HttpClient httpClient; String authorizationHeader; AtomReferenceBindingProvider provider; - - AtomBindingInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader, AtomReferenceBindingProvider bindingProvider) { + + AtomBindingInvoker(Operation operation, + String uri, + org.apache.http.client.HttpClient httpClient, + String authorizationHeader, + AtomReferenceBindingProvider bindingProvider) { this.operation = operation; this.uri = uri; this.httpClient = httpClient; this.authorizationHeader = authorizationHeader; this.provider = bindingProvider; } - + public Message invoke(Message msg) { // Shouldn't get here, as the only supported methods are // defined in the ResourceCollection interface, and implemented @@ -79,7 +87,11 @@ class AtomBindingInvoker implements Invoker { */ public static class GetInvoker extends AtomBindingInvoker { - public GetInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader, AtomReferenceBindingProvider bindingProvider) { + public GetInvoker(Operation operation, + String uri, + org.apache.http.client.HttpClient httpClient, + String authorizationHeader, + AtomReferenceBindingProvider bindingProvider) { super(operation, uri, httpClient, authorizationHeader, bindingProvider); } @@ -89,40 +101,43 @@ class AtomBindingInvoker implements Invoker { String id = (String)((Object[])msg.getBody())[0]; // Send an HTTP GET - GetMethod getMethod = new GetMethod(uri + "/" + id); + HttpGet getMethod = new HttpGet(uri + "/" + id); if (authorizationHeader != null) { - getMethod.setRequestHeader("Authorization", authorizationHeader); + getMethod.setHeader("Authorization", authorizationHeader); } boolean parsing = false; + HttpResponse response = null; try { - httpClient.executeMethod(getMethod); - int status = getMethod.getStatusCode(); + response = httpClient.execute(getMethod); + int status = response.getStatusLine().getStatusCode(); // Read the Atom entry if (status == 200) { - Document<org.apache.abdera.model.Entry> doc = - abderaParser.parse(getMethod.getResponseBodyAsStream()); + Document<org.apache.abdera.model.Entry> doc = abderaParser.parse(response.getEntity().getContent()); parsing = true; org.apache.abdera.model.Entry feedEntry = doc.getRoot(); - + if (provider.supportsFeedEntries()) { - + // Return the Atom entry msg.setBody(feedEntry); - + } else { - + // Convert the feed entry to a data entry and return the data item - Entry<Object, Object> entry = entry(feedEntry, provider.getItemClassType(), - provider.getItemXMLType(), provider.getMediator()); + Entry<Object, Object> entry = + entry(feedEntry, + provider.getItemClassType(), + provider.getItemXMLType(), + provider.getMediator()); msg.setBody(entry.getData()); } } else if (status == 404) { - if ( provider.supportsFeedEntries()) - msg.setFaultBody(new NotFoundException()); - else - msg.setFaultBody(new org.apache.tuscany.sca.data.collection.NotFoundException()); + if (provider.supportsFeedEntries()) + msg.setFaultBody(new NotFoundException()); + else + msg.setFaultBody(new org.apache.tuscany.sca.data.collection.NotFoundException()); } else { msg.setFaultBody(new ServiceRuntimeException("HTTP status code: " + status)); } @@ -133,7 +148,7 @@ class AtomBindingInvoker implements Invoker { if (!parsing) { // Release the connection unless the Abdera parser is // parsing the response, in this case it will release it - getMethod.releaseConnection(); + release(getMethod, response); } } @@ -146,7 +161,11 @@ class AtomBindingInvoker implements Invoker { */ public static class PostInvoker extends AtomBindingInvoker { - public PostInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader, AtomReferenceBindingProvider bindingProvider) { + public PostInvoker(Operation operation, + String uri, + HttpClient httpClient, + String authorizationHeader, + AtomReferenceBindingProvider bindingProvider) { super(operation, uri, httpClient, authorizationHeader, bindingProvider); } @@ -156,58 +175,64 @@ class AtomBindingInvoker implements Invoker { Object[] args = (Object[])msg.getBody(); org.apache.abdera.model.Entry feedEntry; if (provider.supportsFeedEntries()) { - + // Expect an Atom entry feedEntry = (org.apache.abdera.model.Entry)args[0]; } else { - + // Expect a key and data item Entry<Object, Object> entry = new Entry<Object, Object>(args[0], args[1]); - feedEntry = feedEntry(entry, provider.getItemClassType(), - provider.getItemXMLType(), provider.getMediator(), abderaFactory); + feedEntry = + feedEntry(entry, + provider.getItemClassType(), + provider.getItemXMLType(), + provider.getMediator(), + abderaFactory); } // Send an HTTP POST - PostMethod postMethod = new PostMethod(uri); + HttpPost postMethod = new HttpPost(uri); if (authorizationHeader != null) { - postMethod.setRequestHeader("Authorization", authorizationHeader); + postMethod.setHeader("Authorization", authorizationHeader); } boolean parsing = false; + HttpResponse response = null; try { // Write the Atom entry StringWriter writer = new StringWriter(); feedEntry.writeTo(writer); - // postMethod.setRequestHeader("Content-type", "application/atom+xml; charset=utf-8"); - postMethod.setRequestHeader("Content-type", "application/atom+xml;type=entry"); - postMethod.setRequestEntity(new StringRequestEntity(writer.toString())); + // postMethod.setHeader("Content-type", "application/atom+xml; charset=utf-8"); + postMethod.setHeader("Content-type", "application/atom+xml;type=entry"); + postMethod.setEntity(new StringEntity(writer.toString())); - httpClient.executeMethod(postMethod); - int status = postMethod.getStatusCode(); + response = httpClient.execute(postMethod); + int status = response.getStatusLine().getStatusCode(); // Read the Atom entry if (status == 200 || status == 201) { - Document<org.apache.abdera.model.Entry> doc = abderaParser.parse(postMethod.getResponseBodyAsStream()); + Document<org.apache.abdera.model.Entry> doc = + abderaParser.parse(postMethod.getEntity().getContent()); parsing = true; org.apache.abdera.model.Entry createdEntry = doc.getRoot(); // Returns the created Atom entry ID if (provider.supportsFeedEntries()) { - + // Returns the created entry msg.setBody(createdEntry); - + } else { - + // Returns the id of the created entry msg.setBody(createdEntry.getId().toString()); } } else if (status == 404) { - if ( provider.supportsFeedEntries()) - msg.setFaultBody(new NotFoundException()); - else - msg.setFaultBody(new org.apache.tuscany.sca.data.collection.NotFoundException()); + if (provider.supportsFeedEntries()) + msg.setFaultBody(new NotFoundException()); + else + msg.setFaultBody(new org.apache.tuscany.sca.data.collection.NotFoundException()); } else { msg.setFaultBody(new ServiceRuntimeException("HTTP status code: " + status)); } @@ -218,7 +243,7 @@ class AtomBindingInvoker implements Invoker { if (!parsing) { // Release the connection unless the Abdera parser is // parsing the response, in this case it will release it - postMethod.releaseConnection(); + release(postMethod, response); } } @@ -231,7 +256,11 @@ class AtomBindingInvoker implements Invoker { */ public static class PutInvoker extends AtomBindingInvoker { - public PutInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader, AtomReferenceBindingProvider bindingProvider) { + public PutInvoker(Operation operation, + String uri, + HttpClient httpClient, + String authorizationHeader, + AtomReferenceBindingProvider bindingProvider) { super(operation, uri, httpClient, authorizationHeader, bindingProvider); } @@ -242,44 +271,49 @@ class AtomBindingInvoker implements Invoker { String id; org.apache.abdera.model.Entry feedEntry; if (provider.supportsFeedEntries()) { - + // Expect a key and Atom entry id = (String)args[0]; feedEntry = (org.apache.abdera.model.Entry)args[1]; } else { - + // Expect a key and data item id = (String)args[0]; Entry<Object, Object> entry = new Entry<Object, Object>(id, args[1]); - feedEntry = feedEntry(entry, provider.getItemClassType(), - provider.getItemXMLType(), provider.getMediator(), abderaFactory); + feedEntry = + feedEntry(entry, + provider.getItemClassType(), + provider.getItemXMLType(), + provider.getMediator(), + abderaFactory); } // Send an HTTP PUT - PutMethod putMethod = new PutMethod(uri + "/" + id); + HttpPut putMethod = new HttpPut(uri + "/" + id); if (authorizationHeader != null) { - putMethod.setRequestHeader("Authorization", authorizationHeader); + putMethod.setHeader("Authorization", authorizationHeader); } + HttpResponse response = null; try { // Write the Atom entry StringWriter writer = new StringWriter(); feedEntry.writeTo(writer); - putMethod.setRequestHeader("Content-type", "application/atom+xml; charset=utf-8"); - putMethod.setRequestEntity(new StringRequestEntity(writer.toString())); + putMethod.setHeader("Content-type", "application/atom+xml; charset=utf-8"); + putMethod.setEntity(new StringEntity(writer.toString())); - httpClient.executeMethod(putMethod); - int status = putMethod.getStatusCode(); + response = httpClient.execute(putMethod); + int status = response.getStatusLine().getStatusCode(); if (status == 200 || status == 201 || status == 412) { msg.setBody(null); } else if (status == 404) { - if ( provider.supportsFeedEntries()) - msg.setFaultBody(new NotFoundException()); - else - msg.setFaultBody(new org.apache.tuscany.sca.data.collection.NotFoundException()); + if (provider.supportsFeedEntries()) + msg.setFaultBody(new NotFoundException()); + else + msg.setFaultBody(new org.apache.tuscany.sca.data.collection.NotFoundException()); } else { msg.setFaultBody(new ServiceRuntimeException("HTTP status code: " + status)); } @@ -287,7 +321,7 @@ class AtomBindingInvoker implements Invoker { } catch (Exception e) { msg.setFaultBody(new ServiceRuntimeException(e)); } finally { - putMethod.releaseConnection(); + release(putMethod, response); } return msg; @@ -299,7 +333,11 @@ class AtomBindingInvoker implements Invoker { */ public static class DeleteInvoker extends AtomBindingInvoker { - public DeleteInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader, AtomReferenceBindingProvider bindingProvider) { + public DeleteInvoker(Operation operation, + String uri, + HttpClient httpClient, + String authorizationHeader, + AtomReferenceBindingProvider bindingProvider) { super(operation, uri, httpClient, authorizationHeader, bindingProvider); } @@ -309,21 +347,22 @@ class AtomBindingInvoker implements Invoker { String id = (String)((Object[])msg.getBody())[0]; // Send an HTTP DELETE - DeleteMethod deleteMethod = new DeleteMethod(uri + "/" + id); + HttpDelete deleteMethod = new HttpDelete(uri + "/" + id); if (authorizationHeader != null) { - deleteMethod.setRequestHeader("Authorization", authorizationHeader); + deleteMethod.setHeader("Authorization", authorizationHeader); } + HttpResponse response = null; try { - httpClient.executeMethod(deleteMethod); - int status = deleteMethod.getStatusCode(); + response = httpClient.execute(deleteMethod); + int status = response.getStatusLine().getStatusCode(); if (status == 200) { msg.setBody(null); } else if (status == 404) { - if ( provider.supportsFeedEntries()) - msg.setFaultBody(new NotFoundException()); - else - msg.setFaultBody(new org.apache.tuscany.sca.data.collection.NotFoundException()); + if (provider.supportsFeedEntries()) + msg.setFaultBody(new NotFoundException()); + else + msg.setFaultBody(new org.apache.tuscany.sca.data.collection.NotFoundException()); } else { msg.setFaultBody(new ServiceRuntimeException("HTTP status code: " + status)); } @@ -331,7 +370,7 @@ class AtomBindingInvoker implements Invoker { } catch (Exception e) { msg.setFaultBody(new ServiceRuntimeException(e)); } finally { - deleteMethod.releaseConnection(); + release(deleteMethod, response); } return msg; @@ -343,7 +382,11 @@ class AtomBindingInvoker implements Invoker { */ public static class GetAllInvoker extends AtomBindingInvoker { - public GetAllInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader, AtomReferenceBindingProvider bindingProvider) { + public GetAllInvoker(Operation operation, + String uri, + HttpClient httpClient, + String authorizationHeader, + AtomReferenceBindingProvider bindingProvider) { super(operation, uri, httpClient, authorizationHeader, bindingProvider); } @@ -352,51 +395,55 @@ class AtomBindingInvoker implements Invoker { // Get a feed // Send an HTTP GET - GetMethod getMethod = new GetMethod(uri); + HttpGet getMethod = new HttpGet(uri); if (authorizationHeader != null) { - getMethod.setRequestHeader("Authorization", authorizationHeader); + getMethod.setHeader("Authorization", authorizationHeader); } boolean parsing = false; + HttpResponse response = null; try { - httpClient.executeMethod(getMethod); - int status = getMethod.getStatusCode(); + response = httpClient.execute(getMethod); + int status = response.getStatusLine().getStatusCode(); // AtomBindingInvoker.printResponseHeader( getMethod ); // Read the Atom feed if (status == 200) { - Document<Feed> doc = abderaParser.parse(getMethod.getResponseBodyAsStream()); + Document<Feed> doc = abderaParser.parse(response.getEntity().getContent()); parsing = true; - + Feed feed = null; try { - feed = doc.getRoot(); - } catch(Exception e) { + feed = doc.getRoot(); + } catch (Exception e) { throw new IllegalArgumentException("Invalid feed format :" + uri); } if (provider.supportsFeedEntries()) { - + // Returns the Atom feed msg.setBody(feed); - + } else { - + // Returns an array of data entries - List<Entry<Object, Object>> entries = new ArrayList<Entry<Object,Object>>(); - for (org.apache.abdera.model.Entry feedEntry: feed.getEntries()) { - Entry<Object, Object> entry = entry(feedEntry, provider.getItemClassType(), - provider.getItemXMLType(), provider.getMediator()); + List<Entry<Object, Object>> entries = new ArrayList<Entry<Object, Object>>(); + for (org.apache.abdera.model.Entry feedEntry : feed.getEntries()) { + Entry<Object, Object> entry = + entry(feedEntry, + provider.getItemClassType(), + provider.getItemXMLType(), + provider.getMediator()); entries.add(entry); } msg.setBody(entries.toArray(new Entry[entries.size()])); } } else if (status == 404) { - if ( provider.supportsFeedEntries()) { - msg.setFaultBody(new NotFoundException()); - } else { - msg.setFaultBody(new org.apache.tuscany.sca.data.collection.NotFoundException()); - } + if (provider.supportsFeedEntries()) { + msg.setFaultBody(new NotFoundException()); + } else { + msg.setFaultBody(new org.apache.tuscany.sca.data.collection.NotFoundException()); + } } else { msg.setFaultBody(new ServiceRuntimeException("HTTP status code: " + status)); } @@ -407,7 +454,7 @@ class AtomBindingInvoker implements Invoker { if (!parsing) { // Release the connection unless the Abdera parser is // parsing the response, in this case it will release it - getMethod.releaseConnection(); + release(getMethod, response); } } @@ -420,7 +467,11 @@ class AtomBindingInvoker implements Invoker { */ public static class QueryInvoker extends AtomBindingInvoker { - public QueryInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader, AtomReferenceBindingProvider bindingProvider) { + public QueryInvoker(Operation operation, + String uri, + HttpClient httpClient, + String authorizationHeader, + AtomReferenceBindingProvider bindingProvider) { super(operation, uri, httpClient, authorizationHeader, bindingProvider); } @@ -430,44 +481,48 @@ class AtomBindingInvoker implements Invoker { String queryString = (String)((Object[])msg.getBody())[0]; // Send an HTTP GET - GetMethod getMethod = new GetMethod(uri); + HttpGet getMethod = new HttpGet(uri + "?" + queryString); if (authorizationHeader != null) { - getMethod.setRequestHeader("Authorization", authorizationHeader); + getMethod.setHeader("Authorization", authorizationHeader); } - getMethod.setQueryString(queryString); + // getMethod.setQueryString(queryString); boolean parsing = false; + HttpResponse response = null; try { - httpClient.executeMethod(getMethod); - int status = getMethod.getStatusCode(); + response = httpClient.execute(getMethod); + int status = response.getStatusLine().getStatusCode(); // Read the Atom feed if (status == 200) { - Document<Feed> doc = abderaParser.parse(getMethod.getResponseBodyAsStream()); + Document<Feed> doc = abderaParser.parse(response.getEntity().getContent()); parsing = true; Feed feed = doc.getRoot(); if (provider.supportsFeedEntries()) { - + // Returns the Atom feed msg.setBody(feed); - + } else { - + // Returns an array of data entries - List<Entry<Object, Object>> entries = new ArrayList<Entry<Object,Object>>(); - for (org.apache.abdera.model.Entry feedEntry: feed.getEntries()) { - Entry<Object, Object> entry = entry(feedEntry, provider.getItemClassType(), - provider.getItemXMLType(), provider.getMediator()); + List<Entry<Object, Object>> entries = new ArrayList<Entry<Object, Object>>(); + for (org.apache.abdera.model.Entry feedEntry : feed.getEntries()) { + Entry<Object, Object> entry = + entry(feedEntry, + provider.getItemClassType(), + provider.getItemXMLType(), + provider.getMediator()); entries.add(entry); } msg.setBody(entries.toArray(new Entry[entries.size()])); } } else if (status == 404) { - if ( provider.supportsFeedEntries()) - msg.setFaultBody(new NotFoundException()); - else - msg.setFaultBody(new org.apache.tuscany.sca.data.collection.NotFoundException()); + if (provider.supportsFeedEntries()) + msg.setFaultBody(new NotFoundException()); + else + msg.setFaultBody(new org.apache.tuscany.sca.data.collection.NotFoundException()); } else { msg.setFaultBody(new ServiceRuntimeException("HTTP status code: " + status)); } @@ -478,7 +533,7 @@ class AtomBindingInvoker implements Invoker { if (!parsing) { // Release the connection unless the Abdera parser is // parsing the response, in this case it will release it - getMethod.releaseConnection(); + release(getMethod, response); } } @@ -486,18 +541,38 @@ class AtomBindingInvoker implements Invoker { } } + private static void release(HttpRequestBase request, HttpResponse response) { + + if (response != null) { + HttpEntity entity = response.getEntity(); + if (entity != null) { + try { + entity.consumeContent(); + } catch (IOException e) { + if (request != null) { + request.abort(); + } + } + } + } + } + /** * PostMedia operation invoker */ public static class PostMediaInvoker extends AtomBindingInvoker { - public PostMediaInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader, AtomReferenceBindingProvider bindingProvider) { + public PostMediaInvoker(Operation operation, + String uri, + HttpClient httpClient, + String authorizationHeader, + AtomReferenceBindingProvider bindingProvider) { super(operation, uri, httpClient, authorizationHeader, bindingProvider); } @Override public Message invoke(Message msg) { - // PostInvoker can detect media by content type (non-Feed, non-Entry) + // PostInvoker can detect media by content type (non-Feed, non-Entry) return super.invoke(msg); } } @@ -507,13 +582,17 @@ class AtomBindingInvoker implements Invoker { */ public static class PutMediaInvoker extends AtomBindingInvoker { - public PutMediaInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader, AtomReferenceBindingProvider bindingProvider) { + public PutMediaInvoker(Operation operation, + String uri, + HttpClient httpClient, + String authorizationHeader, + AtomReferenceBindingProvider bindingProvider) { super(operation, uri, httpClient, authorizationHeader, bindingProvider); } @Override public Message invoke(Message msg) { - // PutInvoker can detect media by content type (non-Feed, non-Entry) + // PutInvoker can detect media by content type (non-Feed, non-Entry) return super.invoke(msg); } } diff --git a/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingListenerServlet.java b/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingListenerServlet.java index 6dc9524f92..d5ae26e01b 100644 --- a/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingListenerServlet.java +++ b/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingListenerServlet.java @@ -145,13 +145,13 @@ class AtomBindingListenerServlet extends HttpServlet { // Determine the collection item type if (getOperation != null) { itemXMLType = new DataTypeImpl<Class<?>>(String.class.getName(), String.class, String.class); - Class<?> itemClass = getOperation.getOutputType().getPhysical(); + Class<?> itemClass = getOperation.getOutputType().getLogical().get(0).getPhysical(); if (itemClass == org.apache.abdera.model.Entry.class) { supportsFeedEntries = true; } //We assume that the item type is the same for both input and //ouput for all operations on the interface - itemClassType = ((List<DataType>)getOperation.getOutputType().getLogical()).get(0); + itemClassType = getOperation.getOutputType().getLogical().get(0); } } diff --git a/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingUtil.java b/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingUtil.java index fad06a0c99..ad1c8d2fd0 100644 --- a/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingUtil.java +++ b/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingUtil.java @@ -23,8 +23,8 @@ import java.util.Date; import org.apache.abdera.factory.Factory; import org.apache.abdera.model.Content; -import org.apache.abdera.model.Link; import org.apache.abdera.model.Content.Type; +import org.apache.abdera.model.Link; import org.apache.tuscany.sca.data.collection.Entry; import org.apache.tuscany.sca.data.collection.Item; import org.apache.tuscany.sca.databinding.Mediator; @@ -78,9 +78,11 @@ class AtomBindingUtil { } // Create the item from XML + /* if (feedEntry.getContentElement().getElements().size() == 0) { return null; } + */ String value = feedEntry.getContent(); Object data = mediator.mediate(value, itemXMLType, itemClassType, null); diff --git a/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomReferenceBindingProvider.java b/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomReferenceBindingProvider.java index 2801690f41..3612442e90 100644 --- a/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomReferenceBindingProvider.java +++ b/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomReferenceBindingProvider.java @@ -19,17 +19,10 @@ package org.apache.tuscany.sca.binding.atom.provider; -import java.net.URI; - -import org.apache.commons.codec.binary.Base64; -import org.apache.commons.httpclient.Credentials; -import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.HttpConnectionManager; -import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; -import org.apache.commons.httpclient.UsernamePasswordCredentials; -import org.apache.commons.httpclient.auth.AuthScope; +import org.apache.http.client.HttpClient; import org.apache.tuscany.sca.assembly.EndpointReference; import org.apache.tuscany.sca.binding.atom.AtomBinding; +import org.apache.tuscany.sca.common.http.client.HttpClientFactory; import org.apache.tuscany.sca.databinding.Mediator; import org.apache.tuscany.sca.interfacedef.DataType; import org.apache.tuscany.sca.interfacedef.InterfaceContract; @@ -77,10 +70,8 @@ class AtomReferenceBindingProvider implements ReferenceBindingProvider { //authorizationHeader = "Basic " + new String(Base64.encodeBase64(authorization.getBytes())); // Create an HTTP client - HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); - connectionManager.getParams().setDefaultMaxConnectionsPerHost(10); - connectionManager.getParams().setConnectionTimeout(60000); - httpClient = new HttpClient(connectionManager); + HttpClientFactory clientFactory = new HttpClientFactory(); + httpClient = clientFactory.createHttpClient(); } public Invoker createInvoker(Operation operation) { @@ -90,7 +81,6 @@ class AtomReferenceBindingProvider implements ReferenceBindingProvider { // Determine the collection item type itemXMLType = new DataTypeImpl<Class<?>>(String.class.getName(), String.class, String.class); - Class<?> itemClass = operation.getOutputType().getPhysical(); DataType<XMLType> outputType = operation.getOutputType().getLogical().get(0); itemClassType = outputType; if (itemClassType.getPhysical() == org.apache.abdera.model.Entry.class) { diff --git a/sca-java-2.x/trunk/modules/binding-atom-runtime/src/test/java/org/apache/tuscany/sca/binding/atom/MediaCollectionTestCase.java b/sca-java-2.x/trunk/modules/binding-atom-runtime/src/test/java/org/apache/tuscany/sca/binding/atom/MediaCollectionTestCase.java index 6d4a173d30..57eacab0e2 100644 --- a/sca-java-2.x/trunk/modules/binding-atom-runtime/src/test/java/org/apache/tuscany/sca/binding/atom/MediaCollectionTestCase.java +++ b/sca-java-2.x/trunk/modules/binding-atom-runtime/src/test/java/org/apache/tuscany/sca/binding/atom/MediaCollectionTestCase.java @@ -19,7 +19,6 @@ package org.apache.tuscany.sca.binding.atom; import java.io.File; -import java.io.FileInputStream; import java.text.SimpleDateFormat; import java.util.Date; @@ -32,11 +31,13 @@ import org.apache.abdera.model.Entry; import org.apache.abdera.model.Link; import org.apache.abdera.parser.Parser; import org.apache.abdera.protocol.client.AbderaClient; -import org.apache.commons.httpclient.Header; -import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.methods.InputStreamRequestEntity; -import org.apache.commons.httpclient.methods.PostMethod; -import org.apache.commons.httpclient.methods.PutMethod; +import org.apache.http.Header; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.entity.FileEntity; +import org.apache.tuscany.sca.common.http.client.HttpClientFactory; import org.apache.tuscany.sca.node.Contribution; import org.apache.tuscany.sca.node.ContributionLocationHelper; import org.apache.tuscany.sca.node.Node; @@ -62,11 +63,11 @@ public class MediaCollectionTestCase { protected static CustomerClient testService; protected static Abdera abdera; protected static AbderaClient client; - protected static Parser abderaParser; + protected static Parser abderaParser; protected static String eTag; protected static Date lastModified; protected static String mediaId; - protected static final SimpleDateFormat dateFormat = new SimpleDateFormat( "EEE, dd MMM yyyy HH:mm:ss Z" ); // RFC 822 date time + protected static final SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z"); // RFC 822 date time @BeforeClass public static void init() throws Exception { @@ -74,13 +75,15 @@ public class MediaCollectionTestCase { //System.out.println(">>>MediaCollectionTestCase.init"); String contribution = ContributionLocationHelper.getContributionLocation(MediaCollectionTestCase.class); - scaProviderNode = NodeFactory.newInstance().createNode("org/apache/tuscany/sca/binding/atom/ReceiptProvider.composite", new Contribution("provider", contribution)); + scaProviderNode = + NodeFactory.newInstance().createNode("org/apache/tuscany/sca/binding/atom/ReceiptProvider.composite", + new Contribution("provider", contribution)); scaProviderNode.start(); abdera = new Abdera(); client = new AbderaClient(abdera); abderaParser = Abdera.getNewParser(); - } catch(Exception e) { + } catch (Exception e) { e.printStackTrace(); } } @@ -96,7 +99,7 @@ public class MediaCollectionTestCase { @Test public void testPrelim() throws Exception { Assert.assertNotNull(scaProviderNode); - Assert.assertNotNull( client ); + Assert.assertNotNull(client); } @Test @@ -113,23 +116,25 @@ public class MediaCollectionTestCase { // Testing of entry creation String receiptName = "Auto Repair Bill"; String fileName = "target/test-classes/ReceiptToms.gif"; - File input = new File( fileName ); + File input = new File(fileName); boolean exists = input.exists(); - Assert.assertTrue( exists ); + Assert.assertTrue(exists); // Prepare HTTP post // PostMethod post = new PostMethod( colUri.toString() ); - PostMethod post = new PostMethod( providerURI ); - post.addRequestHeader( "Content-Type", "image/gif" ); - post.addRequestHeader( "Title", "Title " + receiptName + "" ); - post.addRequestHeader( "Slug", "Slug " + receiptName + "" ); - post.setRequestEntity( new InputStreamRequestEntity( new FileInputStream( input ), "image/gif" ) ); + HttpPost post = new HttpPost(providerURI); + post.addHeader("Content-Type", "image/gif"); + post.addHeader("Title", "Title " + receiptName + ""); + post.addHeader("Slug", "Slug " + receiptName + ""); + + post.setEntity(new FileEntity(input, "image/gif")); // Get HTTP client - HttpClient httpclient = new HttpClient(); + org.apache.http.client.HttpClient httpclient = new HttpClientFactory().createHttpClient(); try { // Execute request - int result = httpclient.executeMethod(post); + HttpResponse response = httpclient.execute(post); + int result = response.getStatusLine().getStatusCode(); // Pseudo Code (see APP (http://tools.ietf.org/html/rfc5023#section-9.6) // Post response // Tuscany responds with proper media links. Note that the media is @@ -138,31 +143,31 @@ public class MediaCollectionTestCase { // HTTP/1.1 201 Created // Display status code // System.out.println("Response status code: " + result + ", status text=" + post.getStatusText() ); - Assert.assertEquals(201, result ); + Assert.assertEquals(201, result); // Display response // System.out.println("Response body: "); // System.out.println(post.getResponseBodyAsString()); // Warning: BodyAsString recommends BodyAsStream // Location: http://example.org/media/edit/the_beach.atom (REQUIRED) - // System.out.println( "Response Location=" + post.getResponseHeader( "Location" ).getValue() + "." ); - Header header = post.getResponseHeader( "Location" ); - Assert.assertNotNull( header ); - Assert.assertNotNull( header.getValue() ); + // System.out.println( "Response Location=" + response.getFirstHeader( "Location" ).getValue() + "." ); + Header header = response.getFirstHeader("Location"); + Assert.assertNotNull(header); + Assert.assertNotNull(header.getValue()); // ContentLocation: http://example.org/media/edit/the_beach.jpg (REQUIRED) - // System.out.println( "Response Content-Location=" + post.getResponseHeader( "Content-Location" ).getValue() ); - header = post.getResponseHeader( "Content-Location" ); - Assert.assertNotNull( header ); - Assert.assertNotNull( header.getValue() ); + // System.out.println( "Response Content-Location=" + response.getFirstHeader( "Content-Location" ).getValue() ); + header = response.getFirstHeader("Content-Location"); + Assert.assertNotNull(header); + Assert.assertNotNull(header.getValue()); // Content-Type: application/atom+xml;type=entry;charset="utf-8" - // System.out.println( "Response Content-Type=" + post.getResponseHeader( "Content-Type" ).getValue()); - header = post.getResponseHeader( "Content-Type" ); - Assert.assertNotNull( header ); - Assert.assertNotNull( header.getValue() ); + // System.out.println( "Response Content-Type=" + response.getFirstHeader( "Content-Type" ).getValue()); + header = response.getFirstHeader("Content-Type"); + Assert.assertNotNull(header); + Assert.assertNotNull(header.getValue()); // Content-Length: nnn (OPTIONAL) - // System.out.println( "Response Content-Length=" + post.getResponseHeader( "Content-Length" ).getValue() ); - header = post.getResponseHeader( "Content-Length" ); - Assert.assertNotNull( header ); - Assert.assertNotNull( header.getValue() ); + // System.out.println( "Response Content-Length=" + response.getFirstHeader( "Content-Length" ).getValue() ); + header = response.getFirstHeader("Content-Length"); + Assert.assertNotNull(header); + Assert.assertNotNull(header.getValue()); // <?xml version="1.0"?> // <entry xmlns="http://www.w3.org/2005/Atom"> // <title>The Beach</title> (REQUIRED) @@ -174,39 +179,39 @@ public class MediaCollectionTestCase { // <link rel="edit-media" href="http://media.example.org/edit/the_beach.png" /> // <link rel="edit" href="http://example.org/media/edit/the_beach.atom" /> // </entry> - Document<Entry> document = abderaParser.parse( post.getResponseBodyAsStream() ); + Document<Entry> document = abderaParser.parse(response.getEntity().getContent()); Entry entry = document.getRoot(); String title = entry.getTitle(); // System.out.println( "mediaPost entry.title=" + title ); - Assert.assertNotNull( title ); + Assert.assertNotNull(title); IRI id = entry.getId(); // System.out.println( "mediaPost entry.id=" + id ); - Assert.assertNotNull( id ); + Assert.assertNotNull(id); mediaId = id.toString(); - Assert.assertNotNull( mediaId ); // Save for put/update request + Assert.assertNotNull(mediaId); // Save for put/update request Date updated = entry.getUpdated(); // System.out.println( "mediaPost entry.updated=" + updated); - Assert.assertNotNull( updated ); + Assert.assertNotNull(updated); String summary = entry.getSummary(); // System.out.println( "mediaPost entry.summary=" + summary); - Assert.assertNotNull( summary ); + Assert.assertNotNull(summary); IRI contentSrc = entry.getContentSrc(); // System.out.println( "mediaPost entry.content.src=" + contentSrc + ", type=" + entry.getContentType()); - Assert.assertNotNull( contentSrc ); + Assert.assertNotNull(contentSrc); Link editLink = entry.getEditLink(); // System.out.println( "mediaPost entry.editLink" + " rel=" + editLink.getRel() + ", href=" + editLink.getHref() ); - Assert.assertNotNull( editLink ); - Assert.assertNotNull( editLink.getRel() ); - Assert.assertNotNull( editLink.getHref() ); + Assert.assertNotNull(editLink); + Assert.assertNotNull(editLink.getRel()); + Assert.assertNotNull(editLink.getHref()); Link editMediaLink = entry.getEditMediaLink(); // System.out.println( "mediaPost entry.editMediaLink" + " rel=" + editMediaLink.getRel() + ", href=" + editMediaLink.getHref() ); - Assert.assertNotNull( editMediaLink ); - Assert.assertNotNull( editMediaLink.getRel() ); - Assert.assertNotNull( editMediaLink.getHref() ); + Assert.assertNotNull(editMediaLink); + Assert.assertNotNull(editMediaLink.getRel()); + Assert.assertNotNull(editMediaLink.getHref()); } finally { // Release current connection to the connection pool once you are done - post.releaseConnection(); + // post.releaseConnection(); } } @@ -216,9 +221,9 @@ public class MediaCollectionTestCase { // Testing of entry update String receiptName = "Value Autoglass Bill"; String fileName = "target/test-classes/ReceiptValue.jpg"; - File input = new File( fileName ); + File input = new File(fileName); boolean exists = input.exists(); - Assert.assertTrue( exists ); + Assert.assertTrue(exists); // Prepare HTTP put request // PUT /edit/the_beach.png HTTP/1.1 @@ -226,28 +231,29 @@ public class MediaCollectionTestCase { // Content-Type: image/png // Content-Length: nnn // ...binary data... - PutMethod put = new PutMethod( providerURI + "/" + mediaId ); - put.addRequestHeader( "Content-Type", "image/jpg" ); - put.addRequestHeader( "Title", "Title " + receiptName + "" ); - put.addRequestHeader( "Slug", "Slug " + receiptName + "" ); - put.setRequestEntity( - new InputStreamRequestEntity( new FileInputStream( input ), "image/jpg" ) ); + HttpPut put = new HttpPut(providerURI + "/" + mediaId); + put.addHeader("Content-Type", "image/jpg"); + put.addHeader("Title", "Title " + receiptName + ""); + put.addHeader("Slug", "Slug " + receiptName + ""); + put.setEntity(new FileEntity(input, "image/jpg")); // Get HTTP client - HttpClient httpclient = new HttpClient(); + HttpClient httpclient = new HttpClientFactory().createHttpClient(); try { // Execute request - int result = httpclient.executeMethod(put); + HttpResponse response = httpclient.execute(put); + response.getEntity().consumeContent(); + int result = response.getStatusLine().getStatusCode(); // Pseudo Code (see APP (http://tools.ietf.org/html/rfc5023#section-9.6) // Display status code // System.out.println("Response status code: " + result + ", status text=" + put.getStatusText() ); - Assert.assertEquals(200, result ); + Assert.assertEquals(200, result); // Display response. Should be empty for put. // System.out.println("Response body: "); // System.out.println(put.getResponseBodyAsString()); // Warning: BodyAsString recommends BodyAsStream } finally { // Release current connection to the connection pool once you are done - put.releaseConnection(); + // put.releaseConnection(); } } @@ -257,9 +263,9 @@ public class MediaCollectionTestCase { // Testing of entry update String receiptName = "Value Autoglass Bill"; String fileName = "target/test-classes/ReceiptValue.jpg"; - File input = new File( fileName ); + File input = new File(fileName); boolean exists = input.exists(); - Assert.assertTrue( exists ); + Assert.assertTrue(exists); // Prepare HTTP put request // PUT /edit/the_beach.png HTTP/1.1 @@ -267,28 +273,28 @@ public class MediaCollectionTestCase { // Content-Type: image/png // Content-Length: nnn // ...binary data... - PutMethod put = new PutMethod( providerURI + "/" + mediaId + "-bogus" ); // Does not exist. - put.addRequestHeader( "Content-Type", "image/jpg" ); - put.addRequestHeader( "Title", "Title " + receiptName + "" ); - put.addRequestHeader( "Slug", "Slug " + receiptName + "" ); - put.setRequestEntity( - new InputStreamRequestEntity( new FileInputStream( input ), "image/jpg" ) ); + HttpPut put = new HttpPut(providerURI + "/" + mediaId + "-bogus"); // Does not exist. + put.addHeader("Content-Type", "image/jpg"); + put.addHeader("Title", "Title " + receiptName + ""); + put.addHeader("Slug", "Slug " + receiptName + ""); + put.setEntity(new FileEntity(input, "image/jpg")); // Get HTTP client - HttpClient httpclient = new HttpClient(); + HttpClient httpclient = new HttpClientFactory().createHttpClient(); try { // Execute request - int result = httpclient.executeMethod(put); + HttpResponse response = httpclient.execute(put); + int result = response.getStatusLine().getStatusCode(); // Pseudo Code (see APP (http://tools.ietf.org/html/rfc5023#section-9.6) // Display status code // System.out.println("Response status code: " + result + ", status text=" + put.getStatusText() ); - Assert.assertEquals(404, result ); + Assert.assertEquals(404, result); // Display response. Should be empty for put. // System.out.println("Response body: "); // System.out.println(put.getResponseBodyAsString()); // Warning: BodyAsString recommends BodyAsStream } finally { // Release current connection to the connection pool once you are done - put.releaseConnection(); + // put.releaseConnection(); } } } |