Refactored URLEncoing code

This commit is contained in:
Pablo Fernandez committed 2010-11-20 19:27:04 -03:00
1 parent 99a03b4679
commit c0d24b556c
6 files changed
+53 -45

No files matched your search

+1
View File
@@ -11,3 +11,4 @@
* FEATURE: Added setConnectTimeout and setReadTimeout for the Request Object * FEATURE: Added setConnectTimeout and setReadTimeout for the Request Object
* FIX: Fixed Evernote Api (uses GET for request and access tokens) * FIX: Fixed Evernote Api (uses GET for request and access tokens)
* REFACTOR: URLEncoding stuff. LinkedIn specific decoder is no longer needed
@@ -1,11 +1,7 @@
package org.scribe.builder.api; package org.scribe.builder.api;
import org.scribe.extensions.linkedin.*;
import org.scribe.extractors.*;
public class LinkedInApi extends DefaultApi10a public class LinkedInApi extends DefaultApi10a
{ {
@Override @Override
protected String getAccessTokenEndpoint() protected String getAccessTokenEndpoint()
{ {
@@ -17,11 +13,4 @@ public class LinkedInApi extends DefaultApi10a
{ {
return "https://api.linkedin.com/uas/oauth/requestToken"; return "https://api.linkedin.com/uas/oauth/requestToken";
} }
@Override
protected BaseStringExtractor getBaseStringExtractor()
{
return new LinkedInBaseStringExtractorImpl();
}
} }
@@ -1,23 +0,0 @@
package org.scribe.extensions.linkedin;
import org.scribe.extractors.*;
import org.scribe.model.*;
/**
* LinkedIn's specific extractor that does not encode the tilde character.
*
* @author Pablo Fernandez
*/
public class LinkedInBaseStringExtractorImpl extends BaseStringExtractorImpl
{
/**
* {@inheritDoc}
*/
@Override
public String extract(OAuthRequest request)
{
String baseString = super.extract(request);
return baseString.replace("%7E", "~");
}
}
+35 -2
View File
@@ -20,6 +20,17 @@ public class URLUtils
private static final String ERROR_MSG = String.format("Cannot find specified encoding: %s", UTF_8); private static final String ERROR_MSG = String.format("Cannot find specified encoding: %s", UTF_8);
private static final Set<EncodingRule> ENCODING_RULES;
static
{
Set<EncodingRule> rules = new HashSet<EncodingRule>();
rules.add(new EncodingRule("*","%2A"));
rules.add(new EncodingRule("+","%20"));
rules.add(new EncodingRule("%7E", "~"));
ENCODING_RULES = Collections.unmodifiableSet(rules);
}
/** /**
* Turns a map into a form-url-encoded string (key=value&key2=value2) * Turns a map into a form-url-encoded string (key=value&key2=value2)
* *
@@ -57,7 +68,12 @@ public class URLUtils
Preconditions.checkNotNull(string, "Cannot encode null string"); Preconditions.checkNotNull(string, "Cannot encode null string");
try try
{ {
return URLEncoder.encode(string, UTF_8).replaceAll("\\+", "%20"); String encoded = URLEncoder.encode(string, UTF_8);
for(EncodingRule rule : ENCODING_RULES)
{
encoded = rule.apply(encoded);
}
return encoded;
} }
catch (UnsupportedEncodingException uee) catch (UnsupportedEncodingException uee)
{ {
@@ -77,9 +93,26 @@ public class URLUtils
try try
{ {
return URLDecoder.decode(string, UTF_8); return URLDecoder.decode(string, UTF_8);
} catch (UnsupportedEncodingException uee) }
catch (UnsupportedEncodingException uee)
{ {
throw new OAuthException(ERROR_MSG, uee); throw new OAuthException(ERROR_MSG, uee);
} }
} }
private static final class EncodingRule
{
private final String ch;
private final String toCh;
EncodingRule(String ch, String toCh)
{
this.ch = ch;
this.toCh = toCh;
}
String apply(String string) {
return string.replace(ch, toCh);
}
}
} }
@@ -55,8 +55,6 @@ public class LinkedInExample
System.out.println(); System.out.println();
System.out.println("Thats it man! Go and build something awesome with Scribe! :)"); System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
System.out.println(service.getRequestToken());
} }
} }
@@ -8,13 +8,6 @@ import org.junit.*;
public class URLUtilsTest public class URLUtilsTest
{ {
@Before
public void setup()
{
}
@Test @Test
public void shouldPercentEncodeMap() public void shouldPercentEncodeMap()
{ {
@@ -51,6 +44,23 @@ public class URLUtilsTest
assertEquals(expected, URLUtils.percentDecode(toDecode)); assertEquals(expected, URLUtils.percentDecode(toDecode));
} }
@Test
public void shouldEncodeAllSpecialCharacters()
{
String plain = "!*'();:@&=+$,/?#[]";
String encoded = "%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%23%5B%5D";
assertEquals(encoded, URLUtils.percentEncode(plain));
assertEquals(plain, URLUtils.percentDecode(encoded));
}
@Test
public void shouldNotEncodeReservedCharacters()
{
String plain = "abcde123456-._~";
String encoded = plain;
assertEquals(encoded, URLUtils.percentEncode(plain));
}
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionIfMapIsNull() public void shouldThrowExceptionIfMapIsNull()
{ {