View Javadoc

    1   /*
    2    * $Id: ShowFileAction.java 471754 2006-11-06 14:55:09Z 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
    22  package org.apache.struts.webapp.validator;
    23
    24  import javax.servlet.http.HttpServletRequest;
    25  import javax.servlet.http.HttpServletResponse;
    26
    27  import java.io.InputStream;
    28  import java.io.InputStreamReader;
    29
    30  import org.apache.struts.action.Action;
    31  import org.apache.struts.action.ActionForm;
    32  import org.apache.struts.action.ActionForward;
    33  import org.apache.struts.action.ActionMapping;
    34  import org.apache.commons.logging.LogFactory;
    35  import org.apache.commons.logging.Log;
    36
    37  /**
    38   * Action which retrieves a file specified in the parameter
    39   * and stores its contents in the request, so that they
    40   * can be displayed.
    41   */
    42  public class ShowFileAction extends Action {
    43
    44      /** Logging Instance. */
    45      private static final Log log = LogFactory.getLog(ShowFileAction.class);
    46
    47      public ActionForward execute(ActionMapping mapping,
    48                                   ActionForm form,
    49                                   HttpServletRequest request,
    50                                   HttpServletResponse response)
    51                            throws Exception {
    52
    53          // Get the file name
    54          String fileName = mapping.getParameter();
    55          StringBuffer fileContents = new StringBuffer();
    56
    57          if(fileName != null) {
    58
    59              InputStream input = servlet.getServletContext().getResourceAsStream(fileName);
    60              if (input == null) {
    61                  log.warn("File Not Found: "+fileName);
    62              } else {
    63                  InputStreamReader inputReader = new InputStreamReader(input);
    64                  char[] buffer = new char[1000];
    65                  while (true) {
    66                      int lth = inputReader.read(buffer);
    67                      if (lth < 0) {
    68                          break;
    69                      } else {
    70                          fileContents.append(buffer, 0, lth);
    71                      }
    72                  }
    73              }
    74          } else {
    75              log.error("No file name specified.");
    76          }
    77
    78
    79          // Store the File contents and name in the Request
    80          request.setAttribute("fileName", fileName);
    81          request.setAttribute("fileContents", fileContents.toString());
    82
    83          return mapping.findForward("success");
    84      }
    85  }