01: package com.sun.ssoadapter.config;
02:
03: import java.util.*;
04:
05: /**
06: * Configuration Utility class responsible for providing a method to
07: * perform limited character escaping functionality. This method is used
08: * instead of the URLEncoder.encode() method. Original algorithm borrowed
09: * from ContentUtils.
10: *
11: */
12: public class ConfigUtil {
13:
14: /**
15: * This method performs limited character escaping. Characters
16: * are translated to octet representation.
17: *
18: * @param uriFragment
19: * @return escaped String.
20: */
21: public static String encode(String uriFragment) {
22: int length = uriFragment.length();
23:
24: if (length == 0) {
25: return uriFragment;
26: }
27:
28: StringBuffer results = new StringBuffer();
29:
30: for (int i = 0; i < length; i++) {
31: char c = uriFragment.charAt(i);
32: switch (c) {
33: case '&':
34: case '|':
35: case ':':
36: case '@':
37: case '/':
38: case ' ':
39: try {
40: byte[] octets;
41: octets = uriFragment.substring(i, i + 1).getBytes();
42: for (int j = 0; j < octets.length; j++) {
43: String hexVal = Integer.toHexString(octets[j]);
44: if (hexVal.length() == 2) {
45: results.append("%" + hexVal);
46: } else {
47: results.append("%0" + hexVal);
48: }
49: }
50: } catch (Exception e) {
51: }
52:
53: break;
54: default:
55: results.append(c);
56: break;
57: }
58: }
59:
60: return results.toString();
61: }
62:
63: }
|