/
ServiceBuilder.java
169 lines (155 loc) · 4.36 KB
/
ServiceBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package org.scribe.builder;
import java.io.*;
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;
private String scope;
private SignatureType signatureType;
private OutputStream debugStream;
/**
* Default constructor
*/
public ServiceBuilder()
{
this.callback = OAuthConstants.OUT_OF_BAND;
this.signatureType = SignatureType.Header;
this.debugStream = null;
}
/**
* 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;
}
/**
* Configures the {@link Api}
*
* Overloaded version. Let's you use an instance instead of a class.
*
* @param api instance of {@link Api}s
* @return the {@link ServiceBuilder} instance for method chaining
*/
public ServiceBuilder provider(Api api)
{
Preconditions.checkNotNull(api, "Api cannot be null");
this.api = api;
return this;
}
/**
* 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.checkNotNull(callback, "Callback can't be null");
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;
}
/**
* Configures the OAuth scope. This is only necessary in some APIs (like Google's).
*
* @param scope The OAuth scope
* @return the {@link ServiceBuilder} instance for method chaining
*/
public ServiceBuilder scope(String scope)
{
Preconditions.checkEmptyString(scope, "Invalid OAuth scope");
this.scope = scope;
return this;
}
/**
* Configures the signature type, choose between header, querystring, etc. Defaults to Header
*
* @param scope The OAuth scope
* @return the {@link ServiceBuilder} instance for method chaining
*/
public ServiceBuilder signatureType(SignatureType type)
{
Preconditions.checkNotNull(type, "Signature type can't be null");
this.signatureType = type;
return this;
}
public ServiceBuilder debugStream(OutputStream stream)
{
Preconditions.checkNotNull(stream, "debug stream can't be null");
this.debugStream = stream;
return this;
}
public ServiceBuilder debug()
{
this.debugStream(System.out);
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(new OAuthConfig(apiKey, apiSecret, callback, signatureType, scope, debugStream));
}
}