org.apache.shindig.gadgets.http
Class HttpResponse

java.lang.Object
  extended by org.apache.shindig.gadgets.http.HttpResponse
All Implemented Interfaces:
Externalizable, Serializable

public final class HttpResponse
extends Object
implements Externalizable

Represents the results of an HTTP content retrieval operation. HttpResponse objects are immutable in order to allow them to be safely used in concurrent caches and by multiple threads without worrying about concurrent modification.

See Also:
Serialized Form

Field Summary
static int SC_ACCEPTED
           
static int SC_BAD_GATEWAY
           
static int SC_BAD_REQUEST
           
static int SC_CONFLICT
           
static int SC_CONTINUE
           
static int SC_CREATED
           
static int SC_EXPECTATION_FAILED
           
static int SC_FORBIDDEN
           
static int SC_FOUND
           
static int SC_GATEWAY_TIMEOUT
           
static int SC_GONE
           
static int SC_HTTP_VERSION_NOT_SUPPORTED
           
static int SC_INTERNAL_SERVER_ERROR
           
static int SC_LENGTH_REQUIRED
           
static int SC_METHOD_NOT_ALLOWED
           
static int SC_MOVED_PERMANENTLY
           
static int SC_MULTIPLE_CHOICES
           
static int SC_NO_CONTENT
           
static int SC_NON_AUTHORITATIVE_INFORMATION
           
static int SC_NOT_ACCEPTABLE
           
static int SC_NOT_FOUND
           
static int SC_NOT_IMPLEMENTED
           
static int SC_NOT_MODIFIED
           
static int SC_OK
           
static int SC_PARTIAL_CONTENT
           
static int SC_PAYMENT_REQUIRED
           
static int SC_PRECONDITION_FAILED
           
static int SC_PROXY_AUTHENTICATION_REQUIRED
           
static int SC_REQUEST_ENTITY_TOO_LARGE
           
static int SC_REQUEST_TIMEOUT
           
static int SC_REQUEST_URI_TOO_LONG
           
static int SC_REQUESTED_RANGE_NOT_SATISFIABLE
           
static int SC_RESET_CONTENT
           
static int SC_SEE_OTHER
           
static int SC_SERVICE_UNAVAILABLE
           
static int SC_SWITCHING_PROTOCOLS
           
static int SC_TEMPORARY_REDIRECT
           
static int SC_UNAUTHORIZED
           
static int SC_UNSUPPORTED_MEDIA_TYPE
           
static int SC_USE_PROXY
           
 
Constructor Summary
HttpResponse()
          Needed for serialization.
HttpResponse(String body)
           
 
Method Summary
static HttpResponse badrequest(String msg)
           
 boolean equals(Object obj)
           
static HttpResponse error()
           
 long getCacheExpiration()
          Calculate the Cache Expiration for this response.
 long getCacheTtl()
           
 int getContentLength()
           
 long getDefaultTtl()
          returns the default TTL for responses.
 String getEncoding()
           
 Charset getEncodingCharset()
           
 String getHeader(String name)
           
 com.google.common.collect.Multimap<String,String> getHeaders()
           
 Collection<String> getHeaders(String name)
           
 int getHttpStatusCode()
           
 Map<String,String> getMetadata()
           
 InputStream getResponse()
           
 String getResponseAsString()
          Attempts to convert the response body to a string using the Content-Type header.
static TimeSource getTimeSource()
           
 int hashCode()
           
 boolean isError()
           
 boolean isStale()
           
 boolean isStrictNoCache()
           
static com.google.common.collect.Multimap<String,String> newHeaderMultimap()
           
static HttpResponse notFound()
           
 void readExternal(ObjectInput in)
          Expected layout: int - status code Map> - headers int - length of body byte array - body, of previously specified length
static void setTimeSource(TimeSource timeSource)
           
static HttpResponse timeout()
           
 String toString()
           
 void writeExternal(ObjectOutput out)
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

SC_CONTINUE

public static final int SC_CONTINUE
See Also:
Constant Field Values

SC_SWITCHING_PROTOCOLS

