Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e0e8df9

Browse files
Added formURLEncode/Decode methods
1 parent 70358ad commit e0e8df9

File tree

2 files changed

+77
-32
lines changed

2 files changed

+77
-32
lines changed

src/main/java/org/scribe/utils/URLUtils.java

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class URLUtils
3131
}
3232

3333
/**
34-
* Turns a map into a form-url-encoded string (key=value&key2=value2)
34+
* Turns a map into a form-urlencoded string
3535
*
3636
* @param map any map
3737
* @return form-url-encoded string
@@ -47,32 +47,43 @@ private static String doFormUrlEncode(Map<String, String> map)
4747
StringBuffer encodedString = new StringBuffer(map.size() * 20);
4848
for (String key : map.keySet())
4949
{
50-
if(encodedString.length() > 0)
50+
encodedString.append(PARAM_SEPARATOR).append(formURLEncode(key));
51+
if(map.get(key) != null)
5152
{
52-
encodedString.append(PARAM_SEPARATOR);
53+
encodedString.append(PAIR_SEPARATOR).append(formURLEncode(map.get(key)));
5354
}
54-
encodedString.append(percentEncode(key)).append(PAIR_SEPARATOR).append(percentEncode(map.get(key)));
5555
}
56-
return encodedString.toString();
56+
return encodedString.toString().substring(1);
5757
}
5858

5959
/**
6060
* Percent encodes a string
6161
*
62-
* @param plain
62+
* @param string plain string
6363
* @return percent encoded string
6464
*/
6565
public static String percentEncode(String string)
66+
{
67+
String encoded = formURLEncode(string);
68+
for (EncodingRule rule : ENCODING_RULES)
69+
{
70+
encoded = rule.apply(encoded);
71+
}
72+
return encoded;
73+
}
74+
75+
/**
76+
* Translates a string into application/x-www-form-urlencoded format
77+
*
78+
* @param plain
79+
* @return form-urlencoded string
80+
*/
81+
public static String formURLEncode(String string)
6682
{
6783
Preconditions.checkNotNull(string, "Cannot encode null string");
6884
try
6985
{
70-
String encoded = URLEncoder.encode(string, UTF_8);
71-
for(EncodingRule rule : ENCODING_RULES)
72-
{
73-
encoded = rule.apply(encoded);
74-
}
75-
return encoded;
86+
return URLEncoder.encode(string, UTF_8);
7687
}
7788
catch (UnsupportedEncodingException uee)
7889
{
@@ -81,12 +92,12 @@ public static String percentEncode(String string)
8192
}
8293

8394
/**
84-
* Percent decodes a string
95+
* Decodes a application/x-www-form-urlencoded string
8596
*
86-
* @param string percent encoded string
97+
* @param string form-urlencoded string
8798
* @return plain string
8899
*/
89-
public static String percentDecode(String string)
100+
public static String formURLDecode(String string)
90101
{
91102
Preconditions.checkNotNull(string, "Cannot decode null string");
92103
try
@@ -110,12 +121,27 @@ public static String appendParametersToQueryString(String url, Map<String, Strin
110121
{
111122
Preconditions.checkNotNull(url, "Cannot append to null URL");
112123
String queryString = URLUtils.formURLEncodeMap(params);
113-
if (queryString.length() == 0) return url;
124+
if (queryString.equals(EMPTY_STRING))
125+
{
126+
return url;
127+
}
128+
else
129+
{
130+
url += url.indexOf(QUERY_STRING_SEPARATOR) != -1 ? PARAM_SEPARATOR : QUERY_STRING_SEPARATOR;
131+
url += queryString;
132+
return url;
133+
}
134+
}
114135

115-
// Check if there are parameters in the url already and use '&' instead of '?'
116-
url += url.indexOf(QUERY_STRING_SEPARATOR) != -1 ? PARAM_SEPARATOR : QUERY_STRING_SEPARATOR;
117-
url += queryString;
118-
return url;
136+
public static String concatSortedPercentEncodedParams(Map<String, String> params)
137+
{
138+
StringBuilder result = new StringBuilder();
139+
for (String key : params.keySet())
140+
{
141+
result.append(key).append(PAIR_SEPARATOR);
142+
result.append(params.get(key)).append(PARAM_SEPARATOR);
143+
}
144+
return result.toString().substring(0, result.length() - 1);
119145
}
120146

121147
private static final class EncodingRule

src/test/java/org/scribe/utils/URLUtilsTest.java

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public void shouldPercentEncodeMap()
1616
params.put("key with spaces", "value with spaces");
1717
params.put("&symbols!", "#!");
1818

19-
String expected = "key=value&key%20with%20spaces=value%20with%20spaces&%26symbols%21=%23%21";
19+
String expected = "key=value&key+with+spaces=value+with+spaces&%26symbols%21=%23%21";
2020
assertEquals(expected, URLUtils.formURLEncodeMap(params));
2121
}
2222

@@ -28,6 +28,17 @@ public void shouldReturnEmptyStringForEmptyMap()
2828
assertEquals(expected, URLUtils.formURLEncodeMap(params));
2929
}
3030

31+
@Test
32+
public void shouldFormURLEncodeMapWithMissingValues()
33+
{
34+
Map<String, String> params = new LinkedHashMap<String, String>();
35+
params.put("key", "value");
36+
params.put("key with spaces", null);
37+
38+
String expected = "key=value&key+with+spaces";
39+
assertEquals(expected, URLUtils.formURLEncodeMap(params));
40+
}
41+
3142
@Test
3243
public void shouldPercentEncodeString()
3344
{
@@ -37,24 +48,32 @@ public void shouldPercentEncodeString()
3748
}
3849

3950
@Test
40-
public void shouldPercentDecodeString()
51+
public void shouldFormURLEncodeString()
52+
{
53+
String toEncode = "this is a test &^";
54+
String expected = "this+is+a+test+%26%5E";
55+
assertEquals(expected, URLUtils.formURLEncode(toEncode));
56+
}
57+
58+
@Test
59+
public void shouldFormURLDecodeString()
4160
{
4261
String toDecode = "this+is+a+test+%26%5E";
4362
String expected = "this is a test &^";
44-
assertEquals(expected, URLUtils.percentDecode(toDecode));
63+
assertEquals(expected, URLUtils.formURLDecode(toDecode));
4564
}
4665

4766
@Test
48-
public void shouldEncodeAllSpecialCharacters()
67+
public void shouldPercentEncodeAllSpecialCharacters()
4968
{
5069
String plain = "!*'();:@&=+$,/?#[]";
5170
String encoded = "%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%23%5B%5D";
5271
assertEquals(encoded, URLUtils.percentEncode(plain));
53-
assertEquals(plain, URLUtils.percentDecode(encoded));
72+
assertEquals(plain, URLUtils.formURLDecode(encoded));
5473
}
5574

5675
@Test
57-
public void shouldNotEncodeReservedCharacters()
76+
public void shouldNotPercentEncodeReservedCharacters()
5877
{
5978
String plain = "abcde123456-._~";
6079
String encoded = plain;
@@ -79,7 +98,7 @@ public void shouldThrowExceptionIfStringToEncodeIsNull()
7998
public void shouldThrowExceptionIfStringToDecodeIsNull()
8099
{
81100
String toDecode = null;
82-
URLUtils.percentDecode(toDecode);
101+
URLUtils.formURLDecode(toDecode);
83102
}
84103

85104
@Test(expected = IllegalArgumentException.class)
@@ -103,7 +122,7 @@ public void shouldAppendNothingToQuerystringIfGivenEmptyMap()
103122
public void shouldAppendParametersToSimpleUrl()
104123
{
105124
String url = "http://www.example.com";
106-
String expectedUrl = "http://www.example.com?param1=value1&param2=value%20with%20spaces";
125+
String expectedUrl = "http://www.example.com?param1=value1&param2=value+with+spaces";
107126

108127
Map<String, String> params = new HashMap<String, String>();
109128
params.put("param1", "value1");
@@ -117,7 +136,7 @@ public void shouldAppendParametersToSimpleUrl()
117136
public void shouldAppendParametersToUrlWithQuerystring()
118137
{
119138
String url = "http://www.example.com?already=present";
120-
String expectedUrl = "http://www.example.com?already=present&param1=value1&param2=value%20with%20spaces";
139+
String expectedUrl = "http://www.example.com?already=present&param1=value1&param2=value+with+spaces";
121140

122141
Map<String, String> params = new HashMap<String, String>();
123142
params.put("param1", "value1");
@@ -128,7 +147,7 @@ public void shouldAppendParametersToUrlWithQuerystring()
128147
}
129148

130149
@Test
131-
public void shouldEncodePlusSymbol()
150+
public void shouldPercentEncodePlusSymbol()
132151
{
133152
String plain = "7aEP+jNAwvjc0mjhqg0nuXPf";
134153
String encoded = "7aEP%2BjNAwvjc0mjhqg0nuXPf";
@@ -137,11 +156,11 @@ public void shouldEncodePlusSymbol()
137156
}
138157

139158
@Test
140-
public void shouldDecodePlusSymbol()
159+
public void shouldURLDecodePlusSymbol()
141160
{
142161
String encoded = "oauth_verifier=7aEP%2BjNAwvjc0mjhqg0nuXPf";
143162
String expected = "oauth_verifier=7aEP+jNAwvjc0mjhqg0nuXPf";
144163

145-
Assert.assertEquals(expected, URLUtils.percentDecode(encoded));
164+
Assert.assertEquals(expected, URLUtils.formURLDecode(encoded));
146165
}
147166
}

0 commit comments

Comments
 (0)