Restlet + GWT StockWatcher sample integration

How to host GWT Retrieving JSON Data example on Restlet?

Refer to the followings:

Serving Static Files
Getting Parameter Values

Now here's StockResource.

Code:

import java.util.Random;
import org.restlet.data.Form;
import org.restlet.data.Parameter;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
 
public class StockResource extends ServerResource {
  
    private static final double MAX_PRICE = 100.0; // $100.00
    private static final double MAX_PRICE_CHANGE = 0.02; // +/- 2%
  
  @Get
  public String browse(){
    StringBuilder sb = new StringBuilder();
    
      Random rnd = new Random();
 
      sb.append('[');
      Form form = getRequest().getResourceRef().getQueryAsForm();
      for (Parameter parameter : form) {
        if(parameter.getName().equals("q")){
            String[] stockSymbols = parameter.getValue().split(" ");
            for (String stockSymbol : stockSymbols) {
 
              double price = rnd.nextDouble() * MAX_PRICE;
              double change = price * MAX_PRICE_CHANGE * (rnd.nextDouble() * 2f - 1f);
 
              sb.append("  {");
              sb.append("    \"symbol\": \"");
              sb.append(stockSymbol);
              sb.append("\",");
              sb.append("    \"price\": ");
              sb.append(price);
              sb.append(',');
              sb.append("    \"change\": ");
              sb.append(change);
              sb.append("  },");
            }
        }
      }
      sb.append(']');
    
    return sb.toString();
  }
 
}

And Restlet application.

Code:

import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.resource.Directory;
import org.restlet.routing.Router;
 
public class MyApplication extends Application {
  
  private static final String ROOT_URI = "file:///PathToGWTWar/"; // e.g. file:///Users/Me/workspace/war
  
    @Override
    public Restlet createInboundRoot() {
        Router router = new Router();
        // Attach the resource.
        router.attach("/testFileUpload", MyResource.class);
        
        router.attach("/stockwatcher/stockPrices", StockResource.class);
        
        Directory dir = new Directory(getContext(), ROOT_URI);
        router.attach("/", dir);
        
        return router;
    }
 
}

In this case, I added to fileUpload sample. And war folder where GWT generates js and other static files is put at ROOT_URI.

The point is the order to attach resources to Router. Try to see what happens when you attach StockResource at last.

As always, Restlet allows us to get a task done easily.

Feedback awaiting moderation

This post has 2 feedbacks awaiting moderation...

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
PoorExcellent
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
Free Blog Themes and Free Blog Templates