com.google.api.client.http.json
Class JsonHttpClient

java.lang.Object
  extended by com.google.api.client.http.json.JsonHttpClient

public class JsonHttpClient
extends Object

JSON HTTP Client.

Implementation is not thread-safe.

Upgrade warning: prior to version 1.10 there was a builder method in JsonHttpClient, this has been removed in version 1.10. The Builder can now be instantiated with Builder#Builder(HttpTransport, JsonFactory, String, String, HttpRequestInitializer).

Since:
1.6
Author:
Ravi Mistry

Nested Class Summary
static class JsonHttpClient.Builder
          Builder for JsonHttpClient.
 
Constructor Summary
  JsonHttpClient(HttpTransport transport, JsonFactory jsonFactory, String baseUrl)
          Deprecated. (scheduled to be removed in 1.11) Use JsonHttpClient(HttpTransport, JsonFactory, String, String, HttpRequestInitializer).
  JsonHttpClient(HttpTransport transport, JsonFactory jsonFactory, String rootUrl, String servicePath, HttpRequestInitializer httpRequestInitializer)
          Constructor with required parameters.
protected JsonHttpClient(HttpTransport transport, JsonHttpRequestInitializer jsonHttpRequestInitializer, HttpRequestInitializer httpRequestInitializer, JsonFactory jsonFactory, JsonObjectParser jsonObjectParser, String baseUrl, String applicationName)
          Deprecated. (scheduled to be removed in 1.11) Use JsonHttpClient(HttpTransport, JsonHttpRequestInitializer, HttpRequestInitializer, JsonFactory, JsonObjectParser, String, String, String).
protected JsonHttpClient(HttpTransport transport, JsonHttpRequestInitializer jsonHttpRequestInitializer, HttpRequestInitializer httpRequestInitializer, JsonFactory jsonFactory, JsonObjectParser jsonObjectParser, String rootUrl, String servicePath, String applicationName)
          Construct the JsonHttpClient.
protected JsonHttpClient(HttpTransport transport, JsonHttpRequestInitializer jsonHttpRequestInitializer, HttpRequestInitializer httpRequestInitializer, JsonFactory jsonFactory, String baseUrl, String applicationName)
          Deprecated. (scheduled to be removed in 1.11) Use JsonHttpClient(HttpTransport, JsonHttpRequestInitializer, HttpRequestInitializer, JsonFactory, JsonObjectParser, String, String, String).
 
Method Summary
protected  HttpRequest buildHttpRequest(HttpMethod method, GenericUrl url, Object body)
          Create an HttpRequest suitable for use against this service.
protected  JsonHttpParser createParser()
          Deprecated. (scheduled to be removed in 1.11) Use JsonHttpClient.Builder.setObjectParser(JsonObjectParser) instead.
protected  JsonHttpContent createSerializer(Object body)
          Create a JSON serializer for a request object.
protected  InputStream executeAsInputStream(HttpMethod method, GenericUrl url, Object body)
          Builds and executes an HttpRequest and then returns the content input stream of HttpResponse.
protected  HttpResponse executeUnparsed(HttpMethod method, GenericUrl url, Object body)
          Builds and executes a HttpRequest.
protected  HttpResponse executeUnparsed(HttpRequest request)
          Executes the specified HttpRequest.
 String getApplicationName()
          Returns the application name to be sent in the User-Agent header of each request or null for none.
 String getBaseUrl()
          Returns the base URL of the service, for example "https://www.googleapis.com/tasks/v1/" .
 JsonFactory getJsonFactory()
          Returns the JSON Factory.
 JsonHttpParser getJsonHttpParser()
          Deprecated. (scheduled to be removed in 1.11) Use getJsonObjectParser() instead.
 JsonHttpRequestInitializer getJsonHttpRequestInitializer()
          Returns the JSON HTTP request initializer or null for none.
 JsonObjectParser getJsonObjectParser()
          Returns the JSON Object Parser.
 HttpRequestFactory getRequestFactory()
          Returns the HTTP request factory.
 String getRootUrl()
          Returns the root URL of the service, for example https://www.googleapis.com/.
 String getServicePath()
          Returns the service path of the service, for example "tasks/v1/".
protected  void initialize(JsonHttpRequest jsonHttpRequest)
          Initializes a JsonHttpRequest using a JsonHttpRequestInitializer.
protected  boolean isBaseUrlUsed()
          Deprecated. (scheduled to be removed in 1.11) Use getRootUrl() and getServicePath() instead.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JsonHttpClient

@Deprecated
public JsonHttpClient(HttpTransport transport,
                                 JsonFactory jsonFactory,
                                 String baseUrl)
Deprecated. (scheduled to be removed in 1.11) Use JsonHttpClient(HttpTransport, JsonFactory, String, String, HttpRequestInitializer).

Constructor with required parameters.

Use JsonHttpClient.Builder if you need to specify any of the optional parameters.

