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