001: /*
002: * $Id: ResponseUtils.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.util;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025:
026: import java.lang.reflect.InvocationTargetException;
027: import java.lang.reflect.Method;
028:
029: import java.net.URLEncoder;
030:
031: /**
032: * General purpose utility methods related to generating a servlet response in
033: * the Struts controller framework.
034: *
035: * @version $Rev: 471754 $ $Date: 2005-08-21 14:46:28 -0400 (Sun, 21 Aug 2005)
036: * $
037: */
038: public class ResponseUtils {
039: // ------------------------------------------------------- Static Variables
040:
041: /**
042: * The message resources for this package.
043: */
044: protected static MessageResources messages = MessageResources
045: .getMessageResources("org.apache.struts.util.LocalStrings");
046:
047: /**
048: * Java 1.4 encode method to use instead of deprecated 1.3 version.
049: */
050: private static Method encode = null;
051:
052: /**
053: * Commons logging instance.
054: */
055: private static final Log log = LogFactory
056: .getLog(ResponseUtils.class);
057:
058: /**
059: * Initialize the encode variable with the
060: * Java 1.4 method if available.
061: */
062: static {
063: try {
064: // get version of encode method with two String args
065: Class[] args = new Class[] { String.class, String.class };
066:
067: encode = URLEncoder.class.getMethod("encode", args);
068: } catch (NoSuchMethodException e) {
069: log
070: .debug(
071: "Could not find Java 1.4 encode method. Using deprecated version.",
072: e);
073: }
074: }
075:
076: // --------------------------------------------------------- Public Methods
077:
078: /**
079: * Filter the specified string for characters that are senstive to HTML
080: * interpreters, returning the string with these characters replaced by
081: * the corresponding character entities.
082: *
083: * @param value The string to be filtered and returned
084: */
085: public static String filter(String value) {
086: if ((value == null) || (value.length() == 0)) {
087: return value;
088: }
089:
090: StringBuffer result = null;
091: String filtered = null;
092:
093: for (int i = 0; i < value.length(); i++) {
094: filtered = null;
095:
096: switch (value.charAt(i)) {
097: case '<':
098: filtered = "<";
099:
100: break;
101:
102: case '>':
103: filtered = ">";
104:
105: break;
106:
107: case '&':
108: filtered = "&";
109:
110: break;
111:
112: case '"':
113: filtered = """;
114:
115: break;
116:
117: case '\'':
118: filtered = "'";
119:
120: break;
121: }
122:
123: if (result == null) {
124: if (filtered != null) {
125: result = new StringBuffer(value.length() + 50);
126:
127: if (i > 0) {
128: result.append(value.substring(0, i));
129: }
130:
131: result.append(filtered);
132: }
133: } else {
134: if (filtered == null) {
135: result.append(value.charAt(i));
136: } else {
137: result.append(filtered);
138: }
139: }
140: }
141:
142: return (result == null) ? value : result.toString();
143: }
144:
145: /**
146: * URLencodes a string assuming the character encoding is UTF-8.
147: *
148: * @param url
149: * @return String The encoded url in UTF-8
150: */
151: public static String encodeURL(String url) {
152: return encodeURL(url, "UTF-8");
153: }
154:
155: /**
156: * Use the new URLEncoder.encode() method from Java 1.4 if available, else
157: * use the old deprecated version. This method uses reflection to find
158: * the appropriate method; if the reflection operations throw exceptions,
159: * this will return the url encoded with the old URLEncoder.encode()
160: * method.
161: *
162: * @param enc The character encoding the urlencode is performed on.
163: * @return String The encoded url.
164: */
165: public static String encodeURL(String url, String enc) {
166: try {
167: if ((enc == null) || (enc.length() == 0)) {
168: enc = "UTF-8";
169: }
170:
171: // encode url with new 1.4 method and UTF-8 encoding
172: if (encode != null) {
173: return (String) encode.invoke(null, new Object[] { url,
174: enc });
175: }
176: } catch (IllegalAccessException e) {
177: log
178: .debug(
179: "Could not find Java 1.4 encode method. Using deprecated version.",
180: e);
181: } catch (InvocationTargetException e) {
182: log
183: .debug(
184: "Could not find Java 1.4 encode method. Using deprecated version.",
185: e);
186: }
187:
188: return URLEncoder.encode(url);
189: }
190: }
|