Class HTTPRequest

java.lang.Object
io.fusionauth.http.server.HTTPRequest
All Implemented Interfaces:
Buildable<HTTPRequest>

public class HTTPRequest extends Object implements Buildable<HTTPRequest>
An HTTP request that is received by the HTTP server. This contains all the relevant information from the request including any file uploads and the InputStream that the server can read from to handle the HTTP body.

This is mutable because the server is not trying to enforce that the request is always the same as the one it received. There are many cases where requests values are mutated, removed, or replaced. Rather than using a janky delegate or wrapper, this is simply mutable.

Author:
Brian Pontarelli
  • Constructor Details

    • HTTPRequest

      public HTTPRequest()
    • HTTPRequest

      public HTTPRequest(String contextPath, int multipartBufferSize, String scheme, int port, String ipAddress)
  • Method Details

    • addAcceptEncoding

      public void addAcceptEncoding(String encoding)
    • addAcceptEncodings

      public void addAcceptEncodings(List<String> encodings)
    • addCookies

      public void addCookies(Cookie... cookies)
    • addCookies

      public void addCookies(Collection<Cookie> cookies)
    • addHeader

      public void addHeader(String name, String value)
    • addHeaders

      public void addHeaders(String name, String... values)
    • addHeaders

      public void addHeaders(String name, Collection<String> values)
    • addHeaders

      public void addHeaders(Map<String,List<String>> params)
    • addLocales

      public void addLocales(Locale... locales)
    • addLocales

      public void addLocales(Collection<Locale> locales)
    • addURLParameter

      public void addURLParameter(String name, String value)
    • addURLParameters

      public void addURLParameters(String name, String... values)
    • addURLParameters

      public void addURLParameters(String name, Collection<String> values)
    • addURLParameters

      public void addURLParameters(Map<String,List<String>> params)
    • deleteCookie

      public void deleteCookie(String name)
    • getAcceptEncodings

      public List<String> getAcceptEncodings()
    • setAcceptEncodings

      public void setAcceptEncodings(List<String> encodings)
    • getAttribute

      public Object getAttribute(String name)
      Retrieves a request attribute.
      Parameters:
      name - The name of the attribute.
      Returns:
      The attribute or null if it doesn't exist.
    • getAttributes

      public Map<String,Object> getAttributes()
      Retrieves all the request attributes. This returns the direct Map so changes to the Map will affect all attributes.
      Returns:
      The attribute Map.
    • getBaseURL

      public String getBaseURL()
    • getBodyBytes

      public byte[] getBodyBytes() throws BodyException
      Throws:
      BodyException
    • getCharacterEncoding

      public Charset getCharacterEncoding()
    • setCharacterEncoding

      public void setCharacterEncoding(Charset encoding)
    • getContentLength

      public Long getContentLength()
    • setContentLength

      public void setContentLength(Long contentLength)
    • getContentType

      public String getContentType()
    • setContentType

      public void setContentType(String contentType)
    • getContextPath

      public String getContextPath()
    • setContextPath

      public void setContextPath(String contextPath)
    • getCookie

      public Cookie getCookie(String name)
    • getCookies

      public List<Cookie> getCookies()
    • getDateHeader

      public Instant getDateHeader(String name)
    • getFiles

      public List<FileInfo> getFiles()
      Processes the HTTP request body completely by calling getFormData(). If the Content-Type header is multipart, then the processing of the body will extract the files.
      Returns:
      The files, if any.
    • getFormData

      public Map<String,List<String>> getFormData()
      Processes the HTTP request body completely if the Content-Type header is equal to HTTPValues.ContentTypes.Form. If this method is called multiple times, the body is only processed the first time. This is not thread-safe, so you need to ensure you protect against multiple threads calling this method concurrently.

      If the Content-Type is not HTTPValues.ContentTypes.Form, this will always return an empty Map.

      If the InputStream is not ready or complete, this will block until all the bytes are read from the client.

      Returns:
      The Form data body.
    • getHeader

      public String getHeader(String name)
    • getHeaders

      public List<String> getHeaders(String name)
    • getHeaders

      public Map<String,List<String>> getHeaders()
    • setHeaders

      public void setHeaders(Map<String,List<String>> parameters)
    • getHost

      public String getHost()
    • setHost

      public void setHost(String host)
    • getIPAddress

      public String getIPAddress()
    • setIPAddress

      public void setIPAddress(String ipAddress)
    • getInputStream

      public InputStream getInputStream()
    • setInputStream

      public void setInputStream(InputStream inputStream)
    • getLocale

      public Locale getLocale()
    • getLocales

      public List<Locale> getLocales()
    • getMethod

      public HTTPMethod getMethod()
    • setMethod

      public void setMethod(HTTPMethod method)
    • getMultipartBoundary

      public String getMultipartBoundary()
    • getParameter

      public String getParameter(String name)
      Calls getParameters() to combine everything and then returns the first parameter value for the given name.
      Parameters:
      name - The name of the parameter
      Returns:
      The parameter values or null if the parameter doesn't exist.
    • getParameters

      public Map<String,List<String>> getParameters()
      Combines the URL parameters and the form data that might exist in the body of the HTTP request. The Map returned is not linked back to the URL parameters or form data. Changing it will not impact either of those Maps. If this method is called multiple times, the merging of all the data is only done the first time and then cached. This is not thread-safe, so you need to ensure you protect against multiple threads calling this method concurrently.
      Returns:
      The combined parameters.
    • getParameters

      public List<String> getParameters(String name)
      Calls getParameters() to combine everything and then returns the parameters for the given name.
      Parameters:
      name - The name of the parameter
      Returns:
      The parameter values or null if the parameter doesn't exist.
    • getPath

      public String getPath()
    • setPath

      public void setPath(String path)
    • getPort

      public int getPort()
    • setPort

      public void setPort(int port)
    • getProtocol

      public String getProtocol()
    • setProtocol

      public void setProtocol(String protocol)
    • getQueryString

      public String getQueryString()
    • getRawHost

      public String getRawHost()
    • getRawIPAddress

      public String getRawIPAddress()
    • getRawPort

      public int getRawPort()
    • getRawScheme

      public String getRawScheme()
    • getScheme

      public String getScheme()
    • setScheme

      public void setScheme(String scheme)
    • getTransferEncoding

      public String getTransferEncoding()
    • getURLParameter

      public String getURLParameter(String name)
    • getURLParameters

      public List<String> getURLParameters(String name)
    • getURLParameters

      public Map<String,List<String>> getURLParameters()
    • setURLParameters

      public void setURLParameters(Map<String,List<String>> parameters)
    • hasBody

      public boolean hasBody()
      Returns:
      True if the request can reasonably be assumed to have a body. This uses the fact that the request is chunked or that Content-Length header was provided.
    • isChunked

      public boolean isChunked()
    • isKeepAlive

      public boolean isKeepAlive()
      Determines if the request is asking for the server to keep the connection alive. This is based on the Connection header.
      Returns:
      True if the Connection header is missing or not `Close`.
    • isMultipart

      public boolean isMultipart()
    • removeAttribute

      public Object removeAttribute(String name)
      Removes a request attribute.
      Parameters:
      name - The name of the attribute.
      Returns:
      The attribute if it exists.
    • removeHeader

      public void removeHeader(String name)
    • removeHeader

      public void removeHeader(String name, String... values)
    • setAttribute

      public void setAttribute(String name, Object value)
      Sets a request attribute.
      Parameters:
      name - The name to store the attribute under.
      value - The attribute value.
    • setHeader

      public void setHeader(String name, String value)
    • setHeaders

      public void setHeaders(String name, String... values)
    • setHeaders

      public void setHeaders(String name, Collection<String> values)
    • setURLParameter

      public void setURLParameter(String name, String value)
    • setURLParameters

      public void setURLParameters(String name, String... values)
    • setURLParameters

      public void setURLParameters(String name, Collection<String> values)