Exercise 2: RESTful web application on Sun SPOT

Expected time: 20 minutes

Learning goals of this exercise

Familiarize oneself with the "nano" application server platform and associated APIs for creating RESTful services on the Sun SPOTs. Use HTTP to interact with pre-existing services that provide access to status information (e.g. uptime, battery level), allow manipulation of system properties and show meta-information about available services.


  1. A RESTful web application for the Sun SPOT is created by extending the WebApplication abstract class, implementing its processRequest() method and optionally overriding its init() and isAuthorized() methods. The init() method contains initialization code for the application. The processRequest() method takes an HttpRequest object and returns an HttpResponse object. It determines how the application responds to various HTTP requests. HTTP methods that are not supported should return the "Method not Allowed" error code.
  2. Take a look at the processRequest() method in Status.java and in PropertiesManager.java as examples.
  3. Web applications must be registered with the "nano" application server. See lines beginning with nas.registerApp (46, 54, 57) in WoTServer.java as examples. The arguments to the registerApp method include the url and an instance of the WebApplication that will handle HTTP requests to that URL. Note that the WebApplication constructor takes as an argument a properties string specifying meta-information such as name, an alternative short URL and a service description URL. For example, the following line of code indicates that PropertiesManager will handle /props or (alternatively) /p and its description can be found at http://bit.ly/aa8iOZ.
    nas.registerApp("/props", new PropertiesManager("n=Properties manager\nsh=p\nd=http://bit.ly/aa8iOZ"));
  4. These APIs hide away the the complexity of coding/encoding compressed or uncompressed HTTP messages as well as the complexity of offering the service via multiple "channels" (e.g. UDP, TCP or even reverse HTTP)
  5. Make Exercise2 your main project in NetBeans. Connect your SPOT to the USB port (if it isn't already) and stop any previously running applications by resetting the Sun SPOT (don't power it off). Build, deploy and run Exercise 2 on to the SPOT. In NetBeans, you can accomplish this by clicking the right button on the project name to bring up a context-menu and selecting "Run".
  6. After some time, the SPOT will discover the gateway machine and register itself with it.
  7. At that point, you should be able to interact with your SPOT using an HTTP client such as Firefox or a command line tool called curl. For example, to list available services on your SPOT, point your browser to http://<address>:<port>/spot-xxxx/.well-known/r where <address>:<port> is the IP address and port of the gateway machine (your lab instructors will tell you what that is) and xxxx are the last four hex-digits in your SPOT's Id (from the label on the back of the fin).
  8. See the slides for a complete set of interactions you can try but here are some of them:
    List system properties
    % curl --request GET "" 
    Create a new system property (this will assign your spot the name "Louie")
    % curl --request POST --data "spot.name:Louie" "" 
    Get status information
    % curl --request GET "" 
    Blink the LEDS yellow
    % curl --request POST --data "255,255,0" "" 


This exercise showed how RESTful web applications are created on a SPOT and several examples of using HTTP to interact with pre-existing services on the SPOT. In the next two exercises, we'll create additional services.