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.

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)
          Constructor with required parameters.
protected JsonHttpClient(HttpTransport transport, JsonHttpRequestInitializer jsonHttpRequestInitializer, HttpRequestInitializer httpRequestInitializer, JsonFactory jsonFactory, String baseUrl, String applicationName)
          Construct the JsonHttpClient.
 
Method Summary
static JsonHttpClient.Builder builder(HttpTransport transport, JsonFactory jsonFactory, GenericUrl baseUrl)
          Returns an instance of a new builder.
protected  HttpRequest buildHttpRequest(HttpMethod method, GenericUrl url, Object body)
          Create an HttpRequest suitable for use against this service.
protected  HttpRequest buildHttpRequest(HttpMethod method, String uriTemplate, JsonHttpRequest jsonHttpRequest)
          Deprecated. (scheduled to be removed in 1.8) Use buildHttpRequest(HttpMethod, GenericUrl, Object)
protected  JsonHttpParser createParser()
          Creates a JSON parser.
protected  JsonHttpContent createSerializer(Object body)
          Create a JSON serializer for a request object.
protected  HttpResponse execute(HttpMethod method, String uriTemplate, Object body, JsonHttpRequest jsonHttpRequest)
          Deprecated. (scheduled to be removed in 1.8) Use executeUnparsed(com.google.api.client.http.HttpMethod, com.google.api.client.http.GenericUrl, java.lang.Object)
protected  HttpResponse executeUnparsed(HttpMethod method, GenericUrl url, Object body)
          Builds and executes a 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()
          Returns the JSON HTTP Parser.
 JsonHttpRequestInitializer getJsonHttpRequestInitializer()
          Returns the JSON HTTP request initializer or null for none.
 HttpRequestFactory getRequestFactory()
          Returns the HTTP request factory.
protected  void initialize(JsonHttpRequest jsonHttpRequest)
          Initializes a JsonHttpRequest using a JsonHttpRequestInitializer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JsonHttpClient

public JsonHttpClient(HttpTransport transport,
                      JsonFactory jsonFactory,
                      String baseUrl)
Constructor with required parameters.

Use builder(com.google.api.client.http.HttpTransport, com.google.api.client.json.JsonFactory, com.google.api.client.http.GenericUrl) 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

protected JsonHttpClient(HttpTransport transport,
                         JsonHttpRequestInitializer jsonHttpRequestInitializer,
                         HttpRequestInitializer httpRequestInitializer,
                         JsonFactory jsonFactory,
                         String baseUrl,
                         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
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
Method Detail

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.


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

public final JsonHttpParser getJsonHttpParser()
Returns the JSON HTTP Parser. Initializes the parser once and then caches it for all subsequent calls to this method.


createParser

protected JsonHttpParser createParser()
Creates a JSON parser. Subclasses may override if specific JsonHttpParser implementations are required.


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

@Deprecated
protected HttpRequest buildHttpRequest(HttpMethod method,
                                                  String uriTemplate,
                                                  JsonHttpRequest jsonHttpRequest)
                                throws IOException
Deprecated. (scheduled to be removed in 1.8) Use buildHttpRequest(HttpMethod, GenericUrl, Object)

Create an HttpRequest suitable for use against this service. Subclasses may override if specific behavior is required.

Parameters:
method - HTTP Method type
uriTemplate - URI template for the path relative to the base URL. Must not start with a "/"
jsonHttpRequest - JSON HTTP Request type
Returns:
newly created HttpRequest
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

execute

@Deprecated
protected HttpResponse execute(HttpMethod method,
                                          String uriTemplate,
                                          Object body,
                                          JsonHttpRequest jsonHttpRequest)
                        throws IOException
Deprecated. (scheduled to be removed in 1.8) Use executeUnparsed(com.google.api.client.http.HttpMethod, com.google.api.client.http.GenericUrl, java.lang.Object)

Builds and executes a HttpRequest. Subclasses may override if specific behavior is required.

Parameters:
method - HTTP Method type
uriTemplate - URI template for the path relative to the base URL. Must not start with a "/"
body - A POJO that can be serialized into JSON or null for none
jsonHttpRequest - JSON HTTP Request type
Returns:
HttpRequest type
Throws:
IOException - if the request fails

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.

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:
HttpRequest type
Throws:
IOException - if the request fails
Since:
1.7

builder

public static JsonHttpClient.Builder builder(HttpTransport transport,
                                             JsonFactory jsonFactory,
                                             GenericUrl baseUrl)
Returns an instance of a new builder.

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 "/"


Copyright © 2011-2012 Google. All Rights Reserved.