You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by js...@apache.org on 2008/03/23 18:58:54 UTC
svn commit: r640230 -
/incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/
Author: jsdelfino
Date: Sun Mar 23 10:58:53 2008
New Revision: 640230
URL: http://svn.apache.org/viewvc?rev=640230&view=rev
Log:
Fix for TUSCANY-2118. Fixed AtomBindingInvoker to support the correct methods from the binding Collection and data Collection interfaces.
Added:
incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingUtil.java (with props)
Modified:
incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingInvoker.java
incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingListenerServlet.java
incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingProviderFactory.java
incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomReferenceBindingProvider.java
Modified: incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingInvoker.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingInvoker.java?rev=640230&r1=640229&r2=640230&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingInvoker.java (original)
+++ incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingInvoker.java Sun Mar 23 10:58:53 2008
@@ -18,14 +18,19 @@
*/
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.InputStreamReader;
import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.List;
import org.apache.abdera.Abdera;
+import org.apache.abdera.factory.Factory;
import org.apache.abdera.model.Document;
-import org.apache.abdera.model.Entry;
import org.apache.abdera.model.Feed;
-import org.apache.abdera.parser.ParserOptions;
+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;
@@ -33,6 +38,7 @@
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.tuscany.sca.binding.atom.collection.NotFoundException;
+import org.apache.tuscany.sca.implementation.data.collection.Entry;
import org.apache.tuscany.sca.interfacedef.Operation;
import org.apache.tuscany.sca.invocation.Invoker;
import org.apache.tuscany.sca.invocation.Message;
@@ -44,21 +50,24 @@
* @version $Rev$ $Date$
*/
class AtomBindingInvoker implements Invoker {
+
+ private final static Factory abderaFactory = Abdera.getNewFactory();
+ private final static Parser abderaParser = Abdera.getNewParser();
Operation operation;
String uri;
HttpClient httpClient;
String authorizationHeader;
+ AtomReferenceBindingProvider provider;
- //FIXME Support conversion to/from data API entries
-
- AtomBindingInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader) {
+ AtomBindingInvoker(Operation operation, String uri, 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
@@ -71,8 +80,8 @@
*/
public static class GetInvoker extends AtomBindingInvoker {
- public GetInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader) {
- super(operation, uri, httpClient, authorizationHeader);
+ public GetInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader, AtomReferenceBindingProvider bindingProvider) {
+ super(operation, uri, httpClient, authorizationHeader, bindingProvider);
}
@Override
@@ -84,16 +93,29 @@
// Send an HTTP GET
GetMethod getMethod = new GetMethod(uri + "/" + id);
getMethod.setRequestHeader("Authorization", authorizationHeader);
+ boolean parsing = false;
try {
httpClient.executeMethod(getMethod);
int status = getMethod.getStatusCode();
// Read the Atom entry
if (status == 200) {
- Document<Entry> doc = Abdera.getNewParser().parse(new InputStreamReader(getMethod.getResponseBodyAsStream()));
- Entry feedEntry = doc.getRoot();
-
- msg.setBody(feedEntry);
+ Document<org.apache.abdera.model.Entry> doc = abderaParser.parse(new InputStreamReader(getMethod.getResponseBodyAsStream()));
+ 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());
+ msg.setBody(entry.getData());
+ }
} else if (status == 404) {
msg.setFaultBody(new NotFoundException());
@@ -104,8 +126,11 @@
} catch (Exception e) {
msg.setFaultBody(new ServiceRuntimeException(e));
} finally {
- // Will be released by the abdera parser
- //getMethod.releaseConnection();
+ if (!parsing) {
+ // Release the connection unless the Abdera parser is
+ // parsing the response, in this case it will release it
+ getMethod.releaseConnection();
+ }
}
return msg;
@@ -117,19 +142,32 @@
*/
public static class PostInvoker extends AtomBindingInvoker {
- public PostInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader) {
- super(operation, uri, httpClient, authorizationHeader);
+ public PostInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader, AtomReferenceBindingProvider bindingProvider) {
+ super(operation, uri, httpClient, authorizationHeader, bindingProvider);
}
@Override
public Message invoke(Message msg) {
// Post an entry
- Entry feedEntry = (Entry)((Object[])msg.getBody())[0];
+ 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);
+ }
// Send an HTTP POST
PostMethod postMethod = new PostMethod(uri);
postMethod.setRequestHeader("Authorization", authorizationHeader);
+ boolean parsing = false;
try {
// Write the Atom entry
@@ -143,10 +181,21 @@
// Read the Atom entry
if (status == 200 || status == 201) {
- Document<Entry> doc = Abdera.getNewParser().parse(new InputStreamReader(postMethod.getResponseBodyAsStream()));
- Entry createdEntry = doc.getRoot();
-
- msg.setBody(createdEntry);
+ Document<org.apache.abdera.model.Entry> doc = abderaParser.parse(new InputStreamReader(postMethod.getResponseBodyAsStream()));
+ 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) {
msg.setFaultBody(new NotFoundException());
@@ -157,8 +206,11 @@
} catch (Exception e) {
msg.setFaultBody(new ServiceRuntimeException(e));
} finally {
- // Will be released by the abdera parser
- //postMethod.releaseConnection();
+ if (!parsing) {
+ // Release the connection unless the Abdera parser is
+ // parsing the response, in this case it will release it
+ postMethod.releaseConnection();
+ }
}
return msg;
@@ -170,16 +222,30 @@
*/
public static class PutInvoker extends AtomBindingInvoker {
- public PutInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader) {
- super(operation, uri, httpClient, authorizationHeader);
+ public PutInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader, AtomReferenceBindingProvider bindingProvider) {
+ super(operation, uri, httpClient, authorizationHeader, bindingProvider);
}
@Override
public Message invoke(Message msg) {
+
// Put an entry
Object[] args = (Object[])msg.getBody();
- String id = (String)args[0];
- Entry feedEntry = (Entry)args[1];
+ 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);
+ }
// Send an HTTP PUT
PutMethod putMethod = new PutMethod(uri + "/" + id);
@@ -194,17 +260,9 @@
httpClient.executeMethod(putMethod);
int status = putMethod.getStatusCode();
-
- // Read the Atom entry
if (status == 200 || status == 201) {
- try {
- Document<Entry> doc = Abdera.getNewParser().parse(new InputStreamReader(putMethod.getResponseBodyAsStream()));
- Entry updatedEntry = doc.getRoot();
-
- msg.setBody(updatedEntry);
- } catch (Exception e) {
- // Returning the updated entry is optional
- }
+
+ msg.setBody(null);
} else if (status == 404) {
msg.setFaultBody(new NotFoundException());
@@ -215,8 +273,7 @@
} catch (Exception e) {
msg.setFaultBody(new ServiceRuntimeException(e));
} finally {
- // Will be released by the abdera parser
- //putMethod.releaseConnection();
+ putMethod.releaseConnection();
}
return msg;
@@ -228,8 +285,8 @@
*/
public static class DeleteInvoker extends AtomBindingInvoker {
- public DeleteInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader) {
- super(operation, uri, httpClient, authorizationHeader);
+ public DeleteInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader, AtomReferenceBindingProvider bindingProvider) {
+ super(operation, uri, httpClient, authorizationHeader, bindingProvider);
}
@Override
@@ -244,8 +301,6 @@
try {
httpClient.executeMethod(deleteMethod);
int status = deleteMethod.getStatusCode();
-
- // Read the Atom entry
if (status == 200) {
msg.setBody(null);
@@ -270,8 +325,8 @@
*/
public static class GetAllInvoker extends AtomBindingInvoker {
- public GetAllInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader) {
- super(operation, uri, httpClient, authorizationHeader);
+ public GetAllInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader, AtomReferenceBindingProvider bindingProvider) {
+ super(operation, uri, httpClient, authorizationHeader, bindingProvider);
}
@Override
@@ -282,16 +337,100 @@
// Send an HTTP GET
GetMethod getMethod = new GetMethod(uri);
getMethod.setRequestHeader("Authorization", authorizationHeader);
+ boolean parsing = false;
+ try {
+ httpClient.executeMethod(getMethod);
+ int status = getMethod.getStatusCode();
+
+ // Read the Atom feed
+ if (status == 200) {
+ Document<Feed> doc = abderaParser.parse(new InputStreamReader(getMethod.getResponseBodyAsStream()));
+ 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());
+ entries.add(entry);
+ }
+ msg.setBody(entries.toArray(new Entry[entries.size()]));
+ }
+
+ } else if (status == 404) {
+ msg.setFaultBody(new NotFoundException());
+ } else {
+ msg.setFaultBody(new ServiceRuntimeException("HTTP status code: " + status));
+ }
+
+ } catch (Exception e) {
+ msg.setFaultBody(new ServiceRuntimeException(e));
+ } finally {
+ if (!parsing) {
+ // Release the connection unless the Abdera parser is
+ // parsing the response, in this case it will release it
+ getMethod.releaseConnection();
+ }
+ }
+
+ return msg;
+ }
+ }
+
+ /**
+ * Query operation invoker
+ */
+ public static class QueryInvoker extends AtomBindingInvoker {
+
+ public QueryInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader, AtomReferenceBindingProvider bindingProvider) {
+ super(operation, uri, httpClient, authorizationHeader, bindingProvider);
+ }
+
+ @Override
+ public Message invoke(Message msg) {
+
+ // Get a feed from a query
+ String queryString = (String)((Object[])msg.getBody())[0];
+
+ // Send an HTTP GET
+ GetMethod getMethod = new GetMethod(uri);
+ getMethod.setRequestHeader("Authorization", authorizationHeader);
+ getMethod.setQueryString(queryString);
+ boolean parsing = false;
try {
httpClient.executeMethod(getMethod);
int status = getMethod.getStatusCode();
// Read the Atom feed
if (status == 200) {
- Document<Feed> doc = Abdera.getNewParser().parse(new InputStreamReader(getMethod.getResponseBodyAsStream()));
+ Document<Feed> doc = abderaParser.parse(new InputStreamReader(getMethod.getResponseBodyAsStream()));
+ parsing = true;
Feed feed = doc.getRoot();
- msg.setBody(feed);
+ 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());
+ entries.add(entry);
+ }
+ msg.setBody(entries.toArray(new Entry[entries.size()]));
+ }
} else if (status == 404) {
msg.setFaultBody(new NotFoundException());
@@ -302,8 +441,11 @@
} catch (Exception e) {
msg.setFaultBody(new ServiceRuntimeException(e));
} finally {
- // Will be released by the abdera parser
- //getMethod.releaseConnection();
+ if (!parsing) {
+ // Release the connection unless the Abdera parser is
+ // parsing the response, in this case it will release it
+ getMethod.releaseConnection();
+ }
}
return msg;
@@ -315,8 +457,8 @@
*/
public static class PostMediaInvoker extends AtomBindingInvoker {
- public PostMediaInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader) {
- super(operation, uri, httpClient, authorizationHeader);
+ public PostMediaInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader, AtomReferenceBindingProvider bindingProvider) {
+ super(operation, uri, httpClient, authorizationHeader, bindingProvider);
}
@Override
@@ -331,8 +473,8 @@
*/
public static class PutMediaInvoker extends AtomBindingInvoker {
- public PutMediaInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader) {
- super(operation, uri, httpClient, authorizationHeader);
+ public PutMediaInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader, AtomReferenceBindingProvider bindingProvider) {
+ super(operation, uri, httpClient, authorizationHeader, bindingProvider);
}
@Override
Modified: incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingListenerServlet.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingListenerServlet.java?rev=640230&r1=640229&r2=640230&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingListenerServlet.java (original)
+++ incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingListenerServlet.java Sun Mar 23 10:58:53 2008
@@ -18,12 +18,14 @@
*/
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.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URLDecoder;
-import java.util.Date;
import java.util.StringTokenizer;
import java.util.logging.Logger;
@@ -34,18 +36,18 @@
import javax.xml.namespace.QName;
import org.apache.abdera.Abdera;
+import org.apache.abdera.factory.Factory;
import org.apache.abdera.model.Collection;
-import org.apache.abdera.model.Content;
import org.apache.abdera.model.Document;
-import org.apache.abdera.model.Entry;
import org.apache.abdera.model.Feed;
import org.apache.abdera.model.Link;
import org.apache.abdera.model.Service;
import org.apache.abdera.model.Workspace;
import org.apache.abdera.parser.ParseException;
+import org.apache.abdera.parser.Parser;
import org.apache.commons.codec.binary.Base64;
import org.apache.tuscany.sca.databinding.Mediator;
-import org.apache.tuscany.sca.implementation.data.collection.Item;
+import org.apache.tuscany.sca.implementation.data.collection.Entry;
import org.apache.tuscany.sca.interfacedef.DataType;
import org.apache.tuscany.sca.interfacedef.Operation;
import org.apache.tuscany.sca.interfacedef.impl.DataTypeImpl;
@@ -57,7 +59,6 @@
import org.apache.tuscany.sca.runtime.RuntimeWire;
-
/**
* A resource collection binding listener, implemented as a Servlet and
* registered in a Servlet host provided by the SCA hosting runtime.
@@ -66,10 +67,8 @@
private static final Logger logger = Logger.getLogger(AtomBindingListenerServlet.class.getName());
private static final long serialVersionUID = 1L;
- //private final static Namespace APP_NS = Namespace.getNamespace("app", "http://purl.org/atom/app#");
- //private final static Namespace ATOM_NS = Namespace.getNamespace("atom", "http://www.w3.org/2005/Atom");
-
- private final Abdera abdera = new Abdera();
+ private final static Factory abderaFactory = Abdera.getNewFactory();
+ private final static Parser abderaParser = Abdera.getNewParser();
private RuntimeWire wire;
private Invoker getFeedInvoker;
@@ -100,7 +99,7 @@
this.messageFactory = messageFactory;
this.mediator = mediator;
this.title = title;
-
+
// Get the invokers for the supported operations
Operation getOperation = null;
for (InvocationChain invocationChain : this.wire.getInvocationChains()) {
@@ -132,7 +131,7 @@
// Determine the collection item type
itemXMLType = new DataTypeImpl<Class<?>>(String.class.getName(), String.class, String.class);
Class<?> itemClass = getOperation.getOutputType().getPhysical();
- if (itemClass == Entry.class) {
+ if (itemClass == org.apache.abdera.model.Entry.class) {
supportsFeedEntries = true;
}
DataType<XMLType> outputType = getOperation.getOutputType();
@@ -171,10 +170,10 @@
// Return the Atom service document
response.setContentType("application/atomsvc+xml; charset=utf-8");
- Service service = this.abdera.getFactory().newService();
+ Service service = abderaFactory.newService();
//service.setText("service");
- Workspace workspace = this.abdera.getFactory().newWorkspace();
+ Workspace workspace = abderaFactory.newWorkspace();
workspace.setTitle("resource");
String href = request.getRequestURL().toString();
@@ -233,12 +232,12 @@
if (responseMessage.isFault()) {
throw new ServletException((Throwable)responseMessage.getBody());
}
- org.apache.tuscany.sca.implementation.data.collection.Entry<Object, Object>[] collection =
- (org.apache.tuscany.sca.implementation.data.collection.Entry<Object, Object>[])responseMessage.getBody();
+ Entry<Object, Object>[] collection =
+ (Entry<Object, Object>[])responseMessage.getBody();
if (collection != null) {
// Create the feed
- feed = this.abdera.getFactory().newFeed();
+ feed = abderaFactory.newFeed();
// Set the feed title
if (title != null) {
@@ -248,8 +247,8 @@
}
// Add entries to the feed
- for (org.apache.tuscany.sca.implementation.data.collection.Entry<Object, Object> entry: collection) {
- Entry feedEntry = createFeedEntry(entry);
+ for (Entry<Object, Object> entry: collection) {
+ org.apache.abdera.model.Entry feedEntry = feedEntry(entry, itemClassType, itemXMLType, mediator, abderaFactory);
feed.addEntry(feedEntry);
}
}
@@ -270,7 +269,7 @@
} else if (path.startsWith("/")) {
// Return a specific entry in the collection
- Entry feedEntry;
+ org.apache.abdera.model.Entry feedEntry;
// Invoke the get operation on the service implementation
Message requestMessage = messageFactory.createMessage();
@@ -287,7 +286,8 @@
} else {
// The service implementation only returns a data item, create an entry
// from it
- feedEntry = createFeedEntry(new org.apache.tuscany.sca.implementation.data.collection.Entry<Object, Object>(id, responseMessage.getBody()));
+ Entry<Object, Object> entry = new Entry<Object, Object>(id, responseMessage.getBody());
+ feedEntry = feedEntry(entry, itemClassType, itemXMLType, mediator, abderaFactory);
}
// Write the Atom entry
@@ -308,123 +308,6 @@
}
- /**
- * Create an Atom entry for a key and item from a collection.
- * @param key
- * @param item
- * @return
- */
- private Entry createFeedEntry(org.apache.tuscany.sca.implementation.data.collection.Entry<Object, Object> entry) {
- Object key = entry.getKey();
- Object data = entry.getData();
- if (data instanceof Item) {
- Item item = (Item)data;
-
- Entry feedEntry = abdera.getFactory().newEntry();
- if (key != null) {
- feedEntry.setId(key.toString());
- }
- feedEntry.setTitle(item.getTitle());
- feedEntry.setContentAsHtml(item.getContents());
-
- String href = item.getLink();
- if (href == null && key != null) {
- href = key.toString();
- }
-
- if (href != null) {
- feedEntry.addLink(href);
- }
- String related = item.getRelated();
- if (related != null) {
- feedEntry.addLink(related, "related");
- }
- String alternate = item.getAlternate();
- if (alternate != null) {
- feedEntry.addLink(alternate, "alternate");
- }
-
- Date date = item.getDate();
- if (date != null) {
- feedEntry.setUpdated(date);
- }
- return feedEntry;
-
- } else if (data != null) {
- Entry feedEntry = abdera.getFactory().newEntry();
- feedEntry.setId(key.toString());
- feedEntry.setTitle("item");
-
-
- // Convert the item to XML
- String value = mediator.mediate(data, itemClassType, itemXMLType, null).toString();
-
- Content content = this.abdera.getFactory().newContent();
- content.setContentType(Content.Type.XML);
- content.setValue(value);
-
- feedEntry.setContentElement(content);
-
- feedEntry.addLink(key.toString());
-
- return feedEntry;
- } else {
- return null;
- }
- }
-
- /**
- * Create a data item from an Atom entry.
- * @param key
- * @param item
- * @return
- */
- private org.apache.tuscany.sca.implementation.data.collection.Entry<Object, Object> createEntry(Entry feedEntry) {
- if (feedEntry != null) {
- if (itemClassType.getPhysical() == Item.class) {
- String key = feedEntry.getId().toString();
-
- Item item = new Item();
- item.setTitle(feedEntry.getTitle());
- item.setContents(feedEntry.getContent());
-
- for (Link link : feedEntry.getLinks()) {
- if (link.getRel() == null || "self".equals(link.getRel())) {
- if (item.getLink() == null) {
- item.setLink(link.getHref().toString());
- }
- } else if ("related".equals(link.getRel())) {
- item.setRelated(link.getHref().toString());
- } else if ("alternate".equals(link.getRel())) {
- item.setAlternate(link.getHref().toString());
- }
- }
-
- item.setDate(feedEntry.getUpdated());
-
- return new org.apache.tuscany.sca.implementation.data.collection.Entry<Object, Object>(key, item);
-
- } else {
- String key = null;
- if ( feedEntry.getId() != null) {
- feedEntry.getId().toString();
- }
-
- // 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);
-
- return new org.apache.tuscany.sca.implementation.data.collection.Entry<Object, Object>(key, data);
- }
- } else {
- return null;
- }
- }
-
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
@@ -440,16 +323,16 @@
String path = URLDecoder.decode(request.getRequestURI().substring(request.getServletPath().length()), "UTF-8");
if (path == null || path.length() == 0 || path.equals("/")) {
- Entry createdFeedEntry = null;
+ org.apache.abdera.model.Entry createdFeedEntry = null;
// Create a new Atom entry
String contentType = request.getContentType();
if (contentType.startsWith("application/atom+xml")) {
// Read the entry from the request
- Entry feedEntry;
+ org.apache.abdera.model.Entry feedEntry;
try {
- Document<Entry> doc = Abdera.getNewParser().parse(request.getReader());
+ Document<org.apache.abdera.model.Entry> doc = abderaParser.parse(request.getReader());
feedEntry = doc.getRoot();
} catch (ParseException pe) {
throw new ServletException(pe);
@@ -470,14 +353,14 @@
// The service implementation does not support feed entries, pass the data item to it
Message requestMessage = messageFactory.createMessage();
- org.apache.tuscany.sca.implementation.data.collection.Entry<Object, Object> entry = createEntry(feedEntry);
+ Entry<Object, Object> entry = entry(feedEntry, itemClassType, itemXMLType, mediator);
requestMessage.setBody(new Object[] {entry.getKey(), entry.getData()});
Message responseMessage = postInvoker.invoke(requestMessage);
if (responseMessage.isFault()) {
throw new ServletException((Throwable)responseMessage.getBody());
}
entry.setKey(responseMessage.getBody());
- createdFeedEntry = createFeedEntry(entry);
+ createdFeedEntry = feedEntry(entry, itemClassType, itemXMLType, mediator, abderaFactory);
}
} else if (contentType != null) {
@@ -553,9 +436,9 @@
if (contentType.startsWith("application/atom+xml")) {
// Read the entry from the request
- Entry feedEntry;
+ org.apache.abdera.model.Entry feedEntry;
try {
- Document<Entry> doc = Abdera.getNewParser().parse(request.getReader());
+ Document<org.apache.abdera.model.Entry> doc = abderaParser.parse(request.getReader());
feedEntry = doc.getRoot();
} catch (ParseException pe) {
throw new ServletException(pe);
@@ -580,7 +463,7 @@
// The service implementation does not support feed entries, pass the data item to it
Message requestMessage = messageFactory.createMessage();
- org.apache.tuscany.sca.implementation.data.collection.Entry<Object, Object> entry = createEntry(feedEntry);
+ Entry<Object, Object> entry = entry(feedEntry, itemClassType, itemXMLType, mediator);
requestMessage.setBody(new Object[] {entry.getKey(), entry.getData()});
Message responseMessage = putInvoker.invoke(requestMessage);
if (responseMessage.isFault()) {
Modified: incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingProviderFactory.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingProviderFactory.java?rev=640230&r1=640229&r2=640230&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingProviderFactory.java (original)
+++ incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingProviderFactory.java Sun Mar 23 10:58:53 2008
@@ -37,7 +37,7 @@
import org.apache.tuscany.sca.runtime.RuntimeComponentService;
/**
- * Implementation of the Atom binding model.
+ * Implementation of a Binding provider factory for the Atom binding.
*/
public class AtomBindingProviderFactory implements BindingProviderFactory<AtomBinding> {
@@ -57,7 +57,7 @@
public ReferenceBindingProvider createReferenceBindingProvider(RuntimeComponent component,
RuntimeComponentReference reference,
AtomBinding binding) {
- return new AtomReferenceBindingProvider(component, reference, binding);
+ return new AtomReferenceBindingProvider(component, reference, binding, mediator);
}
public ServiceBindingProvider createServiceBindingProvider(RuntimeComponent component,
Added: incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingUtil.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingUtil.java?rev=640230&view=auto
==============================================================================
--- incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingUtil.java (added)
+++ incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingUtil.java Sun Mar 23 10:58:53 2008
@@ -0,0 +1,164 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.sca.binding.atom.provider;
+
+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.tuscany.sca.databinding.Mediator;
+import org.apache.tuscany.sca.implementation.data.collection.Entry;
+import org.apache.tuscany.sca.implementation.data.collection.Item;
+import org.apache.tuscany.sca.interfacedef.DataType;
+
+/**
+ * Utility methods used in this package.
+ *
+ * @version $Rev$ $Date$
+ */
+class AtomBindingUtil {
+
+ /**
+ * Create a data item from an Atom entry.
+ * @param feedEntry
+ * @param itemClassType
+ * @param itemXMLType
+ * @param mediator
+ * @return
+ */
+ static Entry<Object, Object> entry(org.apache.abdera.model.Entry feedEntry,
+ DataType<?> itemClassType, DataType<?> itemXMLType, Mediator mediator) {
+ if (feedEntry != null) {
+ if (itemClassType.getPhysical() == Item.class) {
+ String key = feedEntry.getId().toString();
+
+ Item item = new Item();
+ item.setTitle(feedEntry.getTitle());
+ item.setContents(feedEntry.getContent());
+
+ for (Link link : feedEntry.getLinks()) {
+ if (link.getRel() == null || "self".equals(link.getRel())) {
+ if (item.getLink() == null) {
+ item.setLink(link.getHref().toString());
+ }
+ } else if ("related".equals(link.getRel())) {
+ item.setRelated(link.getHref().toString());
+ } else if ("alternate".equals(link.getRel())) {
+ item.setAlternate(link.getHref().toString());
+ }
+ }
+
+ item.setDate(feedEntry.getUpdated());
+
+ return new Entry<Object, Object>(key, item);
+
+ } else {
+ String key = null;
+ if ( feedEntry.getId() != null) {
+ feedEntry.getId().toString();
+ }
+
+ // 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);
+
+ return new Entry<Object, Object>(key, data);
+ }
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Create an Atom entry for a key and item from a collection.
+ * @param entry
+ * @param itemClassType
+ * @param itemXMLType
+ * @param mediator
+ * @param factory
+ * @return
+ */
+ static org.apache.abdera.model.Entry feedEntry(Entry<Object, Object> entry,
+ DataType<?> itemClassType, DataType<?> itemXMLType, Mediator mediator,
+ Factory factory) {
+ Object key = entry.getKey();
+ Object data = entry.getData();
+ if (data instanceof Item) {
+ Item item = (Item)data;
+
+ org.apache.abdera.model.Entry feedEntry = factory.newEntry();
+ if (key != null) {
+ feedEntry.setId(key.toString());
+ }
+ feedEntry.setTitle(item.getTitle());
+ feedEntry.setContentAsHtml(item.getContents());
+
+ String href = item.getLink();
+ if (href == null && key != null) {
+ href = key.toString();
+ }
+
+ if (href != null) {
+ feedEntry.addLink(href);
+ }
+ String related = item.getRelated();
+ if (related != null) {
+ feedEntry.addLink(related, "related");
+ }
+ String alternate = item.getAlternate();
+ if (alternate != null) {
+ feedEntry.addLink(alternate, "alternate");
+ }
+
+ Date date = item.getDate();
+ if (date != null) {
+ feedEntry.setUpdated(date);
+ }
+ return feedEntry;
+
+ } else if (data != null) {
+ org.apache.abdera.model.Entry feedEntry = factory.newEntry();
+ feedEntry.setId(key.toString());
+ feedEntry.setTitle("item");
+
+
+ // Convert the item to XML
+ String value = mediator.mediate(data, itemClassType, itemXMLType, null).toString();
+
+ Content content = factory.newContent();
+ content.setContentType(Content.Type.XML);
+ content.setValue(value);
+
+ feedEntry.setContentElement(content);
+
+ feedEntry.addLink(key.toString());
+
+ return feedEntry;
+ } else {
+ return null;
+ }
+ }
+
+}
Propchange: incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingUtil.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingUtil.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomReferenceBindingProvider.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomReferenceBindingProvider.java?rev=640230&r1=640229&r2=640230&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomReferenceBindingProvider.java (original)
+++ incubator/tuscany/java/sca/modules/binding-atom-abdera/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomReferenceBindingProvider.java Sun Mar 23 10:58:53 2008
@@ -21,19 +21,29 @@
import java.net.URI;
+import javax.xml.namespace.QName;
+
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.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.tuscany.sca.binding.atom.AtomBinding;
+import org.apache.tuscany.sca.databinding.Mediator;
+import org.apache.tuscany.sca.interfacedef.DataType;
import org.apache.tuscany.sca.interfacedef.InterfaceContract;
import org.apache.tuscany.sca.interfacedef.Operation;
+import org.apache.tuscany.sca.interfacedef.impl.DataTypeImpl;
+import org.apache.tuscany.sca.interfacedef.util.XMLType;
+import org.apache.tuscany.sca.invocation.InvocationChain;
import org.apache.tuscany.sca.invocation.Invoker;
import org.apache.tuscany.sca.provider.ReferenceBindingProvider;
import org.apache.tuscany.sca.runtime.RuntimeComponent;
import org.apache.tuscany.sca.runtime.RuntimeComponentReference;
+import org.apache.tuscany.sca.runtime.RuntimeWire;
/**
* Implementation of the Atom binding provider.
@@ -44,47 +54,72 @@
private AtomBinding binding;
private String authorizationHeader;
private HttpClient httpClient;
+ private Mediator mediator;
+ private DataType<?> itemClassType;
+ private DataType<?> itemXMLType;
+ private boolean supportsFeedEntries;
/**
* Constructs a new AtomReferenceBindingProvider
* @param component
* @param reference
* @param binding
+ * @param mediator
*/
AtomReferenceBindingProvider(RuntimeComponent component,
RuntimeComponentReference reference,
- AtomBinding binding) {
+ AtomBinding binding,
+ Mediator mediator) {
this.reference = reference;
this.binding = binding;
+ this.mediator = mediator;
// Prepare authorization header
String authorization = "admin" + ":" + "admin";
authorizationHeader = "Basic " + new String(Base64.encodeBase64(authorization.getBytes()));
// Create an HTTP client
- httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
- httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(60000);
+ HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
+ connectionManager.getParams().setDefaultMaxConnectionsPerHost(10);
+ connectionManager.getParams().setConnectionTimeout(60000);
+ httpClient = new HttpClient(connectionManager);
}
public Invoker createInvoker(Operation operation) {
+
String operationName = operation.getName();
if (operationName.equals("get")) {
- return new AtomBindingInvoker.GetInvoker(operation, binding.getURI(), httpClient, authorizationHeader);
+
+ // 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();
+ QName qname = outputType.getLogical().getElementName();
+ qname = new QName(qname.getNamespaceURI(), itemClass.getSimpleName());
+ itemClassType = new DataTypeImpl<XMLType>("java:complexType", itemClass, new XMLType(qname, null));
+ if (itemClassType.getPhysical() == org.apache.abdera.model.Entry.class) {
+ supportsFeedEntries = true;
+ }
+
+ return new AtomBindingInvoker.GetInvoker(operation, binding.getURI(), httpClient, authorizationHeader, this);
+
} else if (operationName.equals("post")) {
- return new AtomBindingInvoker.PostInvoker(operation, binding.getURI(), httpClient, authorizationHeader);
+ return new AtomBindingInvoker.PostInvoker(operation, binding.getURI(), httpClient, authorizationHeader, this);
} else if (operationName.equals("put")) {
- return new AtomBindingInvoker.PutInvoker(operation, binding.getURI(), httpClient, authorizationHeader);
+ return new AtomBindingInvoker.PutInvoker(operation, binding.getURI(), httpClient, authorizationHeader, this);
} else if (operationName.equals("delete")) {
- return new AtomBindingInvoker.DeleteInvoker(operation, binding.getURI(), httpClient, authorizationHeader);
+ return new AtomBindingInvoker.DeleteInvoker(operation, binding.getURI(), httpClient, authorizationHeader, this);
} else if (operationName.equals("getFeed") || operationName.equals("getAll")) {
- return new AtomBindingInvoker.GetAllInvoker(operation, binding.getURI(), httpClient, authorizationHeader);
+ return new AtomBindingInvoker.GetAllInvoker(operation, binding.getURI(), httpClient, authorizationHeader, this);
} else if (operationName.equals("postMedia")) {
- return new AtomBindingInvoker.PostMediaInvoker(operation, binding.getURI(), httpClient, authorizationHeader);
+ return new AtomBindingInvoker.PostMediaInvoker(operation, binding.getURI(), httpClient, authorizationHeader, this);
} else if (operationName.equals("putMedia")) {
- return new AtomBindingInvoker.PutMediaInvoker(operation, binding.getURI(), httpClient, authorizationHeader);
+ return new AtomBindingInvoker.PutMediaInvoker(operation, binding.getURI(), httpClient, authorizationHeader, this);
+ } else if (operationName.equals("query")) {
+ return new AtomBindingInvoker.QueryInvoker(operation, binding.getURI(), httpClient, authorizationHeader, this);
}
- return new AtomBindingInvoker(operation, binding.getURI(), httpClient, authorizationHeader);
+ return new AtomBindingInvoker(operation, binding.getURI(), httpClient, authorizationHeader, this);
}
public InterfaceContract getBindingInterfaceContract() {
@@ -98,6 +133,11 @@
httpClient.getParams().setAuthenticationPreemptive(true);
URI uri = URI.create(binding.getURI());
httpClient.getState().setCredentials(new AuthScope(uri.getHost(), uri.getPort()), credentials);
+
+ // Find the get operation on the reference interface
+ if (true) {
+ return;
+ }
}
public void stop() {
@@ -106,6 +146,39 @@
public boolean supportsOneWayInvocation() {
return false;
+ }
+
+ /**
+ * Returns the mediator.
+ * @return
+ */
+ Mediator getMediator() {
+ return mediator;
+ }
+
+ /**
+ * Returns the item class type.
+ * @return
+ */
+ DataType<?> getItemClassType() {
+ return itemClassType;
+ }
+
+ /**
+ * Returns the item XML type.
+ * @return
+ */
+ DataType<?> getItemXMLType() {
+ return itemXMLType;
+ }
+
+ /**
+ * Returns true if the invoker should work with Atom
+ * feed entries.
+ * @return
+ */
+ boolean supportsFeedEntries() {
+ return supportsFeedEntries;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org