users@jersey.java.net

Re: [Jersey] Resource filtering proposal

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 12 Jan 2009 12:56:20 +0100

On Jan 9, 2009, at 6:31 PM, Paul Sandoz wrote:

> Hi,
>
> At the end of the email are proposed interfaces for resource
> filtering.
>
> Resource filtering will reuse the ContainerRequestFilter and
> ContainerResponseFilter interfaces.
>
> There are two ways resource filters can be registered:
>
> 1) Via @ResourceFilters by declaring an array of classes that
> implement ResourceFilter.
> This annotation may occur on a resource class, resource method,
> sub-resource method or sub-resource locator.
>
> 2) Via a registered ResourceFilterFactory instance. A
> ResourceFilterFactory will be applied to the abstract model of
> a resource class, resource method, sub-resource method or sub-
> resource locator. This is a mechanism to provide
> general resource filters dependent on the specifics of a
> resource. A list of ResourceFilterFactory can be registered
> with the application.
>
> Filter processing will occur in the following order:
>
> - list of system-based ResourceFilter, in order, obtained from list
> of system ResourceFilterFactory
>
> - list of ResourceFilter, in order, obtained from list of
> ResourceFilterFactory.
>
> - array of ResourceFilter, in order, declared by @ResourceFilters.
>
> Issues:
>
> - It is not intended that response filters will be processed if an
> exception is thrown before the response filters are to be processed,
> for
> example by client filters or by the application. Part of the reason
> is that there may not be any response information to be processed.
> Exceptions are mapped to responses way up the call stack in the
> WebApplication and thus requiring no logic in the call stack to
> handle error conditions.
>

It should be possible to trap such exceptions and map them to Response
closer to the source. Need to be careful about exceptions thrown in
request and response filters.

Other issues/points:

- The support for @ResourceFilters can be implemented by a
ResourceFilterFactory.

- On sub-resource locators: the response filters will be applied after
the resource method has returned and not after the sub-resource
   locator returns (since a response will not be produced unless an
exception is thrown). Not sure if this is a good idea. Perhaps only
   request filter should be supported? Asynchronous responses (e.g.
supported via Atmosphere) will go through a different thread.

- On resource classes. Request filtering on root resource classes for
per-request life-cycle can occur before construction, but this
   cannot occur for classes/instances returned by sub-resource
locators. So there is an inconsistency. Does it make sense to support
   the following instead?: Filters declared on a classes will applied
pre-post method invocation, Filters declared on a method override
   that declared (if any) on the class?

Paul.

> Paul.
>
> /**
> * A resource filter factory responsible for creating {_at_link
> ResourceFilter}
> * instances that match elements of the abstract resource model.
> *
> * @author Paul.Sandoz_at_Sun.Com
> */
> public interface ResourceFilterFactory {
> /**
> * Create a {_at_link ResourceFilter} instance given an element
> * of the abstract resource model.
> *
> * @param aae the abstract annotated element. This may be an
> instance
> * of the following: {_at_link AbstractResource}, {_at_link
> AbstractResourceMethod},
> * {_at_link AbstractSubResourceMethod} or {_at_link
> AbstractSubResourceLocator}.
> * @return the resource filter, otherwise null if no resource
> filter is
> * associated with the annotated element.
> */
> ResourceFilter create(AbstractAnnotatedElement aae);
> }
>
>
> /**
> * A resource filter to be used to filter a resource class, a
> resource method,
> * a sub-resource method, or a sub-resource locator.
> *
> * @author Paul.Sandoz_at_Sun.Com
> */
> public interface ResourceFilter {
>
> /**
> * Get the request filter.
> *
> * @return the request filter, otherwise null if request filtering
> * is not supported.
> */
> ContainerRequestFilter getRequestFilter();
>
> /**
> * Get the response filter.
> *
> * @return the response filter, otherwise null if response
> filtering
> * is not supported.
> */
> ContainerResponseFilter getResponseFilter();
> }
>
> /**
> * Defines the list of application-declared {_at_link ResourceFilter}
> * associated with a a resource class, a resource method,
> * a sub-resource method, or a sub-resource locator.
> *
> * @author Paul.Sandoz_at_Sun.Com
> */
> @Target({ElementType.TYPE, ElementType.METHOD})
> @Retention(RetentionPolicy.RUNTIME)
> public @interface ResourceFilters {
> Class<ResourceFilter>[] value();
> }
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>