net.oauth.client
Class OAuthClient

java.lang.Object
  extended by net.oauth.client.OAuthClient

public class OAuthClient
extends java.lang.Object

Methods for an OAuth consumer to request tokens from a service provider.

This class can also be used to request access to protected resources, in some cases. But not in all cases. For example, this class can't handle arbitrary HTTP headers.

Methods of this class return a response as an OAuthMessage, from which you can get a body or parameters but not both. Calling a getParameter method will read and close the body (like readBodyAsString), so you can't read it later. If you read or close the body first, then getParameter can't read it. The response headers should tell you whether the response contains encoded parameters, that is whether you should call getParameter or not.

Methods of this class don't follow redirects. When they receive a redirect response, they throw an OAuthProblemException, with properties HttpResponseMessage.STATUS_CODE = the redirect code HttpResponseMessage.LOCATION = the redirect URL. Such a redirect can't be handled at the HTTP level, if the second request must carry another OAuth signature (with different parameters). For example, Google's Service Provider routinely redirects requests for access to protected resources, and requires the redirected request to be signed.

Author:
John Kristian

Nested Class Summary
static class OAuthClient.ParameterStyle
          Deprecated. use net.oauth.ParameterStyle.
 
Field Summary
static java.lang.String ACCEPT_ENCODING
          Deprecated. use OAuthConsumer.ACCEPT_ENCODING instead
protected static java.lang.String CONTENT_LENGTH
           
protected static java.lang.String DELETE
           
protected  java.util.Map<java.lang.String,java.lang.Object> httpParameters
           
static java.lang.String PARAMETER_STYLE
          The name of the OAuthConsumer property whose value is the ParameterStyle to be used by invoke.
protected static java.lang.String POST
           
protected static java.lang.String PUT
           
 
Constructor Summary
OAuthClient(HttpClient http)
           
 
Method Summary
 OAuthResponseMessage access(OAuthMessage request, OAuthClient.ParameterStyle style)
          Deprecated.  
 OAuthResponseMessage access(OAuthMessage request, ParameterStyle style)
          Send a request and return the response.
 OAuthMessage getAccessToken(OAuthAccessor accessor, java.lang.String httpMethod, java.util.Collection<? extends java.util.Map.Entry> parameters)
          Get an access token from the service provider, in exchange for an authorized request token.
 HttpClient getHttpClient()
           
 java.util.Map<java.lang.String,java.lang.Object> getHttpParameters()
          HTTP client parameters, as a map from parameter name to value.
 void getRequestToken(OAuthAccessor accessor)
          Get a fresh request token from the service provider.
 void getRequestToken(OAuthAccessor accessor, java.lang.String httpMethod)
          Get a fresh request token from the service provider.
 void getRequestToken(OAuthAccessor accessor, java.lang.String httpMethod, java.util.Collection<? extends java.util.Map.Entry> parameters)
          Get a fresh request token from the service provider.
 OAuthMessage getRequestTokenResponse(OAuthAccessor accessor, java.lang.String httpMethod, java.util.Collection<? extends java.util.Map.Entry> parameters)
          Get a fresh request token from the service provider.
 OAuthMessage invoke(OAuthAccessor accessor, java.lang.String url, java.util.Collection<? extends java.util.Map.Entry> parameters)
          Construct a request message, send it to the service provider and get the response.
 OAuthMessage invoke(OAuthAccessor accessor, java.lang.String httpMethod, java.lang.String url, java.util.Collection<? extends java.util.Map.Entry> parameters)
          Construct a request message, send it to the service provider and get the response.
 OAuthMessage invoke(OAuthMessage request, OAuthClient.ParameterStyle style)
          Deprecated.  
 OAuthMessage invoke(OAuthMessage request, ParameterStyle style)
          Send a request message to the service provider and get the response.
 void setHttpClient(HttpClient http)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

httpParameters

protected final java.util.Map<java.lang.String,java.lang.Object> httpParameters

PARAMETER_STYLE

public static final java.lang.String PARAMETER_STYLE
The name of the OAuthConsumer property whose value is the ParameterStyle to be used by invoke.

See Also:
Constant Field Values

ACCEPT_ENCODING

@Deprecated
public static final java.lang.String ACCEPT_ENCODING
Deprecated. use OAuthConsumer.ACCEPT_ENCODING instead
The name of the OAuthConsumer property whose value is the Accept-Encoding header in HTTP requests.

See Also:
Constant Field Values

PUT

protected static final java.lang.String PUT
See Also:
Constant Field Values

POST

protected static final java.lang.String POST
See Also:
Constant Field Values

DELETE

protected static final java.lang.String DELETE
See Also:
Constant Field Values

CONTENT_LENGTH

protected static final java.lang.String CONTENT_LENGTH
See Also:
Constant Field Values
Constructor Detail

OAuthClient

public OAuthClient(HttpClient http)
Method Detail

setHttpClient

public void setHttpClient(HttpClient http)

getHttpClient

public HttpClient getHttpClient()

getHttpParameters

public java.util.Map<java.lang.String,java.lang.Object> getHttpParameters()
HTTP client parameters, as a map from parameter name to value.

See Also:
for parameter names.

getRequestToken

public void getRequestToken(OAuthAccessor accessor)
                     throws java.io.IOException,
                            OAuthException,
                            java.net.URISyntaxException
Get a fresh request token from the service provider.

Parameters:
accessor - should contain a consumer that contains a non-null consumerKey and consumerSecret. Also, accessor.consumer.serviceProvider.requestTokenURL should be the URL (determined by the service provider) for getting a request token.
Throws:
OAuthProblemException - the HTTP response status code was not 200 (OK)
java.io.IOException
OAuthException
java.net.URISyntaxException

