Class Response

java.lang.Object
com.yahoo.jdisc.Response

public class Response extends Object

This class represents the single response (which may have any content model that a RequestHandler chooses to implement) of some single request. Contrary to the Request class, this has no active reference to the parent Container (this is tracked internally by counting the number of requests vs the number of responses seen). The ResponseHandler of a Response is implicit in the invocation of RequestHandler.handleRequest(Request, ResponseHandler).

The usage pattern of the Response is similar to that of the Request in that the ResponseHandler returns a ContentChannel into which to write the Response content.

Author:
Simon Thoresen Hult
See Also:
  • Constructor Details

    • Response

      public Response(int status)
      Creates a new instance of this class.
      Parameters:
      status - The status code to assign to this.
    • Response

      public Response(int status, Throwable error)
      Creates a new instance of this class.
      Parameters:
      status - The status code to assign to this.
      error - The error to assign to this.
  • Method Details

    • context

      public Map<String,Object> context()

      Returns the named application context objects. This data is not intended for network transport, rather they are intended for passing shared data between components of an Application.

      Modifying the context map is a thread-unsafe operation -- any changes made after calling ResponseHandler.handleResponse(Response) might never become visible to other threads, and might throw ConcurrentModificationExceptions in other threads.

      Returns:
      The context map.
    • headers

      public HeaderFields headers()

      Returns the set of header fields of this Request. These are the meta-data of the Request, and are not applied to any internal Container logic. Modifying headers is a thread-unsafe operation -- any changes made after calling ResponseHandler.handleResponse(Response) might never become visible to other threads, and might throw ConcurrentModificationExceptions in other threads.

      Returns:
      The header fields.
    • getStatus

      public int getStatus()

      Returns the status code of this response. This is an integer result code of the attempt to understand and satisfy the corresponding Request. It is encouraged, although not enforced, to use the built-in Response.Status codes whenever possible.

      Returns:
      The status code.
      See Also:
    • setStatus

      public Response setStatus(int status)

      Sets the status code of this response. This is an integer result code of the attempt to understand and satisfy the corresponding Request. It is encouraged, although not enforced, to use the built-in Response.Status codes whenever possible.

      Because access to this field is not guarded by any lock, any changes made after calling ResponseHandler.handleResponse(Response) might never become visible to other threads.

      Parameters:
      status - The status code to set.
      Returns:
      This, to allow chaining.
      See Also:
    • getError

      public Throwable getError()

      Returns the error of this response, or null if none has been set. This is typically non-null if the status indicates an unsuccessful response.

      Returns:
      The error.
      See Also:
    • setError

      public Response setError(Throwable error)

      Sets the error of this response. It is encouraged, although not enforced, to use this field to attach additional information to an unsuccessful response.

      Because access to this field is not guarded by any lock, any changes made after calling ResponseHandler.handleResponse(Response) might never become visible to other threads.

      Parameters:
      error - The error to set.
      Returns:
      This, to allow chaining.
      See Also:
    • setRequestType

      public void setRequestType(Request.RequestType requestType)
      Sets the type classification of this request for metric collection purposes
    • getRequestType

      public Request.RequestType getRequestType()
      Returns the type classification of this request for metric collection purposes, or null if not set
    • dispatchTimeout

      public static void dispatchTimeout(ResponseHandler handler)
      This is a convenience method for creating a Response with status Response.Status.GATEWAY_TIMEOUT and passing that to the given ResponseHandler.handleResponse(Response). For trivial implementations of RequestHandler.handleTimeout(Request, ResponseHandler), simply call this method.
      Parameters:
      handler - The handler to pass the timeout Response to.