Package rife.engine

Class SiteResponse

All Implemented Interfaces:
Response
Direct Known Subclasses:
MockResponse

public class SiteResponse extends AbstractResponse
The response of a site to a SiteRequest.

Without a listener, the content is kept in memory, and you can get it with getBytes() and getText() once the request has been handled. With a SiteResponseListener, the content is given to the listener while it's being written, so that it isn't kept.

getHttpHeaders() provides the headers as they would go over HTTP, with a Set-Cookie header for each cookie.

Since:
1.11.0
See Also:
  • Constructor Details

    • SiteResponse

      protected SiteResponse(SiteRequest request)
      Creates the response of a request.
      Parameters:
      request - the request of the response
      Since:
      1.11.0
  • Method Details

    • isHandled

      public boolean isHandled()
      Indicates whether the request was handled by the site or by its static files.

      When no route handled the request and no static file was found, the status is 404 and this is false, so that your application can still answer the request in its own way. When an element sends a 404 itself, this remains true.

      Returns:
      true if the request was handled; or

      false otherwise

      Since:
      1.11.0
    • isFinished

      public boolean isFinished()
      Indicates whether this response has ended, either normally, with a failure, or because its request was cancelled.
      Returns:
      true if the response has ended; or

      false otherwise

      Since:
      1.11.0
    • isDetached

      public boolean isDetached()
      Indicates whether this response is a detached event stream, which stays open after its element returned.
      Returns:
      true if the response is a detached event stream; or

      false otherwise

      Since:
      1.11.0
    • getBytes

      public byte[] getBytes()
      Retrieves an array of all the bytes that have been written to this response.

      For a HEAD request this is empty, since a servlet container doesn't send the content of the response. It's also empty when the response is given to a listener, since the content isn't kept then.

      Returns:
      an array of bytes with the response content
      Since:
      1.11.0
      See Also:
    • getText

      public String getText()
      Retrieves the content of this response as text, with the character encoding of the response.
      Returns:
      the response content as text
      Since:
      1.11.0
      See Also:
    • getContentType

      public String getContentType()
      Retrieves the content type of the response, as it's set in the Content-Type header.
      Specified by:
      getContentType in interface Response
      Overrides:
      getContentType in class AbstractResponse
      Returns:
      the content type; or

      null if it wasn't set

      Since:
      1.11.0
      See Also:
    • _setContentType

      protected void _setContentType(String contentType)
      Description copied from class: AbstractResponse
      This method needs to be implemented by the extending back-end class and will be called by AbstractResponse during the RIFE2-specific additional behaviour. It behaves exactly like its counter-part in the Response interface.
      Specified by:
      _setContentType in class AbstractResponse
      See Also:
    • getCharacterEncoding

      public String getCharacterEncoding()
      Description copied from interface: Response
    • setContentLength

      public void setContentLength(int length)
      Description copied from interface: Response
    • getContentLength

      public int getContentLength()
      Retrieves the content length that was set.
      Returns:
      the content length; or

      -1 if it wasn't set

      Since:
      1.11.0
    • sendRedirect

      public void sendRedirect(String location)
      Description copied from interface: Response
    • _getOutputStream

      protected OutputStream _getOutputStream()
      Description copied from class: AbstractResponse
      This method needs to be implemented by the extending back-end class and will be called by AbstractResponse during the RIFE2-specific additional behaviour. It behaves exactly like its counter-part in the Request interface.
      Specified by:
      _getOutputStream in class AbstractResponse
      See Also:
    • addCookie

      public void addCookie(Cookie cookie)
      Description copied from interface: Response
    • getNewCookies

      public List<Cookie> getNewCookies()
      Retrieves the cookies that were added to this response. A later cookie with the same name, domain and path replaces an earlier one.
      Returns:
      the cookies of the response
      Since:
      1.11.0
    • getHttpHeaders

      public Map<String,List<String>> getHttpHeaders()
      Retrieves the headers of this response as they would go over HTTP, with a Set-Cookie header for each cookie. The header names are case-insensitive.

      When there's no listener, the Content-Length header is also provided once the response is finished.

      Returns:
      the names of the headers with their values
      Since:
      1.11.0
    • getSingleValueHttpHeaders

      public Map<String,String> getSingleValueHttpHeaders()
      Retrieves the headers of this response with one value for each name, which is what the responses of web views on iOS and Android take. The values of a header are joined with a comma, like HTTP allows, and the header names are case-insensitive.

      The Set-Cookie headers are left out, since they can't be joined that way. A web view app keeps its cookies with a SiteCookieJar instead.

      Returns:
      the names of the headers with their value
      Since:
      1.11.0
      See Also:
    • getDateHeader

      public long getDateHeader(String name)
      Returns the value of the specified response header as a long value that represents a Date object. Use this method with headers that contain dates.

      The date is returned as the number of milliseconds since January 1, 1970 GMT. The header name is case-insensitive.

      If the response did not have a header of the specified name, this method returns -1. If the header can't be converted to a date, the method throws an IllegalArgumentException.

      Parameters:
      name - the name of the header
      Returns:
      a long value representing the date specified in the header expressed as the number of milliseconds since January 1, 1970 GMT; or

      -1 if the named header was not included with the response

      Throws:
      IllegalArgumentException - if the header value can't be converted to a date
      Since:
      1.11.0
    • getHeader

      public String getHeader(String name)
      Returns the value of the specified response header as a String. If the response did not include a header of the specified name, this method returns null. The header name is case-insensitive. You can use this method with any response header.
      Parameters:
      name - the name of the header
      Returns:
      a String containing the value of the response header; or

      null if the response does not have a header of that name

      Since:
      1.11.0
    • getHeaderNames

      public Collection<String> getHeaderNames()
      Retrieves the names of the headers that were set, without the cookies.
      Returns:
      the header names
      Since:
      1.11.0
      See Also:
    • getHeaders

      public Collection<String> getHeaders(String name)
      Returns all the values of the specified response header as a Collection of String objects.

      If the response did not include any headers of the specified name, this method returns an empty Collection. The header name is case-insensitive.

      Parameters:
      name - the name of the header
      Returns:
      a Collection containing the values of the response header; or

      if the response does not have any headers of that name return an empty Collection

      Since:
      1.11.0
    • getIntHeader

      public int getIntHeader(String name)
      Returns the value of the specified response header as an int. If the response does not have a header of the specified name, this method returns -1.

      The header name is case-insensitive.

      Parameters:
      name - the name of the header
      Returns:
      an integer expressing the value of the response header; or

      -1 if the response doesn't have a header of this name

      Throws:
      IllegalArgumentException - if the header value can't be converted to an int
      Since:
      1.11.0
    • addHeader

      public void addHeader(String name, String value)
      Description copied from interface: Response
    • addDateHeader

      public void addDateHeader(String name, long date)
      Description copied from interface: Response
    • addIntHeader

      public void addIntHeader(String name, int integer)
      Description copied from interface: Response
    • containsHeader

      public boolean containsHeader(String name)
      Description copied from interface: Response
    • setDateHeader

      public void setDateHeader(String name, long date)
      Description copied from interface: Response
    • setHeader

      public void setHeader(String name, String value)
      Description copied from interface: Response
    • setIntHeader

      public void setIntHeader(String name, int value)
      Description copied from interface: Response
    • removeHeader

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

      public int getStatus()
      Returns the status code of this response.
      Returns:
      an integer expressing the status code of this response
      Since:
      1.11.0
    • getReason

      public String getReason()
      Returns the error reason of this response, as it was given with sendError(int, String).
      Returns:
      a String expressing the reason of this response error; or

      null if there was none

      Since:
      1.11.0
    • setStatus

      public void setStatus(int statusCode)
      Description copied from interface: Response
    • sendError

      public void sendError(int statusCode) throws EngineException
      Description copied from interface: Response
      Throws:
      EngineException
    • sendError

      public void sendError(int statusCode, String message) throws EngineException
      Description copied from interface: Response
      Throws:
      EngineException
    • encodeURL

      public String encodeURL(String url)
      Description copied from interface: Response
    • setLocale

      public void setLocale(Locale locale)
      Description copied from interface: Response
    • getLocale

      public Locale getLocale()
      Description copied from interface: Response
    • getWriter

      public PrintWriter getWriter() throws IOException
      Description copied from interface: Response
      Throws:
      IOException
    • getHttpServletResponse

      public HttpServletResponse getHttpServletResponse()
      Retrieves the underlying servlet response, which doesn't exist when a site is run by a SiteRunner.
      Returns:
      null
      Since:
      1.11.0