Parameters:
transport - The transport to use for requests
jsonFactory - A factory for creating JSON parsers and serializers
baseUrl - The base URL of the service. Must end with a "/"

JsonHttpClient

public JsonHttpClient(HttpTransport transport,
                      JsonFactory jsonFactory,
                      String rootUrl,
                      String servicePath,
                      HttpRequestInitializer httpRequestInitializer)
Constructor with required parameters.

Use JsonHttpClient.Builder if you need to specify any of the optional parameters.

Parameters:
transport - The transport to use for requests
jsonFactory - A factory for creating JSON parsers and serializers
rootUrl - The root URL of the service. Must end with a "/"
servicePath - The service path of the service. Must end with a "/" and not begin with a "/". It is allowed to be an empty string "" or a forward slash "/", if it is a forward slash then it is treated as an empty string
httpRequestInitializer - The HTTP request initializer or null for none
Since:
1.10

JsonHttpClient

@Deprecated
protected JsonHttpClient(HttpTransport transport,
                                    JsonHttpRequestInitializer jsonHttpRequestInitializer,
                                    HttpRequestInitializer httpRequestInitializer,
                                    JsonFactory jsonFactory,
                                    String baseUrl,
                                    String applicationName)
Deprecated. (scheduled to be removed in 1.11) Use JsonHttpClient(HttpTransport, JsonHttpRequestInitializer, HttpRequestInitializer, JsonFactory, JsonObjectParser, String, String, String).

Construct the JsonHttpClient.

Parameters:
transport - The transport to use for requests
jsonHttpRequestInitializer - The initializer to use when creating an JsonHttpRequest or null for none
httpRequestInitializer - The initializer to use when creating an HttpRequest or null for none
jsonFactory - A factory for creating JSON parsers and serializers
baseUrl - The base URL of the service. Must end with a "/"
applicationName - The application name to be sent in the User-Agent header of requests or null for none

JsonHttpClient

@Deprecated
protected JsonHttpClient(HttpTransport transport,
                                    JsonHttpRequestInitializer jsonHttpRequestInitializer,
                                    HttpRequestInitializer httpRequestInitializer,
                                    JsonFactory jsonFactory,
                                    JsonObjectParser jsonObjectParser,
                                    String baseUrl,
                                    String applicationName)
Deprecated. (scheduled to be removed in 1.11) Use JsonHttpClient(HttpTransport, JsonHttpRequestInitializer, HttpRequestInitializer, JsonFactory, JsonObjectParser, String, String, String).

Construct the JsonHttpClient.

Parameters:
transport - The transport to use for requests
jsonHttpRequestInitializer - The initializer to use when creating an JsonHttpRequest or null for none
httpRequestInitializer - The initializer to use when creating an HttpRequest or null for none
jsonFactory - A factory for creating JSON parsers and serializers
baseUrl - The base URL of the service. Must end with a "/"
applicationName - The application name to be sent in the User-Agent header of requests or null for none
Since:
1.10

JsonHttpClient

protected JsonHttpClient(HttpTransport transport,
                         JsonHttpRequestInitializer jsonHttpRequestInitializer,
                         HttpRequestInitializer httpRequestInitializer,
                         JsonFactory jsonFactory,
                         JsonObjectParser jsonObjectParser,
                         String rootUrl,
                         String servicePath,
                         String applicationName)
Construct the JsonHttpClient.

Parameters:
transport - The transport to use for requests
jsonHttpRequestInitializer - The initializer to use when creating an JsonHttpRequest or null for none
httpRequestInitializer - The initializer to use when creating an HttpRequest or null for none
jsonFactory - A factory for creating JSON parsers and serializers
jsonObjectParser - JSON parser to use or null if unused. null won't be accepted from 1.11 on.
rootUrl - The root URL of the service. Must end with a "/"
servicePath - The service path of the service. Must end with a "/" and not begin with a "/". It is allowed to be an empty string "" or a forward slash "/", if it is a forward slash then it is treated as an empty string
applicationName - The application name to be sent in the User-Agent header of requests or null for none
Since:
1.10
Method Detail

isBaseUrlUsed

@Deprecated
protected final boolean isBaseUrlUsed()
Deprecated. (scheduled to be removed in 1.11) Use getRootUrl() and getServicePath() instead.

Returns if baseUrl is used instead of servicePath and rootUrl.

Since:
1.10

getBaseUrl

public final String getBaseUrl()
Returns the base URL of the service, for example "https://www.googleapis.com/tasks/v1/" . Must be URL-encoded and must end with a "/". This is determined when the library is generated and normally should not be changed.


getRootUrl

public final String getRootUrl()
Returns the root URL of the service, for example https://www.googleapis.com/. Must be URL-encoded and must end with a "/".

Since:
1.10

getServicePath

public final String getServicePath()
Returns the service path of the service, for example "tasks/v1/". Must be URL-encoded and must end with a "/" and not begin with a "/". It is allowed to be an empty string "" or a forward slash "/", if it is a forward slash then it is treated as an empty string

