01: /*
02: * $Id: ShowFileAction.java 471754 2006-11-06 14:55:09Z husted $
03: *
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "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
46: .getLog(ShowFileAction.class);
47:
48: public ActionForward execute(ActionMapping mapping,
49: ActionForm form, HttpServletRequest request,
50: HttpServletResponse response) throws Exception {
51:
52: // Get the file name
53: String fileName = mapping.getParameter();
54: StringBuffer fileContents = new StringBuffer();
55:
56: if (fileName != null) {
57:
58: InputStream input = servlet.getServletContext()
59: .getResourceAsStream(fileName);
60: if (input == null) {
61: log.warn("File Not Found: " + fileName);
62: } else {
63: InputStreamReader inputReader = new InputStreamReader(
64: input);
65: char[] buffer = new char[1000];
66: while (true) {
67: int lth = inputReader.read(buffer);
68: if (lth < 0) {
69: break;
70: } else {
71: fileContents.append(buffer, 0, lth);
72: }
73: }
74: }
75: } else {
76: log.error("No file name specified.");
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: }
|