Subversion Repositories XServices

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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