Since:
1.10

getApplicationName

public final String getApplicationName()
Returns the application name to be sent in the User-Agent header of each request or null for none.


getJsonFactory

public final JsonFactory getJsonFactory()
Returns the JSON Factory.


getRequestFactory

public final HttpRequestFactory getRequestFactory()
Returns the HTTP request factory.


getJsonHttpRequestInitializer

public final JsonHttpRequestInitializer getJsonHttpRequestInitializer()
Returns the JSON HTTP request initializer or null for none.


getJsonHttpParser

@Deprecated
public final JsonHttpParser getJsonHttpParser()
Deprecated. (scheduled to be removed in 1.11) Use getJsonObjectParser() instead.

Returns the JSON HTTP Parser. Initializes the parser once and then caches it for all subsequent calls to this method.


createParser

@Deprecated
protected JsonHttpParser createParser()
Deprecated. (scheduled to be removed in 1.11) Use JsonHttpClient.Builder.setObjectParser(JsonObjectParser) instead.

Creates a JSON parser. Subclasses may override if specific JsonHttpParser implementations are required.


getJsonObjectParser

public final JsonObjectParser getJsonObjectParser()
Returns the JSON Object Parser.

Since:
1.10

createSerializer

protected JsonHttpContent createSerializer(Object body)
Create a JSON serializer for a request object. Subclasses may override if specific JsonHttpContent implementations are required.

Parameters:
body - A POJO that can be serialized into JSON

initialize

protected void initialize(JsonHttpRequest jsonHttpRequest)
                   throws IOException
Initializes a JsonHttpRequest using a JsonHttpRequestInitializer. Subclasses may override if specific behavior is required.

Must be called before the JSON HTTP request is executed, preferably right after the request is instantiated. Sample usage:

    public class Get extends JsonHttpRequest {
      ...
    }

    public Get get(String userId) throws IOException {
      Get result = new Get(userId);
      initialize(result);
      return result;
    }
 

Parameters:
jsonHttpRequest - JSON HTTP Request type
Throws:
IOException

buildHttpRequest

protected HttpRequest buildHttpRequest(HttpMethod method,
                                       GenericUrl url,
                                       Object body)
                                throws IOException
Create an HttpRequest suitable for use against this service.

Subclasses may override if specific behavior is required, for example if a sequence of requests need to be built instead of a single request then subclasses should throw an UnsupportedOperationException. Subclasses which override this method can make use of HttpRequest.addParser(com.google.api.client.http.HttpParser), HttpRequest.setContent(com.google.api.client.http.HttpContent) and HttpRequest.setEnableGZipContent(boolean).

Parameters:
method - HTTP Method type
url - The complete URL of the service where requests should be sent
body - A POJO that can be serialized into JSON or null for none
Returns:
newly created HttpRequest
Throws:
IOException
Since:
1.7

executeUnparsed

protected HttpResponse executeUnparsed(HttpMethod method,
                                       GenericUrl url,
                                       Object body)
                                throws IOException
Builds and executes a HttpRequest. Subclasses may override if specific behavior is required, for example if a sequence of requests need to be built instead of a single request.

Callers are responsible for disconnecting the HTTP response by calling HttpResponse.disconnect(). Example usage:

     HttpResponse response = client.executeUnparsed(method, url, body);
     try {
       // process response..
     } finally {
       response.disconnect();
     }
 

Parameters:
method - HTTP Method type
url - The complete URL of the service where requests should be sent
body - A POJO that can be serialized into JSON or null for none
Returns:
HttpResponse type
Throws:
IOException - if the request fails
Since:
1.7

executeUnparsed

protected HttpResponse executeUnparsed(HttpRequest request)
                                throws IOException
Executes the specified HttpRequest. Subclasses may override if specific behavior is required, for example if a custom error is required to be thrown.

Callers are responsible for disconnecting the HTTP response by calling HttpResponse.disconnect(). Example usage:

     HttpResponse response = client.executeUnparsed(request);
     try {
       // process response..
     } finally {
       response.disconnect();
     }
 

Parameters:
request - HTTP Request
Returns:
HttpResponse type
Throws:
IOException - if the request fails
Since:
1.9

executeAsInputStream

protected InputStream executeAsInputStream(HttpMethod method,
                                           GenericUrl url,
                                           Object body)
                                    throws IOException
Builds and executes an HttpRequest and then returns the content input stream of HttpResponse. Subclasses may override if specific behavior is required.

Callers are responsible for closing the input stream after it is processed. Example usage:

     InputStream is = client.executeAsInputStream();
     try {
       // Process input stream..
     } finally {
       is.close();
     }
 

Parameters:
method - HTTP Method type
url - The complete URL of the service where requests should be sent
body - A POJO that can be serialized into JSON or null for none
Returns:
input stream of the response content
Throws:
IOException - if the request fails
Since:
1.8


Copyright © 2011-2012 Google. All Rights Reserved.