Class Response

java.lang.Object
org.attribyte.api.http.Response
Direct Known Subclasses:
BodyResponse, StreamedResponse

public class Response extends Object
An immutable HTTP response.
  • Field Details

    • statusCode

      public final int statusCode
      The status code.
    • headers

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

      The keys in this map are lower-case.

    • attributes

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

      Beware: The values may not be immutable.

    • cookies

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

      public final Stats stats
      The full stats, if any.
  • Constructor Details

    • Response

      public Response(int statusCode, Map<?,?> headers)
      Creates a response.
      Parameters:
      statusCode - The HTTP response status code.
      headers - The response headers.
    • Response

      public Response(int statusCode, Map<?,?> headers, Map<String,Object> attributes)
      Creates a response with attributes.
      Parameters:
      statusCode - The HTTP response status code.
      headers - The response headers.
      attributes - The attributes.
    • Response

      public Response(int statusCode, Map<?,?> headers, Map<String,Object> attributes, Stats stats)
      Creates a response with attributes and stats.
      Parameters:
      statusCode - The HTTP response status code.
      headers - The response headers.
      attributes - The attributes.
      stats - Stats associated with the response.
    • Response

      public Response(int statusCode, Map<?,?> headers, Map<String,Object> attributes, Stats stats, Collection<Cookie> cookies)
      Creates a response with attributes, stats and cookies.
      Parameters:
      statusCode - The HTTP response status code.
      headers - The response headers.
      attributes - The attributes.
      stats - The stats associated with the response.
      cookies - A collection of cookies.
  • Method Details

    • getStatusCode

      public int getStatusCode()
      Gets the HTTP response code.
      Returns:
      The response code.
    • getBody

      public com.google.protobuf.ByteString getBody() throws IOException
      Gets the response body as an immutable ByteString.
      Returns:
      The response body, or null if none.
      Throws:
      IOException - if the body is streamed and an input error occurs.
    • 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.
    • 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 for this response or the default charset if none specified.
      Parameters:
      defaultCharset - The default charset.
      Returns:
      The charset.
    • getHeaders

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

      public Object getAttribute(String name)
      Gets an attribute.
      Parameters:
      name - The attribute name.
      Returns:
      The attribute or null if none set.
    • getAttribute

      public <T> T getAttribute(String name, T defaultValue)
      Gets an attribute with a default value.
      Type Parameters:
      T - The expected type.
      Parameters:
      name - The name.
      defaultValue - The default value.
      Returns:
      The variable or null.
    • getTiming

      @Deprecated public Timing getTiming()
      Deprecated.
      Gets the request/response timing, if available.
      Returns:
      The timing or null if none set.
      See Also:
    • getStats

      public Stats getStats()
      Gets the stats, if available.
      Returns:
      The stats or null if none set.
    • toString

      public String toString()
      Overrides:
      toString in class Object