1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/net/fortuna/ical4j/util/Uris.java Tue Feb 10 18:12:00 2015 +0100 1.3 @@ -0,0 +1,144 @@ 1.4 +/** 1.5 + * Copyright (c) 2012, Ben Fortuna 1.6 + * All rights reserved. 1.7 + * 1.8 + * Redistribution and use in source and binary forms, with or without 1.9 + * modification, are permitted provided that the following conditions 1.10 + * are met: 1.11 + * 1.12 + * o Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 1.15 + * o Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 1.19 + * o Neither the name of Ben Fortuna nor the names of any other contributors 1.20 + * may be used to endorse or promote products derived from this software 1.21 + * without specific prior written permission. 1.22 + * 1.23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.24 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.25 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.26 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 1.27 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.28 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.29 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.30 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.31 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.32 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.33 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.34 + */ 1.35 +package net.fortuna.ical4j.util; 1.36 + 1.37 +import java.net.URI; 1.38 +import java.net.URISyntaxException; 1.39 +import java.util.regex.Pattern; 1.40 + 1.41 +/** 1.42 + * $Id$ 1.43 + * 1.44 + * Created on 11/09/2005 1.45 + * 1.46 + * Utility methods for working with URIs. 1.47 + * @author Ben Fortuna 1.48 + */ 1.49 +public final class Uris { 1.50 + 1.51 + /** 1.52 + * URI Scheme used when relaxed parsing is enabled and the given input stream lead to an invalid URI. 1.53 + */ 1.54 + public static final String INVALID_SCHEME = "net.fortunal.ical4j.invalid"; 1.55 + 1.56 + private static final Pattern CID_PATTERN = Pattern.compile("(?i)^cid:.*"); 1.57 + private static final Pattern NOTES_CID_REPLACEMENT_PATTERN = Pattern.compile("[<>]"); 1.58 + 1.59 + /** 1.60 + * Constructor made private to enforce static nature. 1.61 + */ 1.62 + private Uris() { 1.63 + } 1.64 + 1.65 + /** 1.66 + * Encodes the specified URI string using the UTF-8 charset. In the event that an exception is thrown, the specifed 1.67 + * URI string is returned unmodified. 1.68 + * @param s a URI string 1.69 + * @return an encoded URI string 1.70 + */ 1.71 + public static String encode(final String s) { 1.72 + /* 1.73 + * try { return URLEncoder.encode(s, ENCODING_CHARSET); } catch (UnsupportedEncodingException use) { 1.74 + * log.error("Error ocurred encoding URI [" + s + "]", use); } 1.75 + */ 1.76 + 1.77 + /* 1.78 + * Lotus Notes does not correctly strip angle brackets from cid uris. From RFC2392: A "cid" URL is converted to 1.79 + * the corresponding Content-ID message header [MIME] by removing the "cid:" prefix, converting the % encoded 1.80 + * character to their equivalent US-ASCII characters, and enclosing the remaining parts with an angle bracket 1.81 + * pair, "<" and ">". For example, "cid:foo4%25foo1@bar.net" corresponds to Content-ID: <foo4%25foo1@bar.net> 1.82 + * Reversing the process and converting URL special characters to their % encodings produces the original cid. A 1.83 + * "mid" URL is converted to a Message-ID or Message-ID/Content-ID pair in a similar fashion. 1.84 + */ 1.85 + if (CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_NOTES_COMPATIBILITY) 1.86 + && CID_PATTERN.matcher(s).matches()) { 1.87 + 1.88 + return NOTES_CID_REPLACEMENT_PATTERN.matcher(s).replaceAll(""); 1.89 + } 1.90 + return s; 1.91 + } 1.92 + 1.93 + /** 1.94 + * Decodes the specified URI string using the UTF-8 charset. In the event that an exception is thrown, the specifed 1.95 + * URI string is returned unmodified. 1.96 + * @param s a URI string 1.97 + * @return an encoded URI string 1.98 + */ 1.99 + public static String decode(final String s) { 1.100 + /* 1.101 + * try { return URLDecoder.decode(s, ENCODING_CHARSET); } catch (UnsupportedEncodingException use) { 1.102 + * log.error("Error ocurred decoding URI [" + s + "]", use); } 1.103 + */ 1.104 + return s; 1.105 + } 1.106 + 1.107 + /** 1.108 + * Attempts to create a URI instance and will optionally swallow any resulting URISyntaxException depending on 1.109 + * configured {@link CompatibilityHints}. Will also automatically attempt encoding of the string representation for 1.110 + * greater compatibility. 1.111 + * <p>When relaxed parsing is enabled and if the string representation is not valid, a second URI creation attempt is made 1.112 + * by extracting the scheme from the scheme specific part and URI encoding that later part. For example, 1.113 + * "mailto: joe smith@example.com" becomes "mailto:joe%20smith@example.com".<p> 1.114 + * <p>If the second attempts also leads to a {@code URISyntaxException}, an opaque URI is constructed with a scheme 1.115 + * of {@code Uris.INVALID_SCHEME} and a value corresponding to the initial representation.</p> 1.116 + * 1.117 + * @param s a string representation of a URI. 1.118 + * @return a URI instance, which may not correspond to the URI string if a valid 1.119 + * URI string is not specified and relaxed parsing is enabled. 1.120 + * @throws URISyntaxException if a valid URI string is not specified and relaxed parsing is disabled 1.121 + */ 1.122 + public static URI create(final String s) throws URISyntaxException { 1.123 + try { 1.124 + return new URI(encode(s)); 1.125 + } 1.126 + catch (URISyntaxException use) { 1.127 + if (CompatibilityHints 1.128 + .isHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING)) { 1.129 + String encoded = encode(s); 1.130 + int index = encoded.indexOf(':'); 1.131 + if (index != -1 && index < encoded.length() -1) { 1.132 + try { 1.133 + return new URI(encoded.substring(0, index), encoded.substring(index + 1), null); 1.134 + } catch (URISyntaxException use2) { 1.135 + } 1.136 + } 1.137 + try { 1.138 + return new URI(INVALID_SCHEME, s, null); 1.139 + } catch (URISyntaxException use2) { 1.140 + // should not happen as we are building an opaque URI 1.141 + throw new IllegalArgumentException("Could not build URI from " + s); 1.142 + } 1.143 + } 1.144 + throw use; 1.145 + } 1.146 + } 1.147 +}