org.springframework.security.oauth.consumer
Class CoreOAuthConsumerSupport

java.lang.Object
  extended by org.springframework.security.oauth.consumer.CoreOAuthConsumerSupport
All Implemented Interfaces:
org.springframework.beans.factory.InitializingBean, OAuthConsumerSupport

public class CoreOAuthConsumerSupport
extends java.lang.Object
implements OAuthConsumerSupport, org.springframework.beans.factory.InitializingBean

Consumer-side support for OAuth. This support uses a URLConnection to interface with the OAuth provider. A proxy will be selected, but it is assumed that the javax.net.ssl.TrustManagers and other connection-related environment variables are already set up.

Author:
Ryan Heaton

Constructor Summary
CoreOAuthConsumerSupport()
           
 
Method Summary
 void afterPropertiesSet()
           
protected  java.net.URL configureURLForProtectedAccess(java.net.URL url, OAuthConsumerToken requestToken, ProtectedResourceDetails details, java.lang.String httpMethod)
          Internal use of configuring the URL for protected access, the resource details already having been loaded.
 java.net.URL configureURLForProtectedAccess(java.net.URL url, OAuthConsumerToken accessToken, java.lang.String httpMethod)
          Create a configured URL.
 OAuthConsumerToken getAccessToken(OAuthConsumerToken requestToken)
          Get an access token for a protected resource.
 java.lang.String getAuthorizationHeader(ProtectedResourceDetails details, OAuthConsumerToken accessToken, java.net.URL url, java.lang.String httpMethod)
          Get the authorization header using the given access token that should be applied to the specified URL.
 int getConnectionTimeout()
          The connection timeout (default 60 seconds).
 NonceFactory getNonceFactory()
          The nonce factory.
 java.lang.String getOAuthQueryString(ProtectedResourceDetails details, OAuthConsumerToken accessToken, java.net.URL url, java.lang.String httpMethod)
          Get the query string that is to be used in the given request.
 ProtectedResourceDetailsService getProtectedResourceDetailsService()
          The protected resource details service.
 java.net.ProxySelector getProxySelector()
          The proxy selector to use.
 int getReadTimeout()
          The read timeout (default 60 seconds).
protected  java.lang.String getSignatureBaseString(java.util.Map<java.lang.String,java.lang.String> oauthParams, java.net.URL requestURL, java.lang.String httpMethod)
          Get the signature base string for the specified parameters.
 OAuthSignatureMethodFactory getSignatureFactory()
          The signature factory to use.
 OAuthURLStreamHandlerFactory getStreamHandlerFactory()
          The URL stream handler factory for connections to an OAuth resource.
protected  OAuthConsumerToken getTokenFromProvider(ProtectedResourceDetails details, java.net.URL tokenURL, OAuthConsumerToken requestToken)
          Get the consumer token with the given parameters and URL.
 OAuthConsumerToken getUnauthorizedRequestToken(java.lang.String resourceId)
          Get an unauthorized request token for a protected resource.
protected  java.util.Map<java.lang.String,java.lang.String> loadOAuthParameters(ProtectedResourceDetails details, java.net.URL requestURL, OAuthConsumerToken requestToken, java.lang.String httpMethod)
          Loads the OAuth parameters for the given resource at the given URL and the given token.
protected  java.net.HttpURLConnection openConnection(java.net.URL requestTokenURL)
          Open a connection to the given URL.
 java.io.InputStream readProtectedResource(java.net.URL url, OAuthConsumerToken accessToken, java.lang.String httpMethod)
          Read a protected resource from the given URL using the specified access token and HTTP method.
protected  java.io.InputStream readResource(ProtectedResourceDetails details, java.net.URL url, OAuthConsumerToken token, java.lang.String httpMethod)
          Read a resource.
protected  java.net.Proxy selectProxy(java.net.URL requestTokenURL)
          Selects a proxy for the given URL.
 void setConnectionTimeout(int connectionTimeout)
          The connection timeout.
 void setNonceFactory(NonceFactory nonceFactory)
          The nonce factory.
 void setProtectedResourceDetailsService(ProtectedResourceDetailsService protectedResourceDetailsService)
          The protected resource details service.
 void setProxySelector(java.net.ProxySelector proxySelector)
          The proxy selector to use.
 void setReadTimeout(int readTimeout)
          The read timeout.
 void setSignatureFactory(OAuthSignatureMethodFactory signatureFactory)
          The signature factory to use.
 void setStreamHandlerFactory(OAuthURLStreamHandlerFactory streamHandlerFactory)
          The URL stream handler factory for connections to an OAuth resource.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CoreOAuthConsumerSupport

