Interface HttpMessage

All Known Subinterfaces:
HttpRequest, HttpRequest.HttpPart

public interface HttpMessage
Represents an HTTP message, either an HTTP request or a part of a multipart HTTP request.
  • Method Summary

    Modifier and Type Method Description
    java.util.Optional<java.lang.String> getCharacterEncoding()
    Returns the character encoding specified in the Content-Type header, or Optional.empty() if there is no Content-Type header or it does not have the charset parameter.
    long getContentLength()
    Returns the numeric value of the Content-Length header.
    java.util.Optional<java.lang.String> getContentType()
    Returns the value of the Content-Type header, if any.
    default java.util.Optional<java.lang.String> getFirstHeader​(java.lang.String name)
    Convenience method that returns the value of the first header with the given name.
    java.util.Map<java.lang.String,​java.util.List<java.lang.String>> getHeaders()
    Returns a map describing the headers of this HTTP request, or this part of a multipart request.
    java.io.InputStream getInputStream()
    Returns an InputStream that can be used to read the body of this HTTP request.
    java.io.BufferedReader getReader()
    Returns a BufferedReader that can be used to read the text body of this HTTP request.
  • Method Details

    • getContentType

      java.util.Optional<java.lang.String> getContentType()
      Returns the value of the Content-Type header, if any.
      Returns:
      the content type, if any.
    • getContentLength

      Returns the numeric value of the Content-Length header.
      Returns:
      the content length.
    • getCharacterEncoding

      java.util.Optional<java.lang.String> getCharacterEncoding()
      Returns the character encoding specified in the Content-Type header, or Optional.empty() if there is no Content-Type header or it does not have the charset parameter.
      Returns:
      the character encoding for the content type, if one is specified.
    • getInputStream

      java.io.InputStream getInputStream() throws java.io.IOException
      Returns an InputStream that can be used to read the body of this HTTP request. Every call to this method on the same HttpMessage will return the same object. This method is typically used to read binary data. If the body is text, the getReader() method is more appropriate.
      Returns:
      an InputStream that can be used to read the body of this HTTP request.
      Throws:
      java.io.IOException - if a valid InputStream cannot be returned for some reason.
      java.lang.IllegalStateException - if getReader() has already been called on this instance.
    • getReader

      java.io.BufferedReader getReader() throws java.io.IOException
      Returns a BufferedReader that can be used to read the text body of this HTTP request. Every call to this method on the same HttpMessage will return the same object.
      Returns:
      a BufferedReader that can be used to read the text body of this HTTP request.
      Throws:
      java.io.IOException - if a valid BufferedReader cannot be returned for some reason.
      java.lang.IllegalStateException - if getInputStream() has already been called on this instance.
    • getHeaders

      java.util.Map<java.lang.String,​java.util.List<java.lang.String>> getHeaders()
      Returns a map describing the headers of this HTTP request, or this part of a multipart request. If the headers look like this...
         Content-Type: text/plain
         Some-Header: some value
         Some-Header: another value
       
      ...then the returned value will map "Content-Type" to a one-element list containing "text/plain", and "Some-Header" to a two-element list containing "some value" and "another value".
      Returns:
      a map where each key is an HTTP header and the corresponding List value has one element for each occurrence of that header.
    • getFirstHeader

      default java.util.Optional<java.lang.String> getFirstHeader​(java.lang.String name)
      Convenience method that returns the value of the first header with the given name. If the headers look like this...
         Content-Type: text/plain
         Some-Header: some value
         Some-Header: another value
       
      ...then getFirstHeader("Some-Header") will return Optional.of("some value"), and getFirstHeader("Another-Header") will return Optional.empty().
      Parameters:
      name - an HTTP header name.
      Returns:
      the first value of the given header, if present.