public class

GoogleService

extends Service
implements AuthTokenFactory.TokenListener
java.lang.Object
   ↳ com.google.gdata.client.Service
     ↳ com.google.gdata.client.GoogleService
Known Direct Subclasses
Known Indirect Subclasses

Class Overview

The GoogleService class extends the basic GData Service abstraction to add support for authentication and cookies.

Summary

Nested Classes
class GoogleService.AccountDeletedException Authentication failed, account has been deleted. 
class GoogleService.AccountDisabledException Authentication failed, account has been disabled. 
class GoogleService.CaptchaRequiredException Authentication failed, CAPTCHA requires answering. 
class GoogleService.InvalidCredentialsException Authentication failed, invalid credentials presented to server. 
class GoogleService.NotVerifiedException Authentication failed, account has not been verified. 
class GoogleService.ServiceUnavailableException Authentication failed, authentication service not available. 
class GoogleService.SessionExpiredException Authentication failed, the token's session has expired. 
class GoogleService.TermsNotAgreedException Authentication failed, user did not agree to the terms of service. 
Fields
private AuthTokenFactory authTokenFactory
private CookieManager cookieManager
[Expand]
Inherited Fields
From class com.google.gdata.client.Service
Public Constructors
GoogleService(String serviceName, String applicationName)
Constructs a GoogleService instance connecting to the service with name serviceName for an application with the name applicationName.
GoogleService(String serviceName, String applicationName, String protocol, String domainName)
Constructs a GoogleService instance connecting to the service with name serviceName for an application with the name applicationName.
GoogleService(String applicationName, Service.GDataRequestFactory requestFactory, AuthTokenFactory authTokenFactory)
Constructs a GoogleService instance connecting to the service for an application with the name applicationName.
Public Methods
void addCookie(GoogleGDataRequest.GoogleCookie cookie)
Adds a new GoogleCookie instance to the cache.
<F extends IFeed> F batch(URL feedUrl, F inputFeed)
Executes several operations (insert, update or delete) on the entries that are part of the input IFeed.
Service.GDataRequest createRequest(Service.GDataRequest.RequestType type, URL requestUrl, ContentType contentType)
Creates a new GDataRequest for use by the service.
void delete(URL entryUrl, String etag)
Deletes an existing entry (and associated media content, if any) using the specified edit URL.
void delete(URL entryUrl)
Deletes an existing entry (and associated media content, if any) using the specified edit URL.
String getAuthToken(String username, String password, String captchaToken, String captchaAnswer, String serviceName, String applicationName)
Retrieves the authentication token for the provided set of credentials.
AuthTokenFactory getAuthTokenFactory()
Returns the AuthTokenFactory currently associated with the service.
CookieManager getCookieManager()
Returns the CookieManager currently associated with the service.
Set<GoogleGDataRequest.GoogleCookie> getCookies()
Returns the set of associated cookies returned by previous requests.
<E extends IEntry> E getEntry(URL entryUrl, Class<E> entryClass, String etag)
Returns an Atom entry instance given the URL of the entry, if its current entity tag is different than the provided value.
<E extends IEntry> E getEntry(URL entryUrl, Class<E> entryClass, DateTime ifModifiedSince)
Returns an Atom entry instance, given the URL of the entry and an if-modified-since date.
<F extends IFeed> F getFeed(URL feedUrl, Class<F> feedClass, DateTime ifModifiedSince)
Returns the Feed associated with a particular feed URL, if it's been modified since the specified date.
<F extends IFeed> F getFeed(Query query, Class<F> feedClass, DateTime ifModifiedSince)
Returns the Feed resulting from executing a query, if it's been modified since the specified date.
<F extends IFeed> F getFeed(Query query, Class<F> feedClass, String etag)
Returns the Feed resulting from query execution, if if the entity tag associated with it has changed.
<F extends IFeed> F getFeed(URL feedUrl, Class<F> feedClass, String etag)
Returns the Feed associated with a particular feed URL if the entity tag associated with it has changed.
boolean handlesCookies()
Returns true if the GoogleService is handling cookies.
<E extends IEntry> E insert(URL feedUrl, E entry)
Inserts a new IEntry into a feed associated with the target service.
static String makePostRequest(URL url, Map<String, String> parameters)
Makes a HTTP POST request to the provided url given the provided parameters.
void setAuthSubToken(String token)
Sets the AuthSub token to be used to authenticate a user.
void setAuthSubToken(String token, PrivateKey key)
Sets the AuthSub token to be used to authenticate a user.
void setAuthTokenFactory(AuthTokenFactory authTokenFactory)
Sets the AuthTokenFactory currently associated with the service.
void setCookieManager(CookieManager cookieManager)
Sets the CookieManager currently associated with the service.
void setHandlesCookies(boolean handlesCookies)
Enables or disables cookie handling.
void setOAuth2Credentials(Credential credential)
Sets the OAuth 2.0 credentials used to generate the authorization header.
void setOAuthCredentials(OAuthParameters parameters, OAuthSigner signer)
Sets the OAuth credentials used to generate the authorization header.
void setUserCredentials(String username, String password, String captchaToken, String captchaAnswer)
Sets the credentials of the user to authenticate requests to the server.
void setUserCredentials(String username, String password)
Sets the credentials of the user to authenticate requests to the server.
void setUserCredentials(String username, String password, String captchaToken, String captchaAnswer, ClientLoginAccountType accountType)
Sets the credentials of the user to authenticate requests to the server.
void setUserCredentials(String username, String password, ClientLoginAccountType accountType)
Sets the credentials of the user to authenticate requests to the server.
void setUserToken(String token)
Sets the AuthToken that should be used to authenticate requests to the server.
void tokenChanged(AuthTokenFactory.AuthToken newToken)
<E extends IEntry> E update(URL entryUrl, E entry)
Updates an existing IEntry by writing it to the specified entry edit URL.
Protected Methods
Service.GDataRequest createRequest(Query query, ContentType contentType)
Creates a new GDataRequest for querying the service.
URL handleRedirectException(RedirectRequiredException redirect)
Handles a redirect exception by generating the new URL to use for the redirect.
void handleSessionExpiredException(GoogleService.SessionExpiredException e)
Delegates session expired exception to AuthTokenFactory.
[Expand]
Inherited Methods
From class com.google.gdata.client.Service
From class java.lang.Object
From interface com.google.gdata.client.AuthTokenFactory.TokenListener

