1
+ /**
2
+ * Copyright (C) 20010-2011 Share Extras contributors.
3
+ *
4
+ * This file is part of the Share Extras project.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ package org .sharextras .webscripts .connector ;
20
+
21
+ import java .util .HashMap ;
22
+ import java .util .Map ;
23
+ import java .util .regex .Matcher ;
24
+ import java .util .regex .Pattern ;
25
+
26
+ import javax .servlet .http .HttpServletRequest ;
27
+ import javax .servlet .http .HttpServletResponse ;
28
+
29
+ import org .apache .commons .logging .Log ;
30
+ import org .apache .commons .logging .LogFactory ;
31
+ import org .springframework .extensions .config .RemoteConfigElement .ConnectorDescriptor ;
32
+ import org .springframework .extensions .webscripts .connector .ConnectorContext ;
33
+ import org .springframework .extensions .webscripts .connector .EndpointManager ;
34
+ import org .springframework .extensions .webscripts .connector .HttpConnector ;
35
+ import org .springframework .extensions .webscripts .connector .HttpOAuthConnector ;
36
+ import org .springframework .extensions .webscripts .connector .RemoteClient ;
37
+ import org .springframework .extensions .webscripts .connector .Response ;
38
+ import org .springframework .extensions .webscripts .connector .ResponseStatus ;
39
+
40
+ public class HttpOAuthConnector extends HttpConnector
41
+ {
42
+ public static final String PARAM_CONSUMER_KEY = "consumer-key" ;
43
+ public static final String PARAM_CONSUMER_SECRET = "consumer-secret" ;
44
+ public static final String PARAM_SIGNATURE_METHOD = "signature-method" ;
45
+
46
+ public static final String SIGNATURE_METHOD_PLAINTEXT = "PLAINTEXT" ;
47
+
48
+ private static Log logger = LogFactory .getLog (HttpOAuthConnector .class );
49
+
50
+ public HttpOAuthConnector (ConnectorDescriptor descriptor ,
51
+ String endpoint ) {
52
+ super (descriptor , endpoint );
53
+ }
54
+
55
+ private String getConsumerKey ()
56
+ {
57
+ return descriptor .getStringProperty (PARAM_CONSUMER_KEY );
58
+ }
59
+
60
+ private String getConsumerSecret ()
61
+ {
62
+ return descriptor .getStringProperty (PARAM_CONSUMER_SECRET );
63
+ }
64
+
65
+ private String getSignatureMethod ()
66
+ {
67
+ return descriptor .getStringProperty (PARAM_SIGNATURE_METHOD );
68
+ }
69
+
70
+ private String generateSignature (Map <String , String > oauthParams )
71
+ {
72
+ if (getSignatureMethod ().equals (SIGNATURE_METHOD_PLAINTEXT ))
73
+ {
74
+ StringBuffer signatureBuffer = new StringBuffer (getConsumerSecret ()).append ("%26" );
75
+ String tokenSecret = oauthParams .get ("oauth_token_secret" );
76
+ if (tokenSecret != null && !tokenSecret .equals ("" ))
77
+ {
78
+ signatureBuffer .append (tokenSecret );
79
+ }
80
+ return signatureBuffer .toString ();
81
+ }
82
+ else
83
+ {
84
+ // TODO do we need to throw an exception?
85
+ return null ;
86
+ }
87
+ }
88
+
89
+ public Response call (String uri , ConnectorContext context , HttpServletRequest req , HttpServletResponse res )
90
+ {
91
+ if (logger .isDebugEnabled ())
92
+ logger .debug ("Requested Method: " + (context != null ? context .getMethod () : "GET" ) + " URI: " + uri );
93
+ Response response = null ;
94
+ if (EndpointManager .allowConnect (this .endpoint ))
95
+ {
96
+ RemoteClient remoteClient = initRemoteClient (context );
97
+
98
+ String auth = req .getHeader ("X-OAuth-Data" );
99
+ if (auth != null && !auth .equals ("" ))
100
+ {
101
+ if (logger .isDebugEnabled ())
102
+ logger .debug ("Found OAuth data " + auth );
103
+
104
+ Pattern p = Pattern .compile ("(.+)=\" (.+)\" " );
105
+ String [] authParams = auth .split ("," );
106
+ Map <String , String > authMap = new HashMap <String , String >(authParams .length );
107
+ for (int i = 0 ; i < authParams .length ; i ++)
108
+ {
109
+ Matcher m = p .matcher (authParams [i ]);
110
+ if (m .matches ())
111
+ {
112
+ authMap .put (m .group (1 ), m .group (2 ));
113
+ }
114
+ }
115
+ if (!authMap .containsKey ("oauth_consumer_key" ))
116
+ {
117
+ authMap .put ("oauth_consumer_key" , getConsumerKey ());
118
+ }
119
+ if (!authMap .containsKey ("oauth_signature" ))
120
+ {
121
+ authMap .put ("oauth_signature" , generateSignature (authMap ));
122
+ }
123
+
124
+ StringBuffer authBuffer = new StringBuffer ("OAuth " );
125
+ authBuffer .append ("oauth_token" ).append ("=\" " ).append (authMap .get ("oauth_token" )).append ("\" ," );
126
+ for (Map .Entry <String , String > entry : authMap .entrySet ())
127
+ {
128
+ if (!entry .getKey ().equals ("oauth_token_secret" ) &&
129
+ !entry .getKey ().equals ("oauth_signature" ) &&
130
+ !entry .getKey ().equals ("oauth_token" ))
131
+ {
132
+ authBuffer .append (entry .getKey ()).append ("=\" " ).append (entry .getValue ()).append ("\" ," );
133
+ }
134
+ }
135
+ authBuffer .append ("oauth_signature" ).append ("=\" " ).append (authMap .get ("oauth_signature" )).append ("\" " );
136
+
137
+ if (logger .isDebugEnabled ())
138
+ logger .debug ("Adding Authorization header with data: " + authBuffer .toString ());
139
+
140
+ Map <String , String > headers = new HashMap <String , String >(1 );
141
+ headers .put ("Authorization" , authBuffer .toString ());
142
+ remoteClient .setRequestProperties (headers );
143
+ }
144
+
145
+ // call client and process response
146
+ response = remoteClient .call (uri , req , res );
147
+ processResponse (remoteClient , response );
148
+ }
149
+ else
150
+ {
151
+ ResponseStatus status = new ResponseStatus ();
152
+ status .setCode (ResponseStatus .STATUS_INTERNAL_SERVER_ERROR );
153
+ response = new Response (status );
154
+ }
155
+ return response ;
156
+ }
157
+ }
0 commit comments