org.brickred.socialauth.util
Class OAuthConsumer

java.lang.Object
  extended by org.brickred.socialauth.util.OAuthConsumer
All Implemented Interfaces:
Serializable, Constants

public class OAuthConsumer
extends Object
implements Serializable, Constants

It contains various method those are required for OAUTH

Author:
tarunn@brickred.com
See Also:
Serialized Form

Field Summary
 
Fields inherited from interface org.brickred.socialauth.util.Constants
ACCESS_TOKEN_PARAMETER_NAME, CONTENT_ENCODING_HEADER, CURRENT_VERSION, ENCODING, EXPIRES, FACEBOOK, FOURSQUARE, GOOGLE, GZIP_CONTENT_ENCODING, HMACSHA1_SIGNATURE, HOTMAIL, HTTP_CONNECTION_TIMEOUT, LINKEDIN, MENDELEY, MYSPACE, OAUTH_ACCESS_TOKEN_URL, OAUTH_AUTHORIZATION_URL, OAUTH_CALLBACK, OAUTH_CALLBACK_CONFIRMED, OAUTH_CONSUMER_KEY, OAUTH_NONCE, OAUTH_PROBLEM, OAUTH_REQUEST_TOKEN_URL, OAUTH_SIGNATURE, OAUTH_SIGNATURE_METHOD, OAUTH_TIMESTAMP, OAUTH_TOKEN, OAUTH_TOKEN_SECRET, OAUTH_VERIFIER, OAUTH_VERSION, OOB, OPENID, PREFIX, PROXY_HOST, PROXY_PORT, RUNKEEPER, SALESFORCE, TWITTER, YAHOO, YAMMER
 
Constructor Summary
OAuthConsumer(OAuthConfig config)
           
 
Method Summary
 StringBuilder buildAuthUrl(String authUrl, AccessGrant token, String callbackUrl)
          Builds the auth url to redirect the user to, based from the given token and callback url.
 String generateSignature(String signatureType, String method, String url, Map<String,String> args, AccessGrant token)
          It returns a signature for signing OAuth request.
 AccessGrant getAccessToken(String accessTokenURL, AccessGrant reqToken)
          It obtains the access token.
 String getAuthHeaderValue(Map<String,String> params)
          Generates Authorization header.
 OAuthConfig getConfig()
           
 AccessGrant getRequestToken(String reqTokenURL, String callbackURL)
          It obtains the request token.
 Response httpGet(String reqURL, Map<String,String> headerParams, AccessGrant token)
          Does an HTTP GET request.
 Response httpPost(String reqURL, Map<String,String> params, Map<String,String> headerParams, String body, AccessGrant token)
          Does an HTTP POST request.
 Response httpPut(String reqURL, Map<String,String> params, Map<String,String> headerParams, String body, AccessGrant token)
          Does an HTTP PUT request.
 Response httpPut(String reqURL, Map<String,String> params, Map<String,String> headerParams, String body, AccessGrant token, boolean isHeaderRequired)
          Does an HTTP PUT request.
 void putNonceAndTimestamp(Map<String,String> params)
          Adds nonce and timestamp in given parameter map.
 Response uploadImage(String reqURL, Map<String,String> paramsMap, Map<String,String> headerParams, InputStream inputStream, String fileParamName, String fileName, String methodName, AccessGrant token, boolean isHeaderRequired)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OAuthConsumer

public OAuthConsumer(OAuthConfig config)
Parameters:
config - Configuration object which contains information of application configuration
Method Detail

generateSignature

public String generateSignature(String signatureType,
                                String method,
                                String url,
                                Map<String,String> args,
                                AccessGrant token)
                         throws Exception
It returns a signature for signing OAuth request. The Signature Base String is a consistent reproducible concatenation of the request elements into a single string. The string is used as an input in hashing or signing algorithms.The request parameters are collected, sorted and concatenated into a normalized string.

Parameters:
signatureType - Type of signature. It can be HMAC-SHA1.
method - Method type can be GET, POST or PUT
url - Requested URL for which generating signature
args - Required arguments to generate signature
token - Token Object
Returns:
The computed signature
Throws:
Exception

getRequestToken

public AccessGrant getRequestToken(String reqTokenURL,
                                   String callbackURL)
                            throws Exception
