View Javadoc

1   /*
2    * $Id: ResponseUtils.java 421119 2006-07-12 04:49:11Z wsmoak $
3    *
4    * Copyright 1999-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts.util;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  import java.lang.reflect.InvocationTargetException;
24  import java.lang.reflect.Method;
25  
26  import java.net.URLEncoder;
27  
28  /***
29   * General purpose utility methods related to generating a servlet response in
30   * the Struts controller framework.
31   *
32   * @version $Rev: 421119 $ $Date: 2005-08-21 14:46:28 -0400 (Sun, 21 Aug 2005)
33   *          $
34   */
35  public class ResponseUtils {
36      // ------------------------------------------------------- Static Variables
37  
38      /***
39       * The message resources for this package.
40       */
41      protected static MessageResources messages =
42          MessageResources.getMessageResources(
43              "org.apache.struts.util.LocalStrings");
44  
45      /***
46       * Java 1.4 encode method to use instead of deprecated 1.3 version.
47       */
48      private static Method encode = null;
49  
50      /***
51       * Commons logging instance.
52       */
53      private static final Log log = LogFactory.getLog(ResponseUtils.class);
54  
55      /***
56       * Initialize the encode variable with the
57       * Java 1.4 method if available.
58       */
59      static {
60          try {
61              // get version of encode method with two String args
62              Class[] args = new Class[] { String.class, String.class };
63  
64              encode = URLEncoder.class.getMethod("encode", args);
65          } catch (NoSuchMethodException e) {
66              log.debug("Could not find Java 1.4 encode method.  Using deprecated version.",
67                  e);
68          }
69      }
70  
71      // --------------------------------------------------------- Public Methods
72  
73      /***
74       * Filter the specified string for characters that are senstive to HTML
75       * interpreters, returning the string with these characters replaced by
76       * the corresponding character entities.
77       *
78       * @param value The string to be filtered and returned
79       */
80      public static String filter(String value) {
81          if ((value == null) || (value.length() == 0)) {
82              return value;
83          }
84  
85          StringBuffer result = null;
86          String filtered = null;
87  
88          for (int i = 0; i < value.length(); i++) {
89              filtered = null;
90  
91              switch (value.charAt(i)) {
92              case '<':
93                  filtered = "&lt;";
94  
95                  break;
96  
97              case '>':
98                  filtered = "&gt;";
99  
100                 break;
101 
102             case '&':
103                 filtered = "&amp;";
104 
105                 break;
106 
107             case '"':
108                 filtered = "&quot;";
109 
110                 break;
111 
112             case '\'':
113                 filtered = "&#39;";
114 
115                 break;
116             }
117 
118             if (result == null) {
119                 if (filtered != null) {
120                     result = new StringBuffer(value.length() + 50);
121 
122                     if (i > 0) {
123                         result.append(value.substring(0, i));
124                     }
125 
126                     result.append(filtered);
127                 }
128             } else {
129                 if (filtered == null) {
130                     result.append(value.charAt(i));
131                 } else {
132                     result.append(filtered);
133                 }
134             }
135         }
136 
137         return (result == null) ? value : result.toString();
138     }
139 
140     /***
141      * URLencodes a string assuming the character encoding is UTF-8.
142      *
143      * @param url
144      * @return String The encoded url in UTF-8
145      */
146     public static String encodeURL(String url) {
147         return encodeURL(url, "UTF-8");
148     }
149 
150     /***
151      * Use the new URLEncoder.encode() method from Java 1.4 if available, else
152      * use the old deprecated version.  This method uses reflection to find
153      * the appropriate method; if the reflection operations throw exceptions,
154      * this will return the url encoded with the old URLEncoder.encode()
155      * method.
156      *
157      * @param enc The character encoding the urlencode is performed on.
158      * @return String The encoded url.
159      */
160     public static String encodeURL(String url, String enc) {
161         try {
162             if ((enc == null) || (enc.length() == 0)) {
163                 enc = "UTF-8";
164             }
165 
166             // encode url with new 1.4 method and UTF-8 encoding
167             if (encode != null) {
168                 return (String) encode.invoke(null, new Object[] { url, enc });
169             }
170         } catch (IllegalAccessException e) {
171             log.debug("Could not find Java 1.4 encode method.  Using deprecated version.",
172                 e);
173         } catch (InvocationTargetException e) {
174             log.debug("Could not find Java 1.4 encode method. Using deprecated version.",
175                 e);
176         }
177 
178         return URLEncoder.encode(url);
179     }
180 }