Subversion Repositories XServices

Compare Revisions

No changes between revisions

Ignore whitespace Rev 91 → Rev 92

/xservices/trunk/src/java/net/brutex/xservices/ws/rs/FileInfo.java
0,0 → 1,63
/*
* Copyright 2012 Brian Rosenberger (Brutex Network)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
package net.brutex.xservices.ws.rs;
 
import java.util.List;
 
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
 
import net.brutex.xservices.types.FileInfoType;
 
 
 
@Path("/FileService/")
@Produces ("application/xml")
public interface FileInfo {
 
/**
* @param dir
* @param withDir
* @param withFiles
* @param depth
* @param search
* @param count
* @param page
* @return List of File
*/
@GET
@Path("getFiles/")
public Response getFiles(@Context HttpHeaders h,
@QueryParam("directory") String dir,
@QueryParam("includeDirectories") @DefaultValue("0") boolean withDir,
@QueryParam("includeFiles") @DefaultValue("1") boolean withFiles,
@QueryParam("depth") @DefaultValue("1") int depth,
@QueryParam("search") String search,
@QueryParam("itemsPerPage") @DefaultValue("50") int count,
@QueryParam("page") @DefaultValue("1") int page);
 
}
 
 
 
Property changes:
Added: svn:mime-type
+text/plain
\ No newline at end of property
/xservices/trunk/src/java/net/brutex/xservices/ws/rs/FileInfoImpl.java
0,0 → 1,92
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;
 
import org.apache.jcs.JCS;
import org.apache.jcs.access.exception.CacheException;
 
import net.brutex.xservices.types.FileInfoType;
 
/**
* @author Brian Rosenberger
*
*/
public class FileInfoImpl implements FileInfo {
 
public Response getFiles(HttpHeaders h, String dir, boolean withDir,
boolean withFiles, int level, String search, int count, int page) {
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");
List<FileInfoType> list = (List<FileInfoType>) 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());
}
 
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<List<FileInfoType>> sublist = new GenericEntity<List<FileInfoType>>(list.subList(fromIndex, toIndex) ){};
return Response.ok( sublist ).build();
} catch (CacheException e) {
Response.serverError().build();
}
return null;
}
 
 
private void setDirectory(List<FileInfoType> list, File dir, final boolean withDirectories, final boolean withFiles, final int depth, final String search) {
if(depth <=0) return;
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==true) list.add(new FileInfoType(dir.getParentFile()));
if(files==null) return;
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));
}
}
}
private List<FileInfoType> setDirectory(String dir, final boolean withDirectories, final boolean withFiles, int depth, String search) {
List<FileInfoType> list = new ArrayList<FileInfoType>();
setDirectory( list, (new File(dir)), withDirectories, withFiles, depth, search);
return list;
}
}
Property changes:
Added: svn:mime-type
+text/plain
\ No newline at end of property
/xservices/trunk/src/java/net/brutex/xservices/ws/rs/FileListType.java
0,0 → 1,18
package net.brutex.xservices.ws.rs;
 
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
 
@XmlRootElement(name="FileList")
public class FileListType {
 
@XmlElement
public String name;
public FileListType(){};
public FileListType(String name) {
this.name = name;
}
 
}
Property changes:
Added: svn:mime-type
+text/plain
\ No newline at end of property