getRequestToken

public void getRequestToken(OAuthAccessor accessor,
                            java.lang.String httpMethod)
                     throws java.io.IOException,
                            OAuthException,
                            java.net.URISyntaxException
Get a fresh request token from the service provider.

Parameters:
accessor - should contain a consumer that contains a non-null consumerKey and consumerSecret. Also, accessor.consumer.serviceProvider.requestTokenURL should be the URL (determined by the service provider) for getting a request token.
httpMethod - typically OAuthMessage.POST or OAuthMessage.GET, or null to use the default method.
Throws:
OAuthProblemException - the HTTP response status code was not 200 (OK)
java.io.IOException
OAuthException
java.net.URISyntaxException

getRequestToken

public void getRequestToken(OAuthAccessor accessor,
                            java.lang.String httpMethod,
                            java.util.Collection<? extends java.util.Map.Entry> parameters)
                     throws java.io.IOException,
                            OAuthException,
                            java.net.URISyntaxException
Get a fresh request token from the service provider.

Parameters:
accessor - should contain a consumer that contains a non-null consumerKey and consumerSecret. Also, accessor.consumer.serviceProvider.requestTokenURL should be the URL (determined by the service provider) for getting a request token.
httpMethod - typically OAuthMessage.POST or OAuthMessage.GET, or null to use the default method.
parameters - additional parameters for this request, or null to indicate that there are no additional parameters.
Throws:
OAuthProblemException - the HTTP response status code was not 200 (OK)
java.io.IOException
OAuthException
java.net.URISyntaxException

getRequestTokenResponse

public OAuthMessage getRequestTokenResponse(OAuthAccessor accessor,
                                            java.lang.String httpMethod,
                                            java.util.Collection<? extends java.util.Map.Entry> parameters)
                                     throws java.io.IOException,
                                            OAuthException,
                                            java.net.URISyntaxException
Get a fresh request token from the service provider.

Parameters:
accessor - should contain a consumer that contains a non-null consumerKey and consumerSecret. Also, accessor.consumer.serviceProvider.requestTokenURL should be the URL (determined by the service provider) for getting a request token.
httpMethod - typically OAuthMessage.POST or OAuthMessage.GET, or null to use the default method.
parameters - additional parameters for this request, or null to indicate that there are no additional parameters.
Returns:
the response from the service provider
Throws:
OAuthProblemException - the HTTP response status code was not 200 (OK)
java.io.IOException
OAuthException
java.net.URISyntaxException

getAccessToken

public OAuthMessage getAccessToken(OAuthAccessor accessor,
                                   java.lang.String httpMethod,
                                   java.util.Collection<? extends java.util.Map.Entry> parameters)
                            throws java.io.IOException,
                                   OAuthException,
                                   java.net.URISyntaxException
Get an access token from the service provider, in exchange for an authorized request token.

Parameters:
accessor - should contain a non-null requestToken and tokenSecret, and a consumer that contains a consumerKey and consumerSecret. Also, accessor.consumer.serviceProvider.accessTokenURL should be the URL (determined by the service provider) for getting an access token.
httpMethod - typically OAuthMessage.POST or OAuthMessage.GET, or null to use the default method.
parameters - additional parameters for this request, or null to indicate that there are no additional parameters.
Returns:
the response from the service provider
Throws:
OAuthProblemException - the HTTP response status code was not 200 (OK)
java.io.IOException
OAuthException
java.net.URISyntaxException

invoke

public OAuthMessage invoke(OAuthAccessor accessor,
                           java.lang.String httpMethod,
                           java.lang.String url,
                           java.util.Collection<? extends java.util.Map.Entry> parameters)
                    throws java.io.IOException,
                           OAuthException,
                           java.net.URISyntaxException
Construct a request message, send it to the service provider and get the response.

Parameters:
httpMethod - the HTTP request method, or null to use the default method
Returns:
the response
Throws:
java.net.URISyntaxException - the given url isn't valid syntactically
OAuthProblemException - the HTTP response status code was not 200 (OK)
java.io.IOException
OAuthException

invoke

public OAuthMessage invoke(OAuthAccessor accessor,
                           java.lang.String url,
                           java.util.Collection<? extends java.util.Map.Entry> parameters)
                    throws java.io.IOException,
                           OAuthException,
                           java.net.URISyntaxException
Construct a request message, send it to the service provider and get the response.

Returns:
the response
Throws:
java.net.URISyntaxException - the given url isn't valid syntactically
OAuthProblemException - the HTTP response status code was not 200 (OK)
java.io.IOException
OAuthException

invoke

public OAuthMessage invoke(OAuthMessage request,
                           ParameterStyle style)
                    throws java.io.IOException,
                           OAuthException
Send a request message to the service provider and get the response.

Returns:
the response
Throws:
java.io.IOException - failed to communicate with the service provider
OAuthProblemException - the HTTP response status code was not 200 (OK)
OAuthException

access

public OAuthResponseMessage access(OAuthMessage request,
                                   ParameterStyle style)
                            throws java.io.IOException
Send a request and return the response. Don't try to decide whether the response indicates success; merely return it.

Throws:
java.io.IOException

invoke

public OAuthMessage invoke(OAuthMessage request,
                           OAuthClient.ParameterStyle style)
                    throws java.io.IOException,
                           OAuthException
Deprecated. 

Throws:
java.io.IOException
OAuthException

access

public OAuthResponseMessage access(OAuthMessage request,
                                   OAuthClient.ParameterStyle style)
                            throws java.io.IOException
Deprecated. 

Throws:
java.io.IOException


Copyright © 2009. All Rights Reserved.