Class HttpRequest

java.lang.Object
org.jdrupes.httpcodec.protocols.http.HttpMessageHeader
org.jdrupes.httpcodec.protocols.http.HttpRequest
All Implemented Interfaces:
MessageHeader

public class HttpRequest extends HttpMessageHeader
Represents an HTTP request header.
  • Field Details

  • Constructor Details

    • HttpRequest

      public HttpRequest(String method, URI requestUri, HttpConstants.HttpProtocol httpProtocol, boolean hasPayload)
      Creates a new request with basic data.

      The host() and port() values are initialized with the values from the requestUri.

      Parameters:
      method - the method
      requestUri - the requested resource
      httpProtocol - the HTTP protocol version
      hasPayload - indicates that the message has a payload body
  • Method Details

    • setField

      public HttpRequest setField(HttpField<?> value)
      Description copied from class: HttpMessageHeader
      Sets a header field for the message.
      Overrides:
      setField in class HttpMessageHeader
      Parameters:
      value - the header field’s value
      Returns:
      the message header for easy chaining
    • setField

      public <T> HttpRequest setField(String name, T value)
      Description copied from class: HttpMessageHeader
      Sets a header field for the message.

      The converter for the field is looked up using HttpField.lookupConverter(String).

      Overrides:
      setField in class HttpMessageHeader
      Type Parameters:
      T - the type of the value
      Parameters:
      name - the field name
      value - the header field’s value
      Returns:
      the message header for easy chaining
    • setHasPayload

      public HttpRequest setHasPayload(boolean hasPayload)
      Description copied from class: HttpMessageHeader
      Set the flag that indicates whether this header is followed by a body.
      Overrides:
      setHasPayload in class HttpMessageHeader
      Parameters:
      hasPayload - new value
      Returns:
      the message for easy chaining
    • method

      public String method()
      Return the method.
      Returns:
      the method
    • requestUri

      public URI requestUri()
      Return the URI of the requested resource.
      Returns:
      the requestUri
    • setHostAndPort

      public HttpRequest setHostAndPort(String host, int port)
      Set the host and port attributes.
      Parameters:
      host - the host
      port - the port
      Returns:
      the request for easy chaining
    • host

      public String host()
      Host.
      Returns:
      the host
    • port

      public int port()
      Port.
      Returns:
      the port
    • setResponse

      Associates the request with a response.

      This method is invoked by the request decoder that initializes the response with basic information that can be derived from the request (e.g. by default the HTTP version is copied). The status code of the preliminary response is 501 “Not implemented”.

      Although not strictly required, users of the API are encouraged to modify this prepared request and use it when building the response.

      Parameters:
      response - the prepared response
      Returns:
      the request for easy chaining
    • response

      Returns the prepared response.
      Returns:
      the prepared response
      See Also:
    • queryData

      Returns the decoded query data from the request URI.

      The result is a lazily created (and cached) unmodifiable map.

      Parameters:
      charset - the charset to use for decoding
      Returns:
      the data
      Throws:
      UnsupportedEncodingException - the unsupported encoding exception
    • queryData

      Short for invoking queryData(Charset) with UTF-8 as charset.
      Returns:
      the map
    • replaceQuery

      public static URI replaceQuery(URI uri, String query)
      Updates the query part of an URI.
      Parameters:
      uri - the uri
      query - the query in raw form, i.e. as it should appear in the request
      Returns:
      the new URI
    • setSimpleQueryData

      Updates the query part of the request URI.
      Parameters:
      data - the data
      charset - the charset to use for encoding keys and values
      Returns:
      the http request
    • setSimpleQueryData

      Updates the query part of the request URI, using UTF-8 to encode the query keys and values.
      Parameters:
      data - the data
      Returns:
      the http request
    • setQueryData

      public HttpRequest setQueryData(Map<String,List<String>> data, Charset charset)
      Updates the query part of the request URI.
      Parameters:
      data - the data
      charset - the charset to use for encoding keys and values
      Returns:
      the http request
    • setQueryData

      Updates the query part of the request URI, using UTF-8 to encode the query keys and values.
      Parameters:
      data - the data
      Returns:
      the http request
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • simpleWwwFormUrlencode

      public static String simpleWwwFormUrlencode(Map<String,String> data, Charset charset)
      Www-form-urlencodes the given data, using the given charset to encode keys and values.
      Parameters:
      data - the data
      charset - the charset to use for encoding keys and values
      Returns:
      the string
    • simpleWwwFormUrlencode

      Www-form-urlencodes the given data, using UTF-8 to encode keys and values.
      Parameters:
      data - the data
      charset - the charset
      Returns:
      the string
    • wwwFormUrlencode

      public static String wwwFormUrlencode(Map<String,List<String>> data, Charset charset)
      Www-form-urlencodes the given data, using the given charset to encode keys and values.
      Parameters:
      data - the data
      charset - the charset to use for encoding keys and values
      Returns:
      the string
    • wwwFormUrlencode

      public static String wwwFormUrlencode(Map<String,List<String>> data)
      Www-form-urlencodes the given data, using UTF-8 to encode keys and values.
      Parameters:
      data - the data
      charset - the charset
      Returns:
      the string