changeset 1151:6d7f7e0e3829

Saving/Loading of icon made resistent against URLs with invalid/without target * netx/net/sourceforge/jnlp/cache/CacheUtil.java: getCachedResource split into getCachedResourceFile and getCachedResourceURL (which is jsut transforming file from getCachedResourceFile to URL) * netx/net/sourceforge/jnlp/util/XDesktopEntry.java: (cacheIcon) calls to getCachedResource replaced by calls to getCachedResourceURL and added nullchecks. New method of cantCache to unify NonFileProtocolException throw.
author Jiri Vanek <jvanek@redhat.com>
date Wed, 11 Feb 2015 09:56:37 +0100
parents 95c4a59a6b8d
children f150f0a8cfcc
files ChangeLog netx/net/sourceforge/jnlp/cache/CacheUtil.java netx/net/sourceforge/jnlp/util/XDesktopEntry.java
diffstat 3 files changed, 52 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon Feb 09 16:44:15 2015 -0500
+++ b/ChangeLog	Wed Feb 11 09:56:37 2015 +0100
@@ -1,4 +1,14 @@
-2014-02-09  Jie Kang  <jkang@redhat.com>
+2015-02-11  Jiri Vanek  <jvanek@redhat.com>
+
+	Saving/Loading of icon made resistent against URLs with invalid/without target
+	* netx/net/sourceforge/jnlp/cache/CacheUtil.java: getCachedResource split
+	into getCachedResourceFile and getCachedResourceURL (which is jsut transforming
+	file from getCachedResourceFile to URL)
+	* netx/net/sourceforge/jnlp/util/XDesktopEntry.java: (cacheIcon) calls to 
+	getCachedResource replaced by calls to getCachedResourceURL and added null
+	checks. New method of cantCache to unify NonFileProtocolException throw.
+
+2015-02-09  Jie Kang  <jkang@redhat.com>
 
 	Fix javascript url error in JSToJSet reproducer.
 	* tests/reproducers/simple/JSToJSet/resources/JSToJSet.html: added quotes
--- a/netx/net/sourceforge/jnlp/cache/CacheUtil.java	Mon Feb 09 16:44:15 2015 -0500
+++ b/netx/net/sourceforge/jnlp/cache/CacheUtil.java	Wed Feb 11 09:56:37 2015 +0100
@@ -70,19 +70,38 @@
      *
      * @param location location of the resource
      * @param version the version, or {@code null}
+     * @param policy how to handle update
      * @return either the location in the cache or the original location
      */
-    public static URL getCachedResource(URL location, Version version, UpdatePolicy policy) {
-        ResourceTracker rt = new ResourceTracker();
-        rt.addResource(location, version, null, policy);
+    public static URL getCachedResourceURL(URL location, Version version, UpdatePolicy policy) {
         try {
-            File f = rt.getCacheFile(location);
+            File f = getCachedResourceFile(location, version, policy);
+            //url was ponting to nowhere eg 404
+            if (f == null){
+                //originally  f.toUrl was throwing NPE
+                return null;
+                //returning null seems to be better
+            }
             // TODO: Should be toURI().toURL()
             return f.toURL();
         } catch (MalformedURLException ex) {
             return location;
         }
     }
+    
+    /**
+     * This is returning File object of cached resource originally from URL
+     * @param location original location of blob
+     * @param version
+     * @param policy
+     * @return location in ITW cache on filesystem 
+     */
+    public static File  getCachedResourceFile(URL location, Version version, UpdatePolicy policy) {
+        ResourceTracker rt = new ResourceTracker();
+        rt.addResource(location, version, null, policy);
+        File f = rt.getCacheFile(location);
+        return f;
+    }
 
     /**
      * Returns the Permission object necessary to access the
--- a/netx/net/sourceforge/jnlp/util/XDesktopEntry.java	Mon Feb 09 16:44:15 2015 -0500
+++ b/netx/net/sourceforge/jnlp/util/XDesktopEntry.java	Wed Feb 11 09:56:37 2015 +0100
@@ -431,10 +431,14 @@
 
         String location = null;
         if (uiconLocation != null) {
-            location = CacheUtil.getCachedResource(uiconLocation, null, UpdatePolicy.SESSION)
-                    .toString();
+            //this throws npe, if url (specified in jnlp) points to 404
+            URL urlLocation = CacheUtil.getCachedResourceURL(uiconLocation, null, UpdatePolicy.SESSION);
+            if (urlLocation == null) {
+                cantCache();
+            }
+            location = urlLocation.toString();
             if (!location.startsWith("file:")) {
-                throw new NonFileProtocolException("Unable to cache icon");
+                cantCache();
             }
         } else {
             //try favicon.ico
@@ -445,10 +449,14 @@
                         file.getCodeBase().getPort(),
                         "/" + FAVICON);
                 JNLPFile.openURL(favico, null, UpdatePolicy.ALWAYS);
-                location = CacheUtil.getCachedResource(favico, null, UpdatePolicy.SESSION)
-                        .toString();
+                //this MAY throw npe, if url (specified in jnlp) points to 404
+                URL urlLocation = CacheUtil.getCachedResourceURL(favico, null, UpdatePolicy.SESSION);
+                if (urlLocation == null) {
+                    cantCache();
+                }
+                location = urlLocation.toString();
                 if (!location.startsWith("file:")) {
-                    throw new NonFileProtocolException("Unable to cache icon");
+                    cantCache();
                 }
             } catch (IOException ex) {
                 //favicon 404 or similar
@@ -474,6 +482,10 @@
         }
     }
 
+    private void cantCache() throws NonFileProtocolException {
+        throw new NonFileProtocolException("Unable to cache icon");
+    }
+
     private String getDesktopIconName() {
         return sanitize(file.createJnlpTitle());
     }