Skip to content

Commit ca0076c

Browse files
krogothunknown
authored andcommitted
Refactored scribe 1.1.0 to handdle properly encodings as per RFC5849. This is the first try for comment.
1 parent 70358ad commit ca0076c

File tree

9 files changed

+86
-32
lines changed

9 files changed

+86
-32
lines changed

src/main/java/org/scribe/builder/api/FacebookApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ public class FacebookApi extends DefaultApi20
1111
public String getAuthorizationUrl(OAuthConfig config)
1212
{
1313
Preconditions.checkValidUrl(config.getCallback(), "Must provide a valid url as callback. Facebook does not support OOB");
14-
return String.format(AUTHORIZE_URL, config.getApiKey(), URLUtils.percentEncode(config.getCallback()));
14+
return String.format(AUTHORIZE_URL, config.getApiKey(), URLUtils.urlEncodeWrapper(config.getCallback()));
1515
}
1616
}

src/main/java/org/scribe/extractors/BaseStringExtractorImpl.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,31 @@ public String extract(OAuthRequest request)
3232
private String getSortedAndEncodedParams(OAuthRequest request)
3333
{
3434
Map<String, String> params = new HashMap<String, String>();
35-
params.putAll(request.getQueryStringParams());
36-
params.putAll(request.getBodyParams());
37-
params.putAll(request.getOauthParameters());
35+
Map<String, String> tmp = null;
36+
//params.putAll(request.getQueryStringParams());
37+
tmp = request.getQueryStringParams();
38+
for (String key:tmp.keySet()){
39+
params.put(
40+
URLUtils.percentEncode(key),
41+
URLUtils.percentEncode(tmp.get(key))
42+
);
43+
}
44+
tmp = request.getBodyParams();
45+
for (String key:tmp.keySet()){
46+
params.put(
47+
URLUtils.percentEncode(key),
48+
URLUtils.percentEncode(tmp.get(key))
49+
);
50+
}
51+
tmp = request.getOauthParameters();
52+
for (String key:tmp.keySet()){
53+
params.put(
54+
URLUtils.percentEncode(key),
55+
URLUtils.percentEncode(tmp.get(key))
56+
);
57+
}
3858
params = MapUtils.sort(params);
39-
return URLUtils.percentEncode(URLUtils.formURLEncodeMap(params));
59+
return URLUtils.percentEncode(URLUtils.concatSortedPercentEncodedParams(params));
4060
}
4161

4262
private void checkPreconditions(OAuthRequest request)

src/main/java/org/scribe/extractors/TokenExtractorImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public Token extract(String response)
2727
Matcher matcher = Pattern.compile(TOKEN_REGEX).matcher(response);
2828
if (matcher.matches())
2929
{
30-
String token = URLUtils.percentDecode(matcher.group(1));
31-
String secret = URLUtils.percentDecode(matcher.group(2));
30+
String token = URLUtils.urlDecodeWrapper(matcher.group(1));
31+
String secret = URLUtils.urlDecodeWrapper(matcher.group(2));
3232
return new Token(token, secret);
3333
} else
3434
{

src/main/java/org/scribe/model/Request.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ public void addPayload(String payload)
144144
* Get a {@link Map} of the query string parameters.
145145
*
146146
* @return a map containing the query string parameters
147+
* @throws UnsupportedEncodingException
147148
*/
148149
public Map<String, String> getQueryStringParams()
149150
{
@@ -156,7 +157,11 @@ public Map<String, String> getQueryStringParams()
156157
for (String param : query.split("&"))
157158
{
158159
String pair[] = param.split("=");
159-
params.put(pair[0], pair[1]);
160+
String valuePart = "";
161+
if (pair.length > 1) {
162+
valuePart = URLUtils.urlDecodeWrapper(pair[1]);
163+
}
164+
params.put(URLUtils.urlDecodeWrapper(pair[0]),valuePart );
160165
}
161166
}
162167
params.putAll(querystringParams);
@@ -165,7 +170,7 @@ public Map<String, String> getQueryStringParams()
165170
catch (MalformedURLException mue)
166171
{
167172
throw new OAuthException("Malformed URL", mue);
168-
}
173+
}
169174
}
170175

