001: /*
002: * URLEncoder.java
003: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/URLEncoder.java,v 1.1 2002/05/11 05:06:25 billbarker Exp $
004: * $Revision: 1.1 $
005: * $Date: 2002/05/11 05:06:25 $
006: *
007: * ====================================================================
008: *
009: * The Apache Software License, Version 1.1
010: *
011: * Copyright (c) 1999 The Apache Software Foundation. All rights
012: * reserved.
013: *
014: * Redistribution and use in source and binary forms, with or without
015: * modification, are permitted provided that the following conditions
016: * are met:
017: *
018: * 1. Redistributions of source code must retain the above copyright
019: * notice, this list of conditions and the following disclaimer.
020: *
021: * 2. Redistributions in binary form must reproduce the above copyright
022: * notice, this list of conditions and the following disclaimer in
023: * the documentation and/or other materials provided with the
024: * distribution.
025: *
026: * 3. The end-user documentation included with the redistribution, if
027: * any, must include the following acknowlegement:
028: * "This product includes software developed by the
029: * Apache Software Foundation (http://www.apache.org/)."
030: * Alternately, this acknowlegement may appear in the software itself,
031: * if and wherever such third-party acknowlegements normally appear.
032: *
033: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
034: * Foundation" must not be used to endorse or promote products derived
035: * from this software without prior written permission. For written
036: * permission, please contact apache@apache.org.
037: *
038: * 5. Products derived from this software may not be called "Apache"
039: * nor may "Apache" appear in their names without prior written
040: * permission of the Apache Group.
041: *
042: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
043: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
044: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
045: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
046: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
047: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
048: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
049: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
050: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
051: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
052: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
053: * SUCH DAMAGE.
054: * ====================================================================
055: *
056: * This software consists of voluntary contributions made by many
057: * individuals on behalf of the Apache Software Foundation. For more
058: * information on the Apache Software Foundation, please see
059: * <http://www.apache.org/>.
060: *
061: * [Additional notices, if required by prior licensing conditions]
062: *
063: */
064: package org.apache.catalina.util;
065:
066: import java.io.ByteArrayOutputStream;
067: import java.io.IOException;
068: import java.io.OutputStreamWriter;
069: import java.util.BitSet;
070:
071: /**
072: *
073: * This class is very similar to the java.net.URLEncoder class.
074: *
075: * Unfortunately, with java.net.URLEncoder there is no way to specify to the
076: * java.net.URLEncoder which characters should NOT be encoded.
077: *
078: * This code was moved from DefaultServlet.java
079: *
080: * @author Craig R. McClanahan
081: * @author Remy Maucherat
082: */
083: public class URLEncoder {
084: protected static final char[] hexadecimal = { '0', '1', '2', '3',
085: '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
086:
087: //Array containing the safe characters set.
088: protected BitSet safeCharacters = new BitSet(256);
089:
090: public URLEncoder() {
091: for (char i = 'a'; i <= 'z'; i++) {
092: addSafeCharacter(i);
093: }
094: for (char i = 'A'; i <= 'Z'; i++) {
095: addSafeCharacter(i);
096: }
097: for (char i = '0'; i <= '9'; i++) {
098: addSafeCharacter(i);
099: }
100: }
101:
102: public void addSafeCharacter(char c) {
103: safeCharacters.set(c);
104: }
105:
106: public String encode(String path) {
107: int maxBytesPerChar = 10;
108: int caseDiff = ('a' - 'A');
109: StringBuffer rewrittenPath = new StringBuffer(path.length());
110: ByteArrayOutputStream buf = new ByteArrayOutputStream(
111: maxBytesPerChar);
112: OutputStreamWriter writer = null;
113: try {
114: writer = new OutputStreamWriter(buf, "UTF8");
115: } catch (Exception e) {
116: e.printStackTrace();
117: writer = new OutputStreamWriter(buf);
118: }
119:
120: for (int i = 0; i < path.length(); i++) {
121: int c = (int) path.charAt(i);
122: if (safeCharacters.get(c)) {
123: rewrittenPath.append((char) c);
124: } else {
125: // convert to external encoding before hex conversion
126: try {
127: writer.write(c);
128: writer.flush();
129: } catch (IOException e) {
130: buf.reset();
131: continue;
132: }
133: byte[] ba = buf.toByteArray();
134: for (int j = 0; j < ba.length; j++) {
135: // Converting each byte in the buffer
136: byte toEncode = ba[j];
137: rewrittenPath.append('%');
138: int low = (int) (toEncode & 0x0f);
139: int high = (int) ((toEncode & 0xf0) >> 4);
140: rewrittenPath.append(hexadecimal[high]);
141: rewrittenPath.append(hexadecimal[low]);
142: }
143: buf.reset();
144: }
145: }
146: return rewrittenPath.toString();
147: }
148: }
|