changeset 703:0ee3a00bfdd1

Decode local-file URLs leniently
author Adam Domurad <adomurad@redhat.com>
date Fri, 26 Apr 2013 12:44:48 -0400
parents bdd44d6d1d1e
children 62126eb71a52
files ChangeLog netx/net/sourceforge/jnlp/cache/ResourceTracker.java netx/net/sourceforge/jnlp/util/UrlUtils.java tests/netx/unit/net/sourceforge/jnlp/util/UrlUtilsTest.java
diffstat 4 files changed, 33 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Apr 26 17:05:14 2013 +0200
+++ b/ChangeLog	Fri Apr 26 12:44:48 2013 -0400
@@ -1,3 +1,12 @@
+2013-04-26  Adam Domurad  <adomurad@redhat.com>
+
+	* netx/net/sourceforge/jnlp/cache/ResourceTracker.java
+	(getCacheFile): Use decodeUrlAsFile instead of toURI().getPath().
+	* netx/net/sourceforge/jnlp/util/UrlUtils.java
+	(decodeUrlAsFile): New, tolerates ill-formed URLs.
+	* tests/netx/unit/net/sourceforge/jnlp/util/UrlUtilsTest.java:
+	(testDecodeUrlAsFile): Test for (decodeUrlAsFile) 
+
 2013-04-26  Jiri Vanek  <jvanek@redhat.com>
             Jacob Wisor  <gitne@excite.co.jp>
 
--- a/netx/net/sourceforge/jnlp/cache/ResourceTracker.java	Fri Apr 26 17:05:14 2013 +0200
+++ b/netx/net/sourceforge/jnlp/cache/ResourceTracker.java	Fri Apr 26 12:44:48 2013 -0400
@@ -390,7 +390,7 @@
                 return resource.localFile;
 
             if (location.getProtocol().equalsIgnoreCase("file")) {
-                File file = new File(location.toURI().getPath());
+                File file = UrlUtils.decodeUrlAsFile(location);
                 if (file.exists())
                     return file;
             }
@@ -401,9 +401,6 @@
                 ex.printStackTrace();
 
             return null; // need an error exception to throw
-        } catch (URISyntaxException e) {
-            e.printStackTrace();
-            return null;
         }
     }
 
--- a/netx/net/sourceforge/jnlp/util/UrlUtils.java	Fri Apr 26 17:05:14 2013 +0200
+++ b/netx/net/sourceforge/jnlp/util/UrlUtils.java	Fri Apr 26 12:44:48 2013 -0400
@@ -37,6 +37,7 @@
 
 package net.sourceforge.jnlp.util;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.net.MalformedURLException;
@@ -134,4 +135,8 @@
         return normalizeUrlQuietly(url, false);
     }
 
+    /* Decode a URL as a file, being tolerant of URLs with mixed encoded & decoded portions. */
+    public static File decodeUrlAsFile(URL url) {
+        return new File(decodeUrlQuietly(url).getFile());
+    }
 }
--- a/tests/netx/unit/net/sourceforge/jnlp/util/UrlUtilsTest.java	Fri Apr 26 17:05:14 2013 +0200
+++ b/tests/netx/unit/net/sourceforge/jnlp/util/UrlUtilsTest.java	Fri Apr 26 12:44:48 2013 -0400
@@ -1,9 +1,11 @@
 package net.sourceforge.jnlp.util;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 
+import java.io.File;
 import java.net.URL;
-    
+
 import org.junit.Test;
 
 public class UrlUtilsTest {
@@ -57,10 +59,24 @@
         assertEquals("file://example/%20test",
                   UrlUtils.normalizeUrl(new URL("file://example/ test"), true).toString());
     }
+
     @Test
     public void testNormalizeUrlQuietly() throws Exception {
         // This is a wrapper over UrlUtils.normalizeUrl(), simple test suffices
         assertEquals("http://example.com/%20test%20test",
                 UrlUtils.normalizeUrl(new URL("http://example.com/ test%20test  ")).toString());
     }
+
+    @Test
+    public void testDecodeUrlAsFile() throws Exception {
+        String[] testPaths = {"/simple", "/ with spaces", "/with /multiple=/ odd characters?"};
+
+        for (String testPath : testPaths) {
+            File testFile = new File(testPath);
+            URL notEncodedUrl = testFile.toURL();
+            URL encodedUrl = testFile.toURI().toURL();
+            assertEquals(testFile, UrlUtils.decodeUrlAsFile(notEncodedUrl));
+            assertEquals(testFile, UrlUtils.decodeUrlAsFile(encodedUrl));
+        }
+    }
 }
\ No newline at end of file