Sunday, January 23, 2011

REST II

After struggling with the first set of REST Katas we have another sets to be completed. This time is about contacts also known as phone book. We are to manipulate add/edit code to accomplish the following Katas:

TO DO: Kata 5: Contacts resource

Modify the system to support a new resource called "contacts" and its associated URL: http://<host>/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:
<contacts>
  <contact>http://host/contactservice/contact/ID</contact>
  <contact>http://host/contactservice/contact/ID</contact>
</contacts>

Of course, we will substitute the actual <host> and actual <ID> for all <contact> elements in the returned XML.

Our system needs to programmatically determine the URL of the current host.  One way to do this is with code like the following inside your ServerResource method:
  • new Reference(getReference(), "..").getTargetRef().toString()

We will also need to extend the createInboundRoot() method to support this new resource, and add a new package, class, and associated unit test.

RESULT: This is the hardest of all this part's Katas. I'm so confuse on how to do this. I didn't really know how to start implementing this task. With hours of research and help from other classmates I was able to accomplish this. The idea was to iterate through each contact and add it into the XML file. It took me about 4 to finish this Kata.


TO DO: Kata 6: Command line client manipulation of Contacts resource

Now that we 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. 

RESULT: This wasn't bad to implement but it still took time to finish. Most of the time was used to research and try out some code that would work. The idea is to iterate to all the contacts in the XML and do either delete or get. There were a lot of resources that I found to do this but I had to tweak it to accomplish what I needed. It took me about 2 hours for this Kata.


TO DO: Kata 7: Add a telephone number to the Contact resource

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. 

RESULT: This is the easiest Kata. It was just a matter of adding all instance of phone number on all the modules. Although the easiest, it still took time to find the right place in the code to put the phone number. It me about 1 hour for this Kata.


DOWNLOAD: Restlet Katas II

No comments:

Post a Comment