Subversion Repositories XServices

Rev

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