org.restlet.engine.application

## Class Encoder

- java.lang.Object
- - org.restlet.Restlet
  - - org.restlet.routing.Filter
    - - org.restlet.engine.application.Encoder

All Implemented Interfaces:
  Uniform

    public class Encoder
    extends Filter

Filter compressing entities. The best encoding is automatically selected based on the preferences of the client and on the encoding supported by NRE: GZip, Zip and Deflate.

If the `Representation` has an unknown size, it will always be a candidate for encoding. Candidate representations need to respect media type criteria by the lists of accepted and ignored media types. 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:
  Lars Heuer (heuer[at]semagia.com) Semagia, Jerome Louvel

### Field Summary

Fields
`static int` `ENCODE_ALL_SIZES` Indicates if the encoding should always occur, regardless of the size.

### Fields inherited from class org.restlet.routing.Filter

`CONTINUE, SKIP, STOP`

### Constructor Summary

Constructors
Constructor and Description
`Encoder(Context context)` Constructor using the default media types and with `ENCODE_ALL_SIZES` setting.
`Encoder(Context context, boolean encodingInput, boolean encodingOutput, long minimumSize, java.util.List<MediaType> acceptedMediaTypes, java.util.List<MediaType> ignoredMediaTypes)` Constructor.

### Method Summary

Methods
`void` `afterHandle(Request request, Response response)` Allows filtering after its handling by the target Restlet.
`int` `beforeHandle(Request request, Response response)` Allows filtering before its handling by the target Restlet.
`boolean` `canEncode(Representation representation)` Indicates if a representation can be encoded.
`Representation` `encode(ClientInfo client, Representation representation)` Encodes a given representation if an encoding is supported by the client.
`java.util.List<MediaType>` `getAcceptedMediaTypes()` Returns the media types that should be encoded.
`Encoding` `getBestEncoding(ClientInfo client)` Returns the best supported encoding for a given client.
`static java.util.List<MediaType>` `getDefaultAcceptedMediaTypes()` Returns the list of default encoded media types.
`static java.util.List<MediaType>` `getDefaultIgnoredMediaTypes()` Returns the list of default ignored media types.
`java.util.List<MediaType>` `getIgnoredMediaTypes()` Returns the media types that should be ignored.
`long` `getMinimumSize()` Returns the minimum size a representation must have before compression is done.
`java.util.List<Encoding>` `getSupportedEncodings()` Returns the list of supported encodings.
`boolean` `isEncodeRequest()` Deprecated. Use `isEncodingRequest()` instead.
`boolean` `isEncodeResponse()` Deprecated. Use `isEncodingResponse()` instead.
`boolean` `isEncodingRequest()` Indicates if the request entity should be encoded.
`boolean` `isEncodingResponse()` Indicates if the response entity should be encoded.
`void` `setEncodeRequest(boolean encodingRequest)` Deprecated. Use `setEncodingRequest(boolean)` instead.
`void` `setEncodeResponse(boolean encodingResponse)` Deprecated. Use `setEncodingResponse(boolean)` instead.
`void` `setEncodingRequest(boolean encodingRequest)` Indicates if the request entity should be encoded.
`void` `setEncodingResponse(boolean encodingResponse)` Indicates if the response entity should be encoded.
`void` `setMinimumSize(long mininumSize)` Sets the minimum size a representation must have before compression is done.

### Methods inherited from class org.restlet.routing.Filter

`doHandle, getNext, handle, hasNext, setNext, setNext, start, stop`

### 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

#### ENCODE_ALL_SIZES

    public static final int ENCODE_ALL_SIZES

Indicates if the encoding should always occur, regardless of the size.

See Also:
  Constant Field Values

### Constructor Detail

#### Encoder

    public Encoder(Context context)

Constructor using the default media types and with `ENCODE_ALL_SIZES` setting. This constructor will only encode response entities after call handling.

Parameters:
  `context` - The context.

#### Encoder

    public Encoder(Context context,
           boolean encodingInput,
           boolean encodingOutput,
           long minimumSize,
           java.util.List<MediaType> acceptedMediaTypes,
           java.util.List<MediaType> ignoredMediaTypes)

Constructor.

Parameters:
  `context` - The context.
  `encodingInput` - Indicates if the request entities should be encoded.
  `encodingOutput` - Indicates if the response entities should be encoded.
  `minimumSize` - The minimal size of the representation where compression should be used.
  `acceptedMediaTypes` - The media types that should be encoded.
  `ignoredMediaTypes` - The media types that should be ignored.

### Method Detail

