Skip to content

Commit c0d24b5

Browse files
Refactored URLEncoing code
1 parent 99a03b4 commit c0d24b5

File tree

6 files changed

+53
-45
lines changed

6 files changed

+53
-45
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111

1212
* FEATURE: Added setConnectTimeout and setReadTimeout for the Request Object
1313
* FIX: Fixed Evernote Api (uses GET for request and access tokens)
14+
* REFACTOR: URLEncoding stuff. LinkedIn specific decoder is no longer needed
Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package org.scribe.builder.api;
22

3-
import org.scribe.extensions.linkedin.*;
4-
import org.scribe.extractors.*;
5-
63
public class LinkedInApi extends DefaultApi10a
74
{
8-
95
@Override
106
protected String getAccessTokenEndpoint()
117
{
@@ -17,11 +13,4 @@ protected String getRequestTokenEndpoint()
1713
{
1814
return "https://api.linkedin.com/uas/oauth/requestToken";
1915
}
20-
21-
@Override
22-
protected BaseStringExtractor getBaseStringExtractor()
23-
{
24-
return new LinkedInBaseStringExtractorImpl();
25-
}
26-
2716
}

src/main/java/org/scribe/extensions/linkedin/LinkedInBaseStringExtractorImpl.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ public class URLUtils
2020

2121
private static final String ERROR_MSG = String.format("Cannot find specified encoding: %s", UTF_8);
2222

23+
private static final Set<EncodingRule> ENCODING_RULES;
24+
25+
static
26+
{
27+
Set<EncodingRule> rules = new HashSet<EncodingRule>();
28+
rules.add(new EncodingRule("*","%2A"));
29+
rules.add(new EncodingRule("+","%20"));
30+
rules.add(new EncodingRule("%7E", "~"));
31+
ENCODING_RULES = Collections.unmodifiableSet(rules);
32+
}
33+
2334
/**
2435
* Turns a map into a form-url-encoded string (key=value&key2=value2)
2536
*
@@ -57,7 +68,12 @@ public static String percentEncode(String string)
5768
Preconditions.checkNotNull(string, "Cannot encode null string");
5869
try
5970
{
60-
return URLEncoder.encode(string, UTF_8).replaceAll("\\+", "%20");
71+
String encoded = URLEncoder.encode(string, UTF_8);
72+
for(EncodingRule rule : ENCODING_RULES)
73+
{
74+
encoded = rule.apply(encoded);
75+
}
76+
return encoded;
6177
}
6278
catch (UnsupportedEncodingException uee)
6379
{
@@ -77,9 +93,26 @@ public static String percentDecode(String string)
7793
try
7894
{
7995
return URLDecoder.decode(string, UTF_8);
80-
} catch (UnsupportedEncodingException uee)
96+
}
97+
catch (UnsupportedEncodingException uee)
8198
{
8299
throw new OAuthException(ERROR_MSG, uee);
83100
}
84101
}
102+
103+
private static final class EncodingRule
104+
{
105+
private final String ch;
106+
private final String toCh;
107+
108+
EncodingRule(String ch, String toCh)
109+
{
110+
this.ch = ch;
111+
this.toCh = toCh;
112+
}
113+
114+
String apply(String string) {
115+
return string.replace(ch, toCh);
116+
}
117+
}
85118
}

src/test/java/org/scribe/examples/LinkedInExample.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ public static void main(String[] args)
5555

5656
System.out.println();
5757
System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
58-
59-
System.out.println(service.getRequestToken());
6058
}
6159

6260
}

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@
88

99
public class URLUtilsTest
1010
{
11-
12-
@Before
13-
public void setup()
14-
{
15-
16-
}
17-
1811
@Test
1912
public void shouldPercentEncodeMap()
2013
{
@@ -51,6 +44,23 @@ public void shouldPercentDecodeString()
5144
assertEquals(expected, URLUtils.percentDecode(toDecode));
5245
}
5346

47+
@Test
48+
public void shouldEncodeAllSpecialCharacters()
49+
{
50+
String plain = "!*'();:@&=+$,/?#[]";
51+
String encoded = "%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%23%5B%5D";
52+
assertEquals(encoded, URLUtils.percentEncode(plain));
53+
assertEquals(plain, URLUtils.percentDecode(encoded));
54+
}
55+
56+
@Test
57+
public void shouldNotEncodeReservedCharacters()
58+
{
59+
String plain = "abcde123456-._~";
60+
String encoded = plain;
61+
assertEquals(encoded, URLUtils.percentEncode(plain));
62+
}
63+
5464
@Test(expected = IllegalArgumentException.class)
5565
public void shouldThrowExceptionIfMapIsNull()
5666
{

0 commit comments

Comments
 (0)