2012-08-12 09:59:47 +00:00
|
|
|
package net.brutex.xservices.ws.rs;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileFilter;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import javax.ws.rs.core.GenericEntity;
|
|
|
|
import javax.ws.rs.core.HttpHeaders;
|
|
|
|
import javax.ws.rs.core.Response;
|
2012-08-26 14:41:46 +00:00
|
|
|
import net.brutex.xservices.security.StandardSecurityManager;
|
|
|
|
import net.brutex.xservices.security.UserIdentity;
|
2012-08-12 09:59:47 +00:00
|
|
|
import net.brutex.xservices.types.FileInfoType;
|
|
|
|
|
2013-02-05 14:34:01 +00:00
|
|
|
import org.apache.jcs.JCS;
|
|
|
|
import org.apache.jcs.access.exception.CacheException;
|
|
|
|
|
2012-08-12 09:59:47 +00:00
|
|
|
/**
|
2013-02-05 14:34:01 +00:00
|
|
|
* @author Brian Rosenberger, bru(at)brutex.de
|
2012-08-12 09:59:47 +00:00
|
|
|
*
|
|
|
|
*/
|
2013-02-05 14:34:01 +00:00
|
|
|
public class FileInfoImpl implements FileInfo
|
|
|
|
{
|
|
|
|
public Response getFiles(HttpHeaders h, String dir, boolean withDir, boolean withFiles, int level, String search, int count, int page)
|
|
|
|
{
|
|
|
|
StandardSecurityManager sec = new StandardSecurityManager();
|
|
|
|
UserIdentity id = new UserIdentity();
|
|
|
|
|
|
|
|
if (!sec.canExecute(java.lang.Thread.currentThread().getStackTrace()[1].getMethodName(), id)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
System.out.println("Listing directory: " + dir);
|
|
|
|
if (level <= 0) level = 1;
|
|
|
|
if (level > 3) level = 3;
|
|
|
|
if ((!withDir) && (!withFiles)) withFiles = true;
|
|
|
|
String cachekey = level + "||" + withFiles + "||" + withDir + "||" + search + "||" + dir;
|
|
|
|
try {
|
|
|
|
JCS jcs = JCS.getInstance("FileCache");
|
2012-08-12 09:59:47 +00:00
|
|
|
|
2013-02-05 14:34:01 +00:00
|
|
|
List list = (List)jcs.get(cachekey);
|
|
|
|
if (list == null) {
|
|
|
|
list = setDirectory(dir, withDir, withFiles, level, search);
|
|
|
|
jcs.put(cachekey, list);
|
|
|
|
System.out.println("Stored in Cache: " + list.toString());
|
|
|
|
} else {
|
|
|
|
System.out.println("Got from Cache: " + list.toString());
|
|
|
|
}
|
2012-08-26 14:41:46 +00:00
|
|
|
|
2013-02-05 14:34:01 +00:00
|
|
|
int fromIndex = 0;
|
|
|
|
int toIndex = 0;
|
|
|
|
fromIndex = (page - 1) * count;
|
|
|
|
toIndex = page * count;
|
|
|
|
if (toIndex > list.size()) toIndex = list.size();
|
|
|
|
if (fromIndex > toIndex) fromIndex = toIndex;
|
|
|
|
GenericEntity sublist = new GenericEntity(list.subList(fromIndex, toIndex))
|
|
|
|
{
|
|
|
|
};
|
|
|
|
return Response.ok(sublist).build();
|
|
|
|
} catch (CacheException e) {
|
|
|
|
Response.serverError().build();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2012-08-26 14:41:46 +00:00
|
|
|
|
2013-02-05 14:34:01 +00:00
|
|
|
private void setDirectory(List<FileInfoType> list, File dir, boolean withDirectories, boolean withFiles, final int depth, final String search)
|
|
|
|
{
|
|
|
|
if (depth <= 0) return;
|
2012-08-12 09:59:47 +00:00
|
|
|
|
2013-02-05 14:34:01 +00:00
|
|
|
File[] files = dir.listFiles(new FileFilter()
|
|
|
|
{
|
|
|
|
public boolean accept(File pathname) {
|
|
|
|
if ((pathname.isDirectory()) && (depth > 1)) return true;
|
|
|
|
if ((search == null) || (search.equals(""))) return true;
|
|
|
|
if (!pathname.getAbsolutePath().contains(search)) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if ((dir.getParentFile() != null) && (withDirectories)) list.add(new FileInfoType(dir.getParentFile()));
|
|
|
|
if (files == null) return;
|
2012-08-12 09:59:47 +00:00
|
|
|
|
2013-02-05 14:34:01 +00:00
|
|
|
for (File e : files) {
|
|
|
|
if (e.isDirectory()) setDirectory(list, e, withDirectories, withFiles, depth - 1, search);
|
|
|
|
if (((withDirectories) && (e.isDirectory())) || (
|
|
|
|
(withFiles) && (e.isFile())))
|
|
|
|
list.add(new FileInfoType(e));
|
|
|
|
}
|
|
|
|
}
|
2012-08-12 09:59:47 +00:00
|
|
|
|
2013-02-05 14:34:01 +00:00
|
|
|
private List<FileInfoType> setDirectory(String dir, boolean withDirectories, boolean withFiles, int depth, String search)
|
|
|
|
{
|
|
|
|
List list = new ArrayList();
|
|
|
|
setDirectory(list, new File(dir), withDirectories, withFiles, depth, search);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
}
|