Class HttpResponse
- java.lang.Object
-
- com.google.api.client.http.HttpResponse
-
public final class HttpResponse extends java.lang.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
-
-
Field Summary
Fields Modifier and Type Field Description private java.io.InputStream
content
HTTP response content ornull
beforegetContent()
.private static java.lang.String
CONTENT_ENCODING_GZIP
Content encoding for GZipprivate static java.lang.String
CONTENT_ENCODING_XGZIP
Content encoding for GZip (legacy)private java.lang.String
contentEncoding
Content encoding ornull
.private int
contentLoggingLimit
Determines the limit to the content size that will be logged duringgetContent()
.private boolean
contentRead
Signals whether the content has been read from the input stream.private java.lang.String
contentType
Content type ornull
for none.private boolean
loggingEnabled
Determines whether logging should be enabled on this response.private HttpMediaType
mediaType
Parsed content-type/media type ornull
if content-type is null.private HttpRequest
request
HTTP request.(package private) LowLevelHttpResponse
response
Low-level HTTP response.private boolean
returnRawInputStream
WhethergetContent()
should return raw input stream.private int
statusCode
Status code.private java.lang.String
statusMessage
Status message ornull
.
-
Constructor Summary
Constructors Constructor Description HttpResponse(HttpRequest request, LowLevelHttpResponse response)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
disconnect()
Disconnect usingLowLevelHttpResponse.disconnect()
, then close the HTTP response content usingignore()
.void
download(java.io.OutputStream outputStream)
Writes the content of the HTTP response into the given destination output stream.java.io.InputStream
getContent()
Returns the content of the HTTP response.java.nio.charset.Charset
getContentCharset()
Returns theCharset
specified in the Content-Type of this response or the ISO-8859-1 charset as a default.java.lang.String
getContentEncoding()
Returns the content encoding ornull
for none.int
getContentLoggingLimit()
Returns the limit to the content size that will be logged duringgetContent()
.java.lang.String
getContentType()
Returns the content type ornull
for none.HttpHeaders
getHeaders()
Returns the HTTP response headers.HttpMediaType
getMediaType()
Returns the parsed Content-Type in form of aHttpMediaType
ornull
if no content-type was set.HttpRequest
getRequest()
Returns the HTTP request.int
getStatusCode()
Returns the HTTP status code or0
for none.java.lang.String
getStatusMessage()
Returns the HTTP status message ornull
for none.HttpTransport
getTransport()
Returns the HTTP transport.private boolean
hasMessageBody()
Returns whether this response contains a message body as specified in , callingignore()
iffalse
.void
ignore()
Closes the content of the HTTP response fromgetContent()
, 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
(seegetStatusCode()
).<T> T
parseAs(java.lang.Class<T> dataClass)
Parses the content of the HTTP response fromgetContent()
and reads it into a data class of key/value pairs using the parser returned byHttpRequest.getParser()
.java.lang.Object
parseAs(java.lang.reflect.Type dataType)
Parses the content of the HTTP response fromgetContent()
and reads it into a data type of key/value pairs using the parser returned byHttpRequest.getParser()
.java.lang.String
parseAsString()
Parses the content of the HTTP response fromgetContent()
and reads it into a string.private static HttpMediaType
parseMediaType(java.lang.String contentType)
Returns anHttpMediaType
object parsed fromcontentType
, ornull
if ifcontentType
cannot be parsed orcontentType
isnull
.HttpResponse
setContentLoggingLimit(int contentLoggingLimit)
Set the limit to the content size that will be logged duringgetContent()
.HttpResponse
setLoggingEnabled(boolean loggingEnabled)
Sets whether logging should be enabled on this response.
-
-
-
Field Detail
-
content
private java.io.InputStream content
HTTP response content ornull
beforegetContent()
.
-
contentEncoding
private final java.lang.String contentEncoding
Content encoding ornull
.
-
contentType
private final java.lang.String contentType
Content type ornull
for none.
-
mediaType
private final HttpMediaType mediaType
Parsed content-type/media type ornull
if content-type is null.
-
response
LowLevelHttpResponse response
Low-level HTTP response.
-
statusCode
private final int statusCode
Status code.
-
statusMessage
private final java.lang.String statusMessage
Status message ornull
.
-
request
private final HttpRequest request
HTTP request.
-
returnRawInputStream
private final boolean returnRawInputStream
WhethergetContent()
should return raw input stream.
-
CONTENT_ENCODING_GZIP
private static final java.lang.String CONTENT_ENCODING_GZIP
Content encoding for GZip- See Also:
- Constant Field Values
-
CONTENT_ENCODING_XGZIP
private static final java.lang.String CONTENT_ENCODING_XGZIP
Content encoding for GZip (legacy)- See Also:
- Constant Field Values
-
contentLoggingLimit
private int contentLoggingLimit
Determines the limit to the content size that will be logged duringgetContent()
.Content will only be logged if
isLoggingEnabled()
istrue
.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()
.
-
loggingEnabled
private boolean loggingEnabled
Determines whether logging should be enabled on this response.Defaults to
HttpRequest.isLoggingEnabled()
.
-
contentRead
private boolean contentRead
Signals whether the content has been read from the input stream.
-
-
Constructor Detail
-
HttpResponse
HttpResponse(HttpRequest request, LowLevelHttpResponse response) throws java.io.IOException
- Throws:
java.io.IOException
-
-
Method Detail
-
parseMediaType
private static HttpMediaType parseMediaType(java.lang.String contentType)
Returns anHttpMediaType
object parsed fromcontentType
, ornull
if ifcontentType
cannot be parsed orcontentType
isnull
.
-
getContentLoggingLimit
public int getContentLoggingLimit()
Returns the limit to the content size that will be logged duringgetContent()
.Content will only be logged if
isLoggingEnabled()
istrue
.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()
.- Since:
- 1.7
-
setContentLoggingLimit
public HttpResponse setContentLoggingLimit(int contentLoggingLimit)
Set the limit to the content size that will be logged duringgetContent()
.Content will only be logged if
isLoggingEnabled()
istrue
.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()
.- 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 java.lang.String getContentEncoding()
Returns the content encoding ornull
for none.- Since:
- 1.5
-
getContentType
public java.lang.String getContentType()
Returns the content type ornull
for none.- Since:
- 1.5
-
getMediaType
public HttpMediaType getMediaType()
Returns the parsed Content-Type in form of aHttpMediaType
ornull
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
(seegetStatusCode()
).- Since:
- 1.5
-
getStatusCode
public int getStatusCode()
Returns the HTTP status code or0
for none.- Since:
- 1.5
-
getStatusMessage
public java.lang.String getStatusMessage()
Returns the HTTP status message ornull
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 java.io.InputStream getContent() throws java.io.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 returnedInputStream
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:
java.io.IOException
- I/O exception
-
download
public void download(java.io.OutputStream outputStream) throws java.io.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:
java.io.IOException
- I/O exception- Since:
- 1.9
-
ignore
public void ignore() throws java.io.IOException
Closes the content of the HTTP response fromgetContent()
, ignoring any content.- Throws:
java.io.IOException
-
disconnect
public void disconnect() throws java.io.IOException
Disconnect usingLowLevelHttpResponse.disconnect()
, then close the HTTP response content usingignore()
.- Throws:
java.io.IOException
- Since:
- 1.4
-
parseAs
public <T> T parseAs(java.lang.Class<T> dataClass) throws java.io.IOException
Parses the content of the HTTP response fromgetContent()
and reads it into a data class of key/value pairs using the parser returned byHttpRequest.getParser()
.Reference: http://tools.ietf.org/html/rfc2616#section-4.3
- Returns:
- parsed data class or
null
for no content - Throws:
java.io.IOException
-
hasMessageBody
private boolean hasMessageBody() throws java.io.IOException
Returns whether this response contains a message body as specified in , callingignore()
iffalse
.- Throws:
java.io.IOException
-
parseAs
public java.lang.Object parseAs(java.lang.reflect.Type dataType) throws java.io.IOException
Parses the content of the HTTP response fromgetContent()
and reads it into a data type of key/value pairs using the parser returned byHttpRequest.getParser()
.- Returns:
- parsed data type instance or
null
for no content - Throws:
java.io.IOException
- Since:
- 1.10
-
parseAsString
public java.lang.String parseAsString() throws java.io.IOException
Parses the content of the HTTP response fromgetContent()
and reads it into a string.Since this method returns
""
for no content, a simpler check for no content is to check ifgetContent()
isnull
.All content is read from the input content stream rather than being limited by the Content-Length. For the character set, it follows the specification by parsing the "charset" parameter of the Content-Type header or by default
"ISO-8859-1"
if the parameter is missing.- Returns:
- parsed string or
""
for no content - Throws:
java.io.IOException
- I/O exception
-
getContentCharset
public java.nio.charset.Charset getContentCharset()
Returns theCharset
specified in the Content-Type of this response or the ISO-8859-1 charset as a default.- Since:
- 1.10
-
-