forked from scribejava/scribejava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractServiceBuilder.java
More file actions
185 lines (160 loc) · 5.35 KB
/
Copy pathAbstractServiceBuilder.java
File metadata and controls
185 lines (160 loc) · 5.35 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.github.scribejava.core.builder;
import com.github.scribejava.core.builder.api.DefaultApi10a;
import com.github.scribejava.core.builder.api.DefaultApi20;
import com.github.scribejava.core.model.OAuthConfig;
import java.io.OutputStream;
import com.github.scribejava.core.model.OAuthConstants;
import com.github.scribejava.core.model.SignatureType;
import com.github.scribejava.core.oauth.OAuth10aService;
import com.github.scribejava.core.oauth.OAuth20Service;
import com.github.scribejava.core.utils.Preconditions;
abstract class AbstractServiceBuilder<T extends AbstractServiceBuilder<T>> {
private String callback;
private String apiKey;
private String apiSecret;
private String scope;
private String state;
private SignatureType signatureType;
private OutputStream debugStream;
private String grantType;
AbstractServiceBuilder() {
this.callback = OAuthConstants.OUT_OF_BAND;
this.signatureType = SignatureType.Header;
}
/**
* 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
*/
@SuppressWarnings("unchecked")
public T callback(String callback) {
Preconditions.checkNotNull(callback, "Callback can't be null");
this.callback = callback;
return (T) this;
}
/**
* Configures the api key
*
* @param apiKey The api key for your application
* @return the {@link ServiceBuilder} instance for method chaining
*/
@SuppressWarnings("unchecked")
public T apiKey(String apiKey) {
Preconditions.checkEmptyString(apiKey, "Invalid Api key");
this.apiKey = apiKey;
return (T) this;
}
/**
* Configures the api secret
*
* @param apiSecret The api secret for your application
* @return the {@link ServiceBuilder} instance for method chaining
*/
@SuppressWarnings("unchecked")
public T apiSecret(String apiSecret) {
Preconditions.checkEmptyString(apiSecret, "Invalid Api secret");
this.apiSecret = apiSecret;
return (T) 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
*/
@SuppressWarnings("unchecked")
public T scope(String scope) {
Preconditions.checkEmptyString(scope, "Invalid OAuth scope");
this.scope = scope;
return (T) this;
}
/**
* Configures the anti forgery session state. This is available in some APIs (like Google's).
*
* @param state The OAuth state
* @return the {@link ServiceBuilder} instance for method chaining
*/
@SuppressWarnings("unchecked")
public T state(String state) {
Preconditions.checkEmptyString(state, "Invalid OAuth state");
this.state = state;
return (T) this;
}
/**
* Configures the signature type, choose between header, querystring, etc. Defaults to Header
*
* @param type SignatureType
* @return the {@link ServiceBuilder} instance for method chaining
*/
@SuppressWarnings("unchecked")
public T signatureType(SignatureType type) {
Preconditions.checkNotNull(type, "Signature type can't be null");
this.signatureType = type;
return (T) this;
}
@SuppressWarnings("unchecked")
public T debugStream(OutputStream stream) {
Preconditions.checkNotNull(stream, "debug stream can't be null");
this.debugStream = stream;
return (T) this;
}
@SuppressWarnings("unchecked")
public T grantType(String grantType) {
Preconditions.checkEmptyString(grantType, "Invalid OAuth grantType");
this.grantType = grantType;
return (T) this;
}
@SuppressWarnings("unchecked")
public T debug() {
debugStream(System.out);
return (T) this;
}
public void checkPreconditions() {
Preconditions.checkEmptyString(apiKey, "You must provide an api key");
Preconditions.checkEmptyString(apiSecret, "You must provide an api secret");
}
public String getCallback() {
return callback;
}
public String getApiKey() {
return apiKey;
}
public String getApiSecret() {
return apiSecret;
}
public String getScope() {
return scope;
}
public String getState() {
return state;
}
public SignatureType getSignatureType() {
return signatureType;
}
public OutputStream getDebugStream() {
return debugStream;
}
public String getGrantType() {
return grantType;
}
protected abstract OAuthConfig createConfig();
/**
* Returns the fully configured {@link OAuth10aService}
*
* @param api will build Service for this API
* @return fully configured {@link OAuth10aService}
*/
public OAuth10aService build(DefaultApi10a api) {
return api.createService(createConfig());
}
/**
* Returns the fully configured {@link OAuth20Service}
*
* @param api will build Service for this API
* @return fully configured {@link OAuth20Service}
*/
public OAuth20Service build(DefaultApi20 api) {
return api.createService(createConfig());
}
}