com.google.api.client.http
Class HttpResponse

java.lang.Object
  extended by com.google.api.client.http.HttpResponse

public final class HttpResponse
extends Object

HTTP response.

Callers should call disconnect() when the HTTP response object is no longer needed. However, disconnect() does not have to be called if the response stream is properly closed. Example usage:

   HttpResponse response = request.execute();
   try {
     // process the HTTP response object
   } finally {
     response.disconnect();
   }
 

Implementation is not thread-safe.

Since:
1.0
Author:
Yaniv Inbar

Method Summary
 void disconnect()
          Close the HTTP response content and disconnect using LowLevelHttpResponse.disconnect().
 void download(OutputStream outputStream)
          Writes the content of the HTTP response into the given destination output stream.
 InputStream getContent()
          Returns the content of the HTTP response.
 Charset getContentCharset()
          Returns the Charset specified in the Content-Type of this response or the "ISO-8859-1" charset as a default.
 String getContentEncoding()
          Returns the content encoding or null for none.
 int getContentLoggingLimit()
          Returns the limit to the content size that will be logged during getContent().
 String getContentType()
          Returns the content type or null for none.
 HttpHeaders getHeaders()
          Returns the HTTP response headers.
 HttpMediaType getMediaType()
          Returns the parsed Content-Type in form of a HttpMediaType or null if no content-type was set.
 HttpParser getParser()
          Deprecated. (scheduled to be removed in 1.11) Use getRequest(). HttpRequest.getParser() instead
 HttpRequest getRequest()
          Returns the HTTP request.
 int getStatusCode()
          Returns the HTTP status code or 0 for none.
 String getStatusMessage()
          Returns the HTTP status message or null for none.
 HttpTransport getTransport()
          Returns the HTTP transport.
 void ignore()
          Closes the content of the HTTP response from getContent(), ignoring any content.
 boolean isLoggingEnabled()
          Returns whether logging should be enabled on this response.
 boolean isSuccessStatusCode()
          Returns whether received a successful HTTP status code >= 200 && < 300 (see getStatusCode()).
<T> T
parseAs(Class<T> dataClass)
          Parses the content of the HTTP response from getContent() and reads it into a data class of key/value pairs using the parser returned by getParser().
 Object parseAs(Type dataType)
          Parses the content of the HTTP response from getContent() and reads it into a data type of key/value pairs using the parser returned by getParser().
 String parseAsString()
          Parses the content of the HTTP response from getContent() and reads it into a string.
 HttpResponse setContentLoggingLimit(int contentLoggingLimit)
          Set the limit to the content size that will be logged during getContent().
 HttpResponse setLoggingEnabled(boolean loggingEnabled)
          Sets whether logging should be enabled on this response.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getContentLoggingLimit

public int getContentLoggingLimit()
Returns the limit to the content size that will be logged during getContent().

Content will only be logged if isLoggingEnabled() is true.

If the content size is greater than this limit then it will not be logged.

Can be set to 0 to disable content logging. This is useful for example if content has sensitive data such as authentication information.

Defaults to HttpRequest.getContentLoggingLimit().

Upgrade warning: prior to version 1.9, the default was 100,000 bytes, but now it is HttpRequest.getContentLoggingLimit().

Since:
1.7

setContentLoggingLimit

public HttpResponse setContentLoggingLimit(int contentLoggingLimit)
Set the limit to the content size that will be logged during getContent().

Content will only be logged if isLoggingEnabled() is true.

If the content size is greater than this limit then it will not be logged.

Can be set to 0 to disable content logging. This is useful for example if content has sensitive data such as authentication information.

Defaults to HttpRequest.getContentLoggingLimit().

Upgrade warning: prior to version 1.9, the default was 100,000 bytes, but now it is HttpRequest.getContentLoggingLimit().

Since:
1.7

isLoggingEnabled

public boolean isLoggingEnabled()
Returns whether logging should be enabled on this response.

Defaults to HttpRequest.isLoggingEnabled().

Since:
1.9

setLoggingEnabled

public HttpResponse setLoggingEnabled(boolean loggingEnabled)
Sets whether logging should be enabled on this response.

Defaults to HttpRequest.isLoggingEnabled().

Since:
1.9

getContentEncoding

public String getContentEncoding()
Returns the content encoding or null for none.

