Initial commit
This commit is contained in:
commit
bfbe536886
59 files changed
+2540
No files matched your search
@@ -0,0 +1,107 @@
|
||||
package org.scribe.builder;
|
||||
|
||||
import org.scribe.builder.api.*;
|
||||
import org.scribe.exceptions.*;
|
||||
|
||||
import org.scribe.model.*;
|
||||
import org.scribe.oauth.*;
|
||||
import org.scribe.utils.*;
|
||||
|
||||
/**
|
||||
* Implementation of the Builder pattern, with a fluent interface that creates a
|
||||
* {@link OAuthService}
|
||||
*
|
||||
* @author Pablo Fernandez
|
||||
*
|
||||
*/
|
||||
public class ServiceBuilder
|
||||
{
|
||||
private String apiKey;
|
||||
private String apiSecret;
|
||||
private String callback;
|
||||
private Api api;
|
||||
|
||||
public ServiceBuilder()
|
||||
{
|
||||
this.callback = OAuthConstants.OUT_OF_BAND;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the {@link Api}
|
||||
*
|
||||
* @param apiClass the class of one of the existent {@link Api}s on org.scribe.api package
|
||||
* @return the {@link ServiceBuilder} instance for method chaining
|
||||
*/
|
||||
public ServiceBuilder provider(Class<? extends Api> apiClass)
|
||||
{
|
||||
this.api = createApi(apiClass);
|
||||
return this;
|
||||
}
|
||||
|
||||
private Api createApi(Class<? extends Api> apiClass)
|
||||
{
|
||||
Preconditions.checkNotNull(apiClass, "Api class cannot be null");
|
||||
Api api;
|
||||
try
|
||||
{
|
||||
api = apiClass.newInstance();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
throw new OAuthException("Error while creating the Api object", e);
|
||||
}
|
||||
return api;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an OAuth callback url
|
||||
*
|
||||
* @param callback callback url. Must be a valid url or 'oob' for out of band OAuth
|
||||
* @return the {@link ServiceBuilder} instance for method chaining
|
||||
*/
|
||||
public ServiceBuilder callback(String callback)
|
||||
{
|
||||
Preconditions.checkValidOAuthCallback(callback, "Callback must be a valid URL or 'oob'");
|
||||
this.callback = callback;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the api key
|
||||
*
|
||||
* @param apiKey The api key for your application
|
||||
* @return the {@link ServiceBuilder} instance for method chaining
|
||||
*/
|
||||
public ServiceBuilder apiKey(String apiKey)
|
||||
{
|
||||
Preconditions.checkEmptyString(apiKey, "Invalid Api key");
|
||||
this.apiKey = apiKey;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the api secret
|
||||
*
|
||||
* @param apiSecret The api secret for your application
|
||||
* @return the {@link ServiceBuilder} instance for method chaining
|
||||
*/
|
||||
public ServiceBuilder apiSecret(String apiSecret)
|
||||
{
|
||||
Preconditions.checkEmptyString(apiSecret, "Invalid Api secret");
|
||||
this.apiSecret = apiSecret;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the fully configured {@link OAuthService}
|
||||
*
|
||||
* @return fully configured {@link OAuthService}
|
||||
*/
|
||||
public OAuthService build()
|
||||
{
|
||||
Preconditions.checkNotNull(api, "You must specify a valid api through the provider() method");
|
||||
Preconditions.checkEmptyString(apiKey, "You must provide an api key");
|
||||
Preconditions.checkEmptyString(apiSecret, "You must provide an api secret");
|
||||
return api.createService(apiKey, apiSecret, callback);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.scribe.builder.api;
|
||||
|
||||
import org.scribe.oauth.*;
|
||||
|
||||
/**
|
||||
* Contains all the configuration needed to instantiate a valid {@link OAuthService}
|
||||
*
|
||||
* @author Pablo Fernandez
|
||||
*
|
||||
*/
|
||||
public interface Api
|
||||
{
|
||||
/**
|
||||
* Creates an {@link OAuthService}
|
||||
*
|
||||
* @param apiKey your application api key
|
||||
* @param apiSecret your application api secret
|
||||
* @param callback the callback url (or 'oob' for out of band OAuth)
|
||||
*
|
||||
* @return fully configured {@link OAuthService}
|
||||
*/
|
||||
OAuthService createService(String apiKey, String apiSecret, String callback);
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package org.scribe.builder.api;
|
||||
|
||||
import org.scribe.extractors.*;
|
||||
import org.scribe.model.OAuthConfig;
|
||||
import org.scribe.model.Request.Verb;
|
||||
import org.scribe.oauth.OAuth10aServiceImpl;
|
||||
import org.scribe.oauth.OAuthService;
|
||||
import org.scribe.services.*;
|
||||
|
||||
public abstract class DefaultApi10a implements Api
|
||||
{
|
||||
public AccessTokenExtractor getAccessTokenExtractor()
|
||||
{
|
||||
return new TokenExtractorImpl();
|
||||
}
|
||||
|
||||
public BaseStringExtractor getBaseStringExtractor()
|
||||
{
|
||||
return new BaseStringExtractorImpl();
|
||||
}
|
||||
|
||||
public HeaderExtractor getHeaderExtractor()
|
||||
{
|
||||
return new HeaderExtractorImpl();
|
||||
}
|
||||
|
||||
public RequestTokenExtractor getRequestTokenExtractor()
|
||||
{
|
||||
return new TokenExtractorImpl();
|
||||
}
|
||||
|
||||
public SignatureService getSignatureService()
|
||||
{
|
||||
return new HMACSha1SignatureService();
|
||||
}
|
||||
|
||||
public TimestampService getTimestampService()
|
||||
{
|
||||
return new TimestampServiceImpl();
|
||||
}
|
||||
|
||||
public Verb getAccessTokenVerb()
|
||||
{
|
||||
return Verb.POST;
|
||||
}
|
||||
|
||||
public Verb getRequestTokenVerb()
|
||||
{
|
||||
return Verb.POST;
|
||||
}
|
||||
|
||||
public abstract String getRequestTokenEndpoint();
|
||||
public abstract String getAccessTokenEndpoint();
|
||||
|
||||
@Override
|
||||
public OAuthService createService(String apiKey, String apiSecret, String callback)
|
||||
{
|
||||
return new OAuth10aServiceImpl( getSignatureService(),
|
||||
getTimestampService(),
|
||||
getBaseStringExtractor(),
|
||||
getHeaderExtractor(),
|
||||
getRequestTokenExtractor(),
|
||||
getAccessTokenExtractor(),
|
||||
createConfig(apiKey, apiSecret, callback));
|
||||
}
|
||||
|
||||
private OAuthConfig createConfig(String apiKey, String apiSecret, String callback)
|
||||
{
|
||||
OAuthConfig config = new OAuthConfig();
|
||||
config.setRequestTokenVerb(getRequestTokenVerb());
|
||||
config.setRequestTokenEndpoint(getRequestTokenEndpoint());
|
||||
config.setAccessTokenVerb(getAccessTokenVerb());
|
||||
config.setAccessTokenEndpoint(getAccessTokenEndpoint());
|
||||
config.setApiKey(apiKey);
|
||||
config.setApiSecret(apiSecret);
|
||||
config.setCallback(callback);
|
||||
return config;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.scribe.builder.api;
|
||||
|
||||
public class FoursquareApi extends DefaultApi10a
|
||||
{
|
||||
@Override
|
||||
public String getAccessTokenEndpoint()
|
||||
{
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRequestTokenEndpoint()
|
||||
{
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.scribe.builder.api;
|
||||
|
||||
public class GoogleApi extends DefaultApi10a
|
||||
{
|
||||
@Override
|
||||
public String getAccessTokenEndpoint()
|
||||
{
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRequestTokenEndpoint()
|
||||
{
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package org.scribe.builder.api;
|
||||
|
||||
import org.scribe.extensions.linkedin.*;
|
||||
import org.scribe.extractors.*;
|
||||
|
||||
public class LinkedInApi extends DefaultApi10a
|
||||
{
|
||||
|
||||
@Override
|
||||
public String getAccessTokenEndpoint()
|
||||
{
|
||||
return "https://api.linkedin.com/uas/oauth/accessToken";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRequestTokenEndpoint()
|
||||
{
|
||||
return "https://api.linkedin.com/uas/oauth/requestToken";
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseStringExtractor getBaseStringExtractor()
|
||||
{
|
||||
return new LinkedInBaseStringExtractorImpl();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.scribe.builder.api;
|
||||
|
||||
public class SalesForceApi extends DefaultApi10a
|
||||
{
|
||||
@Override
|
||||
public String getAccessTokenEndpoint()
|
||||
{
|
||||
return "https://login.salesforce.com/_nc_external/system/security/oauth/AccessTokenHandler";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRequestTokenEndpoint()
|
||||
{
|
||||
return "https://login.salesforce.com/_nc_external/system/security/oauth/RequestTokenHandler";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.scribe.builder.api;
|
||||
|
||||
public class TwitterApi extends DefaultApi10a
|
||||
{
|
||||
@Override
|
||||
public String getAccessTokenEndpoint()
|
||||
{
|
||||
return "http://twitter.com/oauth/access_token";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRequestTokenEndpoint()
|
||||
{
|
||||
return "http://twitter.com/oauth/request_token";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.scribe.builder.api;
|
||||
|
||||
public class YahooApi extends DefaultApi10a
|
||||
{
|
||||
@Override
|
||||
public String getAccessTokenEndpoint()
|
||||
{
|
||||
return "https://api.login.yahoo.com/oauth/v2/get_token";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRequestTokenEndpoint()
|
||||
{
|
||||
return "https://api.login.yahoo.com/oauth/v2/get_request_token";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user