diff options
-rw-r--r-- | ChangeLog | 20 | ||||
-rw-r--r-- | NEWS | 1 | ||||
-rw-r--r-- | netx/net/sourceforge/jnlp/runtime/ApplicationInstance.java | 10 | ||||
-rw-r--r-- | netx/net/sourceforge/jnlp/util/XDesktopEntry.java | 119 | ||||
-rw-r--r-- | tests/netx/unit/net/sourceforge/jnlp/util/XDesktopEntryTest.java | 188 |
5 files changed, 333 insertions, 5 deletions
@@ -1,5 +1,25 @@ 2013-01-09 Jiri Vanek <[email protected]> + First part of fix of recreating desktop icon + * NEWS: mentioned PR725 + * netx/net/sourceforge/jnlp/runtime/ApplicationInstance.java: + (addMenuAndDesktopEntries)added check for already existing icon + * netx/net/sourceforge/jnlp/util/XDesktopEntry.java: Added methods for + digging the already existing icon from system + (getShortcutTmpFile) tmpfile fo generating the desktop icon + (getDesktopIconName) for getting filename from application title + (findFreedesktopOrgDesktopPathCatch) public method to find final desktop file + (findFreedesktopOrgDesktopPath) to get into ~/.config/user-dirs.dirs + (getFreedesktopOrgDesktopPathFrom) to find XDG_DESKTOP_DIR value + (filterQuotes) to handle simple quotations + (evaluateLinuxVariables) to handle possible variables in XDG_DESKTOP_DIR + value + * tests/netx/unit/net/sourceforge/jnlp/util/XDesktopEntryTest.java: + New tests focused on parsing of desktop location from stream (variables + and quotations) + +2013-01-09 Jiri Vanek <[email protected]> + Logging methods made synchronized * tests/test-extensions/net/sourceforge/jnlp/LoggingBottleneck.java: (processLogs) (getDefaultLoggingBottleneck) (writeXmlLog) (addToXmlLog) @@ -16,6 +16,7 @@ New in release 1.4 (2012-XX-XX): - CVE-2012-3423, RH841345: Incorrect handling of not 0-terminated strings * NetX - PR1027: DownloadService is not supported by IcedTea-Web + - PR725: JNLP applications will prompt for creating desktop shortcuts every time they are run * Plugin - PR1106: Buffer overflow in plugin table- - PR1166: Embedded JNLP File is not supported in applet tag diff --git a/netx/net/sourceforge/jnlp/runtime/ApplicationInstance.java b/netx/net/sourceforge/jnlp/runtime/ApplicationInstance.java index 8de23bf..f74cce6 100644 --- a/netx/net/sourceforge/jnlp/runtime/ApplicationInstance.java +++ b/netx/net/sourceforge/jnlp/runtime/ApplicationInstance.java @@ -17,6 +17,7 @@ package net.sourceforge.jnlp.runtime; import java.awt.Window; +import java.io.File; import java.net.URL; import java.security.AccessControlContext; import java.security.AccessController; @@ -146,7 +147,14 @@ public class ApplicationInstance { private void addMenuAndDesktopEntries() { XDesktopEntry entry = new XDesktopEntry(file); ShortcutDesc sd = file.getInformation().getShortcut(); - + File possibleDesktopFile = entry.getLinuxDesktopIconFile(); + if (possibleDesktopFile.exists()) { + if (JNLPRuntime.isDebug()) { + System.out.println("ApplicationInstance.addMenuAndDesktopEntries(): file - " + + possibleDesktopFile.getAbsolutePath() + " already exists. Not proceeding with desktop additions"); + } + return; + } if (shouldCreateShortcut(sd)) { entry.createDesktopShortcut(); } diff --git a/netx/net/sourceforge/jnlp/util/XDesktopEntry.java b/netx/net/sourceforge/jnlp/util/XDesktopEntry.java index b06222b..05c451e 100644 --- a/netx/net/sourceforge/jnlp/util/XDesktopEntry.java +++ b/netx/net/sourceforge/jnlp/util/XDesktopEntry.java @@ -16,16 +16,25 @@ package net.sourceforge.jnlp.util; +import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.FileReader; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.StringReader; import java.net.URL; import java.nio.charset.Charset; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; import net.sourceforge.jnlp.IconDesc; import net.sourceforge.jnlp.JNLPFile; @@ -78,7 +87,7 @@ public class XDesktopEntry { String fileContents = "[Desktop Entry]\n"; fileContents += "Version=1.0\n"; - fileContents += "Name=" + sanitize(file.getTitle()) + "\n"; + fileContents += "Name=" + getDesktopIconName() + "\n"; fileContents += "GenericName=Java Web Start Application\n"; fileContents += "Comment=" + sanitize(file.getInformation().getDescription()) + "\n"; fileContents += "Type=Application\n"; @@ -122,6 +131,12 @@ public class XDesktopEntry { return iconSize; } + public File getShortcutTmpFile() { + String userTmp = JNLPRuntime.getConfiguration().getProperty(DeploymentConfiguration.KEY_USER_TMP_DIR); + File shortcutFile = new File(userTmp + File.separator + FileUtils.sanitizeFileName(file.getTitle()) + ".desktop"); + return shortcutFile; + } + /** * Set the icon size to use for the desktop shortcut * @@ -148,9 +163,7 @@ public class XDesktopEntry { * Install this XDesktopEntry into the user's desktop as a launcher */ private void installDesktopLauncher() { - File shortcutFile = new File(JNLPRuntime.getConfiguration() - .getProperty(DeploymentConfiguration.KEY_USER_TMP_DIR) - + File.separator + FileUtils.sanitizeFileName(file.getTitle()) + ".desktop"); + File shortcutFile = getShortcutTmpFile(); try { if (!shortcutFile.getParentFile().isDirectory() && !shortcutFile.getParentFile().mkdirs()) { @@ -234,4 +247,102 @@ public class XDesktopEntry { } } + public String getDesktopIconName() { + return sanitize(file.getTitle()); + } + + public File getLinuxDesktopIconFile() { + return new File(findFreedesktopOrgDesktopPathCatch() + "/" + getDesktopIconName() + ".desktop"); + } + + private static String findFreedesktopOrgDesktopPathCatch() { + try { + return findFreedesktopOrgDesktopPath(); + } catch (Exception ex) { + ex.printStackTrace(); + return System.getProperty("user.home") + "/Desktop/"; + } + } + + /** + * Instead of having all this parsing of user-dirs.dirs and replacing + * variables we can execute `echo $(xdg-user-dir DESKTOP)` and it will do + * all the job in case approaches below become failing + * + * @return variables (if declared) and quotation marks (unless escaped) free + * path + * @throws IOException if no file do not exists or key with desktop do not + * exists + */ + private static String findFreedesktopOrgDesktopPath() throws IOException { + File userDirs = new File(System.getProperty("user.home") + "/.config/user-dirs.dirs"); + if (!userDirs.exists()) { + return System.getProperty("user.home") + "/Desktop/"; + } + return getFreedesktopOrgDesktopPathFrom(userDirs); + } + + private static String getFreedesktopOrgDesktopPathFrom(File userDirs) throws IOException { + BufferedReader r = new BufferedReader(new FileReader(userDirs)); + try { + return getFreedesktopOrgDesktopPathFrom(r); + } finally { + r.close(); + } + + } + static final String XDG_DESKTOP_DIR = "XDG_DESKTOP_DIR"; + + static String getFreedesktopOrgDesktopPathFrom(BufferedReader r) throws IOException { + while (true) { + String s = r.readLine(); + if (s == null) { + throw new IOException("End of user-dirs found, but no " + XDG_DESKTOP_DIR + " key found"); + } + s = s.trim(); + if (s.startsWith(XDG_DESKTOP_DIR)) { + if (!s.contains("=")) { + throw new IOException(XDG_DESKTOP_DIR + " has no value"); + } + String[] keyAndValue = s.split("="); + keyAndValue[1] = keyAndValue[1].trim(); + String filteredQuotes = filterQuotes(keyAndValue[1]); + return evaluateLinuxVariables(filteredQuotes); + } + } + } + private static final String MIC = "MAGIC_QUOTIN_ITW_CONSTANT_FOR_DUMMIES"; + + private static String filterQuotes(String string) { + //get rid of " but not of + String s = string.replaceAll("\\\\\"", MIC); + s = s.replaceAll("\"", ""); + s = s.replaceAll(MIC, "\\\""); + return s; + } + + private static String evaluateLinuxVariables(String orig) { + return evaluateLinuxVariables(orig, System.getenv()); + } + + private static String evaluateLinuxVariables(String orig, Map<String, String> variables) { + Set<Entry<String, String>> env = variables.entrySet(); + List<Entry<String, String>> envVariables = new ArrayList<Entry<String, String>>(env); + Collections.sort(envVariables, new Comparator<Entry<String, String>>() { + @Override + public int compare(Entry<String, String> o1, Entry<String, String> o2) { + return o2.getKey().length() - o1.getKey().length(); + } + }); + while (true) { + String before = orig; + for (Entry<String, String> entry : envVariables) { + orig = orig.replaceAll("\\$" + entry.getKey(), entry.getValue()); + } + if (before.equals(orig)) { + return orig; + } + } + + } } diff --git a/tests/netx/unit/net/sourceforge/jnlp/util/XDesktopEntryTest.java b/tests/netx/unit/net/sourceforge/jnlp/util/XDesktopEntryTest.java new file mode 100644 index 0000000..1cd343c --- /dev/null +++ b/tests/netx/unit/net/sourceforge/jnlp/util/XDesktopEntryTest.java @@ -0,0 +1,188 @@ +/* + Copyright (C) 2012 Red Hat, Inc. + + This file is part of IcedTea. + + IcedTea is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as published by + the Free Software Foundation, version 2. + + IcedTea 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 for more details. + + You should have received a copy of the GNU General Public License + along with IcedTea; see the file COPYING. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. + + Linking this library statically or dynamically with other modules is + making a combined work based on this library. Thus, the terms and + conditions of the GNU General Public License cover the whole + combination. + + As a special exception, the copyright holders of this library give you + permission to link this library with independent modules to produce an + executable, regardless of the license terms of these independent + modules, and to copy and distribute the resulting executable under + terms of your choice, provided that you also meet, for each linked + independent module, the terms and conditions of the license of that + module. An independent module is a module which is not derived from + or based on this library. If you modify this library, you may extend + this exception to your version of the library, but you are not + obligated to do so. If you do not wish to do so, delete this + exception statement from your version. + */ +package net.sourceforge.jnlp.util; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.StringReader; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import net.sourceforge.jnlp.ServerAccess; +import net.sourceforge.jnlp.annotations.KnownToFail; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +public class XDesktopEntryTest { + + private static final String des1 = "/my/little/Desktop"; + private static final String des2name = "Plocha"; + private static final String des2Res = System.getProperty("user.home") + "/" + des2name; + private static final String HOME = "HOME"; + private static final String des2 = "$" + HOME + "/" + des2name; + private static final String des7 = "\"$" + HOME + "/" + des2name + "\""; + private static final String des7res = System.getProperty("user.home") + "/" + des2name; + private static final String des8 = "\\\"$" + HOME + "/" + des2name + "\\\""; + private static final String des8res = "\"" + System.getProperty("user.home") + "/" + des2name + "\""; + private static final String des9 = "\"$" + HOME + "/\\\"" + des2name + "\\\"\""; + private static final String des9res = System.getProperty("user.home") + "/\"" + des2name + "\""; + private static final String src1 = XDesktopEntry.XDG_DESKTOP_DIR + "=" + des1; + private static final String src2 = " " + XDesktopEntry.XDG_DESKTOP_DIR + " = " + des1; + private static final String src3 = "#" + XDesktopEntry.XDG_DESKTOP_DIR + " = " + des1; + private static final String src4 = XDesktopEntry.XDG_DESKTOP_DIR + "=" + des2; + private static final String src5 = " " + XDesktopEntry.XDG_DESKTOP_DIR + " = " + des2; + private static final String src6 = "#" + XDesktopEntry.XDG_DESKTOP_DIR + " = " + des2; + private static final String src7 = XDesktopEntry.XDG_DESKTOP_DIR + " = " + des7; + private static final String src8 = XDesktopEntry.XDG_DESKTOP_DIR + " = " + des8; + private static final String src9 = XDesktopEntry.XDG_DESKTOP_DIR + " = " + des9; + private static Map<String, String> backupedEnv; + + @BeforeClass + public static void ensureHomeVaribale() throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException, ClassNotFoundException { + ServerAccess.logOutputReprint("Environment"); + envToString(); + Map<String, String> env = System.getenv(); + if (env.containsKey(HOME)) { + backupedEnv = null; + } else { + backupedEnv = env; + Map m = new HashMap(env); + m.put(HOME, System.getProperty("user.home")); + fakeEnvironment(m); + ServerAccess.logOutputReprint("Hacked environment"); + envToString(); + } + } + + @AfterClass + public static void restoreHomeVaribale() throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException, ClassNotFoundException { + Map<String, String> env = System.getenv(); + if (backupedEnv != null) { + fakeEnvironment(backupedEnv); + ServerAccess.logOutputReprint("Restored environment"); + envToString(); + } + } + + private static void fakeEnvironment(Map m) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException, ClassNotFoundException { + Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment"); + Field env = processEnvironmentClass.getDeclaredField("theUnmodifiableEnvironment"); + env.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(env, env.getModifiers() & ~Modifier.FINAL); + env.set(null, m); + } + + @Test + @KnownToFail + public void testHomeVariable() { + Assert.assertTrue("Variable home must be in environment of this run, is not", System.getenv().containsKey(HOME)); + Assert.assertNull("Variable home should be declared before test run, but was not and so is faked. This should be ok and is thrown just for record. See output of ensureHomeVaribale and restoreHomeVaribale", backupedEnv); + } + + @Test + public void getFreedesktopOrgDesktopPathFromtestSimple() throws IOException { + String s = XDesktopEntry.getFreedesktopOrgDesktopPathFrom(new BufferedReader(new StringReader(src1))); + Assert.assertEquals(s, des1); + } + + @Test + public void getFreedesktopOrgDesktopPathFromtestSpaced() throws IOException { + String s = XDesktopEntry.getFreedesktopOrgDesktopPathFrom(new BufferedReader(new StringReader(src2))); + Assert.assertEquals(s, des1); + } + + @Test(expected = IOException.class) + public void getFreedesktopOrgDesktopPathFromtestCommented() throws IOException { + String s = XDesktopEntry.getFreedesktopOrgDesktopPathFrom(new BufferedReader(new StringReader(src3))); + } + + @Test + public void getFreedesktopOrgDesktopPathFromtestSimpleWithHome() throws IOException { + String s = XDesktopEntry.getFreedesktopOrgDesktopPathFrom(new BufferedReader(new StringReader(src4))); + Assert.assertEquals(s, des2Res); + } + + @Test + public void getFreedesktopOrgDesktopPathFromtestSpacedWithHome() throws IOException { + String s = XDesktopEntry.getFreedesktopOrgDesktopPathFrom(new BufferedReader(new StringReader(src5))); + Assert.assertEquals(s, des2Res); + } + + @Test + public void getFreedesktopOrgDesktopPathFromtestSpacedWithHomeAndQuotes() throws IOException { + String s = XDesktopEntry.getFreedesktopOrgDesktopPathFrom(new BufferedReader(new StringReader(src7))); + Assert.assertEquals(s, des7res); + } + + @Test + public void getFreedesktopOrgDesktopPathFromtestSpacedWithHomeAndEscapedQuotes() throws IOException { + String s = XDesktopEntry.getFreedesktopOrgDesktopPathFrom(new BufferedReader(new StringReader(src8))); + Assert.assertEquals(s, des8res); + } + @Test + public void getFreedesktopOrgDesktopPathFromtestSpacedWithHomeAndMixedQuotes() throws IOException { + String s = XDesktopEntry.getFreedesktopOrgDesktopPathFrom(new BufferedReader(new StringReader(src9))); + Assert.assertEquals(s, des9res); + } + + @Test(expected = IOException.class) + public void getFreedesktopOrgDesktopPathFromtestCommentedWithHome() throws IOException { + String s = XDesktopEntry.getFreedesktopOrgDesktopPathFrom(new BufferedReader(new StringReader(src6))); + } + + private static void envToString() { + mapToString(System.getenv()); + } + + private static void mapToString(Map<String, String> variables) { + Set<Map.Entry<String, String>> env = variables.entrySet(); + for (Map.Entry<String, String> entry : env) { + ServerAccess.logOutputReprint(entry.getKey() + " = " + entry.getValue()); + } + } +}
\ No newline at end of file |