diff options
Diffstat (limited to 'netx/net/sourceforge/jnlp/util/UrlUtils.java')
-rw-r--r-- | netx/net/sourceforge/jnlp/util/UrlUtils.java | 75 |
1 files changed, 71 insertions, 4 deletions
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); + } + } |