Fields

private AuthTokenFactory authTokenFactory

private CookieManager cookieManager

Public Constructors

public GoogleService (String serviceName, String applicationName)

Constructs a GoogleService instance connecting to the service with name serviceName for an application with the name applicationName. The default domain (www.google.com) and the default Google authentication methods will be used to authenticate. A simple cookie manager is used.

Parameters
serviceName The name of the Google service to which we are connecting. Sample names of services might include "cl" (Calendar), "mail" (GMail), or "blogger" (Blogger)
applicationName The name of the client application accessing the service. Application names should preferably have the format [company-id]-[app-name]-[app-version]. The name will be used by the Google servers to monitor the source of authentication.

public GoogleService (String serviceName, String applicationName, String protocol, String domainName)

Constructs a GoogleService instance connecting to the service with name serviceName for an application with the name applicationName. The service will authenticate at the provided domainName. The default Google authentication methods will be used to authenticate. A simple cookie manager is used.

Parameters
serviceName The name of the Google service to which we are connecting. Sample names of services might include "cl" (Calendar), "mail" (GMail), or "blogger" (Blogger)
applicationName The name of the client application accessing the service. Application names should preferably have the format [company-id]-[app-name]-[app-version]. The name will be used by the Google servers to monitor the source of authentication.
protocol Name of protocol to use for authentication ("http"/"https")
domainName The name of the domain hosting the login handler

public GoogleService (String applicationName, Service.GDataRequestFactory requestFactory, AuthTokenFactory authTokenFactory)

Constructs a GoogleService instance connecting to the service for an application with the name applicationName. The provided GDataRequestFactory will create requests, and the given AuthTokenFactory will be used to generate auth tokens. A simple cookie manager is used.

Parameters
applicationName The name of the client application accessing the service. Application names should preferably have the format [company-id]-[app-name]-[app-version]. The name will be used by the Google servers to monitor the source of authentication.
requestFactory The request factory that generates gdata requests
authTokenFactory The auth token factory that generates auth tokens

Public Methods

public void addCookie (GoogleGDataRequest.GoogleCookie cookie)

Adds a new GoogleCookie instance to the cache.

Parameters
cookie

public F batch (URL feedUrl, F inputFeed)

Executes several operations (insert, update or delete) on the entries that are part of the input IFeed. It will return another feed that describes what was done while executing these operations. It is possible for one batch operation to fail even though other operations have worked, so this method won't throw a ServiceException unless something really wrong is going on. You need to check the entries in the returned feed to know which operations succeeded and which operations failed (see BatchStatus and BatchInterrupted extensions.)

Parameters
feedUrl The POST URI associated with the target feed.
inputFeed A description of the operations to execute, described using tags in the batch: namespace
Returns
  • a feed with the result of each operation in a separate entry
Throws
IOException error communicating with the GData service.
ParseException error parsing the return entry data.
ServiceException insert request failed due to system error.
BatchInterruptedException if something really wrong was detected by the server while parsing the request, like invalid XML data. Some operations might have succeeded when this exception is thrown. Check getIFeed().

public Service.GDataRequest createRequest (Service.GDataRequest.RequestType type, URL requestUrl, ContentType contentType)

