moved map functions to MapUtils

This commit is contained in:
Pablo Fernandez committed 2011-08-15 23:40:15 -03:00
1 parent 7f89dc3546
commit 1932ad28c1
4 files changed
+45 -43

No files matched your search

@@ -36,7 +36,7 @@ public class BaseStringExtractorImpl implements BaseStringExtractor
MapUtils.decodeAndAppendEntries(request.getBodyParams(), params);
MapUtils.decodeAndAppendEntries(request.getOauthParameters(), params);
params = MapUtils.sort(params);
return URLUtils.percentEncode(URLUtils.concatSortedPercentEncodedParams(params));
return URLUtils.percentEncode(MapUtils.concatSortedPercentEncodedParams(params));
}
private void checkPreconditions(OAuthRequest request)
+1 -1
View File
@@ -177,7 +177,7 @@ class Request
{
Map<String, String> params = new HashMap<String, String>();
String queryString = new URL(url).getQuery();
params.putAll(URLUtils.queryStringToMap(queryString));
params.putAll(MapUtils.queryStringToMap(queryString));
params.putAll(this.querystringParams);
return params;
}
@@ -9,6 +9,10 @@ import java.util.*;
*/
public class MapUtils
{
private static final String EMPTY_STRING = "";
private static final String PAIR_SEPARATOR = "=";
private static final String PARAM_SEPARATOR = "&";
/**
* Sorts a Map
*
@@ -48,4 +52,43 @@ public class MapUtils
target.put(URLUtils.percentEncode(key), URLUtils.percentEncode(source.get(key)));
}
}
/**
* Concats a key-value map into a querystring-like String
*
* @param params key-value map
* @return querystring-like String
*/
public static String concatSortedPercentEncodedParams(Map<String, String> params)
{
StringBuilder result = new StringBuilder();
for (String key : params.keySet())
{
result.append(key).append(PAIR_SEPARATOR);
result.append(params.get(key)).append(PARAM_SEPARATOR);
}
return result.toString().substring(0, result.length() - 1);
}
/**
* Parses and form-urldecodes a querystring-like string into a map
*
* @param queryString querystring-like String
* @return a map with the form-urldecoded parameters
*/
public static Map<String, String> queryStringToMap(String queryString)
{
Map<String, String> result = new HashMap<String, String>();
if (queryString != null && queryString.length() > 0)
{
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;
result.put(key, value);
}
}
return result;
}
}
@@ -133,47 +133,6 @@ public class URLUtils
}
}
/**
* Concats a key-value map into a querystring-like String
*
* @param params key-value map
* @return querystring-like String
*/
// TODO Move to MapUtils
public static String concatSortedPercentEncodedParams(Map<String, String> params)
{
StringBuilder result = new StringBuilder();
for (String key : params.keySet())
{
result.append(key).append(PAIR_SEPARATOR);
result.append(params.get(key)).append(PARAM_SEPARATOR);
}
return result.toString().substring(0, result.length() - 1);
}
/**
* Parses and form-urldecodes a querystring-like string into a map
*
* @param queryString querystring-like String
* @return a map with the form-urldecoded parameters
*/
// TODO Move to MapUtils
public static Map<String, String> queryStringToMap(String queryString)
{
Map<String, String> result = new HashMap<String, String>();
if (queryString != null && queryString.length() > 0)
{
for (String param : queryString.split(PARAM_SEPARATOR))
{
String pair[] = param.split(PAIR_SEPARATOR);
String key = formURLDecode(pair[0]);
String value = pair.length > 1 ? formURLDecode(pair[1]) : EMPTY_STRING;
result.put(key, value);
}
}
return result;
}
private static final class EncodingRule
{
private final String ch;