001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.commons.net;
020
021import java.io.UnsupportedEncodingException;
022import java.nio.charset.Charset;
023
024import lucee.commons.lang.StringUtil;
025
026/**
027 * Utility class for HTML form encoding. This class contains static methods
028 * for converting a String to the <CODE>application/x-www-form-urlencoded</CODE> MIME
029 * format. For more information about HTML form encoding, consult the HTML 
030 * <A HREF="http://www.w3.org/TR/html4/">specification</A>. 
031 *
032 * <p>
033 * When encoding a String, the following rules apply:
034 *
035 * <p>
036 * <ul>
037 * <li>The alphanumeric characters &quot;<code>a</code>&quot; through
038 *     &quot;<code>z</code>&quot;, &quot;<code>A</code>&quot; through
039 *     &quot;<code>Z</code>&quot; and &quot;<code>0</code>&quot; 
040 *     through &quot;<code>9</code>&quot; remain the same.
041 * <li>The special characters &quot;<code>.</code>&quot;,
042 *     &quot;<code>-</code>&quot;, &quot;<code>*</code>&quot;, and
043 *     &quot;<code>_</code>&quot; remain the same. 
044 * <li>The space character &quot;<code>&nbsp;</code>&quot; is
045 *     converted into a plus sign &quot;<code>+</code>&quot;.
046 * <li>All other characters are unsafe and are first converted into
047 *     one or more bytes using some encoding scheme. Then each byte is
048 *     represented by the 3-character string
049 *     &quot;<code>%<i>xy</i></code>&quot;, where <i>xy</i> is the
050 *     two-digit hexadecimal representation of the byte. 
051 *     The recommended encoding scheme to use is UTF-8. However, 
052 *     for compatibility reasons, if an encoding is not specified, 
053 *     then the default encoding of the platform is used.
054 * </ul>
055 *
056 * <p>
057 * For example using UTF-8 as the encoding scheme the string &quot;The
058 * string &#252;@foo-bar&quot; would get converted to
059 * &quot;The+string+%C3%BC%40foo-bar&quot; because in UTF-8 the character
060 * &#252; is encoded as two bytes C3 (hex) and BC (hex), and the
061 * character @ is encoded as one byte 40 (hex).
062 *
063 * @author  Herb Jellinek
064 * @version 1.25, 12/03/01
065 * @since   JDK1.0
066 */
067public class URLEncoder {
068    
069    /**
070     * You can't call the constructor.
071     */
072    private URLEncoder() { }
073
074    /**
075     * Translates a string into <code>x-www-form-urlencoded</code>
076     * format. This method uses the platform's default encoding
077     * as the encoding scheme to obtain the bytes for unsafe characters.
078     *
079     * @param   s   <code>String</code> to be translated.
080     * @deprecated The resulting string may vary depending on the platform's
081     *             default encoding. Instead, use the encode(String,String)
082     *             method to specify the encoding.
083     * @return  the translated <code>String</code>.
084     */
085    public static String encode(String s) {
086        s= java.net.URLEncoder.encode(s);
087        if(s.indexOf('+')!=-1)s=StringUtil.replace(s, "+", "%20",false);
088        return s;
089    }
090
091    /**
092     * @deprecated use instead <code>encode(String s, Charset cs)</code>
093     * @param s
094     * @param enc
095     * @return
096     * @throws UnsupportedEncodingException
097     */
098    public static String encode(String s, String enc) throws UnsupportedEncodingException {
099        s= java.net.URLEncoder.encode(s, enc);
100        if(s.indexOf('+')!=-1)s=StringUtil.replace(s, "+", "%20",false);
101        return s;
102    }
103    
104    public static String encode(String s, Charset cs) throws UnsupportedEncodingException {
105        s= java.net.URLEncoder.encode(s, cs.name());
106        if(s.indexOf('+')!=-1)s=StringUtil.replace(s, "+", "%20",false);
107        return s;
108    }
109}