|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectoauth.signpost.AbstractOAuthProvider
public abstract class AbstractOAuthProvider
ABC for all provider implementations. If you're writing a custom provider, you will probably inherit from this class, since it takes a lot of work from you.
Constructor Summary | |
---|---|
AbstractOAuthProvider(java.lang.String requestTokenEndpointUrl,
java.lang.String accessTokenEndpointUrl,
java.lang.String authorizationWebsiteUrl)
|
Method Summary | |
---|---|
protected void |
closeConnection(HttpRequest request,
HttpResponse response)
Called when the connection is being finalized after receiving the response. |
protected abstract HttpRequest |
createRequest(java.lang.String endpointUrl)
Overrride this method if you want to customize the logic for building a request object for the given endpoint URL. |
java.lang.String |
getAccessTokenEndpointUrl()
|
java.lang.String |
getAuthorizationWebsiteUrl()
|
java.util.Map<java.lang.String,java.lang.String> |
getRequestHeaders()
|
java.lang.String |
getRequestTokenEndpointUrl()
|
protected java.lang.String |
getResponseParameter(java.lang.String key)
Returns a single query parameter as served by the service provider in a token reply. |
HttpParameters |
getResponseParameters()
Any additional non-OAuth parameters returned in the response body of a token request can be obtained through this method. |
protected void |
handleUnexpectedResponse(int statusCode,
HttpResponse response)
|
boolean |
isOAuth10a()
|
void |
removeListener(OAuthProviderListener listener)
|
void |
retrieveAccessToken(OAuthConsumer consumer,
java.lang.String oauthVerifier)
Queries the service provider for an access token. |
java.lang.String |
retrieveRequestToken(OAuthConsumer consumer,
java.lang.String callbackUrl)
Queries the service provider for a request token. |
protected void |
retrieveToken(OAuthConsumer consumer,
java.lang.String endpointUrl,
java.lang.String... additionalParameters)
Implemented by subclasses. |
protected abstract HttpResponse |
sendRequest(HttpRequest request)
Override this method if you want to customize the logic for how the given request is sent to the server. |
void |
setListener(OAuthProviderListener listener)
|
void |
setOAuth10a(boolean isOAuth10aProvider)
|
void |
setRequestHeader(java.lang.String header,
java.lang.String value)
Use this method to set custom HTTP headers to be used for the requests which are sent to retrieve tokens. |
void |
setResponseParameters(HttpParameters parameters)
Subclasses must use this setter to preserve any non-OAuth query parameters contained in the server response. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public AbstractOAuthProvider(java.lang.String requestTokenEndpointUrl, java.lang.String accessTokenEndpointUrl, java.lang.String authorizationWebsiteUrl)
Method Detail |
---|
public java.lang.String retrieveRequestToken(OAuthConsumer consumer, java.lang.String callbackUrl) throws OAuthMessageSignerException, OAuthNotAuthorizedException, OAuthExpectationFailedException, OAuthCommunicationException
OAuthProvider
Pre-conditions: the given OAuthConsumer
must have a valid
consumer key and consumer secret already set.
Post-conditions: the given OAuthConsumer
will have an
unauthorized request token and token secret set.
retrieveRequestToken
in interface OAuthProvider
consumer
- the OAuthConsumer
that should be used to sign the requestcallbackUrl
- Pass an actual URL if your app can receive callbacks and you want
to get informed about the result of the authorization process.
Pass OAuth.OUT_OF_BAND
if the service provider implements
OAuth 1.0a and your app cannot receive callbacks. Pass null if the
service provider implements OAuth 1.0 and your app cannot receive
callbacks. Please note that some services (among them Twitter)
will fail authorization if you pass a callback URL but register
your application as a desktop app (which would only be able to
handle OOB requests).
OAuthMessageSignerException
- if signing the request failed
OAuthNotAuthorizedException
- if the service provider rejected the consumer
OAuthExpectationFailedException
- if required parameters were not correctly set by the consumer or
service provider
OAuthCommunicationException
- if server communication failedpublic void retrieveAccessToken(OAuthConsumer consumer, java.lang.String oauthVerifier) throws OAuthMessageSignerException, OAuthNotAuthorizedException, OAuthExpectationFailedException, OAuthCommunicationException
OAuthProvider
Pre-conditions: the given OAuthConsumer
must have a valid
consumer key, consumer secret, authorized request token and token secret
already set.
Post-conditions: the given OAuthConsumer
will have an
access token and token secret set.
retrieveAccessToken
in interface OAuthProvider
consumer
- the OAuthConsumer
that should be used to sign the requestoauthVerifier
- NOTE: Only applies to service providers implementing OAuth
1.0a. Set to null if the service provider is still using OAuth
1.0. The verification code issued by the service provider
after the the user has granted the consumer authorization. If the
callback method provided in the previous step was
OAuth.OUT_OF_BAND
, then you must ask the user for this
value. If your app has received a callback, the verfication code
was passed as part of that request instead.
OAuthMessageSignerException
- if signing the request failed
OAuthNotAuthorizedException
- if the service provider rejected the consumer
OAuthExpectationFailedException
- if required parameters were not correctly set by the consumer or
service provider
OAuthCommunicationException
- if server communication failedprotected void retrieveToken(OAuthConsumer consumer, java.lang.String endpointUrl, java.lang.String... additionalParameters) throws OAuthMessageSignerException, OAuthCommunicationException, OAuthNotAuthorizedException, OAuthExpectationFailedException
Implemented by subclasses. The responsibility of this method is to contact the service provider at the given endpoint URL and fetch a request or access token. What kind of token is retrieved solely depends on the URL being used.
Correct implementations of this method must guarantee the following post-conditions:
OAuthConsumer
passed to this method must have a valid
OAuth.OAUTH_TOKEN
and OAuth.OAUTH_TOKEN_SECRET
set by
calling OAuthConsumer.setTokenWithSecret(String, String)
getResponseParameters()
must return the set of query
parameters served by the service provider in the token response, with all
OAuth specific parameters being removed
consumer
- the OAuthConsumer
that should be used to sign the requestendpointUrl
- the URL at which the service provider serves the OAuth token that
is to be fetchedadditionalParameters
- you can pass parameters here (typically OAuth parameters such as
oauth_callback or oauth_verifier) which will go directly into the
signer, i.e. you don't have to put them into the request first,
just so the consumer pull them out again. Pass them sequentially
in key/value order.
OAuthMessageSignerException
- if signing the token request fails
OAuthCommunicationException
- if a network communication error occurs
OAuthNotAuthorizedException
- if the server replies 401 - Unauthorized
OAuthExpectationFailedException
- if an expectation has failed, e.g. because the server didn't
reply in the expected formatprotected void handleUnexpectedResponse(int statusCode, HttpResponse response) throws java.lang.Exception
java.lang.Exception
protected abstract HttpRequest createRequest(java.lang.String endpointUrl) throws java.lang.Exception
endpointUrl
- the URL to which the request will go
java.lang.Exception
- if something breaksprotected abstract HttpResponse sendRequest(HttpRequest request) throws java.lang.Exception
request
- the request to send
java.lang.Exception
- if something breaksprotected void closeConnection(HttpRequest request, HttpResponse response) throws java.lang.Exception
request
- the request that has been sentresponse
- the response that has been received
java.lang.Exception
- if something breakspublic HttpParameters getResponseParameters()
OAuthProvider
getResponseParameters
in interface OAuthProvider
protected java.lang.String getResponseParameter(java.lang.String key)
setResponseParameters(oauth.signpost.http.HttpParameters)
with the set of
parameters before using this method.
key
- the parameter name
public void setResponseParameters(HttpParameters parameters)
OAuthProvider
setResponseParameters
in interface OAuthProvider
parameters
- the map of query parameters served by the service provider in the
token responsepublic void setOAuth10a(boolean isOAuth10aProvider)
setOAuth10a
in interface OAuthProvider
isOAuth10aProvider
- set to true if the service provider supports OAuth 1.0a. Note that
you need only call this method if you reconstruct a provider
object in between calls to retrieveRequestToken() and
retrieveAccessToken() (i.e. if the object state isn't preserved).
If instead those two methods are called on the same provider
instance, this flag will be deducted automatically based on the
server response during retrieveRequestToken(), so you can simply
ignore this method.public boolean isOAuth10a()
isOAuth10a
in interface OAuthProvider
public java.lang.String getRequestTokenEndpointUrl()
getRequestTokenEndpointUrl
in interface OAuthProvider
public java.lang.String getAccessTokenEndpointUrl()
getAccessTokenEndpointUrl
in interface OAuthProvider
public java.lang.String getAuthorizationWebsiteUrl()
getAuthorizationWebsiteUrl
in interface OAuthProvider
public void setRequestHeader(java.lang.String header, java.lang.String value)
OAuthProvider
OAuthProviderListener
to customize requests.
setRequestHeader
in interface OAuthProvider
header
- The header name (e.g. 'WWW-Authenticate')value
- The header value (e.g. 'realm=www.example.com')public java.util.Map<java.lang.String,java.lang.String> getRequestHeaders()
getRequestHeaders
in interface OAuthProvider
OAuthProvider.setRequestHeader(java.lang.String, java.lang.String)
public void setListener(OAuthProviderListener listener)
setListener
in interface OAuthProvider
public void removeListener(OAuthProviderListener listener)
removeListener
in interface OAuthProvider
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |