com.google.api.client.auth.oauth2.draft10
Class AccessProtectedResource

java.lang.Object
  extended by com.google.api.client.auth.oauth2.draft10.AccessProtectedResource
All Implemented Interfaces:
HttpExecuteInterceptor, HttpRequestInitializer, HttpUnsuccessfulResponseHandler

public class AccessProtectedResource
extends Object
implements HttpExecuteInterceptor, HttpRequestInitializer, HttpUnsuccessfulResponseHandler

Thread-safe OAuth 2.0 (draft 10) method for specifying and refreshing the access token parameter as a request parameter as specified in Accessing a Protected Resource.

Sample usage, taking advantage that this class implements HttpRequestInitializer:

  public static HttpRequestFactory createRequestFactoryNoRefresh(HttpTransport transport,
      JsonFactory jsonFactory, AccessTokenResponse accessTokenResponse) {
    return transport.createRequestFactory(new AccessProtectedResource(
        accessTokenResponse.accessToken, Method.AUTHORIZATION_HEADER));
  }

  public static HttpRequestFactory createRequestFactory(HttpTransport transport,
      JsonFactory jsonFactory, AccessTokenResponse accessTokenResponse) {
    return transport.createRequestFactory(new AccessProtectedResource(
        accessTokenResponse.accessToken, Method.AUTHORIZATION_HEADER, transport, jsonFactory,
        "https://server.example.com/authorize", "s6BhdRkqt3", "gX1fBat3bV",
        accessTokenResponse.refreshToken));
  }
 

If you need to persist the access token in a data store, override onAccessToken(String).

If you have a custom request initializer, request execute interceptor, or unsuccessful response handler, take a look at the sample usage for HttpExecuteInterceptor and HttpUnsuccessfulResponseHandler, which are interfaces that this class also implements.

Since:
1.4
Author:
Yaniv Inbar

Nested Class Summary
static class AccessProtectedResource.Method
          Method of accessing protected resources.
 
Constructor Summary
AccessProtectedResource(String accessToken, AccessProtectedResource.Method method)
           
AccessProtectedResource(String accessToken, AccessProtectedResource.Method method, HttpTransport transport, JsonFactory jsonFactory, String authorizationServerUrl, String clientId, String clientSecret, String refreshToken)
          Constructor to use to be able to refresh token when an access token expires.
 
Method Summary
protected  boolean executeRefreshToken()
          Request a new access token from the authorization endpoint.
 String getAccessToken()
          Returns the access token or null for none.
 String getAuthorizationServerUrl()
          Returns the encoded authorization server URL or null for none.
 String getClientId()
          Returns the client identifier or null for none.
 String getClientSecret()
          Returns the client secret or null for none.
 JsonFactory getJsonFactory()
          Returns the JSON factory to use for parsing response for refresh token request or null for none.
 AccessProtectedResource.Method getMethod()
          Returns the method of accessing protected resources.
 String getRefreshToken()
          Returns the refresh token associated with the access token to be refreshed or null for none.
 HttpTransport getTransport()
          Return the HTTP transport for executing refresh token request or null for none.
 boolean handleResponse(HttpRequest request, HttpResponse response, boolean retrySupported)
          Handler that will be invoked when an abnormal response is received.
 void initialize(HttpRequest request)
          Initializes a request.
 void intercept(HttpRequest request)
          Invoked at the start of HttpRequest.execute() before executing the HTTP request.
protected  void onAccessToken(String accessToken)
          Notifies of a new access token.
 boolean refreshToken()
          Request a new access token from the authorization endpoint, acquiring a lock on the access token so other threads calling getAccessToken() must wait until the new access token has been retrieved.
 void setAccessToken(String accessToken)
          Sets the access token.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AccessProtectedResource

public AccessProtectedResource(String accessToken,
                               AccessProtectedResource.Method method)
Parameters:
accessToken - access token or null for none (does not call setAccessToken(String))
method - method of accessing protected resources

AccessProtectedResource

