Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / server / webdav / URLEncoder.java @ fc19c028

History | View | Annotate | Download (3 kB)

1 14ad7326 pastith
/*
2 14ad7326 pastith
 * Licensed to the Apache Software Foundation (ASF) under one or more
3 14ad7326 pastith
 * contributor license agreements.  See the NOTICE file distributed with
4 14ad7326 pastith
 * this work for additional information regarding copyright ownership.
5 14ad7326 pastith
 * The ASF licenses this file to You under the Apache License, Version 2.0
6 14ad7326 pastith
 * (the "License"); you may not use this file except in compliance with
7 14ad7326 pastith
 * the License.  You may obtain a copy of the License at
8 14ad7326 pastith
 *
9 14ad7326 pastith
 *      http://www.apache.org/licenses/LICENSE-2.0
10 14ad7326 pastith
 *
11 14ad7326 pastith
 * Unless required by applicable law or agreed to in writing, software
12 14ad7326 pastith
 * distributed under the License is distributed on an "AS IS" BASIS,
13 14ad7326 pastith
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 14ad7326 pastith
 * See the License for the specific language governing permissions and
15 14ad7326 pastith
 * limitations under the License.
16 14ad7326 pastith
 */
17 14ad7326 pastith
package gr.ebs.gss.server.webdav;
18 14ad7326 pastith
19 14ad7326 pastith
import java.io.ByteArrayOutputStream;
20 14ad7326 pastith
import java.io.IOException;
21 14ad7326 pastith
import java.io.OutputStreamWriter;
22 14ad7326 pastith
import java.util.BitSet;
23 14ad7326 pastith
24 14ad7326 pastith
/**
25 14ad7326 pastith
 *
26 14ad7326 pastith
 * This class is very similar to the java.net.URLEncoder class.
27 14ad7326 pastith
 *
28 14ad7326 pastith
 * Unfortunately, with java.net.URLEncoder there is no way to specify to the
29 14ad7326 pastith
 * java.net.URLEncoder which characters should NOT be encoded.
30 14ad7326 pastith
 *
31 14ad7326 pastith
 * This code was moved from DefaultServlet.java
32 14ad7326 pastith
 *
33 14ad7326 pastith
 * @author Craig R. McClanahan
34 14ad7326 pastith
 * @author Remy Maucherat
35 14ad7326 pastith
 */
36 14ad7326 pastith
public class URLEncoder {
37 14ad7326 pastith
        /**
38 14ad7326 pastith
         *
39 14ad7326 pastith
         */
40 14ad7326 pastith
        protected static final char[] hexadecimal =
41 14ad7326 pastith
        {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
42 14ad7326 pastith
                'A', 'B', 'C', 'D', 'E', 'F'};
43 14ad7326 pastith
44 14ad7326 pastith
        /**
45 14ad7326 pastith
         * Array containing the safe characters set.
46 14ad7326 pastith
         */
47 14ad7326 pastith
        protected BitSet safeCharacters = new BitSet(256);
48 14ad7326 pastith
49 14ad7326 pastith
        /**
50 14ad7326 pastith
         *
51 14ad7326 pastith
         */
52 14ad7326 pastith
        public URLEncoder() {
53 14ad7326 pastith
                for (char i = 'a'; i <= 'z'; i++)
54 14ad7326 pastith
                        addSafeCharacter(i);
55 14ad7326 pastith
                for (char i = 'A'; i <= 'Z'; i++)
56 14ad7326 pastith
                        addSafeCharacter(i);
57 14ad7326 pastith
                for (char i = '0'; i <= '9'; i++)
58 14ad7326 pastith
                        addSafeCharacter(i);
59 14ad7326 pastith
        }
60 14ad7326 pastith
61 14ad7326 pastith
        /**
62 14ad7326 pastith
         * @param c
63 14ad7326 pastith
         */
64 14ad7326 pastith
        public void addSafeCharacter( char c ) {
65 14ad7326 pastith
                safeCharacters.set( c );
66 14ad7326 pastith
        }
67 14ad7326 pastith
68 14ad7326 pastith
        /**
69 14ad7326 pastith
         * @param path
70 14ad7326 pastith
         * @return the encoded string
71 14ad7326 pastith
         */
72 14ad7326 pastith
        public String encode( String path ) {
73 14ad7326 pastith
                int maxBytesPerChar = 10;
74 14ad7326 pastith
                StringBuffer rewrittenPath = new StringBuffer(path.length());
75 14ad7326 pastith
                ByteArrayOutputStream buf = new ByteArrayOutputStream(maxBytesPerChar);
76 14ad7326 pastith
                OutputStreamWriter writer = null;
77 14ad7326 pastith
                try {
78 14ad7326 pastith
                        writer = new OutputStreamWriter(buf, "UTF8");
79 14ad7326 pastith
                } catch (Exception e) {
80 14ad7326 pastith
                        e.printStackTrace();
81 14ad7326 pastith
                        writer = new OutputStreamWriter(buf);
82 14ad7326 pastith
                }
83 14ad7326 pastith
84 14ad7326 pastith
                for (int i = 0; i < path.length(); i++) {
85 14ad7326 pastith
                        int c = path.charAt(i);
86 14ad7326 pastith
                        if (safeCharacters.get(c))
87 14ad7326 pastith
                                rewrittenPath.append((char)c);
88 14ad7326 pastith
                        else {
89 14ad7326 pastith
                                // convert to external encoding before hex conversion
90 14ad7326 pastith
                                try {
91 14ad7326 pastith
                                        writer.write((char)c);
92 14ad7326 pastith
                                        writer.flush();
93 14ad7326 pastith
                                } catch(IOException e) {
94 14ad7326 pastith
                                        buf.reset();
95 14ad7326 pastith
                                        continue;
96 14ad7326 pastith
                                }
97 14ad7326 pastith
                                byte[] ba = buf.toByteArray();
98 14ad7326 pastith
                                for (int j = 0; j < ba.length; j++) {
99 14ad7326 pastith
                                        // Converting each byte in the buffer
100 14ad7326 pastith
                                        byte toEncode = ba[j];
101 14ad7326 pastith
                                        rewrittenPath.append('%');
102 14ad7326 pastith
                                        int low = toEncode & 0x0f;
103 14ad7326 pastith
                                        int high = (toEncode & 0xf0) >> 4;
104 14ad7326 pastith
                                rewrittenPath.append(hexadecimal[high]);
105 14ad7326 pastith
                                rewrittenPath.append(hexadecimal[low]);
106 14ad7326 pastith
                                }
107 14ad7326 pastith
                                buf.reset();
108 14ad7326 pastith
                        }
109 14ad7326 pastith
                }
110 14ad7326 pastith
                return rewrittenPath.toString();
111 14ad7326 pastith
        }
112 14ad7326 pastith
}