How to handle XML received as part of a POST Request in Restlet?


I have used this in Restlet 2.0 only so far.. So thats a small disclaimer that I’m not sure how this will work out in the lower versions.

Right, lets jump in and get our hands dirty. The requirement (so to say) is that in my webservice, the incoming data will be in a known XML format and the webservice has to perform some computation using this data, and send out XML.

Lets make some assumptions so we can understand how the XML gets handled.

Lets assume that the XML will come in the format – <examples><example><fruit>Mango</fruit><vegetable>Tomato</vegetable></example><example><fruit>apple</fruit><vegetable>potato</vegetable></example></examples>

Another assumption(this is a requirement probably) is that the Sender(the HTML page or the flex app or whoever sends this data) must encode it as “application/xml” and NOT as “www/form-url-encoded”. These are two ways by which POST data can be encoded and sent. So one must make sure that the incoming POST request is encoded as “application/xml”. If this is missed out, or If the encoding is not as mentioned just now, the procedure described below will lead to a NullPointerException. So in case you got that exception already, you know who’s the black sheep.

Here are the exact steps -

In your webservice “resource” start off your POST request handler method with the @Post annotation

public class ExampleResource extends ServerResource{

@Post

public void examplePostHandler(DomRepresentation entity)

{

}

}

The XML that got sent in the request is sitting inside the DomRepresentation object. We need to use the getNodes() function. This getNodes function takes a string argument, which has to be an XPath expression for you to get xml nodes in a usable form. For the uninitiated, XPath expression is to XML like regular expressions are to regular strings. See here for tips on basic XPath expressions.

So my code segment now looks like this  -

@Post

public void examplePostHandler(DomRepresentation entity)

{

ArrayList<String> collectedData = new ArrayList<String>();

NodeSet t = entity.getNodes(“//example”);

Iterator itrT = t.iterator();

while(itrT.hasNext())
{
Node node = (Node)itrT.next();

Node start = node.getFirstChild();

while((next=start.getNextSibling())!=null)
{

collectedData.add(next.getTextContent());
start=next;

}

}

}

}

By using the XPath expression “//example”, all the corresponding  <example> tags with their children are collected as Node objects in the NodeSet object. The NodeSet object is similar to the List object in java.util.* .  And I use an iterator object to traverse through it. The interesting part is that the way the children nodes(<fruit>, and <vegetable> ) are to be accessed. The Node object, for some reason unknown to me, does not have a getChildren(String nameOfTag) function. So we catch hold of the first child of every Node in the collected NodeSet, and loop through the node until no more children are left. This explains the while((next=start.getNextSibling())!=null) statement.
Just a Note- We can set parameters to the @Post annotation to signify that this post request handler method will specifically handle that type  as described in the docs here - But I haven’t used this, so I dont know what exactly it entails to use those extra parameters for that @Post annotation.

About these ads

About vatsalad

Hi, I'm Vatsala
This entry was posted in How To..., j2ee, java and tagged , , , , , , . Bookmark the permalink.

2 Responses to How to handle XML received as part of a POST Request in Restlet?

  1. Darwin says:

    Vatsala,

    Would you happen to have a simple Flex project that you could share? (I am basically trying to do the same thing: Use Flex for my GUI and RESTlet for my Web services layer…)

    Take care,
    Darwin

    • vatsalad says:

      Hi Darwin, I dont have any project at hand right now, will post it here if find one. Currently busy with something else.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s