com.noelios.restlet.application
Class Encoder

java.lang.Object
  extended by org.restlet.Uniform
      extended by org.restlet.Restlet
          extended by org.restlet.Filter
              extended by com.noelios.restlet.application.Encoder

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
static int ENCODE_ALL_SIZES
          Indicates if the encoding should always occur, regardless of the size.
 
Fields inherited from class org.restlet.Filter
CONTINUE, SKIP, STOP
 
Constructor Summary
Encoder(Context context)
          Constructor using the default media types and with ENCODE_ALL_SIZES setting.
Encoder(Context context, boolean encodeInput, boolean encodeOutput, long minimumSize, java.util.List<MediaType> acceptedMediaTypes, java.util.List<MediaType> ignoredMediaTypes)
          Constructor.
 
Method Summary
 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.
 boolean isEncodeRequest()
          Indicates if the request entity should be encoded.
 boolean isEncodeResponse()
          Indicates if the response entity should be encoded.
 void setEncodeRequest(boolean encodeRequest)
          Indicates if the request entity should be encoded.
 void setEncodeResponse(boolean encodeResponse)
          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.Filter
doHandle, getNext, handle, hasNext, setNext, setNext
 
Methods inherited from class org.restlet.Restlet
getApplication, getContext, getLogger, init, isStarted, isStopped, setContext, start, stop
 
Methods inherited from class org.restlet.Uniform
delete, delete, get, get, handle, head, head, options, options, post, post, put, put
 
Methods inherited from class java.lang.Object
clone, equals, finalize, 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 encodeInput,
               boolean encodeOutput,
               long minimumSize,
               java.util.List<MediaType> acceptedMediaTypes,
               java.util.List<MediaType> ignoredMediaTypes)
Constructor.

Parameters:
context - The context.
encodeInput - Indicates if the request entities should be encoded.
encodeOutput - 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 overriden by subclasses. By default, all media types are encoded (except those explicitely 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 overriden 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.

isEncodeRequest

public boolean isEncodeRequest()
Indicates if the request entity should be encoded.

Returns:
True if the request entity should be encoded.

isEncodeResponse

public boolean isEncodeResponse()
Indicates if the response entity should be encoded.

Returns:
True if the response entity should be encoded.

setEncodeRequest

public void setEncodeRequest(boolean encodeRequest)
Indicates if the request entity should be encoded.

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

setEncodeResponse

public void setEncodeResponse(boolean encodeResponse)
Indicates if the response entity should be encoded.

Parameters:
encodeResponse - 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.


Copyright © 2005-2008 Noelios Technologies.