Creates a new GDataRequest for use by the service.

Clients should be sure to call end() on the returned request once they have finished using it.

Parameters
type
requestUrl
contentType
Throws
IOException
ServiceException

public void delete (URL entryUrl, String etag)

Deletes an existing entry (and associated media content, if any) using the specified edit URL. This delete is conditional upon the provided tag matching the current entity tag for the entry. If (and only if) they match, the deletion will be performed.

Parameters
entryUrl The edit or medit edit url associated with the resource.
etag The entity tag value that is the expected value for the target resource. A value of null will not set an etag precondition and a value of "*" will perform an unconditional delete.
Throws
IOException
ServiceException

public void delete (URL entryUrl)

Deletes an existing entry (and associated media content, if any) using the specified edit URL.

Parameters
entryUrl The edit or medit edit url associated with the resource.
Throws
IOException
ServiceException

public String getAuthToken (String username, String password, String captchaToken, String captchaAnswer, String serviceName, String applicationName)

Retrieves the authentication token for the provided set of credentials.

Parameters
username The name of the user (an email address)
password The password of the user
captchaToken The CAPTCHA token if CAPTCHA is required (Optional)
captchaAnswer The respective answer of the CAPTCHA token (Optional)
serviceName The name of the service to which a token is required
applicationName The application requesting the token
Returns
  • the token
Throws
AuthenticationException if authentication failed

public AuthTokenFactory getAuthTokenFactory ()

Returns the AuthTokenFactory currently associated with the service.

public CookieManager getCookieManager ()

Returns the CookieManager currently associated with the service.

public Set<GoogleGDataRequest.GoogleCookie> getCookies ()

Returns the set of associated cookies returned by previous requests.

public E getEntry (URL entryUrl, Class<E> entryClass, String etag)

Returns an Atom entry instance given the URL of the entry, if its current entity tag is different than the provided value.

Parameters
entryUrl Resource URL for the entry.
entryClass Class used to represent service entries, not null.
etag Used to provide an entity tag that indicates the entry should be returned only if the entity tag of the current representation is different from the provided value. A value of null indicates unconditional return.
Throws
IOException
ServiceException

public E getEntry (URL entryUrl, Class<E> entryClass, DateTime ifModifiedSince)

Returns an Atom entry instance, given the URL of the entry and an if-modified-since date.

Parameters
entryUrl Resource URL for the entry.
entryClass Class used to represent service entries, not null.
ifModifiedSince Used to set a precondition date that indicates the entry should be returned only if it has been modified after the specified date. A value of null indicates no precondition.
Returns
  • the entry referenced by the URL parameter.
Throws
IOException
ServiceException

public F getFeed (URL feedUrl, Class<F> feedClass, DateTime ifModifiedSince)

Returns the Feed associated with a particular feed URL, if it's been modified since the specified date.

Parameters
feedUrl The URL associated with a feed. This URL can include GData query parameters.
feedClass The class used to represent a service Feed.
ifModifiedSince Used to set a precondition date that indicates the feed should be returned only if it has been modified after the specified date. A value of null indicates no precondition.
Returns
  • Feed resource referenced by the input URL.
Throws
IOException
ServiceException

public F getFeed (Query query, Class<F> feedClass, DateTime ifModifiedSince)

Returns the Feed resulting from executing a query, if it's been modified since the specified date.

Parameters
query Feed query.
feedClass The class used to represent a service Feed.
ifModifiedSince Used to set a precondition date that indicates the feed should be returned only if it has been modified after the specified date. A value of null indicates no precondition.
Returns
  • Feed resource referenced by the input URL.
Throws
IOException
ServiceException

public F getFeed (Query query, Class<F> feedClass, String etag)

Returns the Feed resulting from query execution, if if the entity tag associated with it has changed.

Parameters
query Feed query.
feedClass The class used to represent query results.
etag Used to provide an entity tag that indicates the feed should be returned only if the entity tag of the current representation is different from the provided value. A value of null indicates unconditional return.
Throws
IOException
ServiceException

public F getFeed (URL feedUrl, Class<F> feedClass, String etag)

Returns the Feed associated with a particular feed URL if the entity tag associated with it has changed.

Parameters
feedUrl The URL associated with a feed. This URL can include GData query parameters.
feedClass The class used to represent a service Feed.
etag Used to provide an entity tag that indicates the feed should be returned only if the entity tag of the current representation is different from the provided value. A value of null indicates unconditional return.
Throws
IOException
ServiceException

public boolean handlesCookies ()

Returns true if the GoogleService is handling cookies.

public E insert (URL feedUrl, E entry)

Inserts a new IEntry into a feed associated with the target service. It will return the inserted entry, including any additional attributes or extensions set by the GData server.

