Subversion Repositories XServices

Rev

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

Rev 94 Rev 109
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;
-
 
6
import java.util.ArrayList;
5
import java.util.ArrayList;
7
import java.util.List;
6
import java.util.List;
8
 
-
 
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;
9
import javax.ws.rs.core.Response;
12
 
-
 
13
import org.apache.jcs.JCS;
-
 
14
import org.apache.jcs.access.exception.CacheException;
-
 
15
 
-
 
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;
18
import net.brutex.xservices.types.FileInfoType;
12
import net.brutex.xservices.types.FileInfoType;
-
 
13
 
-
 
14
import org.apache.jcs.JCS;
-
 
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
 
22
{
26
	public Response getFiles(HttpHeaders h, String dir, boolean withDir,
-
 
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();
25
    StandardSecurityManager sec = new StandardSecurityManager();
30
		UserIdentity id = new UserIdentity();
26
    UserIdentity id = new UserIdentity();
31
 
-
 
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;
29
      return null;
35
		}
30
    }
36
 
-
 
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());
46
        System.out.println("Got from Cache: " + list.toString());
53
		}
47
      }
54
 
-
 
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();
61
		if(fromIndex>toIndex) fromIndex=toIndex;
54
      if (fromIndex > toIndex) fromIndex = toIndex;
62
		GenericEntity<List<FileInfoType>> sublist = new GenericEntity<List<FileInfoType>>(list.subList(fromIndex, toIndex) ){};
55
      GenericEntity sublist = new GenericEntity(list.subList(fromIndex, toIndex))
-
 
56
      {
-
 
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;
62
    return null;
68
	}	
63
  }
69
 
-
 
70
 
-
 
71
	
64
 
-
 
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
			}	
86
    }
93
		}
87
  }
94
	}
-
 
95
	
88
 
-
 
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;
93
    return list;
100
	}
-
 
101
	
-
 
102
	
-
 
103
	
94
  }
104
}
95
}
-
 
96
 
-
 
97
Generated by GNU Enscript 1.6.5.90.
-
 
98
 
-
 
99