Hi,

I upgraded to RC3 and am getting seriously confused.

I have to rewrite most of my application's restlet-related code, and I can't
figure out how I should do it.
I started with Handlers, then I replaced them with Finders (along with
associated Resource classes); and now.
it seems like I can attach resources to a router, and that the framework will
take care of the finder.
My problem is that I don't understand what the Resource classes are supposed to
do. Tthe tutorial and the
code from the example directory are very different; I'm not sure which one I
should follow.


Let me give  you an example. I want to create beans with:
    POST /bean HTTP/1.1
    Host: localhost:8182
    Accept: application/xml
    name=titi

and have the server return an  xml representation of the newly created object.

Beans a retrieve  with:
    GET /bean/we HTTP/1.1
    Host: localhost:8182
    Accept: application/xml


Here is the code:

  public static class MyBean{
    public MyBean(String name){this.name = name;}
    public String name;
  }

  public static class MyBeanResource extends Resource{

    private MyBean myBean;

    public MyBeanResource(Context context, Request request, Response
response){[...]}

    @Override public boolean allowPost() {return true;}
    @Override public boolean allowGet() {return true;}

    @Override public  List<Variant> getVariants(){[...]}
    @Override public void post( Representation entity){[...]}
    @Override public Representation getRepresentation(Variant variant) { [,,,]}
  }

 public void main(String[] args){

    Application application = new Application() {
        @Override public Restlet createRoot() {
          Router router = new Router(getContext());
          router.attach("/bean", MyBeanResource.class);
          return router;
        }
      };
     [...]
      component.start();
}

The finder uses reflection to build a MyResource object.
So for a GET, the constructor should retrieve the approriate bean:

  public MyBeanResource(Context context, Request request, Response response){
      super(context,request,response);
      String name = request.getResourceRef().getLastSegment();
      myBean = new MyBean(name);
    }

So far so good, but what about  POSTs? Should the constructor check the 
method type, like so:


  public MyBeanResource(Context context, Request request, Response response){
      super(context,request,response);
      if (request.getMethod().equals(Method.GET)) {
        String name = request.getResourceRef().getLastSegment();
        myBean = new MyBean(name);
      } else {
        // it's a POST - nothing to do.
      }
    }
This doesn't sound right.


Now, the getVariants method. Is its purpose to return all the representation 
types the resource can have (text/plain, application/xml, etc.)?.

    @Override public  List<Variant> getVariants(){
      Variant v = new Variant(MediaType.TEXT_PLAIN);
      List<Variant> variants = new ArrayList<Variant>();
      variants.add(new Variant(MediaType.TEXT_PLAIN);
      variants.add(new Variant(MediaType.APPLICATION_XML));
      return variants;
    }

The getRepresentation seems pretty straightforward:

@Override public Representation getRepresentation(Variant variant) {
      if (variant.getMediaType().equals(...)){
        return new StringRepresentation(myBean.name);
      } else if (variant.getMediaType().equals(...)){
        return new....
      } else {
        return new StringRepresentation(myBean.name);
      }
  }


As for the post method I would do something like:

 @Override public void post( Representation entity){
      // create the bean
      Form form = new Form(entity);
      String name = form.getValues("name");
      myBean = new MyBean(name);
      getResponse().setStatus(Status.SUCCESS_CREATED);
      getResponse().setRedirectRef("http://localhost:8182/bean/"; + name);
      // return a representation of the newly created bean
      getResponse().setEntity(this.getRepresentation(???);
    }

But, how do I have access to the client's preferred variant (application/xml in
this case)?


Any clarification is welcome.

Thanks, 

-Vincent.

Reply via email to