Parameters
feedUrl The POST URI associated with the target feed.
entry The new entry to insert into the feed.
Returns
  • the newly inserted Entry returned by the service.
Throws
IOException
ServiceException

public static String makePostRequest (URL url, Map<String, String> parameters)

Makes a HTTP POST request to the provided url given the provided parameters. It returns the output from the POST handler as a String object.

Parameters
url The URL to post the request
parameters The parameters to post to the handler
Returns
  • the output from the handler
Throws
IOException if an I/O exception occurs while creating, writing, or reading the request

public void setAuthSubToken (String token)

Sets the AuthSub token to be used to authenticate a user.

Parameters
token The AuthSub token retrieved from Google

public void setAuthSubToken (String token, PrivateKey key)

Sets the AuthSub token to be used to authenticate a user. The token will be used in secure-mode, and the provided private key will be used to sign all requests.

Parameters
token The AuthSub token retrieved from Google
key The private key to be used to sign all requests

public void setAuthTokenFactory (AuthTokenFactory authTokenFactory)

Sets the AuthTokenFactory currently associated with the service.

Parameters
authTokenFactory Authentication factory

public void setCookieManager (CookieManager cookieManager)

Sets the CookieManager currently associated with the service.

Parameters
cookieManager Cookie manager

public void setHandlesCookies (boolean handlesCookies)

Enables or disables cookie handling.

Parameters
handlesCookies

public void setOAuth2Credentials (Credential credential)

Sets the OAuth 2.0 credentials used to generate the authorization header.

Parameters
credential The OAuth 2.0 credentials to use to generate the header

public void setOAuthCredentials (OAuthParameters parameters, OAuthSigner signer)

Sets the OAuth credentials used to generate the authorization header. This header needs to be set per request, as it depends on the request url. The following OAuth parameters are required:

  • oauth_consumer_key
  • oauth_token

Parameters
parameters The OAuth parameters to use to generate the header
signer The signing method to use for signing the header

public void setUserCredentials (String username, String password, String captchaToken, String captchaAnswer)

Sets the credentials of the user to authenticate requests to the server. A CAPTCHA token and a CAPTCHA answer can also be optionally provided to authenticate when the authentication server requires that a CAPTCHA be answered.

Parameters
username The name of the user (an email id)
password The password of the user
captchaToken The CAPTCHA token issued by the server
captchaAnswer The answer to the respective CAPTCHA token
Throws
AuthenticationException if authentication failed

public void setUserCredentials (String username, String password)

Sets the credentials of the user to authenticate requests to the server.

Parameters
username The name of the user (an email address)
password The password of the user
Throws
AuthenticationException if authentication failed.

public void setUserCredentials (String username, String password, String captchaToken, String captchaAnswer, ClientLoginAccountType accountType)

Sets the credentials of the user to authenticate requests to the server. A CAPTCHA token and a CAPTCHA answer can also be optionally provided to authenticate when the authentication server requires that a CAPTCHA be answered.

Parameters
username The name of the user (an email id)
password The password of the user
captchaToken The CAPTCHA token issued by the server
captchaAnswer The answer to the respective CAPTCHA token
accountType The account type: HOSTED, GOOGLE, or HOSTED_OR_GOOGLE
Throws
AuthenticationException if authentication failed

public void setUserCredentials (String username, String password, ClientLoginAccountType accountType)

Sets the credentials of the user to authenticate requests to the server.

Parameters
username The name of the user (an email address)
password The password of the user
accountType The account type: HOSTED, GOOGLE, or HOSTED_OR_GOOGLE
Throws
AuthenticationException if authentication failed.

public void setUserToken (String token)

Sets the AuthToken that should be used to authenticate requests to the server. This is useful if the caller has some other way of accessing the AuthToken, versus calling getAuthToken with credentials to request the AuthToken using ClientLogin.

Parameters
token The AuthToken in ascii form

public void tokenChanged (AuthTokenFactory.AuthToken newToken)

Parameters
newToken

public E update (URL entryUrl, E entry)

Updates an existing IEntry by writing it to the specified entry edit URL. The resulting Entry (after update) will be returned.

Parameters
entryUrl The edit URL associated with the entry.
entry The modified Entry to be written to the server.
Returns
  • the updated Entry returned by the service.
Throws
IOException
ServiceException

Protected Methods

protected Service.GDataRequest createRequest (Query query, ContentType contentType)

Creates a new GDataRequest for querying the service.

Clients should be sure to call end() on the returned request once they have finished using it.

Parameters
query
contentType
Throws
IOException
ServiceException

protected URL handleRedirectException (RedirectRequiredException redirect)

Handles a redirect exception by generating the new URL to use for the redirect.

Parameters
redirect

protected void handleSessionExpiredException (GoogleService.SessionExpiredException e)

Delegates session expired exception to AuthTokenFactory.

Parameters
e