1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.control.action;
16
17 import org.figure8.join.control.JoinAction;
18 import org.figure8.join.util.LogUtil;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.struts.action.ActionForm;
22 import org.apache.struts.action.ActionForward;
23 import org.apache.struts.action.ActionMapping;
24
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import java.io.File;
29 import java.io.FileFilter;
30 import java.io.FileReader;
31 import java.io.BufferedReader;
32 import java.util.ArrayList;
33 import java.util.Date;
34 /**
35 * A simple Struts action for viewing directory and file content.
36 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
37 * @version $Revision: 1.1 $
38 *
39 * @struts.action path="/browseLogs" name="defaultForm"
40 * scope="request" parameter="op" validate="true"
41 * input="/pages/mainpage.jsp?body=/jsp/logs.jsp"
42 * @struts.action-forward name="Next" path="/jsp/logs.jsp"
43 */
44 public class BrowseLogsActions extends JoinAction{
45
46
47
48 /** Get a commons logger. */
49 private static final Log log = LogUtil.getLog(BrowseLogsActions.class);
50
51 /** Operation code for browsing a specific logs directory */
52 public static final String DIR_OP = "dir";
53 /** Operation code for browsing a spcifif file content */
54 public static final String FILE_OP = "file";
55
56
57
58
59 /** Creates a new instance of BrowseLogsActions */
60 public BrowseLogsActions(){
61 }
62
63
64
65
66 /**
67 *
68 * @param operation String representing the operation to invoke on Action
69 * @param mapping Mapping between forwards name and path for this action
70 * @param form The form object containing request parameters
71 * @param request The servlet container request wrapper
72 * @param response The servlet container response wrapper
73 * @return A forward to the next view to render and display
74 * @throws Exception such as InfraStructureExceptions ...
75 */
76 public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
77
78 log.debug("doExecute() called for '" + operation + "' operation");
79
80 if (DIR_OP.equals(operation)){
81 return browseDirectory(mapping, form, request, response);
82 }
83 else if (FILE_OP.equals(operation)){
84 return getFileContent(mapping, form, request, response);
85 }
86
87 return null;
88 }
89
90
91
92
93 /**
94 * Browse a directory content, putting it into a Map.
95 * @return A forward to the next view to render and display
96 */
97 protected ActionForward browseDirectory(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
98 throws Exception{
99
100 String fileId = request.getParameter("id");
101
102 if (fileId != null && fileId.length() > 0){
103
104 File file = new File(fileId);
105 File[] files = file.listFiles(getFileFilter());
106
107
108 ArrayList fileList = new ArrayList(files.length);
109 for (int i=0; i<files.length; i++)
110 fileList.add(new FileHandler(files[i].getName(), new Date(files[i].lastModified())));
111
112
113 request.setAttribute("directory.path", fileId);
114 request.setAttribute("directory.name", file.getName());
115 request.setAttribute("directory", fileList);
116 return mapping.findForward("Next");
117 }
118
119 return null;
120 }
121
122 /**
123 * Get a text file content, putting it into a request attribute.
124 * @return A forward to the next view to render and display
125 */
126 protected ActionForward getFileContent(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
127 throws Exception{
128
129 String fileId = request.getParameter("id");
130
131 if (fileId != null && fileId.length() > 0){
132
133 File file = new File(fileId);
134 StringBuffer result = new StringBuffer();
135 BufferedReader br = new BufferedReader(new FileReader(file));
136
137 try{
138
139 String line;
140 while ((line = br.readLine()) != null)
141 result.append(line + "\n");
142 }
143 catch (Exception e) {e.printStackTrace();}
144 finally {br.close();}
145
146
147 request.setAttribute("file", result.toString());
148 request.setAttribute("file.path", fileId);
149 request.setAttribute("file.name", file.getName());
150 request.setAttribute("file.lastupdate", new Date(file.lastModified()));
151 request.setAttribute("directory.path", fileId.substring(0, fileId.lastIndexOf("/")));
152 return mapping.findForward("Next");
153 }
154
155 return null;
156 }
157
158
159
160
161 /**
162 * Use a FileFilter to select only files (not directories).
163 * @return A FileFilter instance that accept only files (not directories)
164 */
165 private FileFilter getFileFilter(){
166 return new FileFilter(){
167 public boolean accept(File pathname){
168 return pathname.isFile();
169 }
170 };
171 }
172
173 public class FileHandler{
174 private String name;
175 private Date lastModified;
176 public FileHandler(String name, Date lastModified){
177 this.name = name;
178 this.lastModified = lastModified;
179 }
180 public String getName(){
181 return name;
182 }
183 public Date getLastModified(){
184 return lastModified;
185 }
186 }
187 }