The typical life-cycle of a HttpExchange is shown in the sequence
below:
getRequestMethod() to determine the command.
getRequestHeaders() to examine the request headers (if
needed).
getRequestBody() returns an InputStream for
reading the request body. After reading the request body, the stream
should be closed.
getResponseHeaders() to set any response headers, except
content-length.
sendResponseHeaders(int, long) to send the response headers.
Must be called before next step.
getResponseBody() to get a OutputStream to
send the response body. When the response body has been written, the
stream must be closed to terminate the exchange.
InputStream and
response OutputStream are closed. Closing the OutputStream,
implicitly closes the InputStream (if it is not already closed).
However, it is recommended to consume all the data from the InputStream
before closing it. The convenience method close() does all of these
tasks. Closing an exchange without consuming all of the request body is not
an error but may make the underlying TCP connection unusable for following
exchanges. The effect of failing to terminate an exchange is undefined, but
will typically result in resources failing to be freed/reused.
| Modifier and Type | Field and Description |
|---|---|
| public static final long | RSPBODY_CHUNKED
The response body length is unspecified and will be chunk encoded |
| public static final long | RSPBODY_EMPTY
No response body is being sent with this response |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public abstract void | close()
Redeclares java. Ends this exchange by doing the following in sequence:
|
| public abstract Object | Returns: the attribute object, ornull if it does not existthe name of the attribute to retrieve name)
|
| public abstract HttpContext | |
| public abstract InetSocketAddress | Returns: theInetSocketAddress of the local interfaceReturns the local address on which the request was received. |
| public abstract HttpPrincipal | Returns: theHttpPrincipal, or null if no authenticator is setIf an authenticator is set on the |
| public abstract String | Returns: the protocol string from the requestReturns the protocol string from the request in the form protocol/majorVersion.minorVersion. |
| public abstract InetSocketAddress | Returns: theInetSocketAddress of the callerReturns the address of the remote entity invoking this request. |
| public abstract InputStream | Returns: the stream from which the request body can be readReturns a stream from which the request body can be read. |
| public abstract Headers | getRequestHeaders()
Redeclares com. Returns an immutable |
| public abstract String | getRequestMethod()
Redeclares com. Returns the request method. |
| public abstract URI | |
| public abstract OutputStream | Returns: the stream to which the response body is writtenReturns a stream to which the response body must be written. |
| public abstract int | Returns: the response code, if available.-1 if not available yet.Returns the response code, if it has already been set. |
| public abstract Headers | Returns: a writableHeaders which can be used to set response
headers.Returns a mutable |
| public abstract void | sendResponseHeaders(int
the response code to send rCode, long if > 0, specifies a fixed response body
length and that exact number of bytes must be written
to the stream acquired from responseLength)getResponseCode()
If equal to RSPBODY_CHUNKED, then chunked encoding is used,
and an arbitrary number of bytes may be written.
If equal to RSPBODY_EMPTY, then no response body length is
specified and no response body may be written. Any value <= -1
is treated the same as RSPBODY_EMPTY.Starts sending the response back to the client using the current set of response headers and the numeric response code as specified in this method. |
| public abstract void | setAttribute(String
the name to associate with the attribute value name, Object the object to store as the attribute value. value)null
value is permitted.
|
| public abstract void | setStreams(InputStream
the filtered input stream to set as this object's
i, OutputStream Inputstream, or null if no changethe filtered output stream to set as this object's
o)Outputstream, or null if no changeUsed by Filters to wrap either
(or both) of this exchange's |
| RSPBODY_CHUNKED | back to summary |
|---|---|
| public static final long RSPBODY_CHUNKED The response body length is unspecified and will be chunk encoded
| |
| RSPBODY_EMPTY | back to summary |
|---|---|
| public static final long RSPBODY_EMPTY No response body is being sent with this response
| |
| HttpExchange | back to summary |
|---|---|
| protected HttpExchange() Constructor for subclasses to call. | |
| close | back to summary |
|---|---|
| public abstract void close() Redeclares java. Ends this exchange by doing the following in sequence:
| |
| getAttribute | back to summary |
|---|---|
| public abstract Object getAttribute(String name)
Each
| |
| getHttpContext | back to summary |
|---|---|
| public abstract HttpContext getHttpContext() Returns the
| |
| getLocalAddress | back to summary |
|---|---|
| public abstract InetSocketAddress getLocalAddress() Returns the local address on which the request was received.
| |
| getPrincipal | back to summary |
|---|---|
| public abstract HttpPrincipal getPrincipal() If an authenticator is set on the
| |
| getProtocol | back to summary |
|---|---|
| public abstract String getProtocol() Returns the protocol string from the request in the form
protocol/majorVersion.minorVersion. For example,
"
| |
| getRemoteAddress | back to summary |
|---|---|
| public abstract InetSocketAddress getRemoteAddress() Returns the address of the remote entity invoking this request.
| |
| getRequestBody | back to summary |
|---|---|
| public abstract InputStream getRequestBody() Returns a stream from which the request body can be read.
Multiple calls to this method will return the same stream.
It is recommended that applications should consume (read) all of the data
from this stream before closing it. If a stream is closed before all data
has been read, then the
| |
| getRequestHeaders | back to summary |
|---|---|
| public abstract Headers getRequestHeaders() Redeclares com. Doc from com. Returns an immutable The keys in this The keys in | |
| getRequestMethod | back to summary |
|---|---|
| public abstract String getRequestMethod() Redeclares com. Doc from com. Returns the request method. | |
| getRequestURI | back to summary |
|---|---|
| public abstract URI getRequestURI() Redeclares com. Doc from com. Returns the request | |
| getResponseBody | back to summary |
|---|---|
| public abstract OutputStream getResponseBody() Returns a stream to which the response body must be
written. Closing this stream implicitly closes the If the call to
| |
| getResponseCode | back to summary |
|---|---|
| public abstract int getResponseCode() Returns the response code, if it has already been set.
| |
| getResponseHeaders | back to summary |
|---|---|
| public abstract Headers getResponseHeaders() Returns a mutable The keys in the The keys in
| |
| sendResponseHeaders | back to summary |
|---|---|
| public abstract void sendResponseHeaders(int rCode, long responseLength) throws IOException Starts sending the response back to the client using the current set of
response headers and the numeric response code as specified in this
method. The response body length is also specified as follows. If the
response length parameter is greater than If the content-length response header has not already been set then this is set to the appropriate value depending on the response length parameter. This method must be called prior to calling API Note If a response body is to be sent from a byte array and the
length of the array is used as the responseLength parameter, then note
the behavior in the case when the array is empty. In that case, the
responseLength will be zero which is the value of Implementation Note This implementation allows the caller to instruct the
server to force a connection close after the exchange terminates, by
supplying a
| |
| setAttribute | back to summary |
|---|---|
| public abstract void setAttribute(String name, Object value)
Each
| |
| setStreams | back to summary |
|---|---|
| public abstract void setStreams(InputStream i, OutputStream o) Used by Filters to wrap either
(or both) of this exchange's
| |