Class Request

java.lang.Object
org.attribyte.api.http.Request

public final class Request extends Object
An immutable HTTP request.
  • Field Details

    • FORM_ENCODED_CONTENT_TYPE

      public static final String FORM_ENCODED_CONTENT_TYPE
      The expected value for a form encoded header: application/x-www-form-urlencoded.
      See Also:
    • REMOTE_ADDR

      public static final String REMOTE_ADDR
      The name of an attribute that may hold the remote address.
      See Also:
    • method

      public final Request.Method method
      The request method.
    • uri

      public final URI uri
      The request URI.
    • headers

      public final com.google.common.collect.ImmutableMap<String,Header> headers
      An immutable map of headers.

      Keys are lower-cased.

    • parameters

      public final com.google.common.collect.ImmutableMap<String,Parameter> parameters
      An immutable map of parameters.

      If caseSensitiveParameters == false, map keys are lower-cased.

    • attributes

      public final com.google.common.collect.ImmutableMap<String,Object> attributes
      An immutable map of attributes.
    • cookies

      public final com.google.common.collect.ImmutableList<Cookie> cookies
      An immutable list of cookies.
    • body

      public final com.google.protobuf.ByteString body
      The request body. May be null.
    • caseSensitiveParameters

      public final boolean caseSensitiveParameters
      If the request has parameters, are they case-sensitive?
  • Method Details

    • getMethod

      public Request.Method getMethod()
      Gets the HTTP method.
      Returns:
      The method.
    • getQueryString

      public String getQueryString()
      Gets the decoded query string.
      Returns:
      The decoded query string or null if none.
    • getRawQueryString

      public String getRawQueryString()
      Gets the raw query string, without decoding.
      Returns:
      The query string or null if none.
    • getRequestPath

      public String getRequestPath()
      Gets the path component of the request URI.

      Path always begins with '/' and is never null.

      Returns:
      The path.
    • getRequestPath

      public static final String getRequestPath(String uri) throws org.attribyte.api.InvalidURIException
      Gets the path component of a URI.

      Path always begins with '/' and is never null. Components are decoded.

      Parameters:
      uri - The URI.
      Returns:
      The path.
      Throws:
      org.attribyte.api.InvalidURIException - if URI is invalid.
    • getHostURL

      public static final String getHostURL(String uri) throws org.attribyte.api.InvalidURIException
      Gets a URL for the host portion of a URI without decoding.

      Includes the protocol, if specified. Never ends with '/'.

      Parameters:
      uri - The URI.
      Returns:
      The host URL.
      Throws:
      org.attribyte.api.InvalidURIException - if the URI can't be parsed.
    • getURI

      public URI getURI()
      Gets the URI.
      Returns:
      The URI.
    • getHeaderValue

      public String getHeaderValue(String name)
      Gets the first header value.
      Parameters:
      name - The header name.
      Returns:
      The value or null if none.
    • getHeaderValues

      public String[] getHeaderValues(String name)
      Gets all values for a header.
      Parameters:
      name - The header name.
      Returns:
      The values or null if none.
    • getHeaderValueList

      public com.google.common.collect.ImmutableList<String> getHeaderValueList(String name)
      Gets an immutable list of values for a header.
      Parameters:
      name - The header name.
      Returns:
      The values or null if none.
    • getHeader

      public Header getHeader(String name)
      Gets a header.
      Parameters:
      name - The header name.
      Returns:
      The header or null if none.
    • getParameterValue

      public String getParameterValue(String name)
      Gets the first parameter value.
      Parameters:
      name - The parameter name.
      Returns:
      The value or null if none.
    • getParameterValues

      public String[] getParameterValues(String name)
      Gets all values for a parameter.
      Parameters:
      name - The parameter name.
      Returns:
      The values or null if none.
    • getParameterValueList

      public com.google.common.collect.ImmutableList<String> getParameterValueList(String name)
      Gets an immutable list of values for a parameter.
      Parameters:
      name - The parameter name.
      Returns:
      The immutable list of values.
    • getHeaders

      public Collection<Header> getHeaders()
      Gets all headers.
      Returns:
      An unmodifiable collection of headers.
    • getParameters

      public Collection<Parameter> getParameters()
      Gets all parameters.
      Returns:
      An unmodifiable collection of parameters.
    • getBody

      public com.google.protobuf.ByteString getBody()
      Gets the request body, if any.
      Returns:
      The body, or null if none.
    • getContentType

      public String getContentType()
      Gets the value of the Content-Type header.
      Returns:
      The content type, or null if none.
    • getCharset

      public String getCharset(String defaultCharset)
      Gets the charset specified with the request Content-Type header, if any.
      Parameters:
      defaultCharset - The default charset to return if none is specified in the header.
      Returns:
      The charset or the default charset.
    • isFormEncoded

      public boolean isFormEncoded()
      Determine if the request is encoded as a form application/x-www-form-urlencoded
      Returns:
      Is the request encoded as a form?
    • getRemoteAddr

      public String getRemoteAddr()
      Gets the IP address of the client that sent the request.
      Returns:
      The IP address or null if unavailable.
    • getServerName

      public String getServerName()
      Gets the server name (as specified in the Host header.
      Returns:
      The server name, excluding any port, or null if unspecified.
    • getPathComponents

      public List<String> getPathComponents()
      Gets the components of the path (separated by '/').
      Returns:
      The path components, or empty list if none.
    • getPathComponents

      public static final List<String> getPathComponents(String uri) throws org.attribyte.api.InvalidURIException
      Gets the components of the path (separated by '/').
      Parameters:
      uri - The URI.
      Returns:
      The path components, or empty list if none.
      Throws:
      org.attribyte.api.InvalidURIException - on invalid URI.
    • addHeaders

      public Request addHeaders(Collection<Header> headers)
      Adds headers to the existing headers, replacing any that are duplicated.
      Parameters:
      headers - The headers to add.
      Returns:
      The request with additional headers added.
    • bodyFromInputStream

      public static final byte[] bodyFromInputStream(InputStream is, int maxBytesRead) throws IOException
      Reads the request body from an input stream.

      Reads the body up to the specified maximum number of bytes. If byte limit is exceeded, continues to read the rest of the stream, ignoring the data.

      Parameters:
      is - The input stream.
      maxBytesRead - The maximum bytes read.
      Returns:
      The body.
      Throws:
      IOException - on input exception, or data limit exceeded.
    • parseParameters

      public static final Map<String,Parameter> parseParameters(String queryString)
      Parse parameters from a query string.

      Case is ignored for all names - all are converted to lower-case.

      Parameters:
      queryString - The query string.
      Returns:
      The map of parameters.
    • parseParametersPreserveNameCase

      public static final Map<String,Parameter> parseParametersPreserveNameCase(String queryString)
      Parse parameters from a query string, preserving case in parameter names.
      Parameters:
      queryString - The query string.
      Returns:
      The map of parameters.
    • parseParameters

      public static final Map<String,Parameter> parseParameters(String queryString, boolean caseSensitiveNames)
      Parse parameters from a query string.

      Query string should already be unescaped.

      Parameters:
      queryString - The query string.
      caseSensitiveNames - Should case be preserved for parameter names?
      Returns:
      The map of parameters.
    • toString

      public String toString()
      Overrides:
      toString in class Object