2013-12-20 07:46:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 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.
|
|
|
|
*/
|
|
|
|
|
2012-08-12 09:59:47 +00:00
|
|
|
package net.brutex.xservices.ws.rs;
|
|
|
|
|
|
|
|
import java.io.File;
|
2013-12-20 07:46:30 +00:00
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.net.URI;
|
|
|
|
import java.nio.file.DirectoryStream;
|
|
|
|
import java.nio.file.FileSystems;
|
|
|
|
import java.nio.file.FileVisitOption;
|
|
|
|
import java.nio.file.FileVisitResult;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.PathMatcher;
|
|
|
|
import java.nio.file.SimpleFileVisitor;
|
|
|
|
import java.nio.file.attribute.BasicFileAttributeView;
|
|
|
|
import java.nio.file.attribute.BasicFileAttributes;
|
2012-08-12 09:59:47 +00:00
|
|
|
import java.util.ArrayList;
|
2013-12-20 07:46:30 +00:00
|
|
|
import java.util.EnumSet;
|
2012-08-12 09:59:47 +00:00
|
|
|
import java.util.List;
|
2013-12-20 07:46:30 +00:00
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
|
|
|
|
import javax.ws.rs.NotAuthorizedException;
|
|
|
|
import javax.ws.rs.WebApplicationException;
|
2012-08-12 09:59:47 +00:00
|
|
|
import javax.ws.rs.core.GenericEntity;
|
|
|
|
import javax.ws.rs.core.HttpHeaders;
|
2013-12-20 07:46:30 +00:00
|
|
|
import javax.ws.rs.core.MediaType;
|
2012-08-12 09:59:47 +00:00
|
|
|
import javax.ws.rs.core.Response;
|
2013-12-20 07:46:30 +00:00
|
|
|
import javax.ws.rs.core.StreamingOutput;
|
|
|
|
import javax.ws.rs.core.UriInfo;
|
|
|
|
|
|
|
|
import net.brutex.xservices.security.DirectoryPermission;
|
2012-08-12 09:59:47 +00:00
|
|
|
import net.brutex.xservices.types.FileInfoType;
|
2013-12-20 07:46:30 +00:00
|
|
|
import net.brutex.xservices.util.FileWalker;
|
2012-08-12 09:59:47 +00:00
|
|
|
|
2018-05-03 05:24:04 +00:00
|
|
|
import org.apache.commons.jcs.JCS;
|
|
|
|
import org.apache.commons.jcs.access.CacheAccess;
|
|
|
|
import org.apache.commons.jcs.access.exception.CacheException;
|
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2013-12-20 07:46:30 +00:00
|
|
|
import org.apache.shiro.SecurityUtils;
|
|
|
|
import org.apache.shiro.authz.UnauthorizedException;
|
2013-02-05 14:34:01 +00:00
|
|
|
|
2012-08-12 09:59:47 +00:00
|
|
|
/**
|
2013-12-20 07:46:30 +00:00
|
|
|
* The Class FileInfoImpl.
|
2012-08-12 09:59:47 +00:00
|
|
|
*
|
2013-12-20 07:46:30 +00:00
|
|
|
* @author Brian Rosenberger, bru(at)brutex.de
|
2012-08-12 09:59:47 +00:00
|
|
|
*/
|
2013-12-20 07:46:30 +00:00
|
|
|
public class FileInfoImpl implements FileInfo {
|
|
|
|
|
|
|
|
|
2018-05-03 05:24:04 +00:00
|
|
|
Logger logger = LogManager.getLogger();
|
2013-12-20 07:46:30 +00:00
|
|
|
|
2013-02-05 14:34:01 +00:00
|
|
|
|
2013-12-20 07:46:30 +00:00
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see net.brutex.xservices.ws.rs.FileInfo#getFiles(javax.ws.rs.core.HttpHeaders, java.lang.String, boolean, boolean, int, java.lang.String, int, int)
|
|
|
|
*/
|
|
|
|
public Response getFiles(HttpHeaders h, UriInfo uriInfo, String dir, boolean withDir, boolean withFiles, int level, String search, int count, int page, boolean useCache)
|
|
|
|
{
|
2015-08-24 07:58:52 +00:00
|
|
|
if(dir==null) {
|
|
|
|
dir = "c:/";
|
|
|
|
logger.warn("No directory specified. Default is 'c:/'.");
|
|
|
|
}
|
2013-12-20 07:46:30 +00:00
|
|
|
isPermitted(dir);
|
|
|
|
|
|
|
|
URI baseuri = URI.create(uriInfo.getBaseUri()+FileInfo.BASE_PATH+"getFile?file=");
|
|
|
|
|
|
|
|
logger.info(String.format("Listing directory '%s'.", dir));
|
2013-02-05 14:34:01 +00:00
|
|
|
if (level <= 0) level = 1;
|
2013-12-20 07:46:30 +00:00
|
|
|
|
2013-02-05 14:34:01 +00:00
|
|
|
if ((!withDir) && (!withFiles)) withFiles = true;
|
|
|
|
String cachekey = level + "||" + withFiles + "||" + withDir + "||" + search + "||" + dir;
|
|
|
|
try {
|
2013-12-20 07:46:30 +00:00
|
|
|
logger.debug(String.format("Hitting cache with cachekey '%s'", cachekey));
|
2018-05-03 05:24:04 +00:00
|
|
|
CacheAccess<Object, Object> jcs = JCS.getInstance("FileCache");
|
2012-08-12 09:59:47 +00:00
|
|
|
|
2013-12-20 07:46:30 +00:00
|
|
|
/*Try to retrieve the file list from the cache*/
|
|
|
|
List<FileInfoType> list = (List<FileInfoType>)jcs.get(cachekey);
|
|
|
|
|
|
|
|
if (list == null || !useCache) {
|
|
|
|
list = setDirectory(baseuri, dir, withDir, withFiles, level, search);
|
2013-02-05 14:34:01 +00:00
|
|
|
jcs.put(cachekey, list);
|
2013-12-20 07:46:30 +00:00
|
|
|
logger.debug("Stored in Cache: " + list.toString());
|
2013-02-05 14:34:01 +00:00
|
|
|
} else {
|
2013-12-20 07:46:30 +00:00
|
|
|
logger.debug("Got from Cache: " + list.toString());
|
2013-02-05 14:34:01 +00:00
|
|
|
}
|
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;
|
2013-12-20 07:46:30 +00:00
|
|
|
GenericEntity<List<FileInfoType>> sublist = new GenericEntity<List<FileInfoType>>(list.subList(fromIndex, toIndex)) {};
|
|
|
|
logger.info(String.format("Returning items %s to %s from total of %s items in the list.", fromIndex, toIndex, list.size()));
|
2013-02-05 14:34:01 +00:00
|
|
|
return Response.ok(sublist).build();
|
|
|
|
} catch (CacheException e) {
|
2015-08-24 07:58:52 +00:00
|
|
|
return Response.serverError().build();
|
2013-02-05 14:34:01 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-26 14:41:46 +00:00
|
|
|
|
2013-12-20 07:46:30 +00:00
|
|
|
/**
|
|
|
|
* Sets the directory.
|
|
|
|
*
|
|
|
|
* @param list the list
|
|
|
|
* @param dir the dir
|
|
|
|
* @param withDirectories the with directories
|
|
|
|
* @param withFiles the with files
|
|
|
|
* @param depth the depth
|
|
|
|
* @param search the search
|
|
|
|
*/
|
|
|
|
private void setDirectory(final URI baseuri, final List<FileInfoType> list, File dir, boolean withDirectories, boolean withFiles, final int depth, String search)
|
2013-02-05 14:34:01 +00:00
|
|
|
{
|
|
|
|
if (depth <= 0) return;
|
2013-12-20 07:46:30 +00:00
|
|
|
|
|
|
|
if(search==null || search.equals("") ) {
|
|
|
|
search = "*";
|
|
|
|
logger.info("No search pattern supplied, using default '*'.");
|
|
|
|
}
|
|
|
|
|
|
|
|
FileWalker finder = new FileWalker(search);
|
|
|
|
try {
|
|
|
|
Files.walkFileTree(dir.toPath(), EnumSet.of(FileVisitOption.FOLLOW_LINKS), depth, finder);
|
|
|
|
logger.info("FileWalker returned '"+finder.getCount()+"' hits. '" + finder.getTotal() + "' files have been scanned.");
|
|
|
|
List<Path> result = finder.getResult();
|
|
|
|
for(Path f : result) {
|
2014-05-20 10:18:19 +00:00
|
|
|
if(! withDirectories) {
|
|
|
|
if(f.toFile().isDirectory()) continue;
|
|
|
|
}
|
|
|
|
if(! withFiles) {
|
|
|
|
if(f.toFile().isFile()) continue;
|
|
|
|
}
|
2013-12-20 07:46:30 +00:00
|
|
|
list.add(new FileInfoType(f, baseuri));
|
|
|
|
}
|
|
|
|
} catch (IOException e2) {
|
|
|
|
logger.error(e2.getMessage(), e2);;
|
|
|
|
}
|
2013-02-05 14:34:01 +00:00
|
|
|
}
|
2013-12-20 07:46:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the directory.
|
|
|
|
*
|
|
|
|
* @param dir the dir
|
|
|
|
* @param withDirectories the with directories
|
|
|
|
* @param withFiles the with files
|
|
|
|
* @param depth the depth
|
|
|
|
* @param search the search
|
|
|
|
* @return the list
|
|
|
|
*/
|
|
|
|
private List<FileInfoType> setDirectory(URI baseuri, String dir, boolean withDirectories, boolean withFiles, int depth, String search)
|
2013-02-05 14:34:01 +00:00
|
|
|
{
|
2013-12-20 07:46:30 +00:00
|
|
|
List<FileInfoType> list = new ArrayList<FileInfoType>();
|
|
|
|
setDirectory(baseuri, list, new File(dir), withDirectories, withFiles, depth, search);
|
2013-02-05 14:34:01 +00:00
|
|
|
return list;
|
|
|
|
}
|
2013-12-20 07:46:30 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Response getFile(HttpHeaders paramHttpHeaders, String file) {
|
|
|
|
isPermitted(file);
|
|
|
|
try {
|
|
|
|
Path path = FileSystems.getDefault().getPath(file);
|
|
|
|
|
|
|
|
BasicFileAttributeView basicView = Files.getFileAttributeView(path, BasicFileAttributeView.class);
|
|
|
|
BasicFileAttributes basic;
|
|
|
|
basic = basicView.readAttributes();
|
|
|
|
|
|
|
|
|
|
|
|
//In case this is a directory
|
|
|
|
//we zip it and return the zip stream
|
|
|
|
if(basic.isDirectory()) return getDirectoryAsZip(path);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MediaType mime = MediaType.APPLICATION_OCTET_STREAM_TYPE;
|
|
|
|
try {
|
|
|
|
mime = MediaType.valueOf(Files.probeContentType(path));
|
|
|
|
} catch (IllegalArgumentException | IOException e) {
|
|
|
|
//In case we can not find the media type for some reason
|
|
|
|
//the default assignment is taken, so we can
|
|
|
|
//ignore this error.
|
|
|
|
logger.debug(String.format("Could not probe media type for file '%s'. Default is '%s'", path.toString(), mime.getType()), e);
|
|
|
|
}
|
|
|
|
Response r = Response.ok(path.toFile(), mime).build();
|
|
|
|
String fileName = path.getFileName().toString();
|
|
|
|
if(mime == MediaType.APPLICATION_OCTET_STREAM_TYPE) r.getHeaders().add("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
|
|
|
|
return r;
|
|
|
|
} catch (IOException e1) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
logger.error(e1.getMessage(), e1);
|
|
|
|
return Response.serverError().build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Response getDirectoryAsZip(final Path path) {
|
|
|
|
|
|
|
|
StreamingOutput output = new StreamingOutput() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void write(OutputStream os) throws IOException,
|
|
|
|
WebApplicationException {
|
|
|
|
ZipOutputStream zos = new ZipOutputStream(os);
|
|
|
|
|
|
|
|
//read directory content (files only)
|
|
|
|
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
|
|
|
|
for (Path file: stream) {
|
|
|
|
//skip anything not being a file
|
|
|
|
if(! file.toFile().isFile()) continue;
|
|
|
|
|
|
|
|
//ZipEntry
|
|
|
|
String filename = file.getFileName().toString();
|
|
|
|
ZipEntry ze = new ZipEntry(filename);
|
|
|
|
zos.putNextEntry( ze );
|
|
|
|
|
|
|
|
//read a file and put it into the output stream
|
|
|
|
FileInputStream fis = new FileInputStream(file.toFile());
|
|
|
|
byte[] buffer = new byte[1024];
|
|
|
|
int len;
|
|
|
|
while ((len = fis.read(buffer)) > 0) {
|
|
|
|
zos.write(buffer, 0, len);
|
|
|
|
}
|
|
|
|
zos.flush();
|
|
|
|
fis.close();
|
|
|
|
}
|
|
|
|
zos.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Response r = Response.ok(output, MediaType.APPLICATION_OCTET_STREAM_TYPE).build();
|
|
|
|
String zipname = (path.getFileName()==null) ? "null.zip" : path.getFileName().toString()+".zip";
|
|
|
|
r.getHeaders().add("Content-Disposition", "attachment; filename=\"" + zipname + "\"");
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isPermitted(String dir) {
|
|
|
|
if(! SecurityUtils.getSubject().isPermitted( new DirectoryPermission(dir))) {
|
|
|
|
logger.warn(String.format("User '%s' does not have permission to access '%s'.",SecurityUtils.getSubject().getPrincipal(), dir ));
|
|
|
|
throw new NotAuthorizedException(new UnauthorizedException("User does not have permission to access "+ dir));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2015-08-24 07:58:52 +00:00
|
|
|
|
|
|
|
//http://stackoverflow.com/questions/3758606/how-to-convert-byte-size-into-human-readable-format-in-java
|
|
|
|
private static String humanReadableByteCount(long bytes, boolean si) {
|
|
|
|
int unit = si ? 1000 : 1024;
|
|
|
|
if (bytes < unit) return bytes + " B";
|
|
|
|
int exp = (int) (Math.log(bytes) / Math.log(unit));
|
|
|
|
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
|
|
|
|
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
|
|
|
|
}
|
|
|
|
|
2013-02-05 14:34:01 +00:00
|
|
|
}
|