org.restlet.routing

## Class Filter

- java.lang.Object
- - org.restlet.Restlet
  - - org.restlet.routing.Filter

All Implemented Interfaces:
  Uniform

Direct Known Subclasses:
  Authenticator, Authorizer, Decoder, Encoder, Extractor, Guard, LogFilter, RangeFilter, StatusFilter, TemplateRoute, TunnelFilter, Validator

    public abstract class Filter
    extends Restlet

Restlet filtering calls before passing them to an attached Restlet. The purpose is to do some pre-processing or post-processing on the calls going through it before or after they are actually handled by an attached Restlet. Also note that you can attach and detach targets while handling incoming calls as the filter is ensured to be thread-safe.
Concurrency note: instances of this class or its subclasses can be invoked by several threads at the same time and therefore must be thread-safe. You should be especially careful when storing state in member variables.

Author:
  Jerome Louvel

### Field Summary

Fields
`static int` `CONTINUE` Indicates that the request processing should continue normally.
`static int` `SKIP` Indicates that after the `beforeHandle(Request, Response)` method, the request processing should skip the `doHandle(Request, Response)` method to continue with the `afterHandle(Request, Response)` method.
`static int` `STOP` Indicates that the request processing should stop and return the current response from the filter.

### Constructor Summary

Constructors
Constructor and Description
`Filter()` Constructor.
`Filter(Context context)` Constructor.
`Filter(Context context, Restlet next)` Constructor.

### Method Summary

Methods
`protected void` `afterHandle(Request request, Response response)` Allows filtering after processing by the next Restlet.
`protected int` `beforeHandle(Request request, Response response)` Allows filtering before processing by the next Restlet.
`protected int` `doHandle(Request request, Response response)` Handles the call by distributing it to the next Restlet.
`Restlet` `getNext()` Returns the next Restlet.
`void` `handle(Request request, Response response)` Handles a call by first invoking the beforeHandle() method for pre-filtering, then distributing the call to the next Restlet via the doHandle() method.
`boolean` `hasNext()` Indicates if there is a next Restlet.
`void` `setNext(java.lang.Class<?> targetClass)` Sets the next Restlet as a Finder for a given `Handler` or `ServerResource` class.
`void` `setNext(Restlet next)` Sets the next Restlet.
`void` `start()` Starts the filter and the next Restlet if attached.
`void` `stop()` Stops the filter and the next Restlet if attached.

### Methods inherited from class org.restlet.Restlet

`finalize, getApplication, getAuthor, getContext, getDescription, getLogger, getName, getOwner, isStarted, isStopped, setAuthor, setContext, setDescription, setName, setOwner`

### Methods inherited from class java.lang.Object

`clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### CONTINUE

    public static final int CONTINUE

Indicates that the request processing should continue normally. If returned from the `beforeHandle(Request, Response)` method, the filter then invokes the `doHandle(Request, Response)` method. If returned from the `doHandle(Request, Response)` method, the filter then invokes the `afterHandle(Request, Response)` method.

See Also:
  Constant Field Values

#### SKIP

    public static final int SKIP

Indicates that after the `beforeHandle(Request, Response)` method, the request processing should skip the `doHandle(Request, Response)` method to continue with the `afterHandle(Request, Response)` method.

See Also:
  Constant Field Values

#### STOP

    public static final int STOP

Indicates that the request processing should stop and return the current response from the filter.

See Also:
  Constant Field Values

### Constructor Detail

#### Filter

    public Filter()

Constructor.

#### Filter

    public Filter(Context context)

Constructor.

Parameters:
  `context` - The context.

#### Filter

    public Filter(Context context,
          Restlet next)

Constructor.

Parameters:
  `context` - The context.
  `next` - The next Restlet.

### Method Detail

#### afterHandle

    protected void afterHandle(Request request,
                   Response response)

Allows filtering after processing by the next Restlet. Does nothing by default.

Parameters:
  `request` - The request to handle.
  `response` - The response to update.

#### beforeHandle

    protected int beforeHandle(Request request,
                   Response response)

Allows filtering before processing by the next Restlet. Returns `CONTINUE` by default.

Parameters:
  `request` - The request to handle.
  `response` - The response to update.
Returns:
  The continuation status. Either `CONTINUE` or `SKIP` or `STOP`.

#### doHandle

    protected int doHandle(Request request,
               Response response)

Handles the call by distributing it to the next Restlet. If no Restlet is attached, then a `Status.SERVER_ERROR_INTERNAL` status is returned. Returns `CONTINUE` by default.

Parameters:
  `request` - The request to handle.
  `response` - The response to update.
Returns:
  The continuation status. Either `CONTINUE` or `STOP`.

#### getNext

    public Restlet getNext()

Returns the next Restlet.

Returns:
  The next Restlet or null.

#### handle

    public final void handle(Request request,
              Response response)

Handles a call by first invoking the beforeHandle() method for pre-filtering, then distributing the call to the next Restlet via the doHandle() method. When the handling is completed, it finally invokes the afterHandle() method for post-filtering.

Specified by:
  `handle` in interface `Uniform`
Overrides:
  `handle` in class `Restlet`
Parameters:
  `request` - The request to handle.
  `response` - The response to update.

#### hasNext

    public boolean hasNext()

Indicates if there is a next Restlet.

Returns:
  True if there is a next Restlet.

#### setNext

    public void setNext(java.lang.Class<?> targetClass)

Sets the next Restlet as a Finder for a given `Handler` or `ServerResource` class. When the call is delegated to the Finder instance, a new instance of the resource class will be created and will actually handle the request.

Parameters:
  `targetClass` - The target resource class to attach.

#### setNext

    public void setNext(Restlet next)

Sets the next Restlet. In addition, this method will set the context of the next Restlet if it is null by passing a reference to its own context.

Parameters:
  `next` - The next Restlet.

#### start

    public void start()
               throws java.lang.Exception

Starts the filter and the next Restlet if attached.

Overrides:
  `start` in class `Restlet`
Throws:
  `java.lang.Exception`

#### stop

    public void stop()
              throws java.lang.Exception

Stops the filter and the next Restlet if attached.

Overrides:
  `stop` in class `Restlet`
Throws:
  `java.lang.Exception`