|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.google.api.client.auth.oauth2.draft10.AccessProtectedResource
public class AccessProtectedResource
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.
| 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 |
|---|
public AccessProtectedResource(String accessToken,
AccessProtectedResource.Method method)
accessToken - access token or null for none (does not call
setAccessToken(String))method - method of accessing protected resources
public AccessProtectedResource(String accessToken,
AccessProtectedResource.Method method,
HttpTransport transport,
JsonFactory jsonFactory,
String authorizationServerUrl,
String clientId,
String clientSecret,
String refreshToken)
accessToken - access token or null for none (does not call
setAccessToken(String))method - method of accessing protected resourcestransport - HTTP transport for executing refresh token requestjsonFactory - JSON factory to use for parsing response for refresh token requestauthorizationServerUrl - encoded authorization server URLclientId - client identifierclientSecret - client secretrefreshToken - refresh token associated with the access token to be refreshed| Method Detail |
|---|
public final String getAccessToken()
null for none.
public final void setAccessToken(String accessToken)
accessToken - access token or null for nonepublic final AccessProtectedResource.Method getMethod()
public HttpTransport getTransport()
null for none.
public JsonFactory getJsonFactory()
null
for none.
public String getAuthorizationServerUrl()
null for none.
public String getClientId()
null for none.
public String getClientSecret()
null for none.
public String getRefreshToken()
null for
none.
public final boolean refreshToken()
throws IOException
getAccessToken() must wait until the new access token
has been retrieved.
IOException
public final void initialize(HttpRequest request)
throws IOException
HttpRequestInitializer
initialize in interface HttpRequestInitializerrequest - HTTP request
IOException
public void intercept(HttpRequest request)
throws IOException
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.
intercept in interface HttpExecuteInterceptorIOException
public boolean handleResponse(HttpRequest request,
HttpResponse response,
boolean retrySupported)
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.
handleResponse in interface HttpUnsuccessfulResponseHandlerrequest - Request object that can be read from for context or modified before retryresponse - Response to processretrySupported - 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).
protected boolean executeRefreshToken()
throws IOException
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.
IOException - I/O exceptionprotected void onAccessToken(String accessToken)
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.
accessToken - access token or null for none
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||