Wednesday, January 26, 2011

A05. Restlet Code Katas Part II

Continuing with REST: We continued our journey through the REST architecture this week. I haven't had too much experience with XML, and it was interesting to see how Restlet and Java are used to generate and read XML documents in a similar fashion to Java property or configuration files. I'm still a little unclear on how REST interacts between servers; so far, what we've been working on has been an introduction (similar to learning Wicket) to the Restlet framework and familiarizing ourselves with how data is stored and retrieved. The one thing that I have been noticing is the reliance on the XML format. And this I think will be important in allowing data to flow between different computers or servers since it's a commonly accepted text format.

Kata 5:
Modify the system to support a new resource called "contacts" and its associated URL: http:///contactservice/contacts. This resource only supports the GET operation and when it is invoked, it returns an XML representation of all of the contacts in the system. To support the RESTful style of hypertextual links, your XML should look like the following:

http://host/contactservice/contact/ID
http://host/contactservice/contact/ID

The journey: What took the longest here was reading through the code to understand where to create functions in order to create the unique XML document required for the kata. Since there's a snapshot of the database available at any given time that returns a collection of the contacts, I used this collection to generate the necessary XML document.

Kata 6:
Now that you have a contacts resource, extend the command line client to support a "get-all" and "delete-all" operation.

The getall operation should invoke the GET contacts command to get the XML representation containing links to all the contacts, then extract the URL of each contact and perform a GET to retrieve its representation. Then print out the contact info associated with that URL (I.e. first name, last name, info).

The delete-all operation should use the GET contacts command to get the XML representation containing links to all of the contacts, then extract the URLs, then call DELETE to delete them.

The journey: The most difficult part about this kata was the parsing of the XML file that was generated in the previous kata. I also had to change the client resource each time I looped through the XML file in order to grab the contact information for the "get-all" command. It seems that there should be perhaps an easier way to do this rather than creating a new client object each time I want to go to a new URI.

Kata 7
Finally, extend the Contact resource to include a telephone number. The telephone number must contain only numbers and the "-" character. When clients do a PUT of a Contact representation in XML format, the server must check that the telephone number field has the correct format and set the Status field to indicate an error if the telephone number is incorrectly formatted. Write unit tests to check both valid and invalid PUTs of your enhanced Contact resource.

The journey: this was the easiest of the three katas, mostly because I'm fairly familiar with the source files and setup of the contact service system. It was simple to add another field to house the telephone number. Regular expressions seemed like the way to go as far as limiting the input of the telephone numbers to valid formats. So after looking up the java implementation of regular expressions and coming up with the following reg ex:

(\\d+)( \\- (\\d+) ) *

This was probably the portion that took the longest time since I had to do a quick refresher on regular expressions.


Kata 5 Time Duration: 2 hrs
Kata 6 Time Duration: 2 hrs
Kata 7 Time Duration: 1.5hrs

Total Time: 5.5 hrs

No comments:

Post a Comment