removed URLUtils

This commit is contained in:
Pablo Fernandez committed 2011-12-14 16:18:06 -03:00
1 parent b380b3af3d
commit 83b62f6079
16 files changed
+104 -303

No files matched your search

@@ -3,7 +3,6 @@ package org.scribe.builder.api;
import org.scribe.model.*;
import org.scribe.utils.*;
import static org.scribe.utils.URLUtils.*;
public class FacebookApi extends DefaultApi20
{
@@ -24,11 +23,11 @@ public class FacebookApi extends DefaultApi20
// Append scope if present
if(config.hasScope())
{
return String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(), formURLEncode(config.getCallback()), formURLEncode(config.getScope()));
return String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()), OAuthEncoder.encode(config.getScope()));
}
else
{
return String.format(AUTHORIZE_URL, config.getApiKey(), formURLEncode(config.getCallback()));
return String.format(AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()));
}
}
}
@@ -18,7 +18,7 @@ public class Foursquare2Api extends DefaultApi20
public String getAuthorizationUrl(OAuthConfig config)
{
Preconditions.checkValidUrl(config.getCallback(), "Must provide a valid url as callback. Foursquare2 does not support OOB");
return String.format(AUTHORIZATION_URL, config.getApiKey(), URLUtils.formURLEncode(config.getCallback()));
return String.format(AUTHORIZATION_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()));
}
@Override
@@ -1,13 +1,9 @@
package org.scribe.builder.api;
import org.scribe.extractors.*;
import org.scribe.model.*;
import org.scribe.utils.*;
import static org.scribe.utils.URLUtils.*;
public class LiveApi extends DefaultApi20
{
@@ -28,10 +24,11 @@ public class LiveApi extends DefaultApi20
// Append scope if present
if (config.hasScope())
{
return String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(), formURLEncode(config.getCallback()), formURLEncode(config.getScope()));
} else
return String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()), OAuthEncoder.encode(config.getScope()));
}
else
{
return String.format(AUTHORIZE_URL, config.getApiKey(), formURLEncode(config.getCallback()));
return String.format(AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()));
}
}
@@ -25,11 +25,11 @@ public class VkontakteApi extends DefaultApi20
Preconditions.checkValidUrl(config.getCallback(), "Valid url is required for a callback. Vkontakte does not support OOB");
if(config.hasScope())// Appending scope if present
{
return String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(), URLUtils.formURLEncode(config.getCallback()),URLUtils.formURLEncode(config.getScope()));
return String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()), OAuthEncoder.encode(config.getScope()));
}
else
{
return String.format(AUTHORIZE_URL, config.getApiKey(), URLUtils.formURLEncode(config.getCallback()));
return String.format(AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()));
}
}
@@ -1,7 +1,5 @@
package org.scribe.extractors;
import java.util.*;
import org.scribe.exceptions.*;
import org.scribe.model.*;
import org.scribe.utils.*;
@@ -23,8 +21,8 @@ public class BaseStringExtractorImpl implements BaseStringExtractor
public String extract(OAuthRequest request)
{
checkPreconditions(request);
String verb = URLUtils.percentEncode(request.getVerb().name());
String url = URLUtils.percentEncode(request.getSanitizedUrl());
String verb = OAuthEncoder.encode(request.getVerb().name());
String url = OAuthEncoder.encode(request.getSanitizedUrl());
String params = getSortedAndEncodedParams(request);
return String.format(AMPERSAND_SEPARATED_STRING, verb, url, params);
}
@@ -32,7 +32,7 @@ public class HeaderExtractorImpl implements HeaderExtractor
{
header.append(PARAM_SEPARATOR);
}
header.append(String.format("%s=\"%s\"", key, URLUtils.percentEncode(parameters.get(key))));
header.append(String.format("%s=\"%s\"", key, OAuthEncoder.encode(parameters.get(key))));
}
return header.toString();
}
@@ -25,7 +25,7 @@ public class TokenExtractor20Impl implements AccessTokenExtractor
Matcher matcher = Pattern.compile(TOKEN_REGEX).matcher(response);
if (matcher.find())
{
String token = URLUtils.formURLDecode(matcher.group(1));
String token = OAuthEncoder.decode(matcher.group(1));
return new Token(token, EMPTY_SECRET, response);
}
else
@@ -34,7 +34,7 @@ public class TokenExtractorImpl implements RequestTokenExtractor, AccessTokenExt
Matcher matcher = p.matcher(response);
if (matcher.find() && matcher.groupCount() >= 1)
{
return URLUtils.formURLDecode(matcher.group(1));
return OAuthEncoder.decode(matcher.group(1));
}
else
{
@@ -1,14 +1,13 @@
package org.scribe.model;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.scribe.utils.OAuthEncoder;
import org.scribe.utils.Preconditions;
import org.scribe.utils.URLUtils;
/**
* @author: Pablo Fernandez
@@ -98,8 +97,8 @@ public class ParameterList
for (String param : queryString.split(PARAM_SEPARATOR))
{
String pair[] = param.split(PAIR_SEPARATOR);
String key = URLUtils.formURLDecode(pair[0]);
String value = pair.length > 1 ? URLUtils.formURLDecode(pair[1]) : EMPTY_STRING;
String key = OAuthEncoder.decode(pair[0]);
String value = pair.length > 1 ? OAuthEncoder.decode(pair[1]) : EMPTY_STRING;
params.add(new Parameter(key, value));
}
}
@@ -30,7 +30,7 @@ public class HMACSha1SignatureService implements SignatureService
{
Preconditions.checkEmptyString(baseString, "Base string cant be null or empty string");
Preconditions.checkEmptyString(apiSecret, "Api secret cant be null or empty string");
return doSign(baseString, URLUtils.percentEncode(apiSecret) + '&' + URLUtils.percentEncode(tokenSecret));
return doSign(baseString, OAuthEncoder.encode(apiSecret) + '&' + OAuthEncoder.encode(tokenSecret));
}
catch (Exception e)
{
@@ -21,7 +21,7 @@ public class PlaintextSignatureService implements SignatureService
try
{
Preconditions.checkEmptyString(apiSecret, "Api secret cant be null or empty string");
return URLUtils.percentEncode(apiSecret) + '&' + URLUtils.percentEncode(tokenSecret);
return OAuthEncoder.encode(apiSecret) + '&' + OAuthEncoder.encode(tokenSecret);
}
catch (Exception e)
{
@@ -1,12 +1,10 @@
package org.scribe.utils;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import org.scribe.exceptions.OAuthException;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.regex.*;
import org.scribe.exceptions.*;
/**
* @author: Pablo Fernandez
@@ -52,6 +50,13 @@ public class OAuthEncoder
public static String decode(String encoded)
{
Preconditions.checkNotNull(encoded, "Cannot decode null object");
return encoded;
try
{
return URLDecoder.decode(encoded, CHARSET);
}
catch(UnsupportedEncodingException uee)
{
throw new OAuthException("Charset not found while decoding string: " + CHARSET, uee);
}
}
}
@@ -1,128 +0,0 @@
package org.scribe.utils;
import java.io.*;
import java.net.*;
import java.util.*;
/**
* Utils to deal with URL and url-encodings
*
* @author Pablo Fernandez
*/
public class URLUtils
{
private static final String EMPTY_STRING = "";
private static final String UTF_8 = "UTF-8";
private static final String PAIR_SEPARATOR = "=";
private static final String PARAM_SEPARATOR = "&";
private static final char QUERY_STRING_SEPARATOR = '?';
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-urlencoded string
*
* @param map any map
* @return form-url-encoded string
*/
public static String formURLEncodeMap(Map<String, String> map)
{
Preconditions.checkNotNull(map, "Cannot url-encode a null object");
return (map.size() <= 0) ? EMPTY_STRING : doFormUrlEncode(map);
}
private static String doFormUrlEncode(Map<String, String> map)
{
StringBuffer encodedString = new StringBuffer(map.size() * 20);
for (String key : map.keySet())
{
encodedString.append(PARAM_SEPARATOR).append(formURLEncode(key));
if(map.get(key) != null)
{
encodedString.append(PAIR_SEPARATOR).append(formURLEncode(map.get(key)));
}
}
return encodedString.toString().substring(1);
}
/**
* Percent encodes a string
*
* @param string plain string
* @return percent encoded string
*/
public static String percentEncode(String string)
{
String encoded = formURLEncode(string);
for (EncodingRule rule : ENCODING_RULES)
{
encoded = rule.apply(encoded);
}
return encoded;
}
/**
* Translates a string into application/x-www-form-urlencoded format
*
* @param plain
* @return form-urlencoded string
*/
public static String formURLEncode(String string)
{
Preconditions.checkNotNull(string, "Cannot encode null string");
try
{
return URLEncoder.encode(string, UTF_8);
}
catch (UnsupportedEncodingException uee)
{
throw new IllegalStateException(ERROR_MSG, uee);
}
}
/**
* Decodes a application/x-www-form-urlencoded string
*
* @param string form-urlencoded string
* @return plain string
*/
public static String formURLDecode(String string)
{
Preconditions.checkNotNull(string, "Cannot decode null string");
try
{
return URLDecoder.decode(string, UTF_8);
}
catch (UnsupportedEncodingException uee)
{
throw new IllegalStateException(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);
}
}
}
@@ -1,13 +1,8 @@
package org.scribe.model;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.scribe.utils.URLUtils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
@@ -81,8 +76,8 @@ public class ParameterListTest
params.add("param1", "v1");
params.add("param6", "v2");
params.add("a_param", "v3");
params.add("param2", "v4");
Assert.assertEquals("a_param=v3&param1=v1&param2=v4&param6=v2", params.sort().asFormUrlEncodedString());
params.add("param1", "v4");
Assert.assertEquals("a_param=v3&param1=v1&param1=v4&param6=v2", params.sort().asFormUrlEncodedString());
}
@Test
@@ -0,0 +1,70 @@
package org.scribe.utils;
import org.junit.*;
import static org.junit.Assert.*;
/**
* @author: Pablo Fernandez
*/
public class OAuthEncoderTest
{
@Test
public void shouldPercentEncodeString()
{
String plain = "this is a test &^";
String encoded = "this%20is%20a%20test%20%26%5E";
assertEquals(encoded, OAuthEncoder.encode(plain));
}
@Test
public void shouldFormURLDecodeString()
{
String encoded = "this+is+a+test+%26%5E";
String plain = "this is a test &^";
assertEquals(plain, OAuthEncoder.decode(encoded));
}
@Test
public void shouldPercentEncodeAllSpecialCharacters()
{
String plain = "!*'();:@&=+$,/?#[]";
String encoded = "%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%23%5B%5D";
assertEquals(encoded, OAuthEncoder.encode(plain));
assertEquals(plain, OAuthEncoder.decode(encoded));
}
@Test
public void shouldNotPercentEncodeReservedCharacters()
{
String plain = "abcde123456-._~";
String encoded = plain;
assertEquals(encoded, OAuthEncoder.encode(plain));
}
@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionIfStringToEncodeIsNull()
{
String toEncode = null;
OAuthEncoder.encode(toEncode);
}
@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionIfStringToDecodeIsNull()
{
String toDecode = null;
OAuthEncoder.decode(toDecode);
}
@Test
public void shouldPercentEncodeCorrectlyTwitterCodingExamples()
{
// These tests are part of the Twitter dev examples here -> https://dev.twitter.com/docs/auth/percent-encoding-parameters
String sources[] = {"Ladies + Gentlemen", "An encoded string!", "Dogs, Cats & Mice"};
String encoded[] = {"Ladies%20%2B%20Gentlemen", "An%20encoded%20string%21", "Dogs%2C%20Cats%20%26%20Mice"};
for(int i = 0; i < sources.length; i++)
{
Assert.assertEquals(encoded[i], OAuthEncoder.encode(sources[i]));
}
}
}
@@ -1,134 +0,0 @@
package org.scribe.utils;
import static org.junit.Assert.*;
import java.util.*;
import org.junit.*;
public class URLUtilsTest
{
@Test
public void shouldPercentEncodeMap()
{
Map<String, String> params = new LinkedHashMap<String, String>();
params.put("key", "value");
params.put("key with spaces", "value with spaces");
params.put("&symbols!", "#!");
String expected = "key=value&key+with+spaces=value+with+spaces&%26symbols%21=%23%21";
assertEquals(expected, URLUtils.formURLEncodeMap(params));
}
@Test
public void shouldReturnEmptyStringForEmptyMap()
{
Map<String, String> params = new LinkedHashMap<String, String>();
String expected = "";
assertEquals(expected, URLUtils.formURLEncodeMap(params));
}
@Test
public void shouldFormURLEncodeMapWithMissingValues()
{
Map<String, String> params = new LinkedHashMap<String, String>();
params.put("key", "value");
params.put("key with spaces", null);
String expected = "key=value&key+with+spaces";
assertEquals(expected, URLUtils.formURLEncodeMap(params));
}
@Test
public void shouldPercentEncodeString()
{
String toEncode = "this is a test &^";
String expected = "this%20is%20a%20test%20%26%5E";
assertEquals(expected, URLUtils.percentEncode(toEncode));
}
@Test
public void shouldFormURLEncodeString()
{
String toEncode = "this is a test &^";
String expected = "this+is+a+test+%26%5E";
assertEquals(expected, URLUtils.formURLEncode(toEncode));
}
@Test
public void shouldFormURLDecodeString()
{
String toDecode = "this+is+a+test+%26%5E";
String expected = "this is a test &^";
assertEquals(expected, URLUtils.formURLDecode(toDecode));
}
@Test
public void shouldPercentEncodeAllSpecialCharacters()
{
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.formURLDecode(encoded));
}
@Test
public void shouldNotPercentEncodeReservedCharacters()
{
String plain = "abcde123456-._~";
String encoded = plain;
assertEquals(encoded, URLUtils.percentEncode(plain));
}
@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionIfMapIsNull()
{
Map<String, String> nullMap = null;
URLUtils.formURLEncodeMap(nullMap);
}
@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionIfStringToEncodeIsNull()
{
String toEncode = null;
URLUtils.percentEncode(toEncode);
}
@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionIfStringToDecodeIsNull()
{
String toDecode = null;
URLUtils.formURLDecode(toDecode);
}
@Test
public void shouldPercentEncodePlusSymbol()
{
String plain = "7aEP+jNAwvjc0mjhqg0nuXPf";
String encoded = "7aEP%2BjNAwvjc0mjhqg0nuXPf";
Assert.assertEquals(encoded, URLUtils.percentEncode(plain));
}
@Test
public void shouldURLDecodePlusSymbol()
{
String encoded = "oauth_verifier=7aEP%2BjNAwvjc0mjhqg0nuXPf";
String expected = "oauth_verifier=7aEP+jNAwvjc0mjhqg0nuXPf";
Assert.assertEquals(expected, URLUtils.formURLDecode(encoded));
}
@Test
public void shouldPercentEncodeCorrectlyTwitterCodingExamples()
{
// These tests are part of the Twitter dev examples here -> https://dev.twitter.com/docs/auth/percent-encoding-parameters
String sources[] = {"Ladies + Gentlemen", "An encoded string!", "Dogs, Cats & Mice"};
String encoded[] = {"Ladies%20%2B%20Gentlemen", "An%20encoded%20string%21", "Dogs%2C%20Cats%20%26%20Mice"};
for(int i = 0; i < sources.length; i++)
{
Assert.assertEquals(encoded[i], URLUtils.percentEncode(sources[i]));
}
}
}