/hg/icedtea-web: Handle absolute paths passed into jnlp_href's v...

ddadacha at icedtea.classpath.org ddadacha at icedtea.classpath.org
Mon Jun 4 10:35:51 PDT 2012


changeset c2396c2ee991 in /hg/icedtea-web
details: http://icedtea.classpath.org/hg/icedtea-web?cmd=changeset;node=c2396c2ee991
author: Danesh Dadachanji <ddadacha at redhat.com>
date: Mon Jun 04 13:35:25 2012 -0400

	Handle absolute paths passed into jnlp_href's value.


diffstat:

 ChangeLog                                                  |  14 +
 netx/net/sourceforge/jnlp/JNLPCreator.java                 |  35 ++++
 netx/net/sourceforge/jnlp/PluginBridge.java                |  22 ++-
 tests/netx/unit/net/sourceforge/jnlp/PluginBridgeTest.java |  99 ++++++++++++++
 4 files changed, 165 insertions(+), 5 deletions(-)

diffs (219 lines):

diff -r bdc092f4b4ff -r c2396c2ee991 ChangeLog
--- a/ChangeLog	Mon Jun 04 12:24:04 2012 -0400
+++ b/ChangeLog	Mon Jun 04 13:35:25 2012 -0400
@@ -1,3 +1,17 @@
+2012-06-04  Danesh Dadachanji  <ddadacha at redhat.com>
+
+	Fix to handle absolute paths passed into jnlp_href's value.
+	* netx/net/sourceforge/jnlp/PluginBridge.java
+	(PluginBridge): Uses context of codebase to evaluate jnlp_href's value.
+	Uses JNLPCreator's create method to make new JNLPFile variables.
+	New constructor that wraps around the original one, creating a new
+	JNLPCreator to use.
+	* netx/net/sourceforge/jnlp/JNLPCreator.java: New strategy pattern class
+	to be used to wrap around the creation of a JNLPFile. Replace this creator
+	when unit testing to skip running parsing code.
+	* tests/netx/unit/net/sourceforge/jnlp/PluginBridgeTest.java:
+	New class to unit test getEvaluatedJNLPHref.
+
 2012-06-04  Adam Domurad  <adomurad at redhat.com>
 
 	Added self to AUTHORS.
diff -r bdc092f4b4ff -r c2396c2ee991 netx/net/sourceforge/jnlp/JNLPCreator.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/netx/net/sourceforge/jnlp/JNLPCreator.java	Mon Jun 04 13:35:25 2012 -0400
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ * This file is part of IcedTea, http://icedtea.classpath.org
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+package net.sourceforge.jnlp;
+
+import java.io.IOException;
+import java.net.URL;
+
+import net.sourceforge.jnlp.cache.UpdatePolicy;
+
+public class JNLPCreator {
+    public JNLPFile create(URL location, Version version, boolean strict,
+            UpdatePolicy policy, URL forceCodebase) throws IOException, ParseException {
+        return new JNLPFile(location, version, strict, policy, forceCodebase);
+    }
+}
diff -r bdc092f4b4ff -r c2396c2ee991 netx/net/sourceforge/jnlp/PluginBridge.java
--- a/netx/net/sourceforge/jnlp/PluginBridge.java	Mon Jun 04 12:24:04 2012 -0400
+++ b/netx/net/sourceforge/jnlp/PluginBridge.java	Mon Jun 04 13:35:25 2012 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright 2007 Red Hat, Inc.
+ * Copyright 2012 Red Hat, Inc.
  * This file is part of IcedTea, http://icedtea.classpath.org
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
@@ -46,10 +46,20 @@
     private boolean codeBaseLookup;
     private boolean useJNLPHref;
 
