001: /*
002: * $Id: PlainTextResult.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.dispatcher;
022:
023: import java.io.InputStreamReader;
024: import java.io.PrintWriter;
025: import java.nio.charset.Charset;
026:
027: import javax.servlet.ServletContext;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: import com.opensymphony.xwork2.ActionInvocation;
034:
035: /**
036: * <!-- START SNIPPET: description -->
037: *
038: * A result that send the content out as plain text. Usefull typically when needed
039: * to display the raw content of a JSP or Html file for example.
040: *
041: * <!-- END SNIPPET: description -->
042: *
043: *
044: * <!-- START SNIPPET: params -->
045: *
046: * <ul>
047: * <li>location (default) = location of the file (jsp/html) to be displayed as plain text.</li>
048: * <li>charSet (optional) = character set to be used. This character set will be used to set the
049: * response type (eg. Content-Type=text/plain; charset=UTF-8) and when reading
050: * using a Reader. Some example of charSet would be UTF-8, ISO-8859-1 etc.
051: * </ul>
052: *
053: * <!-- END SNIPPET: params -->
054: *
055: *
056: * <pre>
057: * <!-- START SNIPPET: example -->
058: *
059: * <action name="displayJspRawContent" >
060: * <result type="plaintext">/myJspFile.jsp</result>
061: * </action>
062: *
063: *
064: * <action name="displayJspRawContent" >
065: * <result type="plaintext">
066: * <param name="location">/myJspFile.jsp</param>
067: * <param name="charSet">UTF-8</param>
068: * </result>
069: * </action>
070: *
071: * <!-- END SNIPPET: example -->
072: * </pre>
073: *
074: */
075: public class PlainTextResult extends StrutsResultSupport {
076:
077: public static final int BUFFER_SIZE = 1024;
078:
079: private static final Log _log = LogFactory
080: .getLog(PlainTextResult.class);
081:
082: private static final long serialVersionUID = 3633371605905583950L;
083:
084: private String charSet;
085:
086: public PlainTextResult() {
087: super ();
088: }
089:
090: public PlainTextResult(String location) {
091: super (location);
092: }
093:
094: /**
095: * Set the character set
096: *
097: * @return The character set
098: */
099: public String getCharSet() {
100: return charSet;
101: }
102:
103: /**
104: * Set the character set
105: *
106: * @param charSet The character set
107: */
108: public void setCharSet(String charSet) {
109: this .charSet = charSet;
110: }
111:
112: /* (non-Javadoc)
113: * @see org.apache.struts2.dispatcher.StrutsResultSupport#doExecute(java.lang.String, com.opensymphony.xwork2.ActionInvocation)
114: */
115: protected void doExecute(String finalLocation,
116: ActionInvocation invocation) throws Exception {
117:
118: // verify charset
119: Charset charset = null;
120: if (charSet != null) {
121: if (Charset.isSupported(charSet)) {
122: charset = Charset.forName(charSet);
123: } else {
124: _log.warn("charset [" + charSet
125: + "] is not recognized ");
126: charset = null;
127: }
128: }
129:
130: HttpServletResponse response = (HttpServletResponse) invocation
131: .getInvocationContext().get(HTTP_RESPONSE);
132: ServletContext servletContext = (ServletContext) invocation
133: .getInvocationContext().get(SERVLET_CONTEXT);
134:
135: if (charset != null) {
136: response.setContentType("text/plain; charset=" + charSet);
137: } else {
138: response.setContentType("text/plain");
139: }
140: response.setHeader("Content-Disposition", "inline");
141:
142: PrintWriter writer = response.getWriter();
143: InputStreamReader reader = null;
144: try {
145: if (charset != null) {
146: reader = new InputStreamReader(servletContext
147: .getResourceAsStream(finalLocation), charset);
148: } else {
149: reader = new InputStreamReader(servletContext
150: .getResourceAsStream(finalLocation));
151: }
152: if (reader == null) {
153: _log
154: .warn("resource at location ["
155: + finalLocation
156: + "] cannot be obtained (return null) from ServletContext !!! ");
157: } else {
158: char[] buffer = new char[BUFFER_SIZE];
159: int charRead = 0;
160: while ((charRead = reader.read(buffer)) != -1) {
161: writer.write(buffer, 0, charRead);
162: }
163: }
164: } finally {
165: if (reader != null)
166: reader.close();
167: if (writer != null) {
168: writer.flush();
169: writer.close();
170: }
171: }
172: }
173: }
|