001: /**
002: * $Id: ContentUtils.java,v 1.2 2004/01/21 20:41:45 sathyakn Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and iPlanet
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.mobile.util;
014:
015: import java.util.*;
016: import com.iplanet.am.util.Debug;
017: import netscape.ldap.util.DN;
018:
019: /**
020: * Content Util Class
021: *
022: * @author John Saare
023: * @author Robert O'Brien
024: * @author Dean Polla
025: * @version 1.0
026: */
027: public class ContentUtils {
028:
029: private static char[] escapeArray = null;
030:
031: //
032: // SubConfig cannot handle "/" in the name. And the resource bundles cannot
033: // handle whitespaces in keys (clientType property).
034: //
035: private static char[] localEscapeArray = { ' ', '\n', '\r', '\t',
036: '/', '=', ':' };
037:
038: static {
039: int len = DN.ESCAPED_CHAR.length + localEscapeArray.length;
040: escapeArray = new char[len];
041:
042: int i = 0;
043: for (i = 0; i < DN.ESCAPED_CHAR.length; i++)
044: escapeArray[i] = DN.ESCAPED_CHAR[i];
045:
046: for (int j = 0; j < localEscapeArray.length; i++, j++)
047: escapeArray[i] = localEscapeArray[j];
048: }
049:
050: /**
051: * Escape HTTP URI fragment per RFC2396
052: *
053: * @param uriFragment
054: * @return escaped String.
055: */
056: public static String rfc2396Escape(String encode, String uriFragment) {
057: int length = uriFragment.length();
058:
059: if (length == 0)
060: return uriFragment;
061:
062: StringBuffer results = new StringBuffer();
063:
064: for (int i = 0; i < length; i++) {
065: char c = uriFragment.charAt(i);
066: switch (c) {
067: case ';':
068: case '/':
069: case '?':
070: case ':':
071: case '@':
072: case '&':
073: case '=':
074: case '+':
075: case '$':
076: case ',':
077: case '%':
078: // Note: Space and double quote are not specified by
079: // RFC2396..., but we need 'em anyway...
080: case ' ':
081: case '"':
082: // TODO: getBytes() should specify encoding???
083: try {
084: byte[] octets;
085: octets = uriFragment.substring(i, i + 1).getBytes();
086: for (int j = 0; j < octets.length; j++) {
087: String hexVal = Integer.toHexString(octets[j]);
088: if (hexVal.length() == 2) {
089: results.append("%" + hexVal);
090: } else {
091: results.append("%0" + hexVal);
092: }
093: }
094: } catch (Exception e) {
095: }
096:
097: break;
098: default:
099: results.append(c);
100: break;
101: }
102: }
103:
104: return results.toString();
105: }
106:
107: /**
108: * "escape" characters in the userAgent that cannot be used for the
109: * clientType - since the clientType is stored as the RDN. replaces them
110: * with "_" (underscores). Used by MAPClientDetector, Transform and
111: * portal desktop
112: */
113: public static String escapeClientType(String userAgent) {
114: int len = userAgent.length();
115: StringBuffer stringBuffer = new StringBuffer(len);
116: for (int i = 0; i < len; i++) {
117: char ch = userAgent.charAt(i);
118: if (isEscape(ch)) {
119: stringBuffer.append('_');
120: } else {
121: stringBuffer.append(ch);
122: }
123: }
124:
125: return (stringBuffer.toString());
126: }
127:
128: private static boolean isEscape(char ch) {
129: for (int i = 0; i < escapeArray.length; i++) {
130: if (ch == escapeArray[i]) {
131: return true;
132: }
133: }
134:
135: return false;
136: }
137: }
|