Tuesday, January 18, 2011

Assignment A04: Restlet Code Katas

Code katas are exercises in programming that help you to refine your skills. In this assignment, we  actively worked with an example REST architecture: the restlet-dateservice.

The RESTLET tutorial proved to be the initial step to solving most of these katas:

Kata 1: Time resources (~1 hour)
For this Kata, I was required to add three new resources called "hour", "minute" and "second" that return the current hour, minute and second.  I then wrote JUnit tests for each of these resources and made sure they passed. The tests simply required that I make simple cases that checked to see if the data returned by the server matched the curent java.util.Calendar date. Since all of these sample tests passed in flying colors and I deleted the test cases as they weren't worth having. My test case for Kata 4 is very similar to these.

Kata 2: Logging (~3 hours)
This particular Kata required that I figure out a way to enable a logging feature for Restlet.
By default, Restlet logs data about each request to the command line using the Java Logging API.  I had to read a few tutorials to get familiar with the API. The trickiest part, I found, was deciphering what exactly I had to put into the logging properties file and what all the commands entailed. This step took a little trial and error while I defined a ~/.dateservice/logging.properties file that enables the user to specify (among other things) that the logging information should go to a file, not the command line.
tell the restlet-dateservice application to read and use the logging properties file in ~/.dateservice/logging.properties

These are the resources I looked for inspiration to solve this particular Kata.
http://www.naviquan.com/blog/restlet-cookbook-log
-This link hand the most concise break down of the appropriate steps to achieve the required functionality.
http://download.oracle.com/javase/6/docs/technotes/guides/logging/
http://onjava.com/pub/a/onjava/2002/06/19/log.html

Kata 3: Authentication (~1 hours)
The current restlet-dateservice system, given to us by Professor Johnson, performs no authentication.  This again required that I vist the user guide (and/or google) for guidance. From here, implementing the HTTP Basic authentication was pretty straight forward. Since all resources stem from the server resource, they all should naturally be should be available due to the fact that it only required that I modify DateServer.java. Thus, HTTP Basic authentication is enabled in my distribution. and passes the username "guest" and password "pw". These were hardcoded to work in the form:

http://guest:pw@localhost:8111/dateserver/...

Kata 4: Wicket (~3 hours)

This was the most tricky kata. This task required that we create a new client for the DateService server that is a web application using Wicket and not a default command line application.  The web application brings up a single page with a single form that requests what aspect of the date (year, month, day, hour, minute second) the user desires. When the user submits the form, the web application uses Restlet to obtain the appropriate information from the DateService server, then presents the results to the user in the page.

It required that one really understand how the restlet http get protocol works in regards to server challenges. If it were not for the authentication feature added in the previous kata, the task would have been extremely straight forward. However, I noticed I was getting authentication errors (401)
The following code describes what must be modified to get the proper retrieval of resources in which a server issues challenges for authentication:

    Reference ref = new Reference("http://guest:pw@localhost:8111/dateserver/" + props.get("Unit"));
    
string[] userinfo = ref.getUserInfo().split(":"); // "username:password"
    
string username = userinfo[0];
    
string password = userinfo[1];
    
ClientResource clientRes = new ClientResource(ref);
    
clientRes.setChallengeResponse(ChallengeScheme.HTTP_BASIC, username, password);
  
    
try {
      add
(new Label("ValueLabel", clientRes.get().getText()));
    
}
    
catch (ResourceException e) {
    
// TODO Auto-generated catch block
    
e.printStackTrace();
    
}


A test case similar to those required in the first kata is provided in the distribution. It simply tests the submission and retrieval of the dateservice/hour.

A link to my distribution file can be found here.

No comments:

Post a Comment