public static final int SC_SWITCHING_PROTOCOLS
See Also:
Constant Field Values

SC_OK

public static final int SC_OK
See Also:
Constant Field Values

SC_CREATED

public static final int SC_CREATED
See Also:
Constant Field Values

SC_ACCEPTED

public static final int SC_ACCEPTED
See Also:
Constant Field Values

SC_NON_AUTHORITATIVE_INFORMATION

public static final int SC_NON_AUTHORITATIVE_INFORMATION
See Also:
Constant Field Values

SC_NO_CONTENT

public static final int SC_NO_CONTENT
See Also:
Constant Field Values

SC_RESET_CONTENT

public static final int SC_RESET_CONTENT
See Also:
Constant Field Values

SC_PARTIAL_CONTENT

public static final int SC_PARTIAL_CONTENT
See Also:
Constant Field Values

SC_MULTIPLE_CHOICES

public static final int SC_MULTIPLE_CHOICES
See Also:
Constant Field Values

SC_MOVED_PERMANENTLY

public static final int SC_MOVED_PERMANENTLY
See Also:
Constant Field Values

SC_FOUND

public static final int SC_FOUND
See Also:
Constant Field Values

SC_SEE_OTHER

public static final int SC_SEE_OTHER
See Also:
Constant Field Values

SC_NOT_MODIFIED

public static final int SC_NOT_MODIFIED
See Also:
Constant Field Values

SC_USE_PROXY

public static final int SC_USE_PROXY
See Also:
Constant Field Values

SC_TEMPORARY_REDIRECT

public static final int SC_TEMPORARY_REDIRECT
See Also:
Constant Field Values

SC_BAD_REQUEST

public static final int SC_BAD_REQUEST
See Also:
Constant Field Values

SC_UNAUTHORIZED

public static final int SC_UNAUTHORIZED
See Also:
Constant Field Values

SC_PAYMENT_REQUIRED

public static final int SC_PAYMENT_REQUIRED
See Also:
Constant Field Values

SC_FORBIDDEN

public static final int SC_FORBIDDEN
See Also:
Constant Field Values

SC_NOT_FOUND

public static final int SC_NOT_FOUND
See Also:
Constant Field Values

SC_METHOD_NOT_ALLOWED

public static final int SC_METHOD_NOT_ALLOWED
See Also:
Constant Field Values

SC_NOT_ACCEPTABLE

public static final int SC_NOT_ACCEPTABLE
See Also:
Constant Field Values

SC_PROXY_AUTHENTICATION_REQUIRED

public static final int SC_PROXY_AUTHENTICATION_REQUIRED
See Also:
Constant Field Values

SC_REQUEST_TIMEOUT

public static final int SC_REQUEST_TIMEOUT
See Also:
Constant Field Values

SC_CONFLICT

public static final int SC_CONFLICT
See Also:
Constant Field Values

SC_GONE

public static final int SC_GONE
See Also:
Constant Field Values

SC_LENGTH_REQUIRED

public static final int SC_LENGTH_REQUIRED
See Also:
Constant Field Values

SC_PRECONDITION_FAILED

public static final int SC_PRECONDITION_FAILED
See Also:
Constant Field Values

SC_REQUEST_ENTITY_TOO_LARGE

public static final int SC_REQUEST_ENTITY_TOO_LARGE
See Also:
Constant Field Values

SC_REQUEST_URI_TOO_LONG

public static final int SC_REQUEST_URI_TOO_LONG
See Also:
Constant Field Values

SC_UNSUPPORTED_MEDIA_TYPE

public static final int SC_UNSUPPORTED_MEDIA_TYPE
See Also:
Constant Field Values

SC_REQUESTED_RANGE_NOT_SATISFIABLE

public static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE
See Also:
Constant Field Values

SC_EXPECTATION_FAILED

public static final int SC_EXPECTATION_FAILED
See Also:
Constant Field Values

SC_INTERNAL_SERVER_ERROR

public static final int SC_INTERNAL_SERVER_ERROR
See Also:
Constant Field Values

SC_NOT_IMPLEMENTED

