File Source: Resources.java
/*
P/P * Method: org.apache.roller.weblogger.ui.struts2.editor.Resources$WeblogResourceComparator__static_init
*/
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */
18
19 package org.apache.roller.weblogger.ui.struts2.editor;
20
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.Comparator;
27 import java.util.List;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.roller.weblogger.business.FileIOException;
31 import org.apache.roller.weblogger.business.FileNotFoundException;
32 import org.apache.roller.weblogger.business.FilePathException;
33 import org.apache.roller.weblogger.business.FileManager;
34 import org.apache.roller.weblogger.business.WebloggerFactory;
35 import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
36 import org.apache.roller.weblogger.pojos.WeblogPermission;
37 import org.apache.roller.weblogger.pojos.ThemeResource;
38 import org.apache.roller.weblogger.ui.struts2.util.UIAction;
39
40
41 /**
42 * Manage user uploaded resources.
43 */
44 public final class Resources extends UIAction {
45
/*
P/P * Method: org.apache.roller.weblogger.ui.struts2.editor.Resources__static_init
*
* Postconditions:
* init'ed(log)
*/
46 private static Log log = LogFactory.getLog(Resources.class);
47
48 // path under resources directory we are viewing
49 private String path = null;
50
51 // an array of files uploaded by the user, if applicable
52 private File[] uploadedFiles = null;
53
54 // an array of content types for upload files
55 private String[] uploadedFilesContentType = null;
56
57 // an array of filenames for uploaded files
58 private String[] uploadedFilesFileName = null;
59
60 // name of new subdir to create, if applicable
61 private String newDir = null;
62
63 // ids of files to delete, if applicable
64 private String[] deleteIds = null;
65
66 // list of files to display
67 private List files = Collections.EMPTY_LIST;
68
69 // is the weblog over the file quota
70 private boolean overQuota = false;
71
72 // total size of all files for weblog
73 private long totalSize = 0;
74
75
/*
P/P * Method: void org.apache.roller.weblogger.ui.struts2.editor.Resources()
*
* Presumptions:
* init'ed(java.util.Collections.EMPTY_LIST)
*
* Postconditions:
* this.actionName == &"resources"
* this.deleteIds == null
* this.newDir == null
* this.path == null
* this.uploadedFiles == null
* this.uploadedFilesContentType == null
* this.uploadedFilesFileName == null
* this.desiredMenu == &"editor"
* this.files == java.util.Collections.EMPTY_LIST
* (soft) init'ed(this.files)
* ...
*/
76 public Resources() {
77 this.actionName = "resources";
78 this.desiredMenu = "editor";
79 this.pageTitle = "uploadFiles.title";
80 }
81
82
83 // requires author role
84 public short requiredWeblogPermissions() {
/*
P/P * Method: short requiredWeblogPermissions()
*
* Presumptions:
* init'ed(org.apache.roller.weblogger.pojos.WeblogPermission.AUTHOR)
*
* Postconditions:
* return_value == org.apache.roller.weblogger.pojos.WeblogPermission.AUTHOR
* (soft) init'ed(return_value)
*/
85 return WeblogPermission.AUTHOR;
86 }
87
88
89 /**
90 * Display weblog resources.
91 */
92 public String execute() {
93
/*
P/P * Method: String execute()
*
* Preconditions:
* log != null
* (soft) init'ed(this.path)
*
* Presumptions:
* org.apache.roller.weblogger.business.FileManager:getFiles(...)@101 != null
* org.apache.roller.weblogger.business.Weblogger:getFileManager(...)@94 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@94 != null
* org.apache.roller.weblogger.ui.struts2.editor.Resources:getActionWeblog(...)@120 != null
* resources.length@101 <= 232-1
*
* Postconditions:
* return_value == &"list"
* this.files == One-of{&new ArrayList(execute#2), old this.files}
* init'ed(this.overQuota)
* possibly_updated(this.totalSize)
* new ArrayList(execute#2) num objects <= 1
*
* Test Vectors:
* this.path: Inverse{null}, Addr_Set{null}
*/
94 FileManager fmgr = WebloggerFactory.getWeblogger().getFileManager();
95
96 setOverQuota(fmgr.overQuota(getActionWeblog()));
97
98 try {
99 // get files, add them to the list
100 long totalSize = 0;
101 ThemeResource[] resources = fmgr.getFiles(getActionWeblog(), getPath());
102 log.debug(resources.length+" files found");
103 for (int i=0; i<resources.length; i++) {
+ 104 totalSize += resources[i].getLength();
105 }
106 List filesList = new ArrayList(Arrays.asList(resources));
107
108 if(getPath() == null) {
109 ThemeResource[] dirs = fmgr.getDirectories(getActionWeblog());
110 filesList.addAll(Arrays.asList(dirs));
111 }
112
113 // sort 'em
114 Collections.sort(filesList, new WeblogResourceComparator());
115
116 setFiles(filesList);
117 setTotalSize(totalSize);
118
119 } catch (Exception ex) {
120 log.error("Error getting files list for weblog - "+getActionWeblog().getHandle(), ex);
121 // TODO: i18n
122 addError("Error getting files list");
123 }
124
125 return LIST;
126 }
127
128
129 /**
130 * Create a subdirectory.
131 */
132 public String createSubdir() {
133
/*
P/P * Method: String createSubdir()
*
* Preconditions:
* log != null
* init'ed(this.newDir)
* init'ed(this.path)
*
* Presumptions:
* org.apache.roller.weblogger.business.Weblogger:getFileManager(...)@149 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@149 != null
*
* Postconditions:
* return_value == &"list"
* this.files == One-of{&new ArrayList(execute#2), old this.files}
* this.newDir == One-of{null, old this.newDir}
* init'ed(this.newDir)
* init'ed(this.overQuota)
* possibly_updated(this.totalSize)
* new ArrayList(execute#2) num objects <= 1
*
* Test Vectors:
* this.newDir: Addr_Set{null}, Inverse{null}
* this.path: Addr_Set{null}, Inverse{null}
* java.lang.String:indexOf(...)@136: {-231..-2, 0..232-1}, {-1}
* java.lang.String:length(...)@136: {0}, {1..232-1}
* java.lang.String:length(...)@144: {0}, {1..232-1}
*/
134 String path = getPath();
135 String newDir = getNewDir();
136 if(newDir != null &&
137 newDir.trim().length() > 0 &&
138 newDir.indexOf("/") == -1 &&
139 newDir.indexOf("\\") == -1 &&
140 newDir.indexOf("..") == -1) {
141
142 // figure the new directory path
143 String newDirPath = newDir;
144 if(path != null && path.trim().length() > 0) {
145 newDirPath = path + "/" + newDir;
146 }
147
148 try {
149 FileManager fmgr = WebloggerFactory.getWeblogger().getFileManager();
150
151 // add the new subdirectory
152 fmgr.createDirectory(getActionWeblog(), newDirPath);
153
154 addMessage("uploadFiles.createdDir", newDirPath);
155
156 // reset newDir prop so it doesn't autopopulate on the form
157 setNewDir(null);
158
159 } catch (FilePathException ex) {
160 addError("uploadFiles.error.badPath", newDirPath);
161 } catch (FileNotFoundException ex) {
162 addError("uploadFiles.error.badPath", newDirPath);
163 } catch (FileIOException ex) {
164 addError("uploadFiles.error.createDir", newDirPath);
165 }
166 } else {
167 addError("uploadFiles.error.badPath", newDir);
168 }
169
170 return execute();
171 }
172
173
174 /**
175 * Upload selected file(s).
176 */
177 public String upload() {
178
179 // make sure uploads are enabled
/*
P/P * Method: String upload()
*
* Preconditions:
* log != null
* (soft) init'ed(this.path)
* (soft) init'ed(this.uploadedFiles)
* (soft) this.uploadedFiles.length <= 232-1
* (soft) this.uploadedFilesContentType != null
* (soft) this.uploadedFilesContentType.length >= 1
* (soft) this.uploadedFiles.length <= this.uploadedFilesContentType.length
* (soft) init'ed(this.uploadedFilesContentType[...])
* (soft) this.uploadedFilesFileName != null
* (soft) this.uploadedFilesFileName.length >= 1
* ...
*
* Presumptions:
* org.apache.roller.weblogger.business.Weblogger:getFileManager(...)@185 != null
* org.apache.roller.weblogger.business.Weblogger:getUrlStrategy(...)@250 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@185 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@250 != null
*
* Postconditions:
* return_value == &"list"
* this.files == One-of{&new ArrayList(execute#2), old this.files}
* init'ed(this.overQuota)
* possibly_updated(this.totalSize)
* new ArrayList(execute#2) num objects <= 1
*
* Test Vectors:
* this.path: Addr_Set{null}, Inverse{null}
* this.uploadedFiles: Addr_Set{null}, Inverse{null}
* this.uploadedFiles.length: {0}, {1..232-1}
* this.uploadedFiles[...]: Addr_Set{null}, Inverse{null}
* java.lang.String:indexOf(...)@200: {-1}, {-231..-2, 0..232-1}
* java.lang.String:indexOf(...)@207: {-231..-2, 0..232-1}, {-1}
* java.lang.String:indexOf(...)@207: {-1}, {-231..-2, 0..232-1}
* java.lang.String:length(...)@215: {0}, {1..232-1}
* java.util.Iterator:hasNext(...)@249: {0}, {1}
* java.util.List:size(...)@246: {-231..0}, {1..232-1}
* ...
*/
180 if(!WebloggerRuntimeConfig.getBooleanProperty("uploads.enabled")) {
181 addError("error.upload.disabled");
182 return execute();
183 }
184
185 FileManager fmgr = WebloggerFactory.getWeblogger().getFileManager();
186
187 List<String> uploaded = new ArrayList();
188 File[] uploads = getUploadedFiles();
189 if (uploads != null && uploads.length > 0) {
190
191 // loop over uploaded files and try saving them
192 for (int i=0; i < uploads.length; i++) {
193
194 // skip null files
195 if (uploads[i] == null || !uploads[i].exists())
196 continue;
197
198 // figure file name and path
199 String fileName = getUploadedFilesFileName()[i];
200 int terminated = fileName.indexOf("\000");
201 if (terminated != -1) {
202 // disallow sneaky null terminated strings
203 fileName = fileName.substring(0, terminated).trim();
204 }
205
206 // make sure fileName is valid
207 if (fileName.indexOf("/") != -1 ||
208 fileName.indexOf("\\") != -1 ||
209 fileName.indexOf("..") != -1) {
210 addError("uploadFiles.error.badPath", fileName);
211 continue;
212 }
213
214 // add on the path element if needed
215 if(getPath() != null && getPath().trim().length() > 0) {
216 fileName = getPath() + "/" + fileName;
217 }
218
219
220 try {
221 fmgr.saveFile(getActionWeblog(),
222 fileName,
223 getUploadedFilesContentType()[i],
224 uploads[i].length(),
225 new FileInputStream(uploads[i]));
226
227 uploaded.add(fileName);
228
229 //destroy the temporary file created
230 uploads[i].delete();
231
232 } catch (FilePathException ex) {
233 addError("uploadFiles.error.badPath", fileName);
234 } catch (FileNotFoundException ex) {
235 addError("uploadFiles.error.badPath", fileName);
236 } catch (FileIOException ex) {
237 addError("uploadFiles.error.upload", fileName);
238 } catch (Exception ex) {
239 log.error("Error reading from uploaded file - "+uploads[i].getAbsolutePath(), ex);
240 // TODO: i18n
241 addError("Error reading uploaded file - "+getUploadedFilesFileName()[i]);
242 }
243 }
244 }
245
246 if(uploaded.size() > 0) {
247 addMessage("uploadFiles.uploadedFiles");
248
249 for(String upload : uploaded) {
250 addMessage("uploadFiles.uploadedFile",
251 WebloggerFactory.getWeblogger().getUrlStrategy().getWeblogResourceURL(getActionWeblog(), upload, true));
252 }
253 }
254
255 return execute();
256 }
257
258
259 /**
260 * Delete selected file(s).
261 */
262 public String remove() {
263
/*
P/P * Method: String remove()
*
* Preconditions:
* log != null
* init'ed(this.deleteIds)
* (soft) this.deleteIds.length <= 232-1
* (soft) this.deleteIds[...] != null
* (soft) init'ed(this.path)
*
* Presumptions:
* org.apache.roller.weblogger.business.Weblogger:getFileManager(...)@269 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@269 != null
*
* Postconditions:
* return_value == &"list"
* this.files == One-of{&new ArrayList(execute#2), old this.files}
* init'ed(this.overQuota)
* possibly_updated(this.totalSize)
* new ArrayList(execute#2) num objects <= 1
*
* Test Vectors:
* this.deleteIds: Addr_Set{null}, Inverse{null}
* java.lang.String:indexOf(...)@271: {-1}, {-231..-2, 0..232-1}
* java.lang.String:startsWith(...)@271: {1}, {0}
*/
264 int numDeleted = 0;
265 String[] deleteFiles = getDeleteIds();
266 if(deleteFiles != null) {
267 log.debug("Attempting to delete "+deleteFiles.length+" files");
268
269 FileManager fmgr = WebloggerFactory.getWeblogger().getFileManager();
270 for (int i=0; i < deleteFiles.length; i++) {
271 if (deleteFiles[i].trim().startsWith("/") ||
272 deleteFiles[i].trim().startsWith("\\") ||
273 deleteFiles[i].indexOf("..") != -1) {
274 // ignore absolute paths, or paths that contiain '..'
275 log.debug("Ignoring delete path - "+deleteFiles[i]);
276 } else {
277 try {
278 log.debug("Deleting file at path - "+deleteFiles[i]);
279 fmgr.deleteFile(getActionWeblog(), deleteFiles[i]);
280 numDeleted++;
281 } catch (FileNotFoundException ex) {
282 addError("uploadFiles.error.badPath");
283 } catch (FilePathException ex) {
284 addError("uploadFiles.error.badPath");
285 } catch (FileIOException ex) {
286 addError("uploadFiles.error.delete", deleteFiles[i]);
287 }
288
289 }
290 }
291
292 if(numDeleted > 0) {
293 addMessage("uploadFiles.deletedFiles", ""+numDeleted);
294 }
295 }
296
297 return execute();
298 }
299
300
301 public String getResourceURL(String path) {
/*
P/P * Method: String getResourceURL(String)
*
* Presumptions:
* org.apache.roller.weblogger.business.Weblogger:getUrlStrategy(...)@302 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@302 != null
*
* Postconditions:
* init'ed(return_value)
*/
302 return WebloggerFactory.getWeblogger().getUrlStrategy().getWeblogResourceURL(getActionWeblog(), path, false);
303 }
304
305
306 public String getPath() {
/*
P/P * Method: String getPath()
*
* Preconditions:
* init'ed(this.path)
*
* Postconditions:
* return_value == this.path
* init'ed(return_value)
*/
307 return path;
308 }
309
310 public void setPath(String path) {
/*
P/P * Method: void setPath(String)
*
* Postconditions:
* this.path == One-of{old this.path, path}
*
* Test Vectors:
* path: Addr_Set{null}, Inverse{null}
* java.lang.String:equals(...)@311: {1}, {0}
* java.lang.String:length(...)@311: {0}, {1..232-1}
*/
311 if(path != null &&
312 path.trim().length() != 0 &&
313 !path.trim().equals("/")) {
314 this.path = path;
315 }
316 }
317
318 public String getNewDir() {
/*
P/P * Method: String getNewDir()
*
* Preconditions:
* init'ed(this.newDir)
*
* Postconditions:
* return_value == this.newDir
* init'ed(return_value)
*/
319 return newDir;
320 }
321
322 public void setNewDir(String newDir) {
/*
P/P * Method: void setNewDir(String)
*
* Postconditions:
* this.newDir == newDir
* init'ed(this.newDir)
*/
323 this.newDir = newDir;
324 }
325
326 public String[] getDeleteIds() {
/*
P/P * Method: String[] getDeleteIds()
*
* Preconditions:
* init'ed(this.deleteIds)
*
* Postconditions:
* return_value == this.deleteIds
* init'ed(return_value)
*/
327 return deleteIds;
328 }
329
330 public void setDeleteIds(String[] deleteIds) {
/*
P/P * Method: void setDeleteIds(String[])
*
* Postconditions:
* this.deleteIds == deleteIds
* init'ed(this.deleteIds)
*/
331 this.deleteIds = deleteIds;
332 }
333
334 public List getFiles() {
/*
P/P * Method: List getFiles()
*
* Preconditions:
* init'ed(this.files)
*
* Postconditions:
* return_value == this.files
* init'ed(return_value)
*/
335 return files;
336 }
337
338 public void setFiles(List files) {
/*
P/P * Method: void setFiles(List)
*
* Postconditions:
* this.files == files
* init'ed(this.files)
*/
339 this.files = files;
340 }
341
342 public boolean isOverQuota() {
/*
P/P * Method: bool isOverQuota()
*
* Preconditions:
* init'ed(this.overQuota)
*
* Postconditions:
* return_value == this.overQuota
* init'ed(return_value)
*/
343 return overQuota;
344 }
345
346 public void setOverQuota(boolean overQuota) {
/*
P/P * Method: void setOverQuota(bool)
*
* Postconditions:
* this.overQuota == overQuota
* init'ed(this.overQuota)
*/
347 this.overQuota = overQuota;
348 }
349
350 public long getTotalSize() {
/*
P/P * Method: long getTotalSize()
*
* Preconditions:
* init'ed(this.totalSize)
*
* Postconditions:
* return_value == this.totalSize
* init'ed(return_value)
*/
351 return totalSize;
352 }
353
354 public void setTotalSize(long totalSize) {
/*
P/P * Method: void setTotalSize(long)
*
* Postconditions:
* this.totalSize == totalSize
* init'ed(this.totalSize)
*/
355 this.totalSize = totalSize;
356 }
357
358 public File[] getUploadedFiles() {
/*
P/P * Method: File[] getUploadedFiles()
*
* Preconditions:
* init'ed(this.uploadedFiles)
*
* Postconditions:
* return_value == this.uploadedFiles
* init'ed(return_value)
*/
359 return uploadedFiles;
360 }
361
362 public void setUploadedFiles(File[] uploadedFiles) {
/*
P/P * Method: void setUploadedFiles(File[])
*
* Postconditions:
* this.uploadedFiles == uploadedFiles
* init'ed(this.uploadedFiles)
*/
363 this.uploadedFiles = uploadedFiles;
364 }
365
366 public String[] getUploadedFilesContentType() {
/*
P/P * Method: String[] getUploadedFilesContentType()
*
* Preconditions:
* init'ed(this.uploadedFilesContentType)
*
* Postconditions:
* return_value == this.uploadedFilesContentType
* init'ed(return_value)
*/
367 return uploadedFilesContentType;
368 }
369
370 public void setUploadedFilesContentType(String[] uploadedFilesContentType) {
/*
P/P * Method: void setUploadedFilesContentType(String[])
*
* Postconditions:
* this.uploadedFilesContentType == uploadedFilesContentType
* init'ed(this.uploadedFilesContentType)
*/
371 this.uploadedFilesContentType = uploadedFilesContentType;
372 }
373
374 public String[] getUploadedFilesFileName() {
/*
P/P * Method: String[] getUploadedFilesFileName()
*
* Preconditions:
* init'ed(this.uploadedFilesFileName)
*
* Postconditions:
* return_value == this.uploadedFilesFileName
* init'ed(return_value)
*/
375 return uploadedFilesFileName;
376 }
377
378 public void setUploadedFilesFileName(String[] uploadedFilesFileName) {
/*
P/P * Method: void setUploadedFilesFileName(String[])
*
* Postconditions:
* this.uploadedFilesFileName == uploadedFilesFileName
* init'ed(this.uploadedFilesFileName)
*/
379 this.uploadedFilesFileName = uploadedFilesFileName;
380 }
381
382
/*
P/P * Method: void org.apache.roller.weblogger.ui.struts2.editor.Resources$WeblogResourceComparator(Resources)
*/
383 public class WeblogResourceComparator implements Comparator {
384 public int compare(Object o1, Object o2) {
/*
P/P * Method: int compare(Object, Object)
*
* Preconditions:
* o1 != null
* (soft) o2 != null
*
* Presumptions:
* org.apache.roller.weblogger.pojos.ThemeResource:getPath(...)@391 != null
* org.apache.roller.weblogger.pojos.ThemeResource:getPath(...)@400 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* org.apache.roller.weblogger.pojos.ThemeResource:isDirectory(...)@389: {0}, {1}
* org.apache.roller.weblogger.pojos.ThemeResource:isDirectory(...)@392: {0}, {1}
* org.apache.roller.weblogger.pojos.ThemeResource:isDirectory(...)@395: {0}, {1}
*/
385 ThemeResource r1 = (ThemeResource)o1;
386 ThemeResource r2 = (ThemeResource)o2;
387
388 // consider directories so they go to the top of the list
389 if(r1.isDirectory() && r2.isDirectory()) {
390 // if we have 2 directories then just go by name
391 return r1.getPath().compareTo(r2.getPath());
392 } else if(r1.isDirectory()) {
393 // directories go before files
394 return -1;
395 } else if(r2.isDirectory()) {
396 // directories go before files
397 return 1;
398 } else {
399 // if we have 2 files then just go by name
400 return r1.getPath().compareTo(r2.getPath());
401 }
402 }
403 public boolean equals(Object o1, Object o2) {
/*
P/P * Method: bool equals(Object, Object)
*
* Preconditions:
* o1 != null
* o2 != null
*
* Presumptions:
* org.apache.roller.weblogger.pojos.ThemeResource:getPath(...)@414 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* org.apache.roller.weblogger.pojos.ThemeResource:isDirectory(...)@408: {0}, {1}
* org.apache.roller.weblogger.pojos.ThemeResource:isFile(...)@408: {0}, {1}
* org.apache.roller.weblogger.pojos.ThemeResource:isFile(...)@408: {1}, {0}
*/
404 ThemeResource r1 = (ThemeResource)o1;
405 ThemeResource r2 = (ThemeResource)o2;
406
407 // need to be same type to be equals, i.e both files or directories
408 if((r1.isDirectory() && !r2.isDirectory()) ||
409 (r1.isFile() && !r2.isFile())) {
410 return false;
411 }
412
413 // after that it's just a matter of comparing paths
414 return r1.getPath().equals(r2.getPath());
415 }
416 }
417
418 }
SofCheck Inspector Build Version : 2.18479
| Resources.java |
2009-Jan-02 14:25:08 |
| Resources.class |
2009-Sep-04 03:12:45 |
| Resources$WeblogResourceComparator.class |
2009-Sep-04 03:12:45 |