View Javadoc

1   /*
2    * $Id: PlainTextResult.java 462586 2006-10-10 21:35:35Z mrdon $
3    *
4    * Copyright 2006 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.struts2.dispatcher;
19  
20  import java.io.InputStreamReader;
21  import java.io.PrintWriter;
22  import java.nio.charset.Charset;
23  
24  import javax.servlet.ServletContext;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  import com.opensymphony.xwork2.ActionInvocation;
31  
32  /***
33   * <!-- START SNIPPET: description -->
34   * 
35   * A result that send the content out as plain text. Usefull typically when needed
36   * to display the raw content of a JSP or Html file for example.
37   * 
38   * <!-- END SNIPPET: description -->
39   * 
40   * 
41   * <!-- START SNIPPET: params -->
42   * 
43   * <ul>
44   * 	<li>location (default) = location of the file (jsp/html) to be displayed as plain text.</li>
45   *  <li>charSet (optional) = character set to be used. This character set will be used to set the
46   *  response type (eg. Content-Type=text/plain; charset=UTF-8) and when reading 
47   *  using a Reader. Some example of charSet would be UTF-8, ISO-8859-1 etc. 
48   * </ul>
49   * 
50   * <!-- END SNIPPET: params -->
51   * 
52   * 
53   * <pre>
54   * <!-- START SNIPPET: example -->
55   * 
56   * &lt;action name="displayJspRawContent" &gt;
57   *   &lt;result type="plaintext"&gt;/myJspFile.jsp&lt;/result&gt;
58   * &lt;/action&gt;
59   * 
60   * 
61   * &lt;action name="displayJspRawContent" &gt;
62   *   &lt;result type="plaintext"&gt;
63   *      &lt;param name="location"&gt;/myJspFile.jsp&lt;/param&gt;
64   *      &lt;param name="charSet"&gt;UTF-8&lt;/param&gt;
65   *   &lt;/result&gt;
66   * &lt;/action&gt;
67   * 
68   * <!-- END SNIPPET: example -->
69   * </pre>
70   * 
71   */
72  public class PlainTextResult extends StrutsResultSupport {
73  
74      public static final int BUFFER_SIZE = 1024;
75      
76  	private static final Log _log = LogFactory.getLog(PlainTextResult.class);
77  
78  	private static final long serialVersionUID = 3633371605905583950L;
79  	
80  	private String charSet;
81  	
82  	public PlainTextResult() {
83  		super();
84  	}
85  	
86  	public PlainTextResult(String location) {
87  		super(location);
88  	}
89  	
90      /***
91       * Set the character set
92       * 
93       * @return The character set
94       */
95  	public String getCharSet() {
96  		return charSet;
97  	}
98      
99      /***
100      * Set the character set
101      * 
102      * @param charSet The character set
103      */
104 	public void setCharSet(String charSet) {
105 		this.charSet = charSet;
106 	}
107 	
108 	/* (non-Javadoc)
109 	 * @see org.apache.struts2.dispatcher.StrutsResultSupport#doExecute(java.lang.String, com.opensymphony.xwork2.ActionInvocation)
110 	 */
111 	protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
112 		
113 		// verify charset 
114 		Charset charset = null;
115 		if (charSet != null) {
116 			if (Charset.isSupported(charSet)) {
117 				charset = Charset.forName(charSet);
118 			}
119 			else {
120 				_log.warn("charset ["+charSet+"] is not recognized ");
121 				charset = null;
122 			}
123 		}
124 		
125 		HttpServletResponse response = (HttpServletResponse) invocation.getInvocationContext().get(HTTP_RESPONSE);
126 		ServletContext servletContext = (ServletContext) invocation.getInvocationContext().get(SERVLET_CONTEXT);
127 
128 		
129 		if (charset != null) {
130 			response.setContentType("text/plain; charset="+charSet);
131 		}
132 		else {
133 			response.setContentType("text/plain");
134 		}
135 		response.setHeader("Content-Disposition", "inline");
136 		
137 		
138 		PrintWriter writer = response.getWriter();
139 		InputStreamReader reader = null;
140 		try {
141 			if (charset != null) {
142 				reader = new InputStreamReader(servletContext.getResourceAsStream(finalLocation), charset);
143 			}
144 			else {
145 				reader = new InputStreamReader(servletContext.getResourceAsStream(finalLocation));
146 			}
147 			if (reader == null) {
148 				_log.warn("resource at location ["+finalLocation+"] cannot be obtained (return null) from ServletContext !!! ");
149 			}
150 			else {
151 				char[] buffer = new char[BUFFER_SIZE];
152 				int charRead = 0;
153 				while((charRead = reader.read(buffer)) != -1) {
154 					writer.write(buffer, 0, charRead);
155 				}
156 			}
157 		}
158 		finally {
159 			if (reader != null)
160 				reader.close();
161 			if (writer != null) {
162 				writer.flush();
163 				writer.close();
164 			}
165 		}
166 	}
167 }