Subversion Repositories XServices

Rev

Rev 92 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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