Since:
1.5

getContentType

public String getContentType()
Returns the content type or null for none.

Since:
1.5

getMediaType

public HttpMediaType getMediaType()
Returns the parsed Content-Type in form of a HttpMediaType or null if no content-type was set.

Since:
1.10

getHeaders

public HttpHeaders getHeaders()
Returns the HTTP response headers.

Since:
1.5

isSuccessStatusCode

public boolean isSuccessStatusCode()
Returns whether received a successful HTTP status code >= 200 && < 300 (see getStatusCode()).

Since:
1.5

getStatusCode

public int getStatusCode()
Returns the HTTP status code or 0 for none.

Since:
1.5

getStatusMessage

public String getStatusMessage()
Returns the HTTP status message or null for none.

Since:
1.5

getTransport

public HttpTransport getTransport()
Returns the HTTP transport.

Since:
1.5

getRequest

public HttpRequest getRequest()
Returns the HTTP request.

Since:
1.5

getContent

public InputStream getContent()
                       throws IOException
Returns the content of the HTTP response.

The result is cached, so subsequent calls will be fast.

Callers should call InputStream.close() after the returned InputStream is no longer needed. Example usage:

     InputStream is = response.getContent();
     try {
       // Process the input stream..
     } finally {
       is.close();
     }
 

disconnect() does not have to be called if the content is closed.

Returns:
input stream content of the HTTP response or null for none
Throws:
IOException - I/O exception

download

public void download(OutputStream outputStream)
              throws IOException
Writes the content of the HTTP response into the given destination output stream.

Sample usage:

     HttpRequest request = requestFactory.buildGetRequest(
         new GenericUrl("https://www.google.com/images/srpr/logo3w.png"));
     OutputStream outputStream = new FileOutputStream(new File("/tmp/logo3w.png"));
     try {
       HttpResponse response = request.execute();
       response.download(outputStream);
     } finally {
       outputStream.close();
     }
    

This method closes the content of the HTTP response from getContent().

This method does not close the given output stream.

Parameters:
outputStream - destination output stream
Throws:
IOException - I/O exception
Since:
1.9

ignore

public void ignore()
            throws IOException
Closes the content of the HTTP response from getContent(), ignoring any content.

Throws:
IOException

disconnect

public void disconnect()
                throws IOException
Close the HTTP response content and disconnect using LowLevelHttpResponse.disconnect().

Upgrade warning: since version 1.10 disconnect() now closes the HTTP response content input stream. This was not done by this method prior to version 1.10.

Throws:
IOException
Since:
1.4

getParser

@Deprecated
public HttpParser getParser()
Deprecated. (scheduled to be removed in 1.11) Use getRequest(). HttpRequest.getParser() instead

Returns the HTTP response content parser to use for the content type of this HTTP response or null for none.


parseAs

public <T> T parseAs(Class<T> dataClass)
          throws IOException
Parses the content of the HTTP response from getContent() and reads it into a data class of key/value pairs using the parser returned by getParser().

Returns:
parsed data class or null for no content
Throws:
IOException - I/O exception
IllegalArgumentException - if no parser is defined for the given content type or if there is no content type defined in the HTTP response

parseAs

public Object parseAs(Type dataType)
               throws IOException
Parses the content of the HTTP response from getContent() and reads it into a data type of key/value pairs using the parser returned by getParser().

Returns:
parsed data type instance or null for no content
Throws:
IOException - I/O exception
IllegalArgumentException - if no parser is defined for this response
Since:
1.10

parseAsString

public String parseAsString()
                     throws IOException
Parses the content of the HTTP response from getContent() and reads it into a string.

Since this method returns "" for no content, a simpler check for no content is to check if getContent() is null.

Warning: in prior version 1.9 the maximum amount of content parsed for un-GZipped content was set by the Content-Length header, but now instead all content is read. Also, prior version assumed the charset was "UTF-8", but now it follows the specification by parsing the "charset" parameter of the Content-Type header or "ISO-8859-1" if missing.

Returns:
parsed string or "" for no content
Throws:
IOException - I/O exception

getContentCharset

public Charset getContentCharset()
Returns the Charset specified in the Content-Type of this response or the "ISO-8859-1" charset as a default.

Since:
1.10


Copyright © 2011-2012 Google. All Rights Reserved.