|
Java HTTP Server | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.sun.net.httpserver.HttpExchange
public abstract class HttpExchange
This class encapsulates a HTTP request received and a response to be generated in one exchange. It provides methods for examining the request from the client, and for building and sending the response.
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 a InputStream
for reading the request body.
After reading the request body, the stream is close.
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.
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.
Constructor Summary | |
---|---|
protected |
HttpExchange()
|
Method Summary | |
---|---|
abstract void |
close()
Ends this exchange by doing the following in sequence: |
abstract Object |
getAttribute(String name)
Filter modules may store arbitrary objects with HttpExchange instances as an out-of-band communication mechanism. |
abstract HttpContext |
getHttpContext()
Get the HttpContext for this exchange |
abstract InetSocketAddress |
getLocalAddress()
Returns the local address on which the request was received |
abstract HttpPrincipal |
getPrincipal()
If an authenticator is set on the HttpContext that owns this exchange, then this method will return the HttpPrincipal that represents
the authenticated user for this HttpExchange. |
abstract String |
getProtocol()
Returns the protocol string from the request in the form protocol/majorVersion.minorVersion. |
abstract InetSocketAddress |
getRemoteAddress()
Returns the address of the remote entity invoking this request |
abstract InputStream |
getRequestBody()
returns a stream from which the request body can be read. |
abstract Headers |
getRequestHeaders()
Returns an immutable Map containing the HTTP headers that were included with this request. |
abstract String |
getRequestMethod()
Get the request method |
abstract URI |
getRequestURI()
Get the request URI |
abstract OutputStream |
getResponseBody()
returns a stream to which the response body must be written. |
abstract int |
getResponseCode()
Returns the response code, if it has already been set |
abstract Headers |
getResponseHeaders()
Returns a mutable Map into which the HTTP response headers can be stored and which will be transmitted as part of this response. |
abstract void |
sendResponseHeaders(int rCode,
long responseLength)
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. |
abstract void |
setAttribute(String name,
Object value)
Filter modules may store arbitrary objects with HttpExchange instances as an out-of-band communication mechanism. |
abstract void |
setStreams(InputStream i,
OutputStream o)
Used by Filters to wrap either (or both) of this exchange's InputStream and OutputStream, with the given filtered streams so that subsequent calls to getRequestBody() will
return the given InputStream , and calls to
getResponseBody() will return the given
OutputStream . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
protected HttpExchange()
Method Detail |
---|
public abstract Headers getRequestHeaders()
The keys in Map are case-insensitive.
public abstract Headers getResponseHeaders()
The keys in Map are case-insensitive.
public abstract URI getRequestURI()
public abstract String getRequestMethod()
public abstract HttpContext getHttpContext()
public abstract void close()
public abstract InputStream getRequestBody()
public abstract OutputStream getResponseBody()
sendResponseHeaders(int,long)
) must be called prior to calling
this method. Multiple calls to this method (for the same exchange)
will return the same stream. In order to correctly terminate
each exchange, the output stream must be closed, even if no
response body is being sent.
Closing this stream implicitly
closes the InputStream returned from getRequestBody()
(if it is not already closed).
If the call to sendResponseHeaders() specified a fixed response body length, then the exact number of bytes specified in that call must be written to this stream. If too many bytes are written, then write() will throw an IOException. If too few bytes are written then the stream close() will throw an IOException. In both cases, the exchange is aborted and the underlying TCP connection closed.
public abstract void sendResponseHeaders(int rCode, long responseLength) throws IOException
zero
, then chunked transfer encoding is
used and an arbitrary amount of data may be sent. The application terminates the
response body by closing the OutputStream. If response length has the value -1
then no response body is being sent.
If the content-length response header has not already been set then this is set to the apropriate value depending on the response length parameter.
This method must be called prior to calling getResponseBody()
.
rCode
- the response code to sendresponseLength
- if > 0, specifies a fixed response body length
and that exact number of bytes must be written
to the stream acquired from getResponseBody(), or else
if equal to 0, then chunked encoding is used,
and an arbitrary number of bytes may be written.
if <= -1, then no response body length is specified and
no response body may be written.
IOException
getResponseBody()
public abstract InetSocketAddress getRemoteAddress()
public abstract int getResponseCode()
-1
if not available yet.public abstract InetSocketAddress getLocalAddress()
public abstract String getProtocol()
public abstract Object getAttribute(String name)
Each Filter class will document the attributes which they make available.
name
- the name of the attribute to retrieve
NullPointerException
- if name is null
public abstract void setAttribute(String name, Object value)
Each Filter class will document the attributes which they make available.
name
- the name to associate with the attribute valuevalue
- the object to store as the attribute value. null
value is permitted.
NullPointerException
- if name is null
public abstract void setStreams(InputStream i, OutputStream o)
getRequestBody()
will
return the given InputStream
, and calls to
getResponseBody()
will return the given
OutputStream
. The streams provided to this
call must wrap the original streams, and may be (but are not
required to be) sub-classes of FilterInputStream
and FilterOutputStream
.
i
- the filtered input stream to set as this object's inputstream,
or null
if no change.o
- the filtered output stream to set as this object's outputstream,
or null
if no change.public abstract HttpPrincipal getPrincipal()
HttpPrincipal
that represents
the authenticated user for this HttpExchange.
null
if no authenticator is set.
|
Java HTTP Server | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |