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