1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 * <action name="displayJspRawContent" >
57 * <result type="plaintext">/myJspFile.jsp</result>
58 * </action>
59 *
60 *
61 * <action name="displayJspRawContent" >
62 * <result type="plaintext">
63 * <param name="location">/myJspFile.jsp</param>
64 * <param name="charSet">UTF-8</param>
65 * </result>
66 * </action>
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
109
110
111 protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
112
113
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 }