BrightSide Workbench Full Report + Source Code
FileUtilAjax.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
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. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.impl.util;
19 
20 import java.io.BufferedReader;
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.OutputStreamWriter;
24 import java.io.PrintWriter;
25 import org.turro.elephant.context.ElephantContext;
26 import org.turro.elephant.context.IAction;
27 import org.turro.elephant.impl.abstracts.AbstractAction;
28 
33 public class FileUtilAjax extends AbstractAction implements IAction {
34 
35  public static final String
36  VALID_ATTACHMENTS = "(?i).*\\.(jpg|gif|png|pdf|doc|txt|zip)$";
37 
39  public FileUtilAjax() {
40  }
41 
42  @Override
43  public int execute() throws Exception {
44  String path = getParameter("path"),
45  action = getActionParameter();
46  boolean selectable = "true".equals(getParameter("sel"));
47  if(path == null) {
48  return IAction.NO_DATA;
49  }
50  File realPath = new File(ElephantContext.getRealPath(path));
51  if("list-folders".equals(action)) {
52  if(realPath.exists()) {
53  return listFolders(realPath);
54  }
55  else {
56  return IAction.ERROR;
57  }
58  }
59  else if("list-files".equals(action)) {
60  if(realPath.exists()) {
61  return listFiles(realPath, selectable);
62  }
63  else {
64  return IAction.ERROR;
65  }
66  }
67  else if("list-attachments".equals(action)) {
68  if(realPath.exists()) {
69  return listAttachments(realPath, selectable);
70  }
71  else {
72  return IAction.ERROR;
73  }
74  }
75  else if("remove-attachments".equals(action)) {
76  return removeAttachments(selectable, realPath);
77  }
78  else if("file-content".equals(action)) {
79  if(realPath.exists()) {
80  return fileContent(realPath);
81  }
82  else {
83  return IAction.ERROR;
84  }
85  }
86  else if("save-content".equals(action)) {
87  return saveContent(realPath);
88  }
89  else if("remove-file".equals(action)) {
90  if(realPath.exists()) {
91  realPath.delete();
92  }
93  else {
94  return IAction.ERROR;
95  }
96  }
97  return IAction.NO_DATA;
98  }
99 
100  private int removeAttachments(final boolean selectable, final File realPath) throws IOException {
101  String[] files = constructor.getParameter("files").split(",");
102  File f;
103  for(int i = 0; i < files.length; i++) {
104  f = new File(ElephantContext.getRealPath(files[i]));
105  if(f.exists())
106  f.delete();
107  }
108  return listAttachments(realPath, selectable);
109  }
110 
111  private int saveContent(final File realPath) throws IOException {
112  PrintWriter out = constructor.getOut();
113  String s = constructor.getParameter("content");
114  if(s != null) {
115  s = s.replaceAll("\\*\\-am\\-\\*","&");
116  s = s.replaceAll("\\*\\-eq\\-\\*","=");
117  s = s.replaceAll("\\*\\-pl\\-\\*","+");
118  OutputStreamWriter osw = FileUtil.getFileWriter(realPath);
119  osw.write(s);
120  osw.close();
121  out.println("Done");
122  }
123  else {
124  out.println("No data!");
125  }
126  return IAction.DONE;
127  }
128 
129  private int fileContent(final File realPath) throws IOException {
130  BufferedReader br = FileUtil.getBufferedFile(realPath);
131  String s;
132  PrintWriter out = constructor.getOut();
133  while((s = br.readLine()) != null) {
134  out.println(s);
135  }
136  br.close();
137  return IAction.DONE;
138  }
139 
140  private int listAttachments(final File realPath, final boolean selectable) throws IOException {
141  File[] children = realPath.listFiles();
142  PrintWriter out = constructor.getOut();
143  out.println("<ul>");
144  String s;
145  for(int i = 0; i < children.length; i++) {
146  s = children[i].getAbsolutePath();
147  if(s.matches(VALID_ATTACHMENTS)) {
148  out.print("<li path='" + ElephantContext.getRelativePath(s) + "'>");
149  if(selectable) {
150  out.print("<input type='checkbox'/>");
151  }
152  if(children[i].isFile()) {
153  out.print("<a class='file'>");
154  }
155  out.print(children[i].getName());
156  out.print("</a>");
157  out.println("</li>");
158  }
159  }
160  out.println("</ul>");
161  return IAction.DONE;
162  }
163 
164  private int listFiles(final File realPath, final boolean selectable) throws IOException {
165  File[] children = realPath.listFiles();
166  PrintWriter out = constructor.getOut();
167  out.println("<ul>");
168  for(int i = 0; i < children.length; i++) {
169  out.print("<li path='" + ElephantContext.getRelativePath(children[i].getAbsolutePath()) + "'>");
170  if(selectable) {
171  out.print("<input type='checkbox'/>");
172  }
173  if(children[i].isDirectory()) {
174  out.print("<a class='folder'>");
175  }
176  else if(children[i].isFile()) {
177  out.print("<a class='file'>");
178  }
179  out.print(children[i].getName());
180  out.print("</a>");
181  out.println("</li>");
182  }
183  out.println("</ul>");
184  return IAction.DONE;
185  }
186 
187  private int listFolders(final File realPath) throws IOException {
188  File[] children = realPath.listFiles();
189  PrintWriter out = constructor.getOut();
190  out.println("<ul>");
191  for(int i = 0; i < children.length; i++) {
192  if(children[i].isDirectory()) {
193  out.print("<li class='collapsed' path='" + ElephantContext.getRelativePath(children[i].getAbsolutePath()) + "'>");
194  out.print("<a class='folder'>");
195  out.print(children[i].getName());
196  out.print("</a>");
197  out.println("</li>");
198  }
199  }
200  out.println("</ul>");
201  return IAction.DONE;
202  }
203 
204 }
String getParameter(String param)