diff options
author | Adam Domurad <[email protected]> | 2013-04-23 13:55:23 -0400 |
---|---|---|
committer | Adam Domurad <[email protected]> | 2013-04-23 13:55:23 -0400 |
commit | 75392ff66c30d88fa173a2b38a94ab9f3ddaf39d (patch) | |
tree | dac20cace8d5408a1e57a6594b7bb7be8e2c856d /netx/net/sourceforge/jnlp | |
parent | 29e4dba60a7ee935ee93e6cb1338554af2bf88ed (diff) |
Introduce more UrlUtils functions.
Diffstat (limited to 'netx/net/sourceforge/jnlp')
4 files changed, 98 insertions, 52 deletions
diff --git a/netx/net/sourceforge/jnlp/cache/CacheUtil.java b/netx/net/sourceforge/jnlp/cache/CacheUtil.java index f2988f5..5e27b84 100644 --- a/netx/net/sourceforge/jnlp/cache/CacheUtil.java +++ b/netx/net/sourceforge/jnlp/cache/CacheUtil.java @@ -18,25 +18,38 @@ package net.sourceforge.jnlp.cache; import static net.sourceforge.jnlp.runtime.Translator.R; -import java.io.*; -import java.net.*; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FilePermission; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; import java.nio.channels.OverlappingFileLockException; +import java.security.Permission; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map.Entry; import java.util.Set; -import java.security.*; -import javax.jnlp.*; -import net.sourceforge.jnlp.*; +import javax.jnlp.DownloadServiceListener; + +import net.sourceforge.jnlp.Version; import net.sourceforge.jnlp.config.DeploymentConfiguration; -import net.sourceforge.jnlp.runtime.*; +import net.sourceforge.jnlp.runtime.ApplicationInstance; +import net.sourceforge.jnlp.runtime.JNLPRuntime; import net.sourceforge.jnlp.util.FileUtils; import net.sourceforge.jnlp.util.PropertiesFile; +import net.sourceforge.jnlp.util.UrlUtils; /** * Provides static methods to interact with the cache, download @@ -72,8 +85,8 @@ public class CacheUtil { return true; } try { - URL nu1 = ResourceTracker.normalizeUrl(u1, false); - URL nu2 = ResourceTracker.normalizeUrl(u2, false); + URL nu1 = UrlUtils.normalizeUrl(u1); + URL nu2 = UrlUtils.normalizeUrl(u2); if (notNullUrlEquals(nu1, nu2)) { return true; } diff --git a/netx/net/sourceforge/jnlp/cache/ResourceTracker.java b/netx/net/sourceforge/jnlp/cache/ResourceTracker.java index 36ef3c6..dffbb3b 100644 --- a/netx/net/sourceforge/jnlp/cache/ResourceTracker.java +++ b/netx/net/sourceforge/jnlp/cache/ResourceTracker.java @@ -50,6 +50,7 @@ import net.sourceforge.jnlp.event.DownloadEvent; import net.sourceforge.jnlp.event.DownloadListener; import net.sourceforge.jnlp.runtime.JNLPRuntime; import net.sourceforge.jnlp.util.StreamUtils; +import net.sourceforge.jnlp.util.UrlUtils; import net.sourceforge.jnlp.util.WeakList; /** @@ -115,15 +116,6 @@ public class ResourceTracker { private static final int ERROR = Resource.ERROR; private static final int STARTED = Resource.STARTED; - // normalization of url - private static final char PATH_DELIMITER_MARK = '/'; - private static final String PATH_DELIMITER = "" + PATH_DELIMITER_MARK; - private static final char QUERY_DELIMITER_MARK = '&'; - private static final String QUERY_DELIMITER = "" + QUERY_DELIMITER_MARK; - private static final char QUERY_MARK = '?'; - private static final char HREF_MARK = '#'; - private static final String UTF8 = "utf-8"; - /** max threads */ private static final int maxThreads = 5; @@ -190,7 +182,7 @@ public class ResourceTracker { if (location == null) throw new IllegalResourceDescriptorException("location==null"); try { - location = normalizeUrl(location, JNLPRuntime.isDebug()); + location = UrlUtils.normalizeUrl(location); } catch (Exception ex) { System.err.println("Normalization of " + location.toString() + " have failed"); ex.printStackTrace(); @@ -1195,30 +1187,4 @@ public class ResourceTracker { // selectNextResource(); } }; - - public static URL normalizeUrl(URL u, boolean debug) throws MalformedURLException, UnsupportedEncodingException, URISyntaxException { - if (u == null) { - return null; - } - String protocol = u.getProtocol(); - - if (protocol == null || "file".equals(protocol)) { - return u; - } - - if (u.getPath() == null) { - return u; - } - - //Decode the URL before encoding - URL decodedURL = new URL(URLDecoder.decode(u.toString(), UTF8)); - - //Create URI with the decoded URL - URI uri = new URI(decodedURL.getProtocol(), null, decodedURL.getHost(), decodedURL.getPort(), decodedURL.getPath(), decodedURL.getQuery(), null); - - //Returns the encoded URL - URL encodedURL = new URL(uri.toASCIIString()); - - return encodedURL; - } } diff --git a/netx/net/sourceforge/jnlp/security/appletextendedsecurity/UnsignedAppletTrustConfirmation.java b/netx/net/sourceforge/jnlp/security/appletextendedsecurity/UnsignedAppletTrustConfirmation.java index afe693f..d90a7c3 100644 --- a/netx/net/sourceforge/jnlp/security/appletextendedsecurity/UnsignedAppletTrustConfirmation.java +++ b/netx/net/sourceforge/jnlp/security/appletextendedsecurity/UnsignedAppletTrustConfirmation.java @@ -99,8 +99,8 @@ public class UnsignedAppletTrustConfirmation { private static UnsignedAppletActionEntry getMatchingItem(UnsignedAppletActionStorage actionStorage, PluginBridge file) { return actionStorage.getMatchingItem( - UrlUtils.normalizeUrlAndStripParams(file.getSourceLocation()).toString(), - UrlUtils.normalizeUrlAndStripParams(file.getCodeBase()).toString(), + UrlUtils.normalizeUrlAndStripParams(file.getSourceLocation(), true /* encode local files */).toString(), + UrlUtils.normalizeUrlAndStripParams(file.getCodeBase(), true /* encode local files */).toString(), toRelativePaths(file.getArchiveJars(), file.getCodeBase().toString())); } @@ -132,8 +132,8 @@ public class UnsignedAppletTrustConfirmation { return; } - URL codebase = UrlUtils.normalizeUrlAndStripParams(file.getCodeBase()); - URL documentbase = UrlUtils.normalizeUrlAndStripParams(file.getSourceLocation()); + URL codebase = UrlUtils.normalizeUrlAndStripParams(file.getCodeBase(), true /* encode local files */); + URL documentbase = UrlUtils.normalizeUrlAndStripParams(file.getSourceLocation(), true /* encode local files */); /* Else, create a new entry */ UrlRegEx codebaseRegex = new UrlRegEx("\\Q" + codebase + "\\E"); diff --git a/netx/net/sourceforge/jnlp/util/UrlUtils.java b/netx/net/sourceforge/jnlp/util/UrlUtils.java index a043bb0..d9d6866 100644 --- a/netx/net/sourceforge/jnlp/util/UrlUtils.java +++ b/netx/net/sourceforge/jnlp/util/UrlUtils.java @@ -38,18 +38,21 @@ exception statement from your version. package net.sourceforge.jnlp.util; import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; +import java.net.URI; import java.net.URISyntaxException; import java.net.URL; - -import net.sourceforge.jnlp.cache.ResourceTracker; +import java.net.URLDecoder; public class UrlUtils { + private static final String UTF8 = "utf-8"; - public static URL normalizeUrlAndStripParams(URL url) { + public static URL normalizeUrlAndStripParams(URL url, boolean encodeFileUrls) { try { String[] urlParts = url.toString().split("\\?"); URL strippedUrl = new URL(urlParts[0]); - return ResourceTracker.normalizeUrl(strippedUrl, false); + return normalizeUrl(strippedUrl, encodeFileUrls); } catch (IOException e) { e.printStackTrace(); } catch (URISyntaxException e) { @@ -58,6 +61,10 @@ public class UrlUtils { return url; } + public static URL normalizeUrlAndStripParams(URL url) { + return normalizeUrlAndStripParams(url, false); + } + public static boolean isLocalFile(URL url) { if (url.getProtocol().equals("file") && @@ -67,4 +74,64 @@ public class UrlUtils { } return false; } + + /* Decode a percent-encoded URL. Catch checked exceptions and log. */ + public static URL decodeUrlQuietly(URL url) { + try { + return new URL(URLDecoder.decode(url.toString(), UTF8)); + } catch (IOException e) { + e.printStackTrace(); + return url; + } + } + + /* Ensure a URL is properly percent-encoded. + * Certain usages require local-file URLs to be encoded, eg for code-base & document-base. */ + public static URL normalizeUrl(URL url, boolean encodeFileUrls) throws MalformedURLException, UnsupportedEncodingException, URISyntaxException { + if (url == null) { + return null; + } + String protocol = url.getProtocol(); + boolean shouldEncode = (encodeFileUrls || !"file".equals(protocol)); + + if (protocol == null || !shouldEncode || url.getPath() == null) { + return url; + } + + //Decode the URL before encoding + URL decodedURL = new URL(URLDecoder.decode(url.toString(), UTF8)); + + //Create URI with the decoded URL + URI uri = new URI(decodedURL.getProtocol(), null, decodedURL.getHost(), decodedURL.getPort(), decodedURL.getPath(), decodedURL.getQuery(), null); + + //Returns the encoded URL + URL encodedURL = new URL(uri.toASCIIString()); + + return encodedURL; + } + + /* Ensure a URL is properly percent-encoded. Does not encode local-file URLs. */ + public static URL normalizeUrl(URL url) throws MalformedURLException, UnsupportedEncodingException, URISyntaxException { + return normalizeUrl(url, false); + } + + /* Ensure a URL is properly percent-encoded. Catch checked exceptions and log. */ + public static URL normalizeUrlQuietly(URL url, boolean encodeFileUrls) { + try { + return normalizeUrl(url, encodeFileUrls); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + return url; + } + + /* Ensure a URL is properly percent-encoded. Catch checked exceptions and log. */ + public static URL normalizeUrlQuietly(URL url) { + return normalizeUrlQuietly(url, false); + } + } |