~s-friedemann/tomdroid/sshfs

« back to all changes in this revision

Viewing changes to lib/signpost/signpost-core/src/main/java/oauth/signpost/AbstractOAuthConsumer.java

  • Committer: Guilherme Salgado
  • Date: 2010-08-31 21:44:24 UTC
  • mfrom: (185.1.51 sync-ui)
  • mto: (185.1.54 sync-ui)
  • mto: This revision was merged to the branch mainline in revision 186.
  • Revision ID: salgado@canonical.com-20100831214424-06b64zz707lwpmcl
mergeĀ lp:~tomdroid-developers/sync-ui

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 2009 Matthias Kaeppler
2
 
 *
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
6
 
 *
7
 
 *     http://www.apache.org/licenses/LICENSE-2.0
8
 
 *
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.
14
 
 */
15
 
package oauth.signpost;
16
 
 
17
 
import java.util.HashMap;
18
 
import java.util.Map;
19
 
 
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;
25
 
 
26
 
@SuppressWarnings("serial")
27
 
public abstract class AbstractOAuthConsumer implements OAuthConsumer {
28
 
 
29
 
        private String consumerKey, consumerSecret;
30
 
 
31
 
        private String token;
32
 
 
33
 
        private SignatureMethod signatureMethod;
34
 
 
35
 
        private OAuthMessageSigner messageSigner;
36
 
 
37
 
        public AbstractOAuthConsumer(String consumerKey, String consumerSecret,
38
 
                        SignatureMethod signatureMethod) {
39
 
 
40
 
                this.consumerKey = consumerKey;
41
 
                this.consumerSecret = consumerSecret;
42
 
                this.signatureMethod = signatureMethod;
43
 
                this.messageSigner = OAuthMessageSigner.create(signatureMethod);
44
 
                messageSigner.setConsumerSecret(consumerSecret);
45
 
        }
46
 
 
47
 
        public HttpRequest sign(HttpRequest request)
48
 
                        throws OAuthMessageSignerException, OAuthExpectationFailedException {
49
 
                if (consumerKey == null) {
50
 
                        throw new OAuthExpectationFailedException("consumer key not set");
51
 
                }
52
 
                if (consumerSecret == null) {
53
 
                        throw new OAuthExpectationFailedException("consumer secret not set");
54
 
                }
55
 
 
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();
62
 
 
63
 
                String signature = messageSigner.sign(request, oauthParams);
64
 
 
65
 
                request.setHeader(OAuth.HTTP_AUTHORIZATION_HEADER, buildOAuthHeader(
66
 
                                oauthParams, signature));
67
 
 
68
 
                return request;
69
 
        }
70
 
 
71
 
        public HttpRequest sign(Object request) throws OAuthMessageSignerException,
72
 
                        OAuthExpectationFailedException {
73
 
                return sign(wrap(request));
74
 
        }
75
 
 
76
 
        protected abstract HttpRequest wrap(Object request);
77
 
 
78
 
        public void setTokenWithSecret(String token, String tokenSecret) {
79
 
                this.token = token;
80
 
                messageSigner.setTokenSecret(tokenSecret);
81
 
        }
82
 
 
83
 
        public String getToken() {
84
 
                return token;
85
 
        }
86
 
 
87
 
        public String getTokenSecret() {
88
 
                return messageSigner.getTokenSecret();
89
 
        }
90
 
 
91
 
        public String getConsumerKey() {
92
 
                return this.consumerKey;
93
 
        }
94
 
 
95
 
        public String getConsumerSecret() {
96
 
                return this.consumerSecret;
97
 
        }
98
 
 
99
 
        private Map<String, String> buildOAuthParameterMap() {
100
 
                HashMap<String, String> map = new HashMap<String, String>();
101
 
 
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);
109
 
                return map;
110
 
        }
111
 
 
112
 
        private String buildOAuthHeader(Map<String, String> oauthParams,
113
 
                        String signature) {
114
 
 
115
 
                StringBuilder sb = new StringBuilder();
116
 
 
117
 
                sb.append("OAuth ");
118
 
 
119
 
                for (String key : oauthParams.keySet()) {
120
 
                        String value = oauthParams.get(key);
121
 
                        sb.append(oauthHeaderElement(key, value));
122
 
                        sb.append(",");
123
 
                }
124
 
 
125
 
                sb.append(oauthHeaderElement(OAuth.OAUTH_SIGNATURE, signature));
126
 
 
127
 
                return sb.toString();
128
 
        }
129
 
 
130
 
        private String oauthHeaderElement(String name, String value) {
131
 
                return OAuth.percentEncode(name) + "=\"" + OAuth.percentEncode(value)
132
 
                                + "\"";
133
 
        }
134
 
}