Package rife.engine

Class SiteRequest

java.lang.Object
rife.engine.SiteRequest
All Implemented Interfaces:
Request
Direct Known Subclasses:
MockRequest

public class SiteRequest extends Object implements Request
A request that your application passes to a site through a SiteRunner, in the same process and without a servlet container.

You provide the method, the headers and the body, while the path and the query string are given to SiteRunner.handle(String, SiteRequest). RIFE2 will then parse the parameters from the query string and from application/x-www-form-urlencoded and multipart/form-data bodies. The cookies are taken from the Cookie header and the locales from the Accept-Language header.

A request can only be handled once.

Since:
1.11.0
See Also:
  • Constructor Details

    • SiteRequest

      public SiteRequest()
      Creates a new GET request.
      Since:
      1.11.0
    • SiteRequest

      public SiteRequest(RequestMethod method)
      Creates a new request with a particular method.
      Parameters:
      method - the method of the request
      Since:
      1.11.0
  • Method Details

    • createResponse

      protected SiteResponse createResponse()
      Creates the response of this request. Extending classes can override this to provide their own type of response.
      Returns:
      a new response
      Since:
      1.11.0
    • parseParameters

      protected void parseParameters(String queryString)
      Parses the parameters of this request from the query string and from the body.

      The values of the query string come first. Urlencoded bodies are read for POST, PUT and PATCH, while multipart bodies are read for any method. A request with a malformed escape, or with a form beyond the limits of the runner, is answered with 400.

      Parameters:
      queryString - the raw query string, or null when there's none
      Since:
      1.11.0
    • cancel

      public void cancel()
      Cancels this request, for instance because the web view stopped loading it. This method can be called from any thread.

      No new call to the listener of the response will start, anything that the element still writes will fail like it does on a closed connection, and a detached event stream will be closed. The listener is told with SiteResponseListener.cancelled() once no call of it is under way anymore.

      Since:
      1.11.0
      See Also:
    • isCancelled

      public boolean isCancelled()
      Indicates whether this request has been cancelled.
      Returns:
      true if the request was cancelled; or

      false otherwise

      Since:
      1.11.0
      See Also:
    • getMethod

      public RequestMethod getMethod()
      Description copied from interface: Request
      Specified by:
      getMethod in interface Request
    • setMethod

      public void setMethod(RequestMethod method)
      Sets the method of this request.

      The method defaults to RequestMethod.GET.

      Parameters:
      method - the method that will be used by this request
      Since:
      1.11.0
      See Also:
    • method

      public SiteRequest method(RequestMethod method)
      Sets the method of this request.
      Parameters:
      method - the method that will be used by this request
      Returns:
      this SiteRequest instance
      Since:
      1.11.0
      See Also:
    • getParameters

      public Map<String,String[]> getParameters()
      Description copied from interface: Request
      Retrieves the parameters that were sent in this request.
      Specified by:
      getParameters in interface Request
      Returns:
      a Map with all the parameter names and values
    • setUploadedFiles

      protected void setUploadedFiles(Map<String,UploadedFile[]> files)
      Sets the uploaded files of this request, for extending classes that provide them themselves.
      Parameters:
      files - the uploaded files by parameter name, or null when there are none
      Since:
      1.11.0
    • setBody

      public void setBody(String body)
      Sets this request's body as a string, which will be converted to bytes with the character encoding of this request when the body is read.
      Parameters:
      body - the body as a string
      Since:
      1.11.0
      See Also:
    • body

      public SiteRequest body(String body)
      Sets this request's body as a string.
      Parameters:
      body - the body as a string
      Returns:
      this SiteRequest instance
      Since:
      1.11.0
      See Also:
    • getBody

      public String getBody()
      Description copied from interface: Request
      Retrieves the body of this request as a string.
      Specified by:
      getBody in interface Request
      Returns:
      the string of the request body
      See Also:
    • setBodyAsBytes

      public void setBodyAsBytes(byte[] bytes)
      Sets this request's body as bytes.
      Parameters:
      bytes - the body as bytes
      Since:
      1.11.0
      See Also:
    • bodyAsBytes

      public SiteRequest bodyAsBytes(byte[] bytes)
      Sets this request's body as bytes.
      Parameters:
      bytes - the body as bytes
      Returns:
      this SiteRequest instance
      Since:
      1.11.0
      See Also:
    • setBodyAsStream

      public void setBodyAsStream(InputStream stream)
      Sets the body of this request as a stream, which will be read when the body is needed. A multipart body is read from the stream as it arrives, so that it isn't kept in memory.
      Parameters:
      stream - the stream with the body
      Since:
      1.11.0
      See Also:
    • bodyAsStream

      public SiteRequest bodyAsStream(InputStream stream)
      Sets the body of this request as a stream.
      Parameters:
      stream - the stream with the body
      Returns:
      this SiteRequest instance
      Since:
      1.11.0
      See Also:
    • getBodyAsBytes

      public byte[] getBodyAsBytes()
      Description copied from interface: Request
      Retrieves the body of this request as a byte array.
      Specified by:
      getBodyAsBytes in interface Request
      Returns:
      the byte array of the request body
      See Also:
    • getFiles

      public Map<String,UploadedFile[]> getFiles()
      Description copied from interface: Request
      Retrieves the files that were uploaded in this request.
      Specified by:
      getFiles in interface Request
      Returns:
      a Map with all the uploaded files
      See Also:
    • hasFile

      public boolean hasFile(String name)
      Description copied from interface: Request
      Checks if a particular file has been uploaded in this request.
      Specified by:
      hasFile in interface Request
      Parameters:
      name - the name of the file
      Returns:
      true if the file was uploaded; or

      false otherwise

      See Also:
    • getFile

      public UploadedFile getFile(String name)
      Description copied from interface: Request
      Retrieves an uploaded file.
      Specified by:
      getFile in interface Request
      Parameters:
      name - the name of the file
      Returns:
      the uploaded file; or

      null if no file was uploaded

      See Also:
    • getFiles

      public UploadedFile[] getFiles(String name)
      Description copied from interface: Request
      Retrieves all files that have been uploaded for a particular name.
      Specified by:
      getFiles in interface Request
      Parameters:
      name - the name of the file
      Returns:
      the uploaded files; or

      null if no files were uploaded for that name

      See Also:
    • getServerRootUrl

      public String getServerRootUrl(int port)
      Description copied from interface: Request
      Returns the root URL of the server that is running this web applications.

      This includes the protocol, the server name and the server port, for example: http://www.somehost.com:8080.

      Specified by:
      getServerRootUrl in interface Request
      Returns:
      the server's root url
    • hasCookie

      public boolean hasCookie(String name)
      Description copied from interface: Request
      Checks whether a cookie is present.
      Specified by:
      hasCookie in interface Request
      Parameters:
      name - the name of the cookie
      Returns:
      true if the cookie was present; or

      false otherwise

      See Also:
    • getCookie

      public Cookie getCookie(String name)
      Description copied from interface: Request
      Retrieves a cookie.
      Specified by:
      getCookie in interface Request
      Parameters:
      name - the name of the cookie.
      Returns:
      the instance of the cookie; or

      null if no such cookie is present

      See Also:
    • getCookies

      public Cookie[] getCookies()
      Description copied from interface: Request
      Specified by:
      getCookies in interface Request
    • getAttribute

      public Object getAttribute(String name)
      Description copied from interface: Request
      Specified by:
      getAttribute in interface Request
    • hasAttribute

      public boolean hasAttribute(String name)
      Description copied from interface: Request
      Checks if a request attribute exists.
      Specified by:
      hasAttribute in interface Request
      Parameters:
      name - a String specifying the name of the attribute

      false otherwise

    • getAttributeNames

      public Enumeration<String> getAttributeNames()
      Description copied from interface: Request
      Specified by:
      getAttributeNames in interface Request
    • removeAttribute

      public void removeAttribute(String name)
      Description copied from interface: Request
      Specified by:
      removeAttribute in interface Request
    • setAttribute

      public void setAttribute(String name, Object object)
      Description copied from interface: Request
      Specified by:
      setAttribute in interface Request
    • getCharacterEncoding

      public String getCharacterEncoding()
      Retrieves the character encoding of this request.

      When none was set, this is the request encoding of the engine, which is what a deployed site uses too.

      Specified by:
      getCharacterEncoding in interface Request
      Returns:
      the character encoding
      Since:
      1.11.0
    • setCharacterEncoding

      public void setCharacterEncoding(String encoding)
      Sets the character encoding of this request.
      Parameters:
      encoding - the name of the character encoding
      Since:
      1.11.0
      See Also:
    • characterEncoding

      public SiteRequest characterEncoding(String encoding)
      Sets the character encoding of this request.
      Parameters:
      encoding - the name of the character encoding
      Returns:
      this SiteRequest instance
      Since:
      1.11.0
      See Also:
    • getContentType

      public String getContentType()
      Retrieves the content type of this request, which is its Content-Type header.
      Specified by:
      getContentType in interface Request
      Returns:
      the content type; or

      null if there's none

      Since:
      1.11.0
    • setContentType

      public void setContentType(String type)
      Sets the content type of this request.
      Parameters:
      type - the content type
      Since:
      1.11.0
      See Also:
    • contentType

      public SiteRequest contentType(String type)
      Sets the content type of this request.
      Parameters:
      type - the content type
      Returns:
      this SiteRequest instance
      Since:
      1.11.0
      See Also:
    • getDateHeader

      public long getDateHeader(String name)
      Description copied from interface: Request
      Specified by:
      getDateHeader in interface Request
    • getHeader

      public String getHeader(String name)
      Description copied from interface: Request
      Specified by:
      getHeader in interface Request
    • getHeaderNames

      public Enumeration<String> getHeaderNames()
      Description copied from interface: Request
      Specified by:
      getHeaderNames in interface Request
    • getHeaders

      public Enumeration<String> getHeaders(String name)
      Description copied from interface: Request
      Specified by:
      getHeaders in interface Request
    • getIntHeader

      public int getIntHeader(String name)
      Description copied from interface: Request
      Specified by:
      getIntHeader in interface Request
    • addHeader

      public SiteRequest addHeader(String name, String value)
      Adds a request header with the given name and value. This method allows request headers to have multiple values. The header name is case-insensitive.
      Parameters:
      name - the name of the header to set
      value - the additional header value
      Returns:
      this SiteRequest instance
      Since:
      1.11.0
    • addDateHeader

      public SiteRequest addDateHeader(String name, long value)
      Adds a request header with the given name and date-value. The date is specified in terms of milliseconds since the epoch. This method allows request headers to have multiple values.
      Parameters:
      name - the name of the header to set
      value - the additional date value
      Returns:
      this SiteRequest instance
      Since:
      1.11.0
    • addIntHeader

      public SiteRequest addIntHeader(String name, int value)
      Adds a request header with the given name and integer value. This method allows request headers to have multiple values.
      Parameters:
      name - the name of the header to set
      value - the additional integer value
      Returns:
      this SiteRequest instance
      Since:
      1.11.0
    • containsHeader

      public boolean containsHeader(String name)
      Checks whether a certain request header is present.
      Parameters:
      name - the name of the header to check
      Returns:
      true if the header was present; or

      false otherwise

      Since:
      1.11.0
    • setDateHeader

      public void setDateHeader(String name, long value)
      Sets a request header with the given name and date-value. The date is specified in terms of milliseconds since the epoch. If the header had already been set, the new value overwrites the previous one. The containsHeader(java.lang.String) method can be used to test for the presence of a header before setting its value.
      Parameters:
      name - the name of the header to set
      value - the assigned date value
      Since:
      1.11.0
      See Also:
    • dateHeader

      public SiteRequest dateHeader(String name, long value)
      Sets a request header with the given name and date-value.
      Parameters:
      name - the name of the header to set
      value - the assigned date value
      Returns:
      this SiteRequest instance
      Since:
      1.11.0
      See Also:
    • setHeader

      public void setHeader(String name, String value)
      Sets a request header with the given name and value. If the header had already been set, the new value overwrites the previous one. The containsHeader(java.lang.String) method can be used to test for the presence of a header before setting its value.
      Parameters:
      name - the name of the header to set
      value - the header value
      Since:
      1.11.0
      See Also:
    • header

      public SiteRequest header(String name, String value)
      Sets a request header with the given name and value.
      Parameters:
      name - the name of the header to set
      value - the header value
      Returns:
      this SiteRequest instance
      Since:
      1.11.0
      See Also:
    • setIntHeader

      public void setIntHeader(String name, int value)
      Sets a request header with the given name and integer value. If the header had already been set, the new value overwrites the previous one. The containsHeader(java.lang.String) method can be used to test for the presence of a header before setting its value.
      Parameters:
      name - the name of the header to set
      value - the assigned integer value
      Since:
      1.11.0
      See Also:
    • intHeader

      public SiteRequest intHeader(String name, int value)
      Sets a request header with the given name and integer value.
      Parameters:
      name - the name of the header to set
      value - the assigned integer value
      Returns:
      this SiteRequest instance
      Since:
      1.11.0
      See Also:
    • removeHeader

      public void removeHeader(String name)
      Removes a request header with the given name.
      Parameters:
      name - the name of the header to remove
      Since:
      1.11.0
    • getLocale

      public Locale getLocale()
      Retrieves the preferred locale of this request, from the locales that were added or otherwise from the Accept-Language header.
      Specified by:
      getLocale in interface Request
      Returns:
      the preferred locale; or

      the default locale if there's none

      Since:
      1.11.0
    • getLocales

      public Enumeration<Locale> getLocales()
      Description copied from interface: Request
      Specified by:
      getLocales in interface Request
    • addLocale

      public void addLocale(Locale locale)
      Adds a Locale to this request, which then takes precedence over the Accept-Language header.
      Parameters:
      locale - the locale to add
      Since:
      1.11.0
      See Also:
    • locale

      public SiteRequest locale(Locale locale)
      Adds a Locale to this request.
      Parameters:
      locale - the locale to add
      Returns:
      this SiteRequest instance
      Since:
      1.11.0
      See Also:
    • getProtocol

      public String getProtocol()
      Description copied from interface: Request
      Specified by:
      getProtocol in interface Request
    • setProtocol

      public void setProtocol(String protocol)
      Sets the protocol of this request.

      The default protocol is "HTTP/1.1".

      Parameters:
      protocol - the protocol to set
      Since:
      1.11.0
      See Also:
    • protocol

      public SiteRequest protocol(String protocol)
      Sets the protocol of this request.
      Parameters:
      protocol - the protocol to set
      Returns:
      this SiteRequest instance
      Since:
      1.11.0
      See Also:
    • getRemoteAddr

      public String getRemoteAddr()
      Description copied from interface: Request
      Specified by:
      getRemoteAddr in interface Request
    • setRemoteAddr

      public void setRemoteAddr(String remoteAddr)
      Sets the remote address of this request.

      The default remote address is "127.0.0.1".

      Parameters:
      remoteAddr - the remote address to set
      Since:
      1.11.0
      See Also:
    • remoteAddr

      public SiteRequest remoteAddr(String remoteAddr)
      Sets the remote address of this request.
      Parameters:
      remoteAddr - the remote address to set
      Returns:
      this SiteRequest instance
      Since:
      1.11.0
      See Also:
    • getRemoteUser

      public String getRemoteUser()
      Description copied from interface: Request
      Specified by:
      getRemoteUser in interface Request
    • setRemoteUser

      public void setRemoteUser(String remoteUser)
      Sets the remote user of this request.

      The default remote user is null.

      Parameters:
      remoteUser - the remote user to set
      Since:
      1.11.0
      See Also:
    • remoteUser

      public SiteRequest remoteUser(String remoteUser)
      Sets the remote user of this request.
      Parameters:
      remoteUser - the remote user to set
      Returns:
      this SiteRequest instance
      Since:
      1.11.0
      See Also:
    • getRemoteHost

      public String getRemoteHost()
      Description copied from interface: Request
      Specified by:
      getRemoteHost in interface Request
    • setRemoteHost

      public void setRemoteHost(String remoteHost)
      Sets the remote host of this request.

      The default remote host is "localhost".

      Parameters:
      remoteHost - the remote host to set
      Since:
      1.11.0
      See Also:
    • remoteHost

      public SiteRequest remoteHost(String remoteHost)
      Sets the remote host of this request.
      Parameters:
      remoteHost - the remote host to set
      Returns:
      this SiteRequest instance
      Since:
      1.11.0
      See Also:
    • getRequestDispatcher

      public RequestDispatcher getRequestDispatcher(String url)
      Description copied from interface: Request
      Specified by:
      getRequestDispatcher in interface Request
    • getSession

      public HttpSession getSession(boolean create)
      Description copied from interface: Request
      Specified by:
      getSession in interface Request
    • getSession

      public HttpSession getSession()
      Description copied from interface: Request
      Specified by:
      getSession in interface Request
    • getServerPort

      public int getServerPort()
      Description copied from interface: Request
      Specified by:
      getServerPort in interface Request
    • getScheme

      public String getScheme()
      Description copied from interface: Request
      Specified by:
      getScheme in interface Request
    • getServerName

      public String getServerName()
      Description copied from interface: Request
      Specified by:
      getServerName in interface Request
    • getContextPath

      public String getContextPath()
      Description copied from interface: Request
      Specified by:
      getContextPath in interface Request
    • isSecure

      public boolean isSecure()
      Indicates whether this request is secure. By default, this is the case when its scheme is https or when the site runner was configured to be secure.
      Specified by:
      isSecure in interface Request
      Returns:
      true if the request is secure; or

      false otherwise

      Since:
      1.11.0
      See Also:
    • setSecure

      public void setSecure(boolean secure)
      Sets whether this request is secure.

      By default, a request is secure when its scheme is https or when the site runner was configured to be secure.

      Parameters:
      secure - true if this request is secure; or

      false otherwise

      Since:
      1.11.0
      See Also:
    • secure

      public SiteRequest secure(boolean secure)
      Sets whether this request is secure.
      Parameters:
      secure - true if this request is secure; or

      false otherwise

      Returns:
      this SiteRequest instance
      Since:
      1.11.0
      See Also:
    • getHttpServletRequest

      public HttpServletRequest getHttpServletRequest()
      Retrieves the underlying servlet request, which doesn't exist when a site is run by a SiteRunner.
      Specified by:
      getHttpServletRequest in interface Request
      Returns:
      null
      Since:
      1.11.0