public class HttpMessage extends 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. |
static class |
HttpMessage.HasBody |
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(InputStream in,
boolean hasHeaders,
HttpMessage.HasBody hasBody)
Constructor for incoming messages.
|
protected |
HttpMessage(InputStream in,
boolean hasHeaders,
HttpMessage.HasBody hasBody,
String loggingPrefix)
Constructor for incoming messages.
|
Modifier and Type | Method and Description |
---|---|
void |
addHeader(String name,
Date value)
Appends another HTTP header.
|
void |
addHeader(String name,
int value)
Appends another HTTP header.
|
void |
addHeader(String name,
long value)
Appends another HTTP header.
|
void |
addHeader(String name,
String value)
Appends another HTTP header.
|
static HttpMessage.Body |
body(de.unkrig.commons.lang.protocol.ConsumerWhichThrows<OutputStream,IOException> writer) |
static HttpMessage.Body |
body(File file) |
static HttpMessage.Body |
body(InputStream in)
|
static HttpMessage.Body |
body(de.unkrig.commons.lang.protocol.ProducerWhichThrows<InputStream,IOException> in)
For the returned objects,
HttpMessage.Body.string(Charset) , HttpMessage.Body.inputStream() and HttpMessage.Body.write(OutputStream) will invoke in to produce an InputStream which will be read and
eventually closed. |
static HttpMessage.Body |
body(String text,
Charset charset) |
Charset |
getCharset()
Determines the charset from the "Content-Type" header.
|
Date |
getDateHeader(String name) |
String |
getHeader(String name) |
List<MessageHeader> |
getHeaders()
The returned list is backed by the
HttpMessage ! |
String[] |
getHeaders(String name) |
int |
getIntHeader(String name) |
long |
getLongHeader(String name) |
protected void |
readBody(ReadableByteChannel in,
Multiplexer multiplexer,
de.unkrig.commons.lang.protocol.RunnableWhichThrows<IOException> finished,
String loggingPrefix)
Reads the body contents of this message into a buffer (depending on the 'Content-Length' and 'Transfer-Encoding'
headers).
|
static void |
readHeaders(ReadableByteChannel in,
Multiplexer multiplexer,
de.unkrig.commons.lang.protocol.ConsumerWhichThrows<List<MessageHeader>,IOException> consumer)
Reads HTTP headers up to and including the terminating empty line.
|
static void |
readHeaders(ReadableByteChannel in,
Multiplexer multiplexer,
de.unkrig.commons.lang.protocol.ConsumerWhichThrows<List<MessageHeader>,IOException> consumer,
String loggingPrefix)
Reads HTTP headers up to and including the terminating empty line.
|
static String |
readLine(InputStream in) |
static void |
readLine(ReadableByteChannel in,
Multiplexer multiplexer,
de.unkrig.commons.lang.protocol.ConsumerWhichThrows<String,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(String name)
Remove all headers with the given name.
|
void |
setAttemptUnstreaming(boolean attemptUnstreaming) |
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(String name,
Date value)
Changes the value of the first header with the given name.
|
void |
setHeader(String name,
int value)
Changes the value of the first header with the given name.
|
void |
setHeader(String name,
long value)
Changes the value of the first header with the given name.
|
void |
setHeader(String name,
String value)
Changes the value of the first header with the given name, or adds a new header.
|
protected void |
writeHeadersAndBody(OutputStream out)
Writes this message's headers and body to the given
OutputStream . |
protected void |
writeHeadersAndBody(String loggingPrefix,
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(InputStream in, boolean hasHeaders, HttpMessage.HasBody hasBody) throws IOException
Notice that in will be read and closed when the body of this message is processed or disposed
(see HttpMessage.Body
).
IOException
protected HttpMessage(InputStream in, boolean hasHeaders, HttpMessage.HasBody hasBody, String loggingPrefix) throws IOException
Notice that in will be read and closed when the body of this message is processed or disposed
(see HttpMessage.Body
).
loggingPrefix
- E.g. ">>> "
IOException
public void setAttemptUnstreaming(boolean attemptUnstreaming)
public void addHeader(String name, int value)
public void addHeader(String name, long value)
public void setHeader(String name, String value)
public void setHeader(String name, int value)
public void setHeader(String name, long value)
public void setHeader(String name, Date value)
public void removeHeader(String name)
@Nullable public final String getHeader(String name)
null
public int getIntHeader(String name) throws IOException
-1
iff a header with this name does not existIOException
public final long getLongHeader(String name) throws IOException
-1L
iff a header with this name does not existIOException
@Nullable public Date getDateHeader(String name) throws IOException
null
iff a header with this name does not existIOException
public String[] getHeaders(String name)
public 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(String text, Charset charset)
HttpMessage.Body
public static HttpMessage.Body body(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.Body
public static HttpMessage.Body body(de.unkrig.commons.lang.protocol.ProducerWhichThrows<InputStream,IOException> in)
HttpMessage.Body.string(Charset)
, HttpMessage.Body.inputStream()
and HttpMessage.Body.write(OutputStream)
will invoke in to produce an InputStream
which will be read and
eventually closed.HttpMessage.Body
public static HttpMessage.Body body(File file) throws FileNotFoundException
FileNotFoundException
HttpMessage.Body
public static HttpMessage.Body body(de.unkrig.commons.lang.protocol.ConsumerWhichThrows<OutputStream,IOException> writer)
writer
- Consumes exactly one OutputStream
, and writes the body data to itHttpMessage.Body
public void setBody(HttpMessage.Body body)
HttpMessage.Body
object as the new body.public Charset getCharset()
protected void writeHeadersAndBody(OutputStream out) throws 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.IOException
protected void writeHeadersAndBody(String loggingPrefix, OutputStream out) throws 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.loggingPrefix
- E.g. "<<< "
IOException
public static String readLine(InputStream in) throws IOException
EOFException
IOException
public static void readLine(ReadableByteChannel in, Multiplexer multiplexer, de.unkrig.commons.lang.protocol.ConsumerWhichThrows<String,IOException> lineConsumer) throws IOException
IOException
public static void readHeaders(ReadableByteChannel in, Multiplexer multiplexer, de.unkrig.commons.lang.protocol.ConsumerWhichThrows<List<MessageHeader>,IOException> consumer) throws IOException
IOException
public static void readHeaders(ReadableByteChannel in, Multiplexer multiplexer, de.unkrig.commons.lang.protocol.ConsumerWhichThrows<List<MessageHeader>,IOException> consumer, String loggingPrefix) throws IOException
loggingPrefix
- E.g. ">>> "
IOException
protected void readBody(ReadableByteChannel in, Multiplexer multiplexer, de.unkrig.commons.lang.protocol.RunnableWhichThrows<IOException> finished, String loggingPrefix) throws IOException
loggingPrefix
- E.g. ">>> "
IOException
Copyright © 2018 Arno Unkrig. All rights reserved.