public final class HttpExchanges extends Object
HttpExchange
s.Modifier and Type | Method and Description |
---|---|
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) |
public static URI getRequestUri(HttpExchange ex)
ex
- exchangepublic static void sendRedirect(HttpExchange ex, URI location) throws IOException
location
. The client should retrieve the
referred location via GET, independent of the method of this request.ex
- exchangelocation
- locationIOException
- ioepublic static void enableCompressionIfSupported(HttpExchange ex) throws IOException
responseLength
of 0
when calling HttpExchange.sendResponseHeaders(int, long)
. The exception is when responding to a
HEAD request, in which -1
is required.ex
- exchangeIOException
- ioepublic static Date getIfModifiedSince(HttpExchange ex)
ex
- exchangepublic static boolean headersSent(HttpExchange ex)
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)
.
ex
- exchangepublic static void setLastModified(HttpExchange ex, Date lastModified)
public static Map<String,List<String>> parseQueryParameters(HttpExchange ex, Charset charset)
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.ex
- exchange whose request query string is to be parsedcharset
- character set used during encoding