diff options
author | Remo Arpagaus | 2014-01-28 11:01:07 +0000 |
---|---|---|
committer | Ken Lee | 2014-01-28 14:14:18 +0000 |
commit | 5e75ddea0cc7a9467c67ad9df3f348325a578d07 (patch) | |
tree | 458b0c82c244ceaec25715c5bf08a7429754f335 | |
parent | a86e9f250e2ae0b13f363a7c00de2f0c7e6210cc (diff) | |
download | org.eclipse.scout.rt-5e75ddea0cc7a9467c67ad9df3f348325a578d07.tar.gz org.eclipse.scout.rt-5e75ddea0cc7a9467c67ad9df3f348325a578d07.tar.xz org.eclipse.scout.rt-5e75ddea0cc7a9467c67ad9df3f348325a578d07.zip |
bug 426769: IOUtility.urlEncode with a null input returns "%20" instead
of null
https://bugs.eclipse.org/bugs/show_bug.cgi?id=426769
Change-Id: I48cfb211c82f1a9e1006270d39178b109b98bca7
Signed-off-by: Remo Arpagaus <remo.arpagaus@bsiag.com>
Reviewed-on: https://git.eclipse.org/r/21191
Tested-by: Hudson CI
Reviewed-by: Ken Lee <kle@bsiag.com>
IP-Clean: Ken Lee <kle@bsiag.com>
-rw-r--r-- | org.eclipse.scout.commons.test/src/org/eclipse/scout/commons/IOUtilityTest.java | 24 | ||||
-rw-r--r-- | org.eclipse.scout.commons/src/org/eclipse/scout/commons/IOUtility.java | 52 |
2 files changed, 60 insertions, 16 deletions
diff --git a/org.eclipse.scout.commons.test/src/org/eclipse/scout/commons/IOUtilityTest.java b/org.eclipse.scout.commons.test/src/org/eclipse/scout/commons/IOUtilityTest.java index 4504ac1cf0..02f02ca43c 100644 --- a/org.eclipse.scout.commons.test/src/org/eclipse/scout/commons/IOUtilityTest.java +++ b/org.eclipse.scout.commons.test/src/org/eclipse/scout/commons/IOUtilityTest.java @@ -134,4 +134,28 @@ public class IOUtilityTest { assertFalse("Temp dir was not deleted.", tempDir.exists()); assertFalse("Temp file was not deleted.", tempFile.exists()); } + + @Test + public void testUrlEncode() throws Exception { + assertEquals("www.google.com", IOUtility.urlEncode("www.google.com")); + assertNull(IOUtility.urlEncode(null)); + assertEquals("", IOUtility.urlEncode("")); + assertEquals("", IOUtility.urlEncode(" ")); + assertEquals("", IOUtility.urlEncode(" \n\t")); + assertEquals("http%3A%2F%2Fwww.google.org", IOUtility.urlEncode(" http://www.google.org ")); + assertEquals("a%20test%20%20with%20%20%20multiple%20%20%20%20spaces", IOUtility.urlEncode(" a test with multiple spaces")); + assertEquals("Expected UTF-8 charset", "%C3%B6%C3%A4%C3%BC%C3%A9%C3%A0%C3%A8", IOUtility.urlEncode("öäüéàè")); + } + + @Test + public void testUrlDecode() throws Exception { + assertEquals("www.google.com", IOUtility.urlDecode("www.google.com")); + assertNull(IOUtility.urlDecode(null)); + assertEquals("", IOUtility.urlDecode("")); + assertEquals("", IOUtility.urlDecode(" ")); + assertEquals("", IOUtility.urlDecode(" \n\t")); + assertEquals("http://www.google.org", IOUtility.urlDecode(" http%3A%2F%2Fwww.google.org ")); + assertEquals("a test with multiple spaces", IOUtility.urlDecode("a%20test%20%20with%20%20%20multiple%20%20%20%20spaces")); + assertEquals("Expected UTF-8 charset", "öäüéàè", IOUtility.urlDecode("%C3%B6%C3%A4%C3%BC%C3%A9%C3%A0%C3%A8")); + } } diff --git a/org.eclipse.scout.commons/src/org/eclipse/scout/commons/IOUtility.java b/org.eclipse.scout.commons/src/org/eclipse/scout/commons/IOUtility.java index ba55ac8c9c..b77211a9d9 100644 --- a/org.eclipse.scout.commons/src/org/eclipse/scout/commons/IOUtility.java +++ b/org.eclipse.scout.commons/src/org/eclipse/scout/commons/IOUtility.java @@ -535,38 +535,58 @@ public final class IOUtility { } } - public static String urlEncode(String o) throws ProcessingException { - String s; - if (o == null) { - s = ""; - } - else { - s = o.toString().trim(); + /** + * A null-safe variant for calling {@link URLEncoder#encode(String, String)}. This method returns null if the given + * <code>url</code> is null or an empty string respectively. Any leading / trailing whitespaces are omitted. + * Furthermore, "%20" is used to represent spaces instead of "+". + * + * @param url + * the URL string which shall be encoded + * @return the encoded URL string + */ + public static String urlEncode(String url) { + if (url == null) { + return null; } + + String s = url.toString().trim(); if (s.length() == 0) { - s = " "; + return ""; } + try { - s = URLEncoder.encode(s, "UTF-8");// Build 158 needed an encoding + s = URLEncoder.encode(s, "UTF-8"); s = StringUtility.replace(s, "+", "%20"); } catch (UnsupportedEncodingException e) { + LOG.error("Unsupported encoding", e); } return s; } - public static String urlDecode(String o) throws ProcessingException { - String s; - if (o == null) { - s = ""; + /** + * a null-safe variant for calling {@link URLDecoder#decode(String, String)}. This method returns null if the given + * <code>url</code> is null or an empty string respectively. Any leading / trailing whitespaces are omitted. + * + * @param encodedUrl + * the encoded URL string which shall be decoded + * @return the decoded URL string + */ + public static String urlDecode(String encodedUrl) { + if (encodedUrl == null) { + return null; } - else { - s = o.toString().trim(); + + String s = encodedUrl.toString().trim(); + if (s.length() == 0) { + return ""; } + try { - s = URLDecoder.decode(s, "UTF-8");// Build 158 needed an encoding + s = URLDecoder.decode(s, "UTF-8"); } catch (UnsupportedEncodingException e) { + LOG.error("Unsupported encoding", e); } return s; } |