171176
/**

src/main/java/org/scribe/model/Verifier.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.scribe.model;
22

3-
import org.scribe.utils.*;
4-
53
/**
64
* Represents an OAuth verifier code.
75
*
@@ -19,7 +17,10 @@ public class Verifier
1917
*/
2018
public Verifier(String value)
2119
{
22-
this.value = URLUtils.percentDecode(value);
20+
if (value == null) {
21+
throw new IllegalArgumentException();
22+
}
23+
this.value = value;
2324
}
2425

2526
public String getValue()

src/main/java/org/scribe/utils/URLUtils.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class URLUtils
3131
}
3232

3333
/**
34-
* Turns a map into a form-url-encoded string (key=value&key2=value2)
34+
* Turns a map into a form-url-encoded string (key=value&key2&key3=value)
3535
*
3636
* @param map any map
3737
* @return form-url-encoded string
@@ -51,7 +51,10 @@ private static String doFormUrlEncode(Map<String, String> map)
5151
{
5252
encodedString.append(PARAM_SEPARATOR);
5353
}
54-
encodedString.append(percentEncode(key)).append(PAIR_SEPARATOR).append(percentEncode(map.get(key)));
54+
encodedString.append(urlEncodeWrapper(key));
55+
if (map.get(key).length() > 0) {
56+
encodedString.append(PAIR_SEPARATOR).append(urlEncodeWrapper(map.get(key)));
57+
}
5558
}
5659
return encodedString.toString();
5760
}
@@ -81,12 +84,31 @@ public static String percentEncode(String string)
8184
}
8285

8386
/**
84-
* Percent decodes a string
87+
* URL encodes a string
88+
*
89+
* @param plain
90+
* @return percent encoded string
91+
*/
92+
public static String urlEncodeWrapper(String string)
93+
{
94+
Preconditions.checkNotNull(string, "Cannot encode null string");
95+
try
96+
{
97+
return URLEncoder.encode(string, UTF_8);
98+
}
99+
catch (UnsupportedEncodingException uee)
100+
{
101+
throw new IllegalStateException(ERROR_MSG, uee);
102+
}
103+
}
104+
105+
/**
106+
* URL decodes a string
85107
*
86108
* @param string percent encoded string
87109
* @return plain string
88110
*/
89-
public static String percentDecode(String string)
111+
public static String urlDecodeWrapper(String string)
90112
{
91113
Preconditions.checkNotNull(string, "Cannot decode null string");
92114
try
@@ -133,4 +155,15 @@ String apply(String string) {
133155
return string.replace(ch, toCh);
134156
}
135157
}
158+
159+
public static String concatSortedPercentEncodedParams(Map<String, String> params) {
160+
StringBuilder target = new StringBuilder();
161+
for (String key:params.keySet()){
162+
target.append(key);
163+
target.append(PAIR_SEPARATOR);
164+
target.append(params.get(key));
165+
target.append(PARAM_SEPARATOR);
166+
}
167+
return target.deleteCharAt(target.length() - 1).toString();
168+
}
136169
}

src/test/java/org/scribe/model/RequestTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void setup() throws Exception
1717
connection = new ConnectionStub();
1818
postRequest = new Request(Verb.POST, "http://example.com");
1919
postRequest.setConnection(connection);
20-
getRequest = new Request(Verb.GET, "http://example.com?qsparam=value&other=value+with+spaces");
20+
getRequest = new Request(Verb.GET, "http://example.com?qsparam=value&other+param=value+with+spaces");
2121
getRequest.setConnection(connection);
2222
}
2323

@@ -34,6 +34,8 @@ public void shouldGetQueryStringParameters()
3434
assertEquals(2, getRequest.getQueryStringParams().size());
3535
assertEquals(0, postRequest.getQueryStringParams().size());
3636
assertTrue(getRequest.getQueryStringParams().containsKey("qsparam"));
37+
assertTrue(getRequest.getQueryStringParams().get("qsparam").equals("value"));
38+
assertTrue(getRequest.getQueryStringParams().get("other param").equals("value with spaces"));
3739
}
3840

3941
@Test
@@ -52,7 +54,7 @@ public void shouldSetBodyParamsAndHeaders()
5254
postRequest.addBodyParameter("param", "value");
5355
postRequest.addBodyParameter("param two", "value with spaces");
5456
postRequest.send();
55-
assertEquals("param%20two=value%20with%20spaces&param=value", postRequest.getBodyContents());
57+
assertEquals("param+two=value+with+spaces&param=value", postRequest.getBodyContents());
5658
assertTrue(connection.getHeaders().containsKey("Content-Length"));
5759
}
5860