public CoreOAuthConsumerSupport()
Method Detail

afterPropertiesSet

public void afterPropertiesSet()
                        throws java.lang.Exception
Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
Throws:
java.lang.Exception

getUnauthorizedRequestToken

public OAuthConsumerToken getUnauthorizedRequestToken(java.lang.String resourceId)
                                               throws OAuthRequestFailedException
Description copied from interface: OAuthConsumerSupport
Get an unauthorized request token for a protected resource.

Specified by:
getUnauthorizedRequestToken in interface OAuthConsumerSupport
Parameters:
resourceId - The id of the protected resource for which to get a consumer token.
Returns:
The unauthorized request token.
Throws:
OAuthRequestFailedException

getAccessToken

public OAuthConsumerToken getAccessToken(OAuthConsumerToken requestToken)
                                  throws OAuthRequestFailedException
Description copied from interface: OAuthConsumerSupport
Get an access token for a protected resource.

Specified by:
getAccessToken in interface OAuthConsumerSupport
Parameters:
requestToken - The (presumably authorized) request token.
Returns:
The access token.
Throws:
OAuthRequestFailedException

readProtectedResource

public java.io.InputStream readProtectedResource(java.net.URL url,
                                                 OAuthConsumerToken accessToken,
                                                 java.lang.String httpMethod)
                                          throws OAuthRequestFailedException
Description copied from interface: OAuthConsumerSupport
Read a protected resource from the given URL using the specified access token and HTTP method.

Specified by:
readProtectedResource in interface OAuthConsumerSupport
Parameters:
url - The URL.
accessToken - The access token.
httpMethod - The HTTP method.
Returns:
The protected resource.
Throws:
OAuthRequestFailedException

readResource

protected java.io.InputStream readResource(ProtectedResourceDetails details,
                                           java.net.URL url,
                                           OAuthConsumerToken token,
                                           java.lang.String httpMethod)
Read a resource.

Parameters:
details - The details of the resource.
url - The URL of the resource.
token - The token.
httpMethod - The http method.
Returns:
The resource.

configureURLForProtectedAccess

public java.net.URL configureURLForProtectedAccess(java.net.URL url,
                                                   OAuthConsumerToken accessToken,
                                                   java.lang.String httpMethod)
                                            throws OAuthRequestFailedException
Create a configured URL. If the HTTP method to access the resource is "POST" or "PUT" and the "Authorization" header isn't supported, then the OAuth parameters will be expected to be sent in the body of the request. Otherwise, you can assume that the given URL is ready to be used without further work.

Specified by:
configureURLForProtectedAccess in interface OAuthConsumerSupport
Parameters:
url - The base URL.
accessToken - The access token.
httpMethod - The HTTP method.
Returns:
The configured URL.
Throws:
OAuthRequestFailedException

configureURLForProtectedAccess

protected java.net.URL configureURLForProtectedAccess(java.net.URL url,
                                                      OAuthConsumerToken requestToken,
                                                      ProtectedResourceDetails details,
                                                      java.lang.String httpMethod)
Internal use of configuring the URL for protected access, the resource details already having been loaded.

Parameters:
url - The URL.
requestToken - The request token.
details - The details.
httpMethod - The http method.
Returns:
The configured URL.

getAuthorizationHeader

public java.lang.String getAuthorizationHeader(ProtectedResourceDetails details,
                                               OAuthConsumerToken accessToken,
                                               java.net.URL url,
                                               java.lang.String httpMethod)
Description copied from interface: OAuthConsumerSupport
Get the authorization header using the given access token that should be applied to the specified URL.

Specified by:
getAuthorizationHeader in interface OAuthConsumerSupport
Parameters:
details - The details of the protected resource.
accessToken - The access token.
url - The URL of the request.
httpMethod - The http method for the protected resource.
Returns:
The authorization header, or null if the authorization header isn't supported by the provider of this resource.

getOAuthQueryString

public java.lang.String getOAuthQueryString(ProtectedResourceDetails details,
                                            OAuthConsumerToken accessToken,
                                            java.net.URL url,
                                            java.lang.String httpMethod)
Description copied from interface: OAuthConsumerSupport
Get the query string that is to be used in the given request. The query string will include any custom query parameters in the URL and any necessary OAuth parameters. Note, however, that an OAuth parameter is not considered "necessary" if the provider of the resource supports the authorization header.

