1
/* Copyright (c) 2009 Matthias Kaeppler
3
* Licensed under the Apache License, Version 2.0 (the "License");
4
* you may not use this file except in compliance with the License.
5
* You may obtain a copy of the License at
7
* http://www.apache.org/licenses/LICENSE-2.0
9
* Unless required by applicable law or agreed to in writing, software
10
* distributed under the License is distributed on an "AS IS" BASIS,
11
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
* See the License for the specific language governing permissions and
13
* limitations under the License.
15
package oauth.signpost;
17
import java.util.HashMap;
20
import oauth.signpost.exception.OAuthExpectationFailedException;
21
import oauth.signpost.exception.OAuthMessageSignerException;
22
import oauth.signpost.http.HttpRequest;
23
import oauth.signpost.signature.OAuthMessageSigner;
24
import oauth.signpost.signature.SignatureMethod;
26
@SuppressWarnings("serial")
27
public abstract class AbstractOAuthConsumer implements OAuthConsumer {
29
private String consumerKey, consumerSecret;
33
private SignatureMethod signatureMethod;
35
private OAuthMessageSigner messageSigner;
37
public AbstractOAuthConsumer(String consumerKey, String consumerSecret,
38
SignatureMethod signatureMethod) {
40
this.consumerKey = consumerKey;
41
this.consumerSecret = consumerSecret;
42
this.signatureMethod = signatureMethod;
43
this.messageSigner = OAuthMessageSigner.create(signatureMethod);
44
messageSigner.setConsumerSecret(consumerSecret);
47
public HttpRequest sign(HttpRequest request)
48
throws OAuthMessageSignerException, OAuthExpectationFailedException {
49
if (consumerKey == null) {
50
throw new OAuthExpectationFailedException("consumer key not set");
52
if (consumerSecret == null) {
53
throw new OAuthExpectationFailedException("consumer secret not set");
56
// FIXME the fact that OAuthConsumer pre-builts the OAuth parameters
57
// and passes them to SBS yields a responsibility problem which makes
58
// things difficult to test. Plus, the Authorization header is in fact
59
// ignored using this approach. Solution: Move param map construction +
60
// accoutning for header params into SBS, and make a getter for the map
61
Map<String, String> oauthParams = buildOAuthParameterMap();
63
String signature = messageSigner.sign(request, oauthParams);
65
request.setHeader(OAuth.HTTP_AUTHORIZATION_HEADER, buildOAuthHeader(
66
oauthParams, signature));
71
public HttpRequest sign(Object request) throws OAuthMessageSignerException,
72
OAuthExpectationFailedException {
73
return sign(wrap(request));
76
protected abstract HttpRequest wrap(Object request);
78
public void setTokenWithSecret(String token, String tokenSecret) {
80
messageSigner.setTokenSecret(tokenSecret);
83
public String getToken() {
87
public String getTokenSecret() {
88
return messageSigner.getTokenSecret();
91
public String getConsumerKey() {
92
return this.consumerKey;
95
public String getConsumerSecret() {
96
return this.consumerSecret;
99
private Map<String, String> buildOAuthParameterMap() {
100
HashMap<String, String> map = new HashMap<String, String>();
102
map.put(OAuth.OAUTH_CONSUMER_KEY, consumerKey);
103
map.put(OAuth.OAUTH_SIGNATURE_METHOD, signatureMethod.toString());
104
map.put(OAuth.OAUTH_TIMESTAMP, Long
105
.toString(System.currentTimeMillis() / 1000L));
106
map.put(OAuth.OAUTH_NONCE, Long.toString(System.nanoTime()));
107
map.put(OAuth.OAUTH_VERSION, OAuth.VERSION_1_0);
108
map.put(OAuth.OAUTH_TOKEN, token);
112
private String buildOAuthHeader(Map<String, String> oauthParams,
115
StringBuilder sb = new StringBuilder();
119
for (String key : oauthParams.keySet()) {
120
String value = oauthParams.get(key);
121
sb.append(oauthHeaderElement(key, value));
125
sb.append(oauthHeaderElement(OAuth.OAUTH_SIGNATURE, signature));
127
return sb.toString();
130
private String oauthHeaderElement(String name, String value) {
131
return OAuth.percentEncode(name) + "=\"" + OAuth.percentEncode(value)