Hi Chris:
Break it up using more than one Router, and include a filter for
whether a license actually exists.

public class App extends Application {

   @Override

   public synchronized Restlet createInboundRoot() {

       Router router = new Router(getContext());


       Router licInfoRouter = new Router(getContext());

       router.attach("/valid", LicensesValidResource.class);

       router.attach("/usage", LicensesUsageResource.class);


       Router licRouter = new Router(getContext());

       router.attach("/{license}", new LicenseFilter(getContext(),
licInfoRouter).setDefaultMatchingMode(Template.MODE_STARTS_WITH));


       router.attach("/licenses", licRouter);

       return router;

   }

}

public class LicenseFilter extends Filter {

   public LicenseFilter(Context ctx, Restlet next) {

       super(ctx, next);

   }

   @Override

   protected int beforeHandle(Request req, Response res) {

       // look in Context for your component that knows about licenses

       LicService svc = (LicService)
getContext().getAttributes().get("license.service.key");

       if (!svc.exists(req.getAttributes().get("license"))) {

            res.setStatus(Status.CLIENT_ERROR_NOT_FOUND, "License does
not exist");

            return STOP;

       }

       return super.beforeHandle(request, response);

   }

}

It's more RESTful to keep the resources for license validity and
license usage separate as you wrote them.

Hope that helps,
Jason

On Mon, Nov 21, 2011 at 4:45 PM, Chris Markle <[email protected]> wrote:
>
> New user here trying to wrap my head around the routing... When I use the 
> following App class createInboundRoot() method I get 404's when I try to 
> access either /licenses/{license}/valid or /licenses/{license}/valid. When I 
> comment out one of the lines or the other the remaining one _can_ be 
> accessed. This happens whether or not I use MODE_STARTS_WITH.
>
> public class App extends Application {
>
>    @Override
>    public synchronized Restlet createInboundRoot() {
>        // Create a router Restlet that routes each call to new instances of 
> the appropriate class
>        Router router = new Router(getContext());
>
>        // Define routes
>        router.setDefaultMatchingMode(Template.MODE_STARTS_WITH);
>        router.attach("/licenses/{license}/valid", 
> LicensesValidResource.class);
>        router.attach("/licenses/{license}/usage", 
> LicensesUsageResource.class);
>
>        return router;
>    }
> }
>
> Any help on how I can route both /licenses/{license}/valid and 
> /licenses/{license}/usage would be appreciated.
>
> Also is it good. bad or indifferent form to route them both (once we get that 
> working) to the same resource class? In this case that'd be LicensesResource.
>
> Thanks in advance.
>
> Chris
>
> ------------------------------------------------------
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2883962



--
907-360-1405

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2884380

Reply via email to