On Wed, Mar 12, 2014 at 3:18 AM, saurabh narayan singh <
[email protected]> wrote:

> I have multiple mappings in the restlet which are supposed to pass through
> the same filter and authenticator, but for the different mappings i have to
> use validationPresence(attribute) for different attributes, refer the code
> below for better understanding of the question :
>
>     @Override
>     public synchronized Restlet createInboundRoot(){
>         Authenticator auth = new myAuthenticator();
>         Validator val = new myValidator(getContext());
>         Filter fil1 = new myFilter();
>         Router r = new Router();
>         r.attach("/hellowrld",HelloWorldResource.class);
>         r.attach("/hello",HelloWorldResource1.class);
>         fil1.setNext(auth);
>         auth.setNext(val);
>         val.setNext(r);
>         val.validatePresence("Name");
>         return fil1;
>     }
>
> but here both mappings will pass through validatePresence("Name"), what i
> want to achieve is pass "/hellowrld" thru one validator and "/hello" thru
> other one . What can i do for that ?
> Any help will be appreciated
>

In that case, you need a different Validator instance for each attachment.
It's probably best to define a helper method for this. That way your
createInboundRoot can be simplified as follows:

@Override
public synchronized /* see below */ Restlet createInboundRoot(){
    Router router = new Router(getContext());
    router.attach("/hellowrld", validateWith("Name2",
HelloWorldResource.class));
    router.attach("/hello", validateWith("Name",
HelloWorldResource1.class));

    Authenticator auth = new MyAuthenticator(getContext());
    auth.setNext(router);

    Filter filter = new MyFilter(getContext());
    filter.setNext(auth);

    return filter;
}

private Restlet validateWith(String key, ServerResource target) {
    Validator validator = new MyValidator(getContext());
    validator.validatePresence(key);
    validator.setNext(target); // assuming Validator extends Filter
    return validator;
}

Note that I've added Context arguments to the constructors. You don't have
to do this, but it's nice to have the option of retrieving context
information later without changing the wiring code.

Side issue: I'm pretty sure you *don't *need the synchronized keyword on
createInboundRoot. All the state you're accessing (apart from the Context,
which is already safe for concurrent access) is constructed in the method
itself and thus confined to one thread.

I use this technique to vary the authorization roles required by different
resources in the same application, e.g.:

router.attach("/path/to/guarded", authenticatorFor(InventoryResource.class,
ADMIN_ROLE, INVENTORY_ROLE));

--tim

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

Reply via email to