com.google.enterprise.adaptor
Class HttpExchanges

java.lang.Object
  extended by com.google.enterprise.adaptor.HttpExchanges

public final class HttpExchanges
extends Object

Utility class for working with HttpExchanges.


Method Summary
static void enableCompressionIfSupported(HttpExchange ex)
          If the client supports it, set the correct headers and streams to provide GZIPed response data to the client.
static Date getIfModifiedSince(HttpExchange ex)
          Retrieves and parses the If-Modified-Since from the request, returning null if there was no such header or there was an error.
static URI getRequestUri(HttpExchange ex)
          Best-effort attempt to reform the identical URI the client used to contact the server.
static boolean headersSent(HttpExchange ex)
          Determines if the headers have already been sent for the exchange.
static Map<String,List<String>> parseQueryParameters(HttpExchange ex, Charset charset)
          Parse request GET query parameters of ex into its parts, correctly taking into account charset.
static void sendRedirect(HttpExchange ex, URI location)
          Redirect client to location.
static void setLastModified(HttpExchange ex, Date lastModified)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getRequestUri

public static URI getRequestUri(HttpExchange ex)
Best-effort attempt to reform the identical URI the client used to contact the server.


sendRedirect

public static void sendRedirect(HttpExchange ex,
                                URI location)
                         throws IOException
Redirect client to location. The client should retrieve the referred location via GET, independent of the method of this request.

Throws:
IOException

enableCompressionIfSupported

public static void enableCompressionIfSupported(HttpExchange ex)
                                         throws IOException
If the client supports it, set the correct headers and streams to provide GZIPed response data to the client. Because the content may become compressed, users of this method should generally use a responseLength of 0 when calling HttpExchange.sendResponseHeaders(int, long). The exception is when responding to a HEAD request, in which -1 is required.

Throws:
IOException

getIfModifiedSince

public static Date getIfModifiedSince(HttpExchange ex)
Retrieves and parses the If-Modified-Since from the request, returning null if there was no such header or there was an error.


headersSent

public static boolean headersSent(HttpExchange ex)
Determines if the headers have already been sent for the exchange.

This implementation currently uses an imperfect heuristic, but should work well in most cases. It checks to see if the Date header is present, which is added during HttpExchange.sendResponseHeaders(int, long).


setLastModified

public static void setLastModified(HttpExchange ex,
                                   Date lastModified)

parseQueryParameters

public static Map<String,List<String>> parseQueryParameters(HttpExchange ex,
                                                            Charset charset)
Parse request GET query parameters of ex into its parts, correctly taking into account charset. The encoding of the GET parameters is not specified in the request parameters, so it must be negotiated elsewhere (i.e., via hard-coding). ISO 8859-1 (Latin-1) and UTF-8 are the only commonly used encodings for query parameters.

Parameters:
ex - exchange whose request query string is to be parsed
charset - character set used during encoding
Returns:
fully-decoded parameter values