public AccessProtectedResource(String accessToken,
                               AccessProtectedResource.Method method,
                               HttpTransport transport,
                               JsonFactory jsonFactory,
                               String authorizationServerUrl,
                               String clientId,
                               String clientSecret,
                               String refreshToken)
Constructor to use to be able to refresh token when an access token expires.

Parameters:
accessToken - access token or null for none (does not call setAccessToken(String))
method - method of accessing protected resources
transport - HTTP transport for executing refresh token request
jsonFactory - JSON factory to use for parsing response for refresh token request
authorizationServerUrl - encoded authorization server URL
clientId - client identifier
clientSecret - client secret
refreshToken - refresh token associated with the access token to be refreshed
Method Detail

getAccessToken

public final String getAccessToken()
Returns the access token or null for none.


setAccessToken

public final void setAccessToken(String accessToken)
Sets the access token.

Parameters:
accessToken - access token or null for none

getMethod

public final AccessProtectedResource.Method getMethod()
Returns the method of accessing protected resources.


getTransport

public HttpTransport getTransport()
Return the HTTP transport for executing refresh token request or null for none.


getJsonFactory

public JsonFactory getJsonFactory()
Returns the JSON factory to use for parsing response for refresh token request or null for none.


getAuthorizationServerUrl

public String getAuthorizationServerUrl()
Returns the encoded authorization server URL or null for none.


getClientId

public String getClientId()
Returns the client identifier or null for none.


getClientSecret

public String getClientSecret()
Returns the client secret or null for none.


getRefreshToken

public String getRefreshToken()
Returns the refresh token associated with the access token to be refreshed or null for none.


refreshToken

public final boolean refreshToken()
                           throws IOException
Request a new access token from the authorization endpoint, acquiring a lock on the access token so other threads calling getAccessToken() must wait until the new access token has been retrieved.

Returns:
whether a new access token was retrieved
Throws:
IOException

initialize

public final void initialize(HttpRequest request)
                      throws IOException
Description copied from interface: HttpRequestInitializer
Initializes a request.

Specified by:
initialize in interface HttpRequestInitializer
Parameters:
request - HTTP request
Throws:
IOException

intercept

public void intercept(HttpRequest request)
               throws IOException
Invoked at the start of HttpRequest.execute() before executing the HTTP request.

Default implementation checks if there is an access token and sets the access token parameter using the appropriate method. Subclasses may override.

Specified by:
intercept in interface HttpExecuteInterceptor
Throws:
IOException

handleResponse

public boolean handleResponse(HttpRequest request,
                              HttpResponse response,
                              boolean retrySupported)
Handler that will be invoked when an abnormal response is received. There are a few simple rules that one must follow:

Default implementation checks for a 401 error code and calls refreshToken(). If executeRefreshToken() throws an I/O exception, this implementation will log the exception and return false. Subclasses may override.

Specified by:
handleResponse in interface HttpUnsuccessfulResponseHandler
Parameters:
request - Request object that can be read from for context or modified before retry
response - Response to process
retrySupported - Whether there will actually be a retry if this handler return true. Some handlers may want to have an effect only when there will actually be a retry after they handle their event (e.g. a handler that implements exponential backoff).
Returns:
Whether or not this handler has made a change that will require the request to be re-sent.

executeRefreshToken

protected boolean executeRefreshToken()
                               throws IOException
Request a new access token from the authorization endpoint.

Default implementation executes the refresh token grant parameter passed to the constructor or false if it was null. Subclasses may override. If a new access token was retrieved, implementations must call setAccessToken(String). Implementations can assume proper thread synchronization is already taken care of inside refreshToken(), where this is called from.

Returns:
whether a new access token was retrieved
Throws:
IOException - I/O exception

onAccessToken

protected void onAccessToken(String accessToken)
Notifies of a new access token.

Default implementation does nothing, but subclasses may override in order to provide functionality like persisting the access token in a data store. Implementations can assume proper thread synchronization is already taken care of inside setAccessToken(String), where this is called from.

Parameters:
accessToken - access token or null for none


Copyright © 2010-2011 Google. All Rights Reserved.