It obtains the request token. The Request Token is a temporary token used to initiate User authorization.

Parameters:
reqTokenURL - Request Token URL
callbackURL - Callback URL
Returns:
The request token
Throws:
Exception

getAccessToken

public AccessGrant getAccessToken(String accessTokenURL,
                                  AccessGrant reqToken)
                           throws Exception
It obtains the access token. The Consumer exchanges the Request Token for an Access Token capable of accessing the Protected Resources.

Parameters:
accessTokenURL - Access Token URL
reqToken - Request Token
Returns:
The Access Token
Throws:
Exception

httpGet

public Response httpGet(String reqURL,
                        Map<String,String> headerParams,
                        AccessGrant token)
                 throws Exception
Does an HTTP GET request.

Parameters:
reqURL - URL to send request to.
headerParams - Header Parameters
token - Token to pass in GET request
Returns:
Response object
Throws:
Exception

httpPost

public Response httpPost(String reqURL,
                         Map<String,String> params,
                         Map<String,String> headerParams,
                         String body,
                         AccessGrant token)
                  throws Exception
Does an HTTP POST request.

Parameters:
reqURL - URL to send request to.
params - Any additional parameters whose signature we want to compute.
headerParams - Header Parameters
body - Request Body
token - Token to pass in POST request
Returns:
Response object
Throws:
Exception

httpPut

public Response httpPut(String reqURL,
                        Map<String,String> params,
                        Map<String,String> headerParams,
                        String body,
                        AccessGrant token)
                 throws Exception
Does an HTTP PUT request.

Parameters:
reqURL - URL to send request to.
params - Any additional parameters whose signature we want to compute
headerParams - Header Parameters
body - Request Body
token - Token to pass in PUT request
Returns:
Response object
Throws:
Exception

httpPut

public Response httpPut(String reqURL,
                        Map<String,String> params,
                        Map<String,String> headerParams,
                        String body,
                        AccessGrant token,
                        boolean isHeaderRequired)
                 throws Exception
Does an HTTP PUT request.

Parameters:
reqURL - URL to send request to.
params - Any additional parameters whose signature we want to compute
headerParams - Header Parameters
body - Request Body
token - Token to pass in PUT request
isHeaderRequired - True if header is required
Returns:
Response object
Throws:
Exception

putNonceAndTimestamp

public void putNonceAndTimestamp(Map<String,String> params)
Adds nonce and timestamp in given parameter map.

Parameters:
params -

buildAuthUrl

public StringBuilder buildAuthUrl(String authUrl,
                                  AccessGrant token,
                                  String callbackUrl)
                           throws Exception
Builds the auth url to redirect the user to, based from the given token and callback url.

Parameters:
authUrl -
token -
callbackUrl - Callback URL
Returns:
url with query string of parameters
Throws:
Exception

getAuthHeaderValue

public String getAuthHeaderValue(Map<String,String> params)
                          throws Exception
Generates Authorization header. The OAuth Protocol Parameters are sent in the Authorization header. Parameter names and values are encoded. For each parameter, the name is immediately followed by an ‘=’ character (ASCII code 61), a '"' character (ASCII code 34), the parameter value (MAY be empty), and another '"’ character (ASCII code 34).Parameters are separated by a comma character (ASCII code 44).

Parameters:
params - Parameters to generate header value
Returns:
Authorize header value
Throws:
Exception

getConfig

public OAuthConfig getConfig()

uploadImage

public Response uploadImage(String reqURL,
                            Map<String,String> paramsMap,
                            Map<String,String> headerParams,
                            InputStream inputStream,
                            String fileParamName,
                            String fileName,
                            String methodName,
                            AccessGrant token,
                            boolean isHeaderRequired)
                     throws Exception
Parameters:
reqURL - URL to send request to.
paramsMap - Any additional parameters whose signature we want to compute.
headerParams - Header Parameters
inputStream - Input Stream of image
fileParamName - Image Filename parameter. It requires in some provider.
fileName - Image file name
methodName - Method type
token - Token to pass in PUT request
isHeaderRequired - True if header is required
Returns:
Response Object
Throws:
Exception


Copyright © 2013. All Rights Reserved.