/LJPro/src/org/scribe/builder/ServiceBuilder.java

https://github.com/jfelectron/LJPro · Java · 123 lines · 68 code · 10 blank · 45 comment · 0 complexity · 15a3969f210737ed8b8dc5de1c82abe4 MD5 · raw file

  1. package org.scribe.builder;
  2. import org.scribe.builder.api.*;
  3. import org.scribe.exceptions.*;
  4. import org.scribe.model.*;
  5. import org.scribe.oauth.*;
  6. import org.scribe.utils.*;
  7. /**
  8. * Implementation of the Builder pattern, with a fluent interface that creates a
  9. * {@link OAuthService}
  10. *
  11. * @author Pablo Fernandez
  12. *
  13. */
  14. public class ServiceBuilder
  15. {
  16. private String apiKey;
  17. private String apiSecret;
  18. private String callback;
  19. private Api api;
  20. private String scope;
  21. /**
  22. * Default constructor
  23. */
  24. public ServiceBuilder()
  25. {
  26. this.callback = OAuthConstants.OUT_OF_BAND;
  27. }
  28. /**
  29. * Configures the {@link Api}
  30. *
  31. * @param apiClass the class of one of the existent {@link Api}s on org.scribe.api package
  32. * @return the {@link ServiceBuilder} instance for method chaining
  33. */
  34. public ServiceBuilder provider(Class<? extends Api> apiClass)
  35. {
  36. this.api = createApi(apiClass);
  37. return this;
  38. }
  39. private Api createApi(Class<? extends Api> apiClass)
  40. {
  41. Preconditions.checkNotNull(apiClass, "Api class cannot be null");
  42. Api api;
  43. try
  44. {
  45. api = apiClass.newInstance();
  46. }
  47. catch(Exception e)
  48. {
  49. throw new OAuthException("Error while creating the Api object", e);
  50. }
  51. return api;
  52. }
  53. /**
  54. * Adds an OAuth callback url
  55. *
  56. * @param callback callback url. Must be a valid url or 'oob' for out of band OAuth
  57. * @return the {@link ServiceBuilder} instance for method chaining
  58. */
  59. public ServiceBuilder callback(String callback)
  60. {
  61. Preconditions.checkValidOAuthCallback(callback, "Callback must be a valid URL or 'oob'");
  62. this.callback = callback;
  63. return this;
  64. }
  65. /**
  66. * Configures the api key
  67. *
  68. * @param apiKey The api key for your application
  69. * @return the {@link ServiceBuilder} instance for method chaining
  70. */
  71. public ServiceBuilder apiKey(String apiKey)
  72. {
  73. Preconditions.checkEmptyString(apiKey, "Invalid Api key");
  74. this.apiKey = apiKey;
  75. return this;
  76. }
  77. /**
  78. * Configures the api secret
  79. *
  80. * @param apiSecret The api secret for your application
  81. * @return the {@link ServiceBuilder} instance for method chaining
  82. */
  83. public ServiceBuilder apiSecret(String apiSecret)
  84. {
  85. Preconditions.checkEmptyString(apiSecret, "Invalid Api secret");
  86. this.apiSecret = apiSecret;
  87. return this;
  88. }
  89. /**
  90. * Configures the OAuth scope. This is only necessary in some APIs (like Google's).
  91. *
  92. * @param scope The OAuth scope
  93. * @return the {@link ServiceBuilder} instance for method chaining
  94. */
  95. public ServiceBuilder scope(String scope)
  96. {
  97. Preconditions.checkEmptyString(scope, "Invalid OAuth scope");
  98. this.scope = scope;
  99. return this;
  100. }
  101. /**
  102. * Returns the fully configured {@link OAuthService}
  103. *
  104. * @return fully configured {@link OAuthService}
  105. */
  106. public OAuthService build()
  107. {
  108. Preconditions.checkNotNull(api, "You must specify a valid api through the provider() method");
  109. Preconditions.checkEmptyString(apiKey, "You must provide an api key");
  110. Preconditions.checkEmptyString(apiSecret, "You must provide an api secret");
  111. return api.createService(new OAuthConfig(apiKey, apiSecret, callback), scope);
  112. }
  113. }