1 /*
2 * $Id$
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 java.io.InputStream;
25 import java.io.InputStreamReader;
26
27 import jakarta.servlet.http.HttpServletRequest;
28 import jakarta.servlet.http.HttpServletResponse;
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.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
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 private static final long serialVersionUID = -3817516672582496435L;
44
45 /**
46 * The {@code Log} instance for this class.
47 */
48 private final static Logger LOG =
49 LoggerFactory.getLogger(ShowFileAction.class);
50
51 public ActionForward execute(ActionMapping mapping,
52 ActionForm form,
53 HttpServletRequest request,
54 HttpServletResponse response)
55 throws Exception {
56
57 // Get the file name
58 String fileName = mapping.getParameter();
59 StringBuilder fileContents = new StringBuilder();
60
61 if(fileName != null) {
62
63 try (InputStream input = servlet.getServletContext().getResourceAsStream(fileName)) {
64 if (input == null) {
65 LOG.warn("File Not Found: {}", fileName);
66 } else {
67 try (InputStreamReader inputReader = new InputStreamReader(input)) {
68 char[] buffer = new char[1000];
69 while (true) {
70 int lth = inputReader.read(buffer);
71 if (lth < 0) {
72 break;
73 } else {
74 fileContents.append(buffer, 0, lth);
75 }
76 }
77 }
78 }
79 }
80 } else {
81 LOG.error("No file name specified.");
82 }
83
84
85 // Store the File contents and name in the Request
86 request.setAttribute("fileName", fileName);
87 request.setAttribute("fileContents", fileContents.toString());
88
89 return mapping.findForward("success");
90 }
91 }