View Javadoc

1   /*
2    * $Id: ShowFileAction.java 421488 2006-07-13 03:43:08Z wsmoak $
3    *
4    * Copyright 2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.struts.webapp.validator;
20  
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  
24  import java.io.InputStream;
25  import java.io.InputStreamReader;
26  
27  import org.apache.struts.action.Action;
28  import org.apache.struts.action.ActionForm;
29  import org.apache.struts.action.ActionForward;
30  import org.apache.struts.action.ActionMapping;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.commons.logging.Log;
33  
34  /***
35   * Action which retrieves a file specified in the parameter
36   * and stores its contents in the request, so that they
37   * can be displayed.
38   */
39  public class ShowFileAction extends Action {
40  
41      /*** Logging Instance. */
42      private static final Log log = LogFactory.getLog(ShowFileAction.class);
43  
44      public ActionForward execute(ActionMapping mapping,
45                                   ActionForm form,
46                                   HttpServletRequest request,
47                                   HttpServletResponse response)
48                            throws Exception {
49  
50          // Get the file name
51          String fileName = mapping.getParameter();
52          StringBuffer fileContents = new StringBuffer();
53  
54          if(fileName != null) {
55  
56              InputStream input = servlet.getServletContext().getResourceAsStream(fileName);
57              if (input == null) {
58                  log.warn("File Not Found: "+fileName);
59              } else {
60                  InputStreamReader inputReader = new InputStreamReader(input);
61                  char[] buffer = new char[1000];
62                  while (true) {
63                      int lth = inputReader.read(buffer);
64                      if (lth < 0) {
65                          break;
66                      } else {
67                          fileContents.append(buffer, 0, lth);
68                      }
69                  }
70              }
71          } else {
72              log.error("No file name specified.");
73          }
74  
75  
76          // Store the File contents and name in the Request
77          request.setAttribute("fileName", fileName);
78          request.setAttribute("fileContents", fileContents.toString());
79  
80          return mapping.findForward("success");
81      }
82  }