Compare commits
5
Commits
multiparam
..
1.0.3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
94df2504ec | ||
|
|
50c35bc686 | ||
|
|
8c8f26e134 | ||
|
|
608f56d59f | ||
|
|
b2147de2e5 |
No files matched your search
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License
|
||||
|
||||
Copyright (c) 2010 Pablo Fenandez
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
@@ -4,7 +4,7 @@
|
||||
<groupId>org.scribe</groupId>
|
||||
<artifactId>scribe</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0.1</version>
|
||||
<version>1.0.3</version>
|
||||
<name>OAuth Library</name>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@@ -22,10 +22,10 @@ public class BaseStringExtractorImpl implements BaseStringExtractor
|
||||
|
||||
private String getSortedAndEncodedParams(OAuthRequest request)
|
||||
{
|
||||
Map<String, List<String>> params = new HashMap<String, List<String>>();
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.putAll(request.getQueryStringParams());
|
||||
params.putAll(request.getBodyParams());
|
||||
params.putAll(MapUtils.toListMap(request.getOauthParameters()));
|
||||
params.putAll(request.getOauthParameters());
|
||||
params = MapUtils.sort(params);
|
||||
return URLUtils.percentEncode(URLUtils.formURLEncodeMap(params));
|
||||
}
|
||||
|
||||
@@ -8,20 +8,24 @@ import org.scribe.utils.*;
|
||||
|
||||
public class HeaderExtractorImpl implements HeaderExtractor
|
||||
{
|
||||
|
||||
private static final String PARAM_SEPARATOR = ", ";
|
||||
private static final String PREAMBLE = "OAuth ";
|
||||
|
||||
public String extract(OAuthRequest request)
|
||||
{
|
||||
checkPreconditions(request);
|
||||
Map<String, String> parameters = request.getOauthParameters();
|
||||
StringBuffer header = new StringBuffer();
|
||||
StringBuffer header = new StringBuffer(parameters.size() * 20);
|
||||
header.append(PREAMBLE);
|
||||
for (String key : parameters.keySet())
|
||||
{
|
||||
addToHeader(header, key, parameters.get(key));
|
||||
if(header.length() > PREAMBLE.length())
|
||||
{
|
||||
header.append(PARAM_SEPARATOR);
|
||||
}
|
||||
header.append(String.format("%s=\"%s\"", key, URLUtils.percentEncode(parameters.get(key))));
|
||||
}
|
||||
return removeTrail(header);
|
||||
return header.toString();
|
||||
}
|
||||
|
||||
private void checkPreconditions(OAuthRequest request)
|
||||
@@ -34,13 +38,4 @@ public class HeaderExtractorImpl implements HeaderExtractor
|
||||
}
|
||||
}
|
||||
|
||||
private void addToHeader(StringBuffer header, String name, String value)
|
||||
{
|
||||
header.append(String.format("%s=\"%s\", ", name, URLUtils.percentEncode(value)));
|
||||
}
|
||||
|
||||
private String removeTrail(StringBuffer header)
|
||||
{
|
||||
return header.toString().substring(0, header.length() - 2);
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@ class Request
|
||||
|
||||
private String url;
|
||||
private Verb verb;
|
||||
private Map<String, List<String>> bodyParams;
|
||||
private Map<String, String> bodyParams;
|
||||
private Map<String, String> headers;
|
||||
private String payload = null;
|
||||
private Integer timeout = null;
|
||||
@@ -35,7 +35,7 @@ class Request
|
||||
{
|
||||
this.verb = verb;
|
||||
this.url = url;
|
||||
this.bodyParams = new HashMap<String, List<String>>();
|
||||
this.bodyParams = new HashMap<String, String>();
|
||||
this.headers = new HashMap<String, String>();
|
||||
try
|
||||
{
|
||||
@@ -108,14 +108,7 @@ class Request
|
||||
*/
|
||||
public void addBodyParameter(String key, String value)
|
||||
{
|
||||
if(bodyParams.get(key) != null)
|
||||
{
|
||||
bodyParams.get(key).add(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
bodyParams.put(key, Arrays.asList(value));
|
||||
}
|
||||
this.bodyParams.put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,26 +131,18 @@ class Request
|
||||
*
|
||||
* @return a map containing the query string parameters
|
||||
*/
|
||||
public Map<String, List<String>> getQueryStringParams()
|
||||
public Map<String, String> getQueryStringParams()
|
||||
{
|
||||
try
|
||||
{
|
||||
Map<String, List<String>> params = new HashMap<String, List<String>>();
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
String query = new URL(url).getQuery();
|
||||
if (query != null)
|
||||
{
|
||||
for (String param : query.split("&"))
|
||||
{
|
||||
String pair[] = param.split("=");
|
||||
String key = pair[0], value = pair[1];
|
||||
if(params.get(key)!= null)
|
||||
{
|
||||
params.get(key).add(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
params.put(key, Arrays.asList(value));
|
||||
}
|
||||
params.put(pair[0], pair[1]);
|
||||
}
|
||||
}
|
||||
return params;
|
||||
@@ -172,7 +157,7 @@ class Request
|
||||
*
|
||||
* @return a map containing the body parameters.
|
||||
*/
|
||||
public Map<String, List<String>> getBodyParams()
|
||||
public Map<String, String> getBodyParams()
|
||||
{
|
||||
return bodyParams;
|
||||
}
|
||||
|
||||
@@ -5,39 +5,22 @@ import java.util.*;
|
||||
public class MapUtils
|
||||
{
|
||||
|
||||
public static final Map<String, List<String>> sort(Map<String, List<String>> map)
|
||||
public static final Map<String, String> sort(Map<String, String> map)
|
||||
{
|
||||
Preconditions.checkNotNull(map, "Cannot sort a null object.");
|
||||
|
||||
Map<String, List<String>> sorted = new LinkedHashMap<String, List<String>>();
|
||||
Map<String, String> sorted = new LinkedHashMap<String, String>();
|
||||
for (String key : getSortedKeys(map))
|
||||
{
|
||||
if(map.get(key).size() > 1)
|
||||
{
|
||||
Collections.sort(map.get(key));
|
||||
}
|
||||
sorted.put(key, map.get(key));
|
||||
}
|
||||
return sorted;
|
||||
}
|
||||
|
||||
private static List<String> getSortedKeys(Map<String, List<String>> map)
|
||||
private static List<String> getSortedKeys(Map<String, String> map)
|
||||
{
|
||||
List<String> keys = new ArrayList<String>(map.keySet());
|
||||
Collections.sort(keys);
|
||||
return keys;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static final <T> Map<String, List<T>> toListMap(Map<String, T> map)
|
||||
{
|
||||
Preconditions.checkNotNull(map, "Cannot sort a null object.");
|
||||
|
||||
Map<String, List<T>> result = new HashMap<String, List<T>>();
|
||||
for (Map.Entry<String, T> entry : map.entrySet())
|
||||
{
|
||||
result.put(entry.getKey(), Arrays.asList(entry.getValue()));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -38,7 +38,7 @@ public class Preconditions
|
||||
|
||||
private static void check(boolean requirements, String error)
|
||||
{
|
||||
String message = (error == null || error.trim().isEmpty()) ? DEFAULT_MESSAGE : error;
|
||||
String message = (error == null || error.trim().length() <= 0) ? DEFAULT_MESSAGE : error;
|
||||
if (!requirements)
|
||||
{
|
||||
throw new IllegalArgumentException(message);
|
||||
|
||||
@@ -16,28 +16,24 @@ public class URLUtils
|
||||
|
||||
private static final String ERROR_MSG = String.format("Cannot find specified encoding: %s", UTF_8);
|
||||
|
||||
public static String formURLEncodeMap(Map<String, List<String>> map)
|
||||
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, List<String>> map)
|
||||
private static String doFormUrlEncode(Map<String, String> map)
|
||||
{
|
||||
StringBuffer encodedString = new StringBuffer();
|
||||
StringBuffer encodedString = new StringBuffer(map.size() * 20);
|
||||
for (String key : map.keySet())
|
||||
{
|
||||
for(String value: map.get(key))
|
||||
if(encodedString.length() > 0)
|
||||
{
|
||||
encodedString.append(percentEncode(key)).append(PAIR_SEPARATOR).append(percentEncode(value)).append(PARAM_SEPARATOR);
|
||||
encodedString.append(PARAM_SEPARATOR);
|
||||
}
|
||||
encodedString.append(percentEncode(key)).append(PAIR_SEPARATOR).append(percentEncode(map.get(key)));
|
||||
}
|
||||
return removeTrailingSeparator(encodedString);
|
||||
}
|
||||
|
||||
private static String removeTrailingSeparator(StringBuffer buffer)
|
||||
{
|
||||
return buffer.toString().substring(0, buffer.length() - 1);
|
||||
return encodedString.toString();
|
||||
}
|
||||
|
||||
public static String percentEncode(String string)
|
||||
@@ -45,8 +41,9 @@ public class URLUtils
|
||||
Preconditions.checkNotNull(string, "Cannot encode null string");
|
||||
try
|
||||
{
|
||||
return URLEncoder.encode(string, UTF_8);
|
||||
} catch (UnsupportedEncodingException uee)
|
||||
return URLEncoder.encode(string, UTF_8).replaceAll("\\+", "%20");
|
||||
}
|
||||
catch (UnsupportedEncodingException uee)
|
||||
{
|
||||
throw new OAuthException(ERROR_MSG, uee);
|
||||
}
|
||||
|
||||
@@ -41,4 +41,12 @@ public class BaseStringExtractorTest
|
||||
OAuthRequest request = new OAuthRequest(Verb.GET, "http://example.com");
|
||||
extractor.extract(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldProperlyEncodeSpaces()
|
||||
{
|
||||
String expected = "GET&http%3A%2F%2Fexample.com&body%3Dthis%2520param%2520has%2520whitespace%26oauth_callback%3Dhttp%253A%252F%252Fexample%252Fcallback%26oauth_consumer_key%3DAS%2523%2524%255E*%2540%2526%26oauth_signature%3DOAuth-Signature%26oauth_timestamp%3D123456";
|
||||
request.addBodyParameter("body", "this param has whitespace");
|
||||
assertEquals(expected, extractor.extract(request));
|
||||
}
|
||||
}
|
||||
@@ -52,7 +52,7 @@ public class RequestTest
|
||||
postRequest.addBodyParameter("param", "value");
|
||||
postRequest.addBodyParameter("param two", "value with spaces");
|
||||
postRequest.send();
|
||||
assertEquals("param+two=value+with+spaces¶m=value", postRequest.getBodyContents());
|
||||
assertEquals("param%20two=value%20with%20spaces¶m=value", postRequest.getBodyContents());
|
||||
assertTrue(connection.getHeaders().containsKey("Content-Length"));
|
||||
}
|
||||
|
||||
|
||||
@@ -9,42 +9,40 @@ import org.junit.*;
|
||||
public class MapUtilsTest
|
||||
{
|
||||
|
||||
private Map<String, List<String>> unsorted;
|
||||
private Map<String, String> unsorted;
|
||||
|
||||
@Before
|
||||
public void setup()
|
||||
{
|
||||
unsorted = new LinkedHashMap<String, List<String>>();
|
||||
unsorted.put("d", Arrays.asList("fourth"));
|
||||
unsorted.put("a", Arrays.asList("first"));
|
||||
unsorted.put("c", Arrays.asList("third"));
|
||||
unsorted.put("b", Arrays.asList("second", "second_two", "second_one"));
|
||||
unsorted = new LinkedHashMap<String, String>();
|
||||
unsorted.put("d", "fourth");
|
||||
unsorted.put("a", "first");
|
||||
unsorted.put("c", "third");
|
||||
unsorted.put("b", "second");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSortMap()
|
||||
{
|
||||
Map<String, List<String>> sorted = MapUtils.sort(unsorted);
|
||||
List<List<String>> values = new ArrayList<List<String>>(sorted.values());
|
||||
assertEquals("first", values.get(0).get(0));
|
||||
assertEquals("second", values.get(1).get(0));
|
||||
assertEquals("second_one", values.get(1).get(1));
|
||||
assertEquals("second_two", values.get(1).get(2));
|
||||
assertEquals("third", values.get(2).get(0));
|
||||
assertEquals("fourth", values.get(3).get(0));
|
||||
Map<String, String> sorted = MapUtils.sort(unsorted);
|
||||
List<String> values = new ArrayList<String>(sorted.values());
|
||||
assertEquals("first", values.get(0));
|
||||
assertEquals("second", values.get(1));
|
||||
assertEquals("third", values.get(2));
|
||||
assertEquals("fourth", values.get(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotModifyTheOriginalMap()
|
||||
{
|
||||
Map<String, List<String>> sorted = MapUtils.sort(unsorted);
|
||||
Map<String, String> sorted = MapUtils.sort(unsorted);
|
||||
assertNotSame(sorted, unsorted);
|
||||
|
||||
List<List<String>> values = new ArrayList<List<String>>(unsorted.values());
|
||||
assertEquals("fourth", values.get(0).get(0));
|
||||
assertEquals("first", values.get(1).get(0));
|
||||
assertEquals("third", values.get(2).get(0));
|
||||
assertEquals("second", values.get(3).get(0));
|
||||
List<String> values = new ArrayList<String>(unsorted.values());
|
||||
assertEquals("fourth", values.get(0));
|
||||
assertEquals("first", values.get(1));
|
||||
assertEquals("third", values.get(2));
|
||||
assertEquals("second", values.get(3));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
|
||||
@@ -18,19 +18,19 @@ public class URLUtilsTest
|
||||
@Test
|
||||
public void shouldPercentEncodeMap()
|
||||
{
|
||||
Map<String, List<String>> params = new LinkedHashMap<String, List<String>>();
|
||||
params.put("key", Arrays.asList("value"));
|
||||
params.put("key with spaces", Arrays.asList("value with spaces"));
|
||||
params.put("&symbols!", Arrays.asList("#!"));
|
||||
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";
|
||||
String expected = "key=value&key%20with%20spaces=value%20with%20spaces&%26symbols%21=%23%21";
|
||||
assertEquals(expected, URLUtils.formURLEncodeMap(params));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnEmptyStringForEmptyMap()
|
||||
{
|
||||
Map<String, List<String>> params = new LinkedHashMap<String, List<String>>();
|
||||
Map<String, String> params = new LinkedHashMap<String, String>();
|
||||
String expected = "";
|
||||
assertEquals(expected, URLUtils.formURLEncodeMap(params));
|
||||
}
|
||||
@@ -39,7 +39,7 @@ public class URLUtilsTest
|
||||
public void shouldPercentEncodeString()
|
||||
{
|
||||
String toEncode = "this is a test &^";
|
||||
String expected = "this+is+a+test+%26%5E";
|
||||
String expected = "this%20is%20a%20test%20%26%5E";
|
||||
assertEquals(expected, URLUtils.percentEncode(toEncode));
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ public class URLUtilsTest
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void shouldThrowExceptionIfMapIsNull()
|
||||
{
|
||||
Map<String, List<String>> nullMap = null;
|
||||
Map<String, String> nullMap = null;
|
||||
URLUtils.formURLEncodeMap(nullMap);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user