001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.dispatcher;
006:
007: import java.io.InputStreamReader;
008: import java.io.PrintWriter;
009: import java.nio.charset.Charset;
010:
011: import javax.servlet.ServletContext;
012: import javax.servlet.http.HttpServletResponse;
013:
014: import org.apache.commons.logging.Log;
015: import org.apache.commons.logging.LogFactory;
016:
017: import com.opensymphony.xwork.ActionInvocation;
018:
019: /**
020: * <!-- START SNIPPET: description -->
021: *
022: * A result that send the content out as plain text. Usefull typically when needed
023: * to display the raw content of a JSP or Html file for example.
024: *
025: * <!-- END SNIPPET: description -->
026: *
027: *
028: * <!-- START SNIPPET: params -->
029: *
030: * <ul>
031: * <li>location (default) = location of the file (jsp/html) to be displayed as plain text.</li>
032: * <li>charSet (optional) = character set to be used. This character set will be used to set the
033: * response type (eg. Content-Type=text/plain; charset=UTF-8) and when reading
034: * using a Reader. Some example of charSet would be UTF-8, ISO-8859-1 etc.
035: * </ul>
036: *
037: * <!-- END SNIPPET: params -->
038: *
039: *
040: * <pre>
041: * <!-- START SNIPPET: example -->
042: *
043: * <action name="displayJspRawContent" >
044: * <result type="plaintext">/myJspFile.jsp</result>
045: * </action>
046: *
047: *
048: * <action name="displayJspRawContent" >
049: * <result type="plaintext">
050: * <param name="location">/myJspFile.jsp</param>
051: * <param name="charSet">UTF-8</param>
052: * </result>
053: * </action>
054: *
055: * <!-- END SNIPPET: example -->
056: * </pre>
057: *
058: * @author tm_jee
059: * @version $Date: 2006-03-12 06:41:05 +0100 (Sun, 12 Mar 2006) $ $Id: PlainTextResult.java 2381 2006-03-12 05:41:05Z tmjee $
060: */
061: public class PlainTextResult extends WebWorkResultSupport {
062:
063: private static final Log _log = LogFactory
064: .getLog(PlainTextResult.class);
065:
066: private static final long serialVersionUID = 3633371605905583950L;
067:
068: public static final int BUFFER_SIZE = 1024;
069:
070: private String charSet;
071:
072: public String getCharSet() {
073: return charSet;
074: }
075:
076: public void setCharSet(String charSet) {
077: this .charSet = charSet;
078: }
079:
080: protected void doExecute(String finalLocation,
081: ActionInvocation invocation) throws Exception {
082:
083: // verify charset
084: Charset charset = null;
085: if (charSet != null) {
086: if (Charset.isSupported(charSet)) {
087: charset = Charset.forName(charSet);
088: } else {
089: _log.warn("charset [" + charSet
090: + "] is not recognized ");
091: charset = null;
092: }
093: }
094:
095: HttpServletResponse response = (HttpServletResponse) invocation
096: .getInvocationContext().get(HTTP_RESPONSE);
097: ServletContext servletContext = (ServletContext) invocation
098: .getInvocationContext().get(SERVLET_CONTEXT);
099:
100: if (charset != null) {
101: response.setContentType("text/plain; charset=" + charSet);
102: } else {
103: response.setContentType("text/plain");
104: }
105: response.setHeader("Content-Disposition", "inline");
106:
107: PrintWriter writer = response.getWriter();
108: InputStreamReader reader = null;
109: try {
110: if (charset != null) {
111: reader = new InputStreamReader(servletContext
112: .getResourceAsStream(location), charset);
113: } else {
114: reader = new InputStreamReader(servletContext
115: .getResourceAsStream(location));
116: }
117: if (reader == null) {
118: _log
119: .warn("resource at location ["
120: + location
121: + "] cannot be obtained (return null) from ServletContext !!! ");
122: } else {
123: char[] buffer = new char[BUFFER_SIZE];
124: int charRead = 0;
125: while ((charRead = reader.read(buffer)) != -1) {
126: writer.write(buffer, 0, charRead);
127: }
128: }
129: } finally {
130: if (reader != null)
131: reader.close();
132: if (writer != null) {
133: writer.flush();
134: writer.close();
135: }
136: }
137: }
138: }
|