4 Commits
Author SHA1 Message Date
Pablo Fernandez 94df2504ec Changed version number 2010-09-22 11:11:22 -03:00
Pablo Fernandez 50c35bc686 Changed whitespace encoding to %21 instead of '+'
Changed version number for release
2010-09-17 23:23:02 -03:00
Pablo Fernandez 8c8f26e134 Improved string concatenation algorithm 2010-09-17 23:09:28 -03:00
Pablo Fernandez 608f56d59f Added MIT license 2010-09-16 20:44:12 -03:00
7 changed files with 51 additions and 27 deletions

No files matched your search

+21
View File
@@ -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.
+1 -1
View File
@@ -4,7 +4,7 @@
<groupId>org.scribe</groupId>
<artifactId>scribe</artifactId>
<packaging>jar</packaging>
<version>1.0.2</version>
<version>1.0.3</version>
<name>OAuth Library</name>
<dependencies>
@@ -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);
}
}
+10 -10
View File
@@ -24,17 +24,16 @@ public class URLUtils
private static String doFormUrlEncode(Map<String, String> map)
{
StringBuffer encodedString = new StringBuffer();
StringBuffer encodedString = new StringBuffer(map.size() * 20);
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);
}
private static String removeTrailingSeparator(StringBuffer buffer)
{
return buffer.toString().substring(0, buffer.length() - 1);
return encodedString.toString();
}
public static String percentEncode(String string)
@@ -42,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&param=value", postRequest.getBodyContents());
assertEquals("param%20two=value%20with%20spaces&param=value", postRequest.getBodyContents());
assertTrue(connection.getHeaders().containsKey("Content-Length"));
}
@@ -23,7 +23,7 @@ public class URLUtilsTest
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));
}
@@ -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));
}