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