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>
|
<groupId>org.scribe</groupId>
|
||||||
<artifactId>scribe</artifactId>
|
<artifactId>scribe</artifactId>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<version>1.0.1</version>
|
<version>1.0.3</version>
|
||||||
<name>OAuth Library</name>
|
<name>OAuth Library</name>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|||||||
@@ -8,20 +8,24 @@ import org.scribe.utils.*;
|
|||||||
|
|
||||||
public class HeaderExtractorImpl implements HeaderExtractor
|
public class HeaderExtractorImpl implements HeaderExtractor
|
||||||
{
|
{
|
||||||
|
private static final String PARAM_SEPARATOR = ", ";
|
||||||
private static final String PREAMBLE = "OAuth ";
|
private static final String PREAMBLE = "OAuth ";
|
||||||
|
|
||||||
public String extract(OAuthRequest request)
|
public String extract(OAuthRequest request)
|
||||||
{
|
{
|
||||||
checkPreconditions(request);
|
checkPreconditions(request);
|
||||||
Map<String, String> parameters = request.getOauthParameters();
|
Map<String, String> parameters = request.getOauthParameters();
|
||||||
StringBuffer header = new StringBuffer();
|
StringBuffer header = new StringBuffer(parameters.size() * 20);
|
||||||
header.append(PREAMBLE);
|
header.append(PREAMBLE);
|
||||||
for (String key : parameters.keySet())
|
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)
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -38,7 +38,7 @@ public class Preconditions
|
|||||||
|
|
||||||
private static void check(boolean requirements, String error)
|
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)
|
if (!requirements)
|
||||||
{
|
{
|
||||||
throw new IllegalArgumentException(message);
|
throw new IllegalArgumentException(message);
|
||||||
|
|||||||
@@ -24,17 +24,16 @@ public class URLUtils
|
|||||||
|
|
||||||
private static String doFormUrlEncode(Map<String, 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 key : map.keySet())
|
||||||
{
|
{
|
||||||
encodedString.append(percentEncode(key)).append(PAIR_SEPARATOR).append(percentEncode(map.get(key))).append(PARAM_SEPARATOR);
|
if(encodedString.length() > 0)
|
||||||
|
{
|
||||||
|
encodedString.append(PARAM_SEPARATOR);
|
||||||
|
}
|
||||||
|
encodedString.append(percentEncode(key)).append(PAIR_SEPARATOR).append(percentEncode(map.get(key)));
|
||||||
}
|
}
|
||||||
return removeTrailingSeparator(encodedString);
|
return encodedString.toString();
|
||||||
}
|
|
||||||
|
|
||||||
private static String removeTrailingSeparator(StringBuffer buffer)
|
|
||||||
{
|
|
||||||
return buffer.toString().substring(0, buffer.length() - 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String percentEncode(String string)
|
public static String percentEncode(String string)
|
||||||
@@ -42,8 +41,9 @@ 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);
|
return URLEncoder.encode(string, UTF_8).replaceAll("\\+", "%20");
|
||||||
} catch (UnsupportedEncodingException uee)
|
}
|
||||||
|
catch (UnsupportedEncodingException uee)
|
||||||
{
|
{
|
||||||
throw new OAuthException(ERROR_MSG, uee);
|
throw new OAuthException(ERROR_MSG, uee);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,4 +41,12 @@ public class BaseStringExtractorTest
|
|||||||
OAuthRequest request = new OAuthRequest(Verb.GET, "http://example.com");
|
OAuthRequest request = new OAuthRequest(Verb.GET, "http://example.com");
|
||||||
extractor.extract(request);
|
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", "value");
|
||||||
postRequest.addBodyParameter("param two", "value with spaces");
|
postRequest.addBodyParameter("param two", "value with spaces");
|
||||||
postRequest.send();
|
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"));
|
assertTrue(connection.getHeaders().containsKey("Content-Length"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ public class URLUtilsTest
|
|||||||
params.put("key with spaces", "value with spaces");
|
params.put("key with spaces", "value with spaces");
|
||||||
params.put("&symbols!", "#!");
|
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));
|
assertEquals(expected, URLUtils.formURLEncodeMap(params));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,7 +39,7 @@ public class URLUtilsTest
|
|||||||
public void shouldPercentEncodeString()
|
public void shouldPercentEncodeString()
|
||||||
{
|
{
|
||||||
String toEncode = "this is a test &^";
|
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));
|
assertEquals(expected, URLUtils.percentEncode(toEncode));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user