#### getDefaultAcceptedMediaTypes

    public static java.util.List<MediaType> getDefaultAcceptedMediaTypes()

Returns the list of default encoded media types. This can be overridden by subclasses. By default, all media types are encoded (except those explicitly ignored).

Returns:
  The list of default encoded media types.

#### getDefaultIgnoredMediaTypes

    public static java.util.List<MediaType> getDefaultIgnoredMediaTypes()

Returns the list of default ignored media types. This can be overridden by subclasses. By default, all archive, audio, image and video media types are ignored.

Returns:
  The list of default ignored media types.

#### afterHandle

    public void afterHandle(Request request,
                   Response response)

Allows filtering after its handling by the target Restlet. Does nothing by default.

Overrides:
  `afterHandle` in class `Filter`
Parameters:
  `request` - The request to filter.
  `response` - The response to filter.

#### beforeHandle

    public int beforeHandle(Request request,
                   Response response)

Allows filtering before its handling by the target Restlet. Does nothing by default.

Overrides:
  `beforeHandle` in class `Filter`
Parameters:
  `request` - The request to filter.
  `response` - The response to filter.
Returns:
  The continuation status.

#### canEncode

    public boolean canEncode(Representation representation)

Indicates if a representation can be encoded.

Parameters:
  `representation` - The representation to test.
Returns:
  True if the call can be encoded.

#### encode

    public Representation encode(ClientInfo client,
                        Representation representation)

Encodes a given representation if an encoding is supported by the client.

Parameters:
  `client` - The client preferences to use.
  `representation` - The representation to encode.
Returns:
  The encoded representation or the original one if no encoding supported by the client.

#### getAcceptedMediaTypes

    public java.util.List<MediaType> getAcceptedMediaTypes()

Returns the media types that should be encoded.

Returns:
  The media types that should be encoded.

#### getBestEncoding

    public Encoding getBestEncoding(ClientInfo client)

Returns the best supported encoding for a given client.

Parameters:
  `client` - The client preferences to use.
Returns:
  The best supported encoding for the given call.

#### getIgnoredMediaTypes

    public java.util.List<MediaType> getIgnoredMediaTypes()

Returns the media types that should be ignored.

Returns:
  The media types that should be ignored.

#### getMinimumSize

    public long getMinimumSize()

Returns the minimum size a representation must have before compression is done.

Returns:
  The minimum size a representation must have before compression is done.

#### getSupportedEncodings

    public java.util.List<Encoding> getSupportedEncodings()

Returns the list of supported encodings. By default it calls `EncodeRepresentation.getSupportedEncodings()` static method.

Returns:
  The list of supported encodings.

#### isEncodeRequest

    @Deprecated
    public boolean isEncodeRequest()

Deprecated. Use `isEncodingRequest()` instead.
Indicates if the request entity should be encoded.

Returns:
  True if the request entity should be encoded.

#### isEncodeResponse

    @Deprecated
    public boolean isEncodeResponse()

Deprecated. Use `isEncodingResponse()` instead.
Indicates if the response entity should be encoded.

Returns:
  True if the response entity should be encoded.

#### isEncodingRequest

    public boolean isEncodingRequest()

Indicates if the request entity should be encoded.

Returns:
  True if the request entity should be encoded.

#### isEncodingResponse

    public boolean isEncodingResponse()

Indicates if the response entity should be encoded.

Returns:
  True if the response entity should be encoded.

#### setEncodeRequest

    @Deprecated
    public void setEncodeRequest(boolean encodingRequest)

Deprecated. Use `setEncodingRequest(boolean)` instead.
Indicates if the request entity should be encoded.

Parameters:
  `encodingRequest` - True if the request entity should be encoded.

#### setEncodeResponse

    @Deprecated
    public void setEncodeResponse(boolean encodingResponse)

Deprecated. Use `setEncodingResponse(boolean)` instead.
Indicates if the response entity should be encoded.

Parameters:
  `encodingResponse` - True if the response entity should be encoded.

#### setEncodingRequest

    public void setEncodingRequest(boolean encodingRequest)

Indicates if the request entity should be encoded.

Parameters:
  `encodingRequest` - True if the request entity should be encoded.

#### setEncodingResponse

    public void setEncodingResponse(boolean encodingResponse)

Indicates if the response entity should be encoded.

Parameters:
  `encodingResponse` - True if the response entity should be encoded.

#### setMinimumSize

    public void setMinimumSize(long mininumSize)

Sets the minimum size a representation must have before compression is done.

Parameters:
  `mininumSize` - The minimum size a representation must have before compression is done.