public class HttpMessage
extends java.lang.Object
| Modifier and Type | Class and Description |
|---|---|
static interface |
HttpMessage.Body
The life cycle of a
HttpMessage.Body is as follows:
Create the object by calling one of the following methods:
body(String, Charset)
body(InputStream)
body(File)
body(ConsumerWhichThrows)
Call exactly one of the following methods:
HttpMessage.Body.string(Charset)
HttpMessage.Body.inputStream()
HttpMessage.Body.write(OutputStream)
HttpMessage.Body.dispose()
Call HttpMessage.Body.dispose() as many times as you want
Otherwise, a resource leak will occur. |
| Modifier and Type | Field and Description |
|---|---|
static HttpMessage.Body |
EMPTY_BODY
Representation of an empty HTTP request/response body.
|
static HttpMessage.Body |
NO_BODY
Representation of a non-existent HTTP request/response body.
|
| Modifier | Constructor and Description |
|---|---|
protected |
HttpMessage(boolean hasBody)
Constructor for outgoing messages.
|
protected |
HttpMessage(java.io.InputStream in,
boolean hasHeaders,
boolean hasBody)
Constructor for incoming messages.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addHeader(java.lang.String name,
java.util.Date value)
Appends another HTTP header.
|
void |
addHeader(java.lang.String name,
int value)
Appends another HTTP header.
|
void |
addHeader(java.lang.String name,
long value)
Appends another HTTP header.
|
void |
addHeader(java.lang.String name,
java.lang.String value)
Appends another HTTP header.
|
static HttpMessage.Body |
body(ConsumerWhichThrows<java.io.OutputStream,java.io.IOException> writer) |
static HttpMessage.Body |
body(java.io.File file) |
static HttpMessage.Body |
body(java.io.InputStream in)
The
InputStream will be closed by HttpMessage.Body.string(Charset), HttpMessage.Body.inputStream(), HttpMessage.Body.write(OutputStream), HttpMessage.Body.dispose() and HttpMessage.Body.dispose(). |
static HttpMessage.Body |
body(java.lang.String text,
java.nio.charset.Charset charset) |
java.nio.charset.Charset |
getCharset()
Determines the charset from the "Content-Type" header.
|
java.util.Date |
getDateHeader(java.lang.String name) |
java.lang.String |
getHeader(java.lang.String name) |
java.util.List<MessageHeader> |
getHeaders()
The returned list is backed by the
HttpMessage! |
java.lang.String[] |
getHeaders(java.lang.String name) |
int |
getIntHeader(java.lang.String name) |
long |
getLongHeader(java.lang.String name) |
protected void |
readBody(java.nio.channels.ReadableByteChannel in,
Multiplexer multiplexer,
RunnableWhichThrows<java.io.IOException> finished)
Reads the body contents of this message into a buffer (depending on the 'Content-Length' and 'Transfer-Encoding'
headers).
|
static void |
readHeaders(java.nio.channels.ReadableByteChannel in,
Multiplexer multiplexer,
ConsumerWhichThrows<java.util.List<MessageHeader>,java.io.IOException> consumer)
Reads HTTP headers up to and including the terminating empty line.
|
static java.lang.String |
readLine(java.io.InputStream in) |
static void |
readLine(java.nio.channels.ReadableByteChannel in,
Multiplexer multiplexer,
ConsumerWhichThrows<java.lang.String,java.io.IOException> lineConsumer)
Reads one HTTP request from
in through the multiplexer and passes it to the requestConsumer. |
HttpMessage.Body |
removeBody()
Removes the body from this
HttpMessage for analysis or modification. |
void |
removeHeader(java.lang.String name)
Remove all headers with the given
name. |
void |
setBody(HttpMessage.Body body)
Disposes the current body of this message and adopts the given
HttpMessage.Body object as the new body. |
void |
setHeader(java.lang.String name,
java.util.Date value)
Changes the value of the first header with the given
name. |
void |
setHeader(java.lang.String name,
int value)
Changes the value of the first header with the given
name. |
void |
setHeader(java.lang.String name,
long value)
Changes the value of the first header with the given
name. |
void |
setHeader(java.lang.String name,
java.lang.String value)
Changes the value of the first header with the given
name. |
protected void |
writeHeadersAndBody(java.lang.String prefix,
java.io.OutputStream out)
Writes this message's headers and body to the given
OutputStream. |
public static final HttpMessage.Body NO_BODY
public static final HttpMessage.Body EMPTY_BODY
protected HttpMessage(boolean hasBody)
protected HttpMessage(java.io.InputStream in,
boolean hasHeaders,
boolean hasBody)
throws java.io.IOException
Notice that in will be read and closed when the body of this message is processed or disposed
(see HttpMessage.Body).
java.io.IOExceptionpublic void addHeader(java.lang.String name,
java.lang.String value)
public void addHeader(java.lang.String name,
int value)
public void addHeader(java.lang.String name,
long value)
public void addHeader(java.lang.String name,
java.util.Date value)
public void setHeader(java.lang.String name,
java.lang.String value)
name.public void setHeader(java.lang.String name,
int value)
name.public void setHeader(java.lang.String name,
long value)
name.public void setHeader(java.lang.String name,
java.util.Date value)
name.public void removeHeader(java.lang.String name)
name.@Nullable public final java.lang.String getHeader(java.lang.String name)
name, or nullpublic int getIntHeader(java.lang.String name)
throws java.io.IOException
-1 iff a header with this name does not existjava.io.IOExceptionpublic final long getLongHeader(java.lang.String name)
throws java.io.IOException
-1L iff a header with this name does not existjava.io.IOException@Nullable public java.util.Date getDateHeader(java.lang.String name) throws java.io.IOException
null iff a header with this name does not existjava.io.IOExceptionpublic java.lang.String[] getHeaders(java.lang.String name)
name, or an empty arraypublic java.util.List<MessageHeader> getHeaders()
HttpMessage!public HttpMessage.Body removeBody()
HttpMessage for analysis or modification. It can later be re-attached to
the same (or a different) HttpMessage through setBody(Body).public static HttpMessage.Body body(java.lang.String text, java.nio.charset.Charset charset)
HttpMessage.Bodypublic static HttpMessage.Body body(java.io.InputStream in)
InputStream will be closed by HttpMessage.Body.string(Charset), HttpMessage.Body.inputStream(), HttpMessage.Body.write(OutputStream), HttpMessage.Body.dispose() and HttpMessage.Body.dispose().HttpMessage.Bodypublic static HttpMessage.Body body(java.io.File file) throws java.io.FileNotFoundException
java.io.FileNotFoundExceptionHttpMessage.Bodypublic static HttpMessage.Body body(ConsumerWhichThrows<java.io.OutputStream,java.io.IOException> writer)
HttpMessage.Bodypublic void setBody(HttpMessage.Body body)
HttpMessage.Body object as the new body.public java.nio.charset.Charset getCharset()
protected void writeHeadersAndBody(java.lang.String prefix,
java.io.OutputStream out)
throws java.io.IOException
OutputStream. Also closes the OutputStream
iff there is neither a "Content-Length:" header, nor a "Transfer-Encoding: chunked" header, nor the message
is very small.java.io.IOExceptionpublic static java.lang.String readLine(java.io.InputStream in)
throws java.io.IOException
java.io.IOExceptionpublic static void readLine(java.nio.channels.ReadableByteChannel in,
Multiplexer multiplexer,
ConsumerWhichThrows<java.lang.String,java.io.IOException> lineConsumer)
throws java.io.IOException
in through the multiplexer and passes it to the requestConsumer.java.io.IOExceptionpublic static void readHeaders(java.nio.channels.ReadableByteChannel in,
Multiplexer multiplexer,
ConsumerWhichThrows<java.util.List<MessageHeader>,java.io.IOException> consumer)
throws java.io.IOException
java.io.IOExceptionprotected void readBody(java.nio.channels.ReadableByteChannel in,
Multiplexer multiplexer,
RunnableWhichThrows<java.io.IOException> finished)
throws java.io.IOException
java.io.IOException