src/test/java/org/scribe/model/VerifierTest.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
package org.scribe.model;
22

3-
import static org.junit.Assert.*;
43

54
import org.junit.*;
65

76
public class VerifierTest
87
{
9-
@Test
10-
public void shouldParse()
11-
{
12-
Verifier verifier = new Verifier("p8k%2BGIjIL9PblXq%2BpH6LmT9l");
13-
assertEquals(verifier.getValue(), "p8k+GIjIL9PblXq+pH6LmT9l");
14-
}
158

169
@Test(expected = IllegalArgumentException.class)
1710
public void shouldThrowErrorForNullString()

src/test/java/org/scribe/utils/URLUtilsTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public void shouldPercentEncodeMap()
1616
params.put("key with spaces", "value with spaces");
1717
params.put("&symbols!", "#!");
1818

19-
String expected = "key=value&key%20with%20spaces=value%20with%20spaces&%26symbols%21=%23%21";
19+
String expected = "key=value&key+with+spaces=value+with+spaces&%26symbols%21=%23%21";
2020
assertEquals(expected, URLUtils.formURLEncodeMap(params));
2121
}
2222

@@ -41,7 +41,7 @@ public void shouldPercentDecodeString()
4141
{
4242
String toDecode = "this+is+a+test+%26%5E";
4343
String expected = "this is a test &^";
44-
assertEquals(expected, URLUtils.percentDecode(toDecode));
44+
assertEquals(expected, URLUtils.urlDecodeWrapper(toDecode));
4545
}
4646

4747
@Test
@@ -50,7 +50,7 @@ public void shouldEncodeAllSpecialCharacters()
5050
String plain = "!*'();:@&=+$,/?#[]";
5151
String encoded = "%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%23%5B%5D";
5252
assertEquals(encoded, URLUtils.percentEncode(plain));
53-
assertEquals(plain, URLUtils.percentDecode(encoded));
53+
assertEquals(plain, URLUtils.urlDecodeWrapper(encoded));
5454
}
5555

5656
@Test
@@ -79,7 +79,7 @@ public void shouldThrowExceptionIfStringToEncodeIsNull()
7979
public void shouldThrowExceptionIfStringToDecodeIsNull()
8080
{
8181
String toDecode = null;
82-
URLUtils.percentDecode(toDecode);
82+
URLUtils.urlDecodeWrapper(toDecode);
8383
}
8484

8585
@Test(expected = IllegalArgumentException.class)
@@ -103,7 +103,7 @@ public void shouldAppendNothingToQuerystringIfGivenEmptyMap()
103103
public void shouldAppendParametersToSimpleUrl()
104104
{
105105
String url = "http://www.example.com";
106-
String expectedUrl = "http://www.example.com?param1=value1&param2=value%20with%20spaces";
106+
String expectedUrl = "http://www.example.com?param1=value1&param2=value+with+spaces";
107107

108108
Map<String, String> params = new HashMap<String, String>();
109109
params.put("param1", "value1");
@@ -117,7 +117,7 @@ public void shouldAppendParametersToSimpleUrl()
117117
public void shouldAppendParametersToUrlWithQuerystring()
118118
{
119119
String url = "http://www.example.com?already=present";
120-
String expectedUrl = "http://www.example.com?already=present&param1=value1&param2=value%20with%20spaces";
120+
String expectedUrl = "http://www.example.com?already=present&param1=value1&param2=value+with+spaces";
121121

122122
Map<String, String> params = new HashMap<String, String>();
123123
params.put("param1", "value1");
@@ -142,6 +142,6 @@ public void shouldDecodePlusSymbol()
142142
String encoded = "oauth_verifier=7aEP%2BjNAwvjc0mjhqg0nuXPf";
143143
String expected = "oauth_verifier=7aEP+jNAwvjc0mjhqg0nuXPf";
144144

145-
Assert.assertEquals(expected, URLUtils.percentDecode(encoded));
145+
Assert.assertEquals(expected, URLUtils.urlDecodeWrapper(encoded));
146146
}
147147
}

0 commit comments

Comments
 (0)