92 |
brianR |
1 |
package net.brutex.xservices.ws.rs;
|
|
|
2 |
|
|
|
3 |
import java.io.File;
|
|
|
4 |
import java.io.FileFilter;
|
94 |
brianR |
5 |
import java.lang.reflect.Method;
|
92 |
brianR |
6 |
import java.util.ArrayList;
|
|
|
7 |
import java.util.List;
|
|
|
8 |
|
|
|
9 |
import javax.ws.rs.core.GenericEntity;
|
|
|
10 |
import javax.ws.rs.core.HttpHeaders;
|
|
|
11 |
import javax.ws.rs.core.Response;
|
|
|
12 |
|
|
|
13 |
import org.apache.jcs.JCS;
|
|
|
14 |
import org.apache.jcs.access.exception.CacheException;
|
|
|
15 |
|
94 |
brianR |
16 |
import net.brutex.xservices.security.StandardSecurityManager;
|
|
|
17 |
import net.brutex.xservices.security.UserIdentity;
|
92 |
brianR |
18 |
import net.brutex.xservices.types.FileInfoType;
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* @author Brian Rosenberger
|
|
|
22 |
*
|
|
|
23 |
*/
|
|
|
24 |
public class FileInfoImpl implements FileInfo {
|
|
|
25 |
|
|
|
26 |
public Response getFiles(HttpHeaders h, String dir, boolean withDir,
|
|
|
27 |
boolean withFiles, int level, String search, int count, int page) {
|
|
|
28 |
|
94 |
brianR |
29 |
StandardSecurityManager sec = new StandardSecurityManager();
|
|
|
30 |
UserIdentity id = new UserIdentity();
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
if( ! sec.canExecute(Thread.currentThread().getStackTrace()[1].getMethodName(), id)) {
|
|
|
34 |
return null;
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
|
92 |
brianR |
38 |
System.out.println("Listing directory: " + dir);
|
|
|
39 |
if(level <= 0) level = 1;
|
|
|
40 |
if(level > 3) level = 3;
|
|
|
41 |
if(!withDir && !withFiles) withFiles = true;
|
|
|
42 |
String cachekey = level +"||"+ withFiles +"||"+ withDir +"||" + search + "||" + dir;
|
|
|
43 |
try {
|
|
|
44 |
JCS jcs = JCS.getInstance("FileCache");
|
|
|
45 |
|
|
|
46 |
List<FileInfoType> list = (List<FileInfoType>) jcs.get(cachekey);
|
|
|
47 |
if(list == null) {
|
|
|
48 |
list = setDirectory(dir, withDir, withFiles, level, search);
|
|
|
49 |
jcs.put(cachekey, list);
|
|
|
50 |
System.out.println("Stored in Cache: " + list.toString());
|
|
|
51 |
} else {
|
|
|
52 |
System.out.println("Got from Cache: " + list.toString());
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
int fromIndex = 0;
|
|
|
57 |
int toIndex = 0;
|
|
|
58 |
fromIndex = (page-1) * count;
|
|
|
59 |
toIndex = (page*count);
|
|
|
60 |
if(toIndex>list.size()) toIndex = list.size();
|
|
|
61 |
if(fromIndex>toIndex) fromIndex=toIndex;
|
|
|
62 |
GenericEntity<List<FileInfoType>> sublist = new GenericEntity<List<FileInfoType>>(list.subList(fromIndex, toIndex) ){};
|
|
|
63 |
return Response.ok( sublist ).build();
|
|
|
64 |
} catch (CacheException e) {
|
|
|
65 |
Response.serverError().build();
|
|
|
66 |
}
|
|
|
67 |
return null;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
private void setDirectory(List<FileInfoType> list, File dir, final boolean withDirectories, final boolean withFiles, final int depth, final String search) {
|
|
|
73 |
if(depth <=0) return;
|
|
|
74 |
|
|
|
75 |
File[] files = dir.listFiles(new FileFilter() {
|
|
|
76 |
|
|
|
77 |
public boolean accept(File pathname) {
|
|
|
78 |
if(pathname.isDirectory() && depth > 1) return true;
|
|
|
79 |
if(search == null || search.equals("")) return true;
|
|
|
80 |
if(!pathname.getAbsolutePath().contains(search)) return false;
|
|
|
81 |
return true;
|
|
|
82 |
}
|
|
|
83 |
});
|
|
|
84 |
if(dir.getParentFile() != null && withDirectories==true) list.add(new FileInfoType(dir.getParentFile()));
|
|
|
85 |
if(files==null) return;
|
|
|
86 |
|
|
|
87 |
for( File e : files) {
|
|
|
88 |
if(e.isDirectory()) setDirectory(list, e, withDirectories, withFiles, depth-1, search);
|
|
|
89 |
if( (withDirectories && e.isDirectory())
|
|
|
90 |
|| (withFiles && e.isFile()) ) {
|
|
|
91 |
list.add(new FileInfoType(e));
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
private List<FileInfoType> setDirectory(String dir, final boolean withDirectories, final boolean withFiles, int depth, String search) {
|
|
|
97 |
List<FileInfoType> list = new ArrayList<FileInfoType>();
|
|
|
98 |
setDirectory( list, (new File(dir)), withDirectories, withFiles, depth, search);
|
|
|
99 |
return list;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
|
|
|
103 |
|
|
|
104 |
}
|