BrightSide Workbench Full Report + Source Code
RepositoryBean.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.repository;
19 
20 import java.io.File;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.TreeSet;
24 import org.apache.commons.fileupload.FileItem;
25 import org.apache.commons.fileupload.FileItemFactory;
26 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
27 import org.apache.commons.fileupload.servlet.ServletFileUpload;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.elephant.context.IConstructor;
30 
35 public class RepositoryBean {
36  public static final String
37  REPOSITORY_JSP = "/WEB-INF/elephant/admin/repository.jsp",
38  VALID_PATTERN = ".*\\.(#)$",
39  VALID_IMAGES = "gif|jpg|png|mpg",
40  VALID_HTML = "html|htm",
41  VALID_DOCUMENTS = "txt|cvs|pdf|odt|ott|odm|oth|ods|ots|odg|otg|odp|otp|odi|odf|odc|odb|doc|xls";
42 
43  private IConstructor constructor;
44  private String context, subFolder;
45  private boolean selectable = true;
46  private String roleUpload = null,
47  roleList = null,
48  roleDelete = null,
49  roleCreateDir = null,
50  validUploads = null,
51  rootPath = null;
52 
53  private String[] htmlFiles;
54 
56  public RepositoryBean() {
57  }
58 
59  public void setRoleUpload(String value) {
60  roleUpload = value;
61  }
62 
63  public void setRoleList(String value) {
64  roleList = value;
65  }
66 
67  public void setRoleDelete(String value) {
68  roleDelete = value;
69  }
70 
71  public void setRoleCreateDir(String value) {
72  roleCreateDir = value;
73  }
74 
75  public void setRootPath(String value) {
76  rootPath = value;
77  }
78 
79  public void setValidUploads(String value) {
80  String sep ="", values[] = value.split(" *, *");
81  validUploads = "";
82  for(int i = 0; i < values.length; i++) {
83  if(values[i].equals("images")) {
84  validUploads += sep + VALID_IMAGES;
85  sep ="|";
86  }
87  else if(values[i].equals("html")) {
88  validUploads += sep + VALID_HTML;
89  sep ="|";
90  }
91  if(values[i].equals("documents")) {
92  validUploads += sep + VALID_DOCUMENTS;
93  sep ="|";
94  }
95  }
96  validUploads = VALID_PATTERN.replaceAll("#", validUploads);
97  }
98 
99  public void deleteFile(String file) {
100  File f = new File(getRealPath(file));
101  if(f.exists()) {
102  org.amic.util.file.FileUtil.deleteFile(f);
103  }
104  }
105 
106  public void createFolder(String folder) {
107  File f = new File(getRealPath(folder));
108  f.mkdir();
109  }
110 
111  public String[] getFiles() {
112  if(!constructor.isInRole(roleList)) {
113  htmlFiles = new String[0];
114  }
115  if(htmlFiles == null) {
116  File f = new File(getRealPath(null));
117  File[] children = f.listFiles();
118  TreeSet set = new TreeSet();
119  String folderLink;
120  if(subFolder != null && subFolder.length() > 0) {
121  folderLink = ElephantContext.getRootWebPath() + "/xprepository?context=" + context;
122  set.add(
123  "<li>" +
124  "<a class='folder' href='" + folderLink + "'>..</a>" +
125  "</li>");
126  }
127  for(int i = 0; children != null && i < children.length; i++) {
128  folderLink = ElephantContext.getRootWebPath() + "/xprepository?context=" + context + "&subfolder=" + children[i].getName();
129  set.add(
130  "<li>" +
131  (isSelectable() ? "<input type='checkbox' name='check' value ='" + children[i].getName() + "'/>" : "") +
132  (children[i].isDirectory() ?
133  "<a class='folder' href='" + folderLink + "'>" :
134  "<a repository=\"" + getImageUrl(children[i].getName()) + "\" class='file'>") +
135  children[i].getName() +
136  "</a>" +
137  "</li>");
138  }
139  htmlFiles = (String[])set.toArray(new String[0]);
140  }
141  return htmlFiles;
142  }
143 
144  public String getRealFolder() {
145  return getRealPath(null);
146  }
147 
148  public String convertToValidName(String fileName) {
149  return fileName.replaceAll("[^a-zA-Z0-9\\-\\_\\.]", "_");
150  }
151 
152  public String getContext() {
153  return context;
154  }
155 
156  public void setContext(String context) {
157  this.context = context;
158  }
159 
161  return constructor;
162  }
163 
164  public void setConstructor(IConstructor constructor) throws Exception {
165  this.constructor = constructor;
166  if(ServletFileUpload.isMultipartContent(constructor.getRequest()) && constructor.isInRole(roleUpload)) {
167  FileItemFactory factory = new DiskFileItemFactory();
168  ServletFileUpload upload = new ServletFileUpload(factory);
169  List items = upload.parseRequest(constructor.getRequest());
170  Iterator iter = items.iterator();
171  while (iter.hasNext()) {
172  FileItem item = (FileItem) iter.next();
173  if(!item.isFormField()) {
174  String onlyName = item.getName();
175  if(onlyName != null && onlyName.length() > 0) {
176  int p = onlyName.lastIndexOf('/');
177  if(p == -1) p = onlyName.lastIndexOf('\\');
178  String realName = getRealPath(convertToValidName(onlyName.substring(p + 1)));
179  if(realName.matches(validUploads)) {
180  File uploadedFile = new File(realName);
181  item.write(uploadedFile);
182  }
183  }
184  }
185  }
186  }
187  else if(constructor.getParameter("removesel") != null && constructor.isInRole(roleDelete)) {
188  String[] check = constructor.getRequest().getParameterValues("check");
189  if(check != null) {
190  for(int i = 0; i < check.length; i++) {
191  deleteFile(check[i]);
192  }
193  }
194  }
195  else if(constructor.getParameter("createdir") != null && constructor.isInRole(roleCreateDir)) {
196  createFolder(convertToValidName(constructor.getParameter("newdir")));
197  }
198  }
199 
200  public String getImageUrl(String file) {
201  String folderPath = "repository(" +
202  (subFolder == null || subFolder.length() == 0 ? "" : "/" + subFolder);
203  return folderPath + "/" + file + ")";
204  }
205 
206  private String getRealPath(String file) {
207  String folderPath = rootPath +
208  (subFolder == null || subFolder.length() == 0 ? "" : "/" + subFolder);
209  if(file == null) {
210  return ElephantContext.getRealPath(folderPath);
211  }
212  else {
213  return ElephantContext.getRealPath(folderPath + "/" + file);
214  }
215  }
216 
217  public boolean isSelectable() {
218  return selectable;
219  }
220 
221  public void setSelectable(boolean selectable) {
222  this.selectable = selectable;
223  }
224 
225  public String getSubFolder() {
226  return subFolder;
227  }
228 
229  public void setSubFolder(String subFolder) {
230  this.subFolder = subFolder;
231  }
232 
233 }