public static final int SC_NOT_IMPLEMENTED
See Also:
Constant Field Values

SC_BAD_GATEWAY

public static final int SC_BAD_GATEWAY
See Also:
Constant Field Values

SC_SERVICE_UNAVAILABLE

public static final int SC_SERVICE_UNAVAILABLE
See Also:
Constant Field Values

SC_GATEWAY_TIMEOUT

public static final int SC_GATEWAY_TIMEOUT
See Also:
Constant Field Values

SC_HTTP_VERSION_NOT_SUPPORTED

public static final int SC_HTTP_VERSION_NOT_SUPPORTED
See Also:
Constant Field Values
Constructor Detail

HttpResponse

public HttpResponse()
Needed for serialization. Do not use this for any other purpose.


HttpResponse

public HttpResponse(String body)
Method Detail

setTimeSource

public static void setTimeSource(TimeSource timeSource)

getTimeSource

public static TimeSource getTimeSource()

error

public static HttpResponse error()

badrequest

public static HttpResponse badrequest(String msg)

timeout

public static HttpResponse timeout()

notFound

public static HttpResponse notFound()

getHttpStatusCode

public int getHttpStatusCode()

isError

public boolean isError()
Returns:
True if the status code is considered to be an error.

getEncoding

public String getEncoding()
Returns:
The encoding of the response body, if we're able to determine it.

getEncodingCharset

public Charset getEncodingCharset()
Returns:
The Charset of the response body's encoding, if we were able to determine it.

getContentLength

public int getContentLength()
Returns:
the content length

getResponse

public InputStream getResponse()
Returns:
An input stream suitable for reading the entirety of the response.

getResponseAsString

public String getResponseAsString()
Attempts to convert the response body to a string using the Content-Type header. If no Content-Type header is specified (or it doesn't include an encoding), we will assume it is DEFAULT_ENCODING.

Returns:
The body as a string.

getHeaders

public com.google.common.collect.Multimap<String,String> getHeaders()
Returns:
All headers for this object.

getHeaders

public Collection<String> getHeaders(String name)
Returns:
All headers with the given name. If no headers are set for the given name, an empty collection will be returned.

getHeader

public String getHeader(String name)
Returns:
The first set header with the given name or null if not set. If you need multiple values for the header, use getHeaders().

getMetadata

public Map<String,String> getMetadata()
Returns:
additional data to embed in responses sent from the JSON proxy.

getCacheExpiration

public long getCacheExpiration()
Calculate the Cache Expiration for this response. For errors (rc >=400) we intentionally ignore cache-control headers for most HTTP error responses, because if we don't we end up hammering sites that have gone down with lots of requests. Certain classes of client errors (authentication) have more severe behavioral implications if we cache them. For errors if the server provides a Retry-After header we use that. We technically shouldn't be caching certain 300 class status codes either, such as 302, but in practice this is a better option for performance.

Returns:
consolidated cache expiration time or -1

getCacheTtl

public long getCacheTtl()
Returns:
Consolidated ttl in milliseconds or -1.

isStale

public boolean isStale()
Returns:
True if this result is stale.

isStrictNoCache

public boolean isStrictNoCache()
Returns:
true if a strict no-cache header is set in Cache-Control or Pragma

getDefaultTtl

public long getDefaultTtl()
returns the default TTL for responses. Used mainly by tests because Guice static injects TTL values.

Returns:
milliseconds of the ttl

hashCode

public int hashCode()
Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

toString

public String toString()
Overrides:
toString in class Object

readExternal

public void readExternal(ObjectInput in)
                  throws IOException,
                         ClassNotFoundException
Expected layout: int - status code Map> - headers int - length of body byte array - body, of previously specified length

Specified by:
readExternal in interface Externalizable
Throws:
IOException
ClassNotFoundException

writeExternal

public void writeExternal(ObjectOutput out)
                   throws IOException
Specified by:
writeExternal in interface Externalizable
Throws:
IOException

newHeaderMultimap

public static com.google.common.collect.Multimap<String,String> newHeaderMultimap()


Copyright © 2007-2012. All Rights Reserved.