View Javadoc

    1   /*
    2    * $Id: PlainTextResult.java 471756 2006-11-06 15:01:43Z husted $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *  http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    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   * &lt;action name="displayJspRawContent" &gt;
    60   *   &lt;result type="plaintext"&gt;/myJspFile.jsp&lt;/result&gt;
    61   * &lt;/action&gt;
    62   *
    63   *
    64   * &lt;action name="displayJspRawContent" &gt;
    65   *   &lt;result type="plaintext"&gt;
    66   *      &lt;param name="location"&gt;/myJspFile.jsp&lt;/param&gt;
    67   *      &lt;param name="charSet"&gt;UTF-8&lt;/param&gt;
    68   *   &lt;/result&gt;
    69   * &lt;/action&gt;
    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     /* (non-Javadoc)
    112      * @see org.apache.struts2.dispatcher.StrutsResultSupport#doExecute(java.lang.String, com.opensymphony.xwork2.ActionInvocation)
    113      */
    114     protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
    115
    116         // verify charset
    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 }