org.clazzes.util.http
Class CompressionHelper

java.lang.Object
  extended by org.clazzes.util.http.CompressionHelper

public abstract class CompressionHelper
extends Object

This class provides static functionality for handling compression algorithms controlled through the Content-Coding header field.


Field Summary
static String[] DEFAULT_SUPPORTED_ENCODINGS
          A default set of supported encodings.
 
Constructor Summary
CompressionHelper()
           
 
Method Summary
static void finishOutputStream(OutputStream os)
          Finish writing to a possibly compressed servlet output stream.
static boolean isCompressed(OutputStream os)
           
static String selectCompressionCoding(String acceptEncoding, String[] supportedEncodings)
           
static void sendError(javax.servlet.http.HttpServletResponse resp, OutputStream os, int status, String message)
          Write an error response to the servlet output stream.
static void sendError(javax.servlet.http.HttpServletResponse resp, OutputStream os, int status, String message, Throwable e)
          Write an error response to the servlet output stream.
static OutputStream setupResponseOutputStream(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
          Parse the Accept-Encoding header of the request and set up a possibly compressed output stream to write to.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_SUPPORTED_ENCODINGS

public static final String[] DEFAULT_SUPPORTED_ENCODINGS
A default set of supported encodings.

Constructor Detail

CompressionHelper

public CompressionHelper()
Method Detail

selectCompressionCoding

public static String selectCompressionCoding(String acceptEncoding,
                                             String[] supportedEncodings)
Parameters:
acceptEncoding - The content of an Accept-Encoding header (see section 14.3 of RFC2616).
supportedEncodings - A list of supported encodings to choose from. This list should not contain the identity encoding, because noop compression is selected through a null encoding value.
Returns:
The encoding selected to use for the given browser of null, if the response should not be compressed.

setupResponseOutputStream

public static OutputStream setupResponseOutputStream(javax.servlet.http.HttpServletRequest req,
                                                     javax.servlet.http.HttpServletResponse resp)
                                              throws IOException
Parse the Accept-Encoding header of the request and set up a possibly compressed output stream to write to. If the output is chosen to be compressed, an appropriate Content-Encoding is set on the passed HTTP response.

Parameters:
req - The HTTP request to examine.
resp - The HTTP response.
Returns:
A compressed or uncompressed stream based on the Accept-Encoding header of the client. The returned stream should be finished after writing the whole response using finishOutputStream(OutputStream).
Throws:
IOException - Upon errors when setting response headers or setting up streams.

isCompressed

public static boolean isCompressed(OutputStream os)
Parameters:
os - An output stream previously set up using setupResponseOutputStream(HttpServletRequest, HttpServletResponse).
Returns:
Whether the output stream is compressed or not.

finishOutputStream

public static void finishOutputStream(OutputStream os)
                               throws IOException
Finish writing to a possibly compressed servlet output stream.

Parameters:
os - An OutputStream returned by setupResponseOutputStream(HttpServletRequest, HttpServletResponse).
Throws:
IOException - Upon I/O errors.

sendError

public static void sendError(javax.servlet.http.HttpServletResponse resp,
                             OutputStream os,
                             int status,
                             String message)
                      throws IOException
Write an error response to the servlet output stream. The error response may be send in compressed for, if setupResponseOutputStream(HttpServletRequest, HttpServletResponse) has been invoked before.

Parameters:
resp - The servlet response.
os - An output stream previously set up using setupResponseOutputStream(HttpServletRequest, HttpServletResponse).
status - A HTTP status, which corresponds to the given error situation.
message - An optional error message.
Throws:
IOException - Upon errors writing the response to the stream.

sendError

public static void sendError(javax.servlet.http.HttpServletResponse resp,
                             OutputStream os,
                             int status,
                             String message,
                             Throwable e)
                      throws IOException
Write an error response to the servlet output stream. The error response may be send in compressed for, if setupResponseOutputStream(HttpServletRequest, HttpServletResponse) has been invoked before.

Parameters:
resp - The servlet response.
os - An output stream previously set up using setupResponseOutputStream(HttpServletRequest, HttpServletResponse).
status - A HTTP status, which corresponds to the given error situation.
message - An optional error message.
e - An optional exception to render.
Throws:
IOException - Upon errors writing the response to the stream.


Copyright © 2013. All Rights Reserved.