de.unkrig.commons.net.http
Class HttpMessage

java.lang.Object
  extended by de.unkrig.commons.net.http.HttpMessage
Direct Known Subclasses:
HttpRequest, HttpResponse

public class HttpMessage
extends java.lang.Object

Representation of an HTTP request or response.


Nested Class Summary
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.
 
Field Summary
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.
 
Constructor Summary
protected HttpMessage(boolean hasBody)
          Constructor for outgoing messages.
protected HttpMessage(java.io.InputStream in, boolean hasHeaders, boolean hasBody)
          Constructor for incoming messages.
 
Method Summary
 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)
           
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_BODY

public static final HttpMessage.Body NO_BODY
Representation of a non-existent HTTP request/response body.


EMPTY_BODY

public static final HttpMessage.Body EMPTY_BODY
Representation of an empty HTTP request/response body.

Constructor Detail

HttpMessage

protected HttpMessage(boolean hasBody)
Constructor for outgoing messages.


HttpMessage

protected HttpMessage(java.io.InputStream in,
                      boolean hasHeaders,
                      boolean hasBody)
               throws java.io.IOException
Constructor for incoming messages.

Throws:
java.io.IOException
Method Detail

addHeader

public void addHeader(java.lang.String name,
                      java.lang.String value)
Appends another HTTP header.


addHeader

public void addHeader(java.lang.String name,
                      int value)
Appends another HTTP header.


addHeader

public void addHeader(java.lang.String name,
                      long value)
Appends another HTTP header.


addHeader

public void addHeader(java.lang.String name,
                      java.util.Date value)
Appends another HTTP header.


setHeader

public void setHeader(java.lang.String name,
                      java.lang.String value)
Changes the value of the first header with the given name.


setHeader

public void setHeader(java.lang.String name,
                      int value)
Changes the value of the first header with the given name.


setHeader

public void setHeader(java.lang.String name,
                      long value)
Changes the value of the first header with the given name.


setHeader

public void setHeader(java.lang.String name,
                      java.util.Date value)
Changes the value of the first header with the given name.


removeHeader

public void removeHeader(java.lang.String name)
Remove all headers with the given name.


getHeader

@Nullable
public final java.lang.String getHeader(java.lang.String name)
Returns:
the value of the first message header with that name, or null

getIntHeader

public int getIntHeader(java.lang.String name)
                 throws java.io.IOException
Returns:
-1 iff a header with this name does not exist
Throws:
java.io.IOException

getLongHeader

public final long getLongHeader(java.lang.String name)
                         throws java.io.IOException
Returns:
-1L iff a header with this name does not exist
Throws:
java.io.IOException

getDateHeader

@Nullable
public java.util.Date getDateHeader(java.lang.String name)
                             throws java.io.IOException
Returns:
null iff a header with this name does not exist
Throws:
java.io.IOException

getHeaders

public java.lang.String[] getHeaders(java.lang.String name)
Returns:
the values of the message headers with that name, or an empty array

getHeaders

public java.util.List<MessageHeader> getHeaders()
The returned list is backed by the HttpMessage!


removeBody

public HttpMessage.Body removeBody()
Removes the body from this HttpMessage for analysis or modification. It can later be re-attached to the same (or a different) HttpMessage through setBody(Body).


body

public static HttpMessage.Body body(java.lang.String text,
                                    java.nio.charset.Charset charset)
See Also:
HttpMessage.Body

body

public static HttpMessage.Body body(java.io.InputStream in)
See Also:
HttpMessage.Body

body

public static HttpMessage.Body body(java.io.File file)
                             throws java.io.FileNotFoundException
Throws:
java.io.FileNotFoundException
See Also:
HttpMessage.Body

body

public static HttpMessage.Body body(ConsumerWhichThrows<java.io.OutputStream,java.io.IOException> writer)
See Also:
HttpMessage.Body

setBody

public void setBody(HttpMessage.Body body)
Disposes the current body of this message and adopts the given HttpMessage.Body object as the new body.


getCharset

public java.nio.charset.Charset getCharset()
Determines the charset from the "Content-Type" header.


writeHeadersAndBody

protected void writeHeadersAndBody(java.lang.String prefix,
                                   java.io.OutputStream out)
                            throws java.io.IOException
Writes this message's headers and body to the given OutputStream.

Throws:
java.io.IOException

readLine

public static java.lang.String readLine(java.io.InputStream in)
                                 throws java.io.IOException
Returns:
the line read, excluding the trailing CRLF
Throws:
java.io.IOException

readLine

public static void readLine(java.nio.channels.ReadableByteChannel in,
                            Multiplexer multiplexer,
                            ConsumerWhichThrows<java.lang.String,java.io.IOException> lineConsumer)
                     throws java.io.IOException
Reads one HTTP request from in through the multiplexer and passes it to the requestConsumer.

Throws:
java.io.IOException

readHeaders

public static void readHeaders(java.nio.channels.ReadableByteChannel in,
                               Multiplexer multiplexer,
                               ConsumerWhichThrows<java.util.List<MessageHeader>,java.io.IOException> consumer)
                        throws java.io.IOException
Reads HTTP headers up to and including the terminating empty line.

Throws:
java.io.IOException

readBody

protected void readBody(java.nio.channels.ReadableByteChannel in,
                        Multiplexer multiplexer,
                        RunnableWhichThrows<java.io.IOException> finished)
                 throws java.io.IOException
Reads the body contents of this message into a buffer (depending on the 'Content-Length' and 'Transfer-Encoding' headers).

Throws:
java.io.IOException