KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > util > ResponseUtils


1 /*
2  * $Id: ResponseUtils.java 164747 2005-04-26 05:47:48Z hrabago $
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
19 package org.apache.struts.util;
20
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.PageContext JavaDoc;
23
24 import org.apache.struts.taglib.TagUtils;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.commons.logging.Log;
27
28 import java.lang.reflect.Method JavaDoc;
29 import java.lang.reflect.InvocationTargetException JavaDoc;
30 import java.net.URLEncoder JavaDoc;
31
32 /**
33  * General purpose utility methods related to generating a servlet response
34  * in the Struts controller framework.
35  *
36  * @version $Rev: 164747 $ $Date: 2005-04-26 06:47:48 +0100 (Tue, 26 Apr 2005) $
37  */

38 public class ResponseUtils {
39
40
41     // ------------------------------------------------------- Static Variables
42

43
44     /**
45      * The message resources for this package.
46      */

47     protected static MessageResources messages =
48         MessageResources.getMessageResources
49         ("org.apache.struts.util.LocalStrings");
50
51
52     /**
53      * Java 1.4 encode method to use instead of deprecated 1.3 version.
54      */

55     private static Method JavaDoc encode = null;
56
57
58     /**
59      * Commons logging instance.
60      */

61     private static final Log log = LogFactory.getLog(ResponseUtils.class);
62
63
64     /**
65      * Initialize the encode variable with the
66      * Java 1.4 method if available.
67      */

68     static {
69
70         try {
71             // get version of encode method with two String args
72
Class JavaDoc[] args = new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class};
73             encode = URLEncoder JavaDoc.class.getMethod("encode", args);
74         } catch (NoSuchMethodException JavaDoc e) {
75             log.debug("Could not find Java 1.4 encode method. Using deprecated version.", e);
76         }
77     }
78
79     
80
81     // --------------------------------------------------------- Public Methods
82

83
84     /**
85      * Filter the specified string for characters that are sensitive to
86      * HTML interpreters, returning the string with these characters replaced
87      * by the corresponding character entities.
88      *
89      * @param value The string to be filtered and returned
90      */

91     public static String JavaDoc filter(String JavaDoc value) {
92
93         if (value == null || value.length() == 0) {
94             return value;
95         }
96
97         StringBuffer JavaDoc result = null;
98         String JavaDoc filtered = null;
99         for (int i = 0; i < value.length(); i++) {
100             filtered = null;
101             switch (value.charAt(i)) {
102                 case '<':
103                     filtered = "&lt;";
104                     break;
105                 case '>':
106                     filtered = "&gt;";
107                     break;
108                 case '&':
109                     filtered = "&amp;";
110                     break;
111                 case '"':
112                     filtered = "&quot;";
113                     break;
114                 case '\'':
115                     filtered = "&#39;";
116                     break;
117             }
118
119             if (result == null) {
120                 if (filtered != null) {
121                     result = new StringBuffer JavaDoc(value.length() + 50);
122                     if (i > 0) {
123                         result.append(value.substring(0, i));
124                     }
125                     result.append(filtered);
126                 }
127             } else {
128                 if (filtered == null) {
129                     result.append(value.charAt(i));
130                 } else {
131                     result.append(filtered);
132                 }
133             }
134         }
135
136         return result == null ? value : result.toString();
137     }
138
139
140
141     
142     /**
143      * <p>URLencodes a string assuming the character encoding is UTF-8.</p>
144      *
145      * @param url
146      * @return String The encoded url in UTF-8
147      */

148     public static String JavaDoc encodeURL(String JavaDoc url) {
149         return encodeURL(url, "UTF-8");
150     }
151
152     
153     /**
154      * Use the new URLEncoder.encode() method from Java 1.4 if available, else
155      * use the old deprecated version. This method uses reflection to find the
156      * appropriate method; if the reflection operations throw exceptions, this
157      * will return the url encoded with the old URLEncoder.encode() method.
158      * @param enc The character encoding the urlencode is performed on.
159      * @return String The encoded url.
160      */

161     public static String JavaDoc encodeURL(String JavaDoc url, String JavaDoc enc) {
162         try {
163
164             if(enc==null || enc.length()==0){
165                 enc = "UTF-8";
166             }
167
168             // encode url with new 1.4 method and UTF-8 encoding
169
if (encode != null) {
170                 return (String JavaDoc) encode.invoke(null, new Object JavaDoc[]{url, enc});
171             }
172
173         } catch (IllegalAccessException JavaDoc e) {
174             log.debug("Could not find Java 1.4 encode method. Using deprecated version.", e);
175         } catch (InvocationTargetException JavaDoc e) {
176             log.debug("Could not find Java 1.4 encode method. Using deprecated version.", e);
177         }
178
179         return URLEncoder.encode(url);
180     }
181
182
183     /**
184      * Write the specified text as the response to the writer associated with
185      * this page. <strong>WARNING</strong> - If you are writing body content
186      * from the <code>doAfterBody()</code> method of a custom tag class that
187      * implements <code>BodyTag</code>, you should be calling
188      * <code>writePrevious()</code> instead.
189      *
190      * @param pageContext The PageContext object for this page
191      * @param text The text to be written
192      *
193      * @exception JspException if an input/output error occurs (already saved)
194      * @deprecated use TagUtils.write() method instead.
195      * This method will be removed after Struts 1.2.
196      */

197     public static void write(PageContext JavaDoc pageContext, String JavaDoc text)
198         throws JspException JavaDoc {
199
200         TagUtils.getInstance().write(pageContext, text);
201
202     }
203
204
205     /**
206      * Write the specified text as the response to the writer associated with
207      * the body content for the tag within which we are currently nested.
208      *
209      * @param pageContext The PageContext object for this page
210      * @param text The text to be written
211      *
212      * @exception JspException if an input/output error occurs (already saved)
213      * @deprecated use TagUtils.writePrevious() method instead.
214      * This method will be removed after Struts 1.2.
215      */

216     public static void writePrevious(PageContext JavaDoc pageContext, String JavaDoc text)
217         throws JspException JavaDoc {
218
219         TagUtils.getInstance().writePrevious(pageContext, text);
220
221     }
222
223
224 }
225
Popular Tags