Subversion Repositories XServices

Rev

Rev 94 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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