The query string is to be used by either applying it to the URL (for HTTP GET) or putting it in the body of the request (for HTTP POST).

Specified by:
getOAuthQueryString in interface OAuthConsumerSupport
Parameters:
details - The resource details.
accessToken - The access token.
url - The URL
httpMethod - The http method.
Returns:
The query string.

getTokenFromProvider

protected OAuthConsumerToken getTokenFromProvider(ProtectedResourceDetails details,
                                                  java.net.URL tokenURL,
                                                  OAuthConsumerToken requestToken)
Get the consumer token with the given parameters and URL. The determination of whether the retrieved token is an access token depends on whether a request token is provided.

Parameters:
details - The resource details.
tokenURL - The token URL.
requestToken - The request token, or null if none.
Returns:
The token.

loadOAuthParameters

protected java.util.Map<java.lang.String,java.lang.String> loadOAuthParameters(ProtectedResourceDetails details,
                                                                               java.net.URL requestURL,
                                                                               OAuthConsumerToken requestToken,
                                                                               java.lang.String httpMethod)
Loads the OAuth parameters for the given resource at the given URL and the given token. These parameters include any query parameters on the URL since they are included in the signature.

Parameters:
details - The resource details.
requestURL - The request URL.
requestToken - The request token.
httpMethod - The http method.
Returns:
The parameters.

openConnection

protected java.net.HttpURLConnection openConnection(java.net.URL requestTokenURL)
Open a connection to the given URL.

Parameters:
requestTokenURL - The request token URL.
Returns:
The HTTP URL connection.

selectProxy

protected java.net.Proxy selectProxy(java.net.URL requestTokenURL)
Selects a proxy for the given URL.

Parameters:
requestTokenURL - The URL
Returns:
The proxy.

getSignatureBaseString

protected java.lang.String getSignatureBaseString(java.util.Map<java.lang.String,java.lang.String> oauthParams,
                                                  java.net.URL requestURL,
                                                  java.lang.String httpMethod)
Get the signature base string for the specified parameters.

Parameters:
oauthParams - The parameters.
requestURL - The request URL.
httpMethod - The http method.
Returns:
The signature base string.

getProtectedResourceDetailsService

public ProtectedResourceDetailsService getProtectedResourceDetailsService()
The protected resource details service.

Returns:
The protected resource details service.

setProtectedResourceDetailsService

public void setProtectedResourceDetailsService(ProtectedResourceDetailsService protectedResourceDetailsService)
The protected resource details service.

Parameters:
protectedResourceDetailsService - The protected resource details service.

getStreamHandlerFactory

public OAuthURLStreamHandlerFactory getStreamHandlerFactory()
The URL stream handler factory for connections to an OAuth resource.

Returns:
The URL stream handler factory for connections to an OAuth resource.

setStreamHandlerFactory

public void setStreamHandlerFactory(OAuthURLStreamHandlerFactory streamHandlerFactory)
The URL stream handler factory for connections to an OAuth resource.

Parameters:
streamHandlerFactory - The URL stream handler factory for connections to an OAuth resource.

getNonceFactory

public NonceFactory getNonceFactory()
The nonce factory.

Returns:
The nonce factory.

setNonceFactory

public void setNonceFactory(NonceFactory nonceFactory)
The nonce factory.

Parameters:
nonceFactory - The nonce factory.

getSignatureFactory

public OAuthSignatureMethodFactory getSignatureFactory()
The signature factory to use.

Returns:
The signature factory to use.

setSignatureFactory

public void setSignatureFactory(OAuthSignatureMethodFactory signatureFactory)
The signature factory to use.

Parameters:
signatureFactory - The signature factory to use.

getProxySelector

public java.net.ProxySelector getProxySelector()
The proxy selector to use.

Returns:
The proxy selector to use.

setProxySelector

public void setProxySelector(java.net.ProxySelector proxySelector)
The proxy selector to use.

Parameters:
proxySelector - The proxy selector to use.

getConnectionTimeout

public int getConnectionTimeout()
The connection timeout (default 60 seconds).

Returns:
The connection timeout.

setConnectionTimeout

public void setConnectionTimeout(int connectionTimeout)
The connection timeout.

Parameters:
connectionTimeout - The connection timeout.

getReadTimeout

public int getReadTimeout()
The read timeout (default 60 seconds).

Returns:
The read timeout.

setReadTimeout

public void setReadTimeout(int readTimeout)
The read timeout.

Parameters:
readTimeout - The read timeout.


Copyright © 2008. All Rights Reserved.