Improved string concatenation algorithm
This commit is contained in:
1 parent
608f56d59f
commit
8c8f26e134
2 files changed
+17
-22
No files matched your search
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -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)
|
||||||
@@ -43,7 +42,8 @@ public class URLUtils
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
return URLEncoder.encode(string, UTF_8);
|
return URLEncoder.encode(string, UTF_8);
|
||||||
} catch (UnsupportedEncodingException uee)
|
}
|
||||||
|
catch (UnsupportedEncodingException uee)
|
||||||
{
|
{
|
||||||
throw new OAuthException(ERROR_MSG, uee);
|
throw new OAuthException(ERROR_MSG, uee);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user