1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.amber.oauth2.client.request;
23
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.apache.amber.oauth2.common.OAuth;
28 import org.apache.amber.oauth2.common.exception.OAuthSystemException;
29 import org.apache.amber.oauth2.common.message.OAuthMessage;
30 import org.apache.amber.oauth2.common.message.types.GrantType;
31 import org.apache.amber.oauth2.common.parameters.BodyURLEncodedParametersApplier;
32 import org.apache.amber.oauth2.common.parameters.OAuthParametersApplier;
33 import org.apache.amber.oauth2.common.parameters.QueryParameterApplier;
34
35
36
37
38
39
40
41
42
43 public class OAuthClientRequest implements OAuthMessage {
44
45 protected String url;
46 protected String body;
47 protected Map<String, String> headers;
48
49 protected OAuthClientRequest(String url) {
50 this.url = url;
51 this.headers=new HashMap<String, String>();
52 }
53
54 public static AuthenticationRequestBuilder authorizationLocation(String url) {
55 return new AuthenticationRequestBuilder(url);
56 }
57
58 public static TokenRequestBuilder tokenLocation(String url) {
59 return new TokenRequestBuilder(url);
60 }
61
62 public String getBody() {
63 return body;
64 }
65
66 public void setBody(String body) {
67 this.body = body;
68 }
69
70 public Map<String, String> getHeaders() {
71 return headers;
72 }
73
74 @Override
75 public void addHeader(String name, String header) {
76 this.headers.put(name, header);
77 }
78
79 public void setHeaders(Map<String, String> headers) {
80 this.headers = headers;
81 }
82
83 public String getLocationUri() {
84 return url;
85 }
86
87 public void setLocationUri(String uri) {
88 this.url = uri;
89 }
90
91 public String getHeader(String name) {
92 return this.headers.get(name);
93 }
94
95 public void setHeader(String name, String value) {
96 this.headers.put(name, value);
97 }
98
99 public abstract static class OAuthRequestBuilder {
100
101 protected OAuthParametersApplier applier;
102 protected Map<String, Object> parameters = new HashMap<String, Object>();
103
104 protected String url;
105
106 protected OAuthRequestBuilder(String url) {
107 this.url = url;
108 }
109
110 public OAuthClientRequest buildQueryMessage() throws OAuthSystemException {
111 OAuthClientRequest request = new OAuthClientRequest(url);
112 this.applier = new QueryParameterApplier();
113 return (OAuthClientRequest)applier.applyOAuthParameters(request, parameters);
114 }
115
116 public OAuthClientRequest buildBodyMessage() throws OAuthSystemException {
117 OAuthClientRequest request = new OAuthClientRequest(url);
118 this.applier = new BodyURLEncodedParametersApplier();
119 return (OAuthClientRequest)applier.applyOAuthParameters(request, parameters);
120 }
121
122 public OAuthClientRequest buildHeaderMessage() throws OAuthSystemException {
123 OAuthClientRequest request = new OAuthClientRequest(url);
124 this.applier = new ClientHeaderParametersApplier();
125 return (OAuthClientRequest)applier.applyOAuthParameters(request, parameters);
126 }
127 }
128
129 public static class AuthenticationRequestBuilder extends OAuthRequestBuilder {
130
131 public AuthenticationRequestBuilder(String url) {
132 super(url);
133 }
134
135 public AuthenticationRequestBuilder setResponseType(String type) {
136 this.parameters.put(OAuth.OAUTH_RESPONSE_TYPE, type);
137 return this;
138 }
139
140 public AuthenticationRequestBuilder setClientId(String clientId) {
141 this.parameters.put(OAuth.OAUTH_CLIENT_ID, clientId);
142 return this;
143 }
144
145 public AuthenticationRequestBuilder setRedirectURI(String uri) {
146 this.parameters.put(OAuth.OAUTH_REDIRECT_URI, uri);
147 return this;
148 }
149
150 public AuthenticationRequestBuilder setState(String state) {
151 this.parameters.put(OAuth.OAUTH_STATE, state);
152 return this;
153 }
154
155 public AuthenticationRequestBuilder setScope(String scope) {
156 this.parameters.put(OAuth.OAUTH_SCOPE, scope);
157 return this;
158 }
159
160 public AuthenticationRequestBuilder setParameter(String paramName, String paramValue) {
161 this.parameters.put(paramName, paramValue);
162 return this;
163 }
164 }
165
166 public static class TokenRequestBuilder extends OAuthRequestBuilder {
167
168 public TokenRequestBuilder(String url) {
169 super(url);
170 }
171
172 public TokenRequestBuilder setGrantType(GrantType grantType) {
173 this.parameters.put(OAuth.OAUTH_GRANT_TYPE, grantType == null ? null : grantType.toString());
174 return this;
175 }
176
177 public TokenRequestBuilder setClientId(String clientId) {
178 this.parameters.put(OAuth.OAUTH_CLIENT_ID, clientId);
179 return this;
180 }
181
182 public TokenRequestBuilder setClientSecret(String secret) {
183 this.parameters.put(OAuth.OAUTH_CLIENT_SECRET, secret);
184 return this;
185 }
186
187 public TokenRequestBuilder setUsername(String username) {
188 this.parameters.put(OAuth.OAUTH_USERNAME, username);
189 return this;
190 }
191
192 public TokenRequestBuilder setPassword(String password) {
193 this.parameters.put(OAuth.OAUTH_PASSWORD, password);
194 return this;
195 }
196
197 public TokenRequestBuilder setScope(String scope) {
198 this.parameters.put(OAuth.OAUTH_SCOPE, scope);
199 return this;
200 }
201
202 public TokenRequestBuilder setCode(String code) {
203 this.parameters.put(OAuth.OAUTH_CODE, code);
204 return this;
205 }
206
207 public TokenRequestBuilder setRedirectURI(String uri) {
208 this.parameters.put(OAuth.OAUTH_REDIRECT_URI, uri);
209 return this;
210 }
211
212 public TokenRequestBuilder setAssertion(String assertion) {
213 this.parameters.put(OAuth.OAUTH_ASSERTION, assertion);
214 return this;
215 }
216
217 public TokenRequestBuilder setAssertionType(String assertionType) {
218 this.parameters.put(OAuth.OAUTH_ASSERTION_TYPE, assertionType);
219 return this;
220 }
221
222 public TokenRequestBuilder setRefreshToken(String token) {
223 this.parameters.put(OAuth.OAUTH_REFRESH_TOKEN, token);
224 return this;
225 }
226
227 public TokenRequestBuilder setParameter(String paramName, String paramValue) {
228 this.parameters.put(paramName, paramValue);
229 return this;
230 }
231
232
233 }
234 }