<chapter id="Interceptors">
    <title>Filters and Interceptors</title>
    <para>&REST-API; has two different concepts for interceptions: Filters and Interceptors.  Filters are mainly used to
    modify or process incoming and outgoing request headers or response headers.  They execute before and after
    request and response processing.</para>
    <sect1>
        <title>Server Side Filters</title>
    <para>
        On the server-side there are two different types of filters.  ContainerRequestFilters
        run before a &REST-API; resource method is invoked.  ContainerResponseFilters run after a &REST-API; resource method
        is invoked.  As an added caveat, ContainerRequestFilters come in two flavors:  pre-match and post-matching.  Pre-matching
        ContainerRequestFilters are designated with the @PreMatching annotation and will execute before the &REST-API; resource
        method is matched with the incoming HTTP request.  Pre-matching filters often are used to modify request attributes
        to change how it matches to a specific resource method (i.e. strip .xml and add an Accept header).
        ContainerRequestFilters can abort the request by calling ContainerRequestContext.abortWith(Response).  A filter
        might want to abort if it implements a custom authentication protocol.
    </para>
    <para>
          After the resource class method is executed, &REST-API; will run all ContainerResponseFilters.  These filters
        allow the outgoing response to be modified before it is marshalling and sent to the client.
        Here is some pseudocode
        to give some understanding of how it works.
    </para>
    <programlisting>
        // execute pre match filters
        for (ContainerRequestFilter filter : preMatchFilters) {
            filter.filter(requestContext);
            if (isAborted(requestContext)) {
               sendAbortionToClient(requestContext);
               return;
            }
        }
        // match the HTTP request to a resource class and method
        JaxrsMethod method = matchMethod(requestContext);

        // Execute post match filters
        for (ContainerRequestFilter filter : postMatchFilters) {
           filter.filter(requestContext);
           if (isAborted(requestContext)) {
              sendAbortionToClient(requestContext);
              return;
           }
        }

        // execute resource class method
        method.execute(request);

        // execute response filters
        for (ContainerResponseFilter filter : responseFilters) {
           filter.filter(requestContext, responseContext);
        }
    </programlisting>
    <sect2>
        <title id="Asynchronous_Filter">Asynchronous filters</title>
        <para>
            It is possible to turn filters into asynchronous filters, if it is needed to suspend execution of the filter
            until a certain resource has become available. This makes the request asynchronous, but requires no change
            to the resource method declaration. In particular, <link linkend="Asynchronous_HTTP_Request_Processing">synchronous
            and asynchronous resource methods</link> continue
            to work as specified, regardless of whether a filter has made the request asynchronous. Similarly,
            one filter making the request asynchronous requires no change in the declaration of other filters.
        </para>
        <para>
            In order to make a filter's execution asynchronous, <code>ContainerRequestContext</code> must be cast to
            a <code>SuspendableContainerRequestContext</code> (for pre/post request filters), or cast the <code>ContainerResponseContext</code> to
            a <code>SuspendableContainerResponseContext</code> (for response filters). 
        </para>
        <para>
            These context objects can make the current filter's execution asynchronous by calling the <code>suspend()</code>
            method. Once asynchronous, the filter chain is suspended, and will only resume after one of the following methods
            is called on the context object:
        </para>
        <variablelist>
            <varlistentry>
                <term><code>abortWith(Response)</code></term>
                <listitem>Terminate the filter chain, return the given <code>Response</code> to the client (only for <code>ContainerRequestFilter</code>).</listitem>
            </varlistentry>
            <varlistentry>
                <term><code>resume()</code></term>
                <listitem>Resume execution of the filter chain by calling the next filter.</listitem>
            </varlistentry>
            <varlistentry>
                <term><code>resume(Throwable)</code></term>
                <listitem>
                    Abort execution of the filter chain by throwing the given exception. This behaves as if the filter were synchronous
                    and threw the given exception.
                </listitem>
            </varlistentry>
        </variablelist>
        <para>
            Async processing can be done inside an <code>AsyncWriterInterceptor</code> (if using
            <link linkend="Asynchronous_IO">Async IO</link>), which is the asynchronous-supporting equivalent to
            <code>WriterInterceptor</code>. In this case, there is no need to manually suspend or resume the request.
        </para>
    </sect2>
    </sect1>
    <sect1>
        <title>Client Side Filters</title>
        <para>
            The client side also has two types of filters: ClientRequestFilter and ClientResponseFilter.
            ClientRequestFilters run before an HTTP request is sent over the wire to the server.  ClientResponseFilters
            run after a response is received from the server, but before the response body is unmarshalled.  ClientRequestFilters
            are allowed to abort the execution of the request and provide a canned response without going over the
            wire to the server.  ClientResponseFilters can modify the Response object before it is handed back to
            application code.  Here's pseudocode to illustrate this.
        </para>
        <programlisting>
            // execute request filters
            for (ClientRequestFilter filter : requestFilters) {
               filter.filter(requestContext);
               if (isAborted(requestContext)) {
                  return requestContext.getAbortedResponseObject();
               }
            }

            // send request over the wire
            response = sendRequest(request);

            // execute response filters
            for (ClientResponseFilter filter : responseFilters) {
               filter.filter(requestContext, responseContext);
            }
        </programlisting>
    </sect1>
    <sect1>
        <title>Reader and Writer Interceptors</title>
        <para>
            While filters modify request or response headers, interceptors deal with message bodies.  Interceptors are executed
            in the same call stack as their corresponding reader or writer.  ReaderInterceptors
            wrap around the execution of MessageBodyReaders.  WriterInterceptors wrap around the execution of MessageBodyWriters.
            They can be used to implement a specific content-encoding.  They can be used to generate digital signatures or
            to post or pre-process a Java object model before or after it is marshalled.
        </para>
        <para>
            Note that in order to support Async IO, <code>AsyncWriterInterceptor</code> can be implemented, which is a subtype of
            <code>WriterInterceptor</code>.
        </para>
    </sect1>
    <sect1>
        <title>Per Resource Method Filters and Interceptors</title>
        <para>
            Sometimes it is desired to have a filter or interceptor only run for a specific resource method.  This can be done in two different
            ways:  register an implementation of DynamicFeature or use the @NameBinding annotation.  The DynamicFeature
            interface is executed at deployment time for each resource method.  Use the Configurable interface
            to register the filters and interceptors wanted for the specific resource method.  @NameBinding works a lot
            like CDI interceptors.  Annotate a custom annotation with @NameBinding and then apply that custom annotation
            to the filter and resource method. The custom annotation must use @Retention(RetentionPolicy.RUNTIME) in order for the
            attribute to be picked up by the RESTEasy runtime when it is deployed.
        </para>
        <programlisting>
            @NameBinding
            @Retention(RetentionPolicy.RUNTIME)
            public @interface DoIt {}

            @DoIt
            public class MyFilter implements ContainerRequestFilter {...}

            @Path("/root")
            public class MyResource {

               @GET
               @DoIt
               public String get() {...}
            }
        </programlisting>
    </sect1>
    <sect1>
        <title>Ordering</title>
        <para>
            Ordering is accomplished by using the @BindingPriority annotation on the filter or interceptor classes.
        </para>
    </sect1>

</chapter>