+    /**
+     * Creates a new PluginBridge using a default JNLPCreator.
+     */
     public PluginBridge(URL codebase, URL documentBase, String jar, String main,
                         int width, int height, Hashtable<String, String> atts,
                         String uKey)
             throws Exception {
+        this(codebase, documentBase, jar, main, width, height, atts, uKey, new JNLPCreator());
+    }
+
+    public PluginBridge(URL codebase, URL documentBase, String jar, String main,
+                        int width, int height, Hashtable<String, String> atts,
+                        String uKey, JNLPCreator jnlpCreator)
+            throws Exception {
         specVersion = new Version("1.0");
         fileVersion = new Version("1.1");
         this.codeBase = codebase;
@@ -59,8 +69,10 @@
         if (atts.containsKey("jnlp_href")) {
             useJNLPHref = true;
             try {
-                URL jnlp = new URL(codeBase.toExternalForm() + atts.get("jnlp_href"));
-                JNLPFile jnlpFile = new JNLPFile(jnlp, null, false, JNLPRuntime.getDefaultUpdatePolicy(), this.codeBase);
+                // Use codeBase as the context for the URL. If jnlp_href's
+                // value is a complete URL, it will replace codeBase's context.
+                URL jnlp = new URL(codeBase, atts.get("jnlp_href"));
+                JNLPFile jnlpFile = jnlpCreator.create(jnlp, null, false, JNLPRuntime.getDefaultUpdatePolicy(), codeBase);
                 Map<String, String> jnlpParams = jnlpFile.getApplet().getParameters();
                 info = jnlpFile.info;
 
@@ -76,8 +88,8 @@
             } catch (MalformedURLException e) {
                 // Don't fail because we cannot get the jnlp file. Parameters are optional not required.
                 // it is the site developer who should ensure that file exist.
-                System.err.println("Unable to get JNLP file at: " + codeBase.toExternalForm()
-                        + atts.get("jnlp_href"));
+                System.err.println("Unable to get JNLP file at: " + atts.get("jnlp_href")
+                        + " with context of URL as: " + codeBase.toExternalForm());
             }
         } else {
             // Should we populate this list with applet attribute tags?
diff -r bdc092f4b4ff -r c2396c2ee991 tests/netx/unit/net/sourceforge/jnlp/PluginBridgeTest.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/netx/unit/net/sourceforge/jnlp/PluginBridgeTest.java	Mon Jun 04 13:35:25 2012 -0400
@@ -0,0 +1,99 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ * This file is part of IcedTea, http://icedtea.classpath.org
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+package net.sourceforge.jnlp;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Hashtable;
+
+import net.sourceforge.jnlp.cache.UpdatePolicy;
+
+import org.junit.Test;
+
+public class PluginBridgeTest {
+    private class MockJNLPCreator extends JNLPCreator {
+
+        private URL JNLPHref;
+
+        public URL getJNLPHref() {
+            return JNLPHref;
+        }
+
+        public JNLPFile create(URL location, Version version, boolean strict,
+                UpdatePolicy policy, URL forceCodebase) throws IOException, ParseException {
+            JNLPHref = location;
+            return new MockJNLPFile();
+        }
+    }
+
+    private class MockJNLPFile extends JNLPFile {
+        public AppletDesc getApplet() {
+            return new AppletDesc(null, null, null, 0, 0, new HashMap<String, String>());
+        }
+
+        public ResourcesDesc getResources() {
+            return new ResourcesDesc(null, null, null, null);
+        }
+    }
+
+    @Test
+    public void testAbsoluteJNLPHref() throws MalformedURLException, Exception {
+        URL codeBase = new URL("http://undesired.absolute.codebase.com");
+        String absoluteLocation = "http://absolute.href.com/test.jnlp";
+        Hashtable<String, String> atts = new Hashtable<String, String>();
+        atts.put("jnlp_href", absoluteLocation);
+        MockJNLPCreator mockCreator = new MockJNLPCreator();
+        PluginBridge pb = new PluginBridge(codeBase, null, "", "", 0, 0, atts, "", mockCreator);
+        assertEquals(absoluteLocation, mockCreator.getJNLPHref().toExternalForm());
+    }
+
+    @Test
+    public void testRelativeJNLPHref() throws MalformedURLException, Exception {
+        URL codeBase = new URL("http://desired.absolute.codebase.com/");
+        String relativeLocation = "sub/dir/test.jnlp";
+        Hashtable<String, String> atts = new Hashtable<String, String>();
+        atts.put("jnlp_href", relativeLocation);
+        MockJNLPCreator mockCreator = new MockJNLPCreator();
+        PluginBridge pb = new PluginBridge(codeBase, null, "", "", 0, 0, atts, "", mockCreator);
+        assertEquals(codeBase.toExternalForm() + relativeLocation,
+                     mockCreator.getJNLPHref().toExternalForm());
+    }
+
+    @Test
+    public void testNoSubDirInCodeBase() throws MalformedURLException, Exception {
+        String desiredDomain = "http://desired.absolute.codebase.com";
+        URL codeBase = new URL(desiredDomain + "/undesired/sub/dir");
+        String relativeLocation = "/app/test/test.jnlp";
+        Hashtable<String, String> atts = new Hashtable<String, String>();
+        atts.put("jnlp_href", relativeLocation);
+        MockJNLPCreator mockCreator = new MockJNLPCreator();
+        PluginBridge pb = new PluginBridge(codeBase, null, "", "", 0, 0, atts, "", mockCreator);
+        assertEquals(desiredDomain + relativeLocation,
+                     mockCreator.getJNLPHref().toExternalForm());
+    }
+
+}
\ No newline at end of file



More information about the distro-pkg-dev mailing list