Subversion Repositories XServices

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
147 brianR 1
/*
2
 *   Copyright 2013 Brian Rosenberger (Brutex Network)
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
6
 *   You may obtain a copy of the License at
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
15
 */
16
 
92 brianR 17
package net.brutex.xservices.ws.rs;
18
 
19
import java.io.File;
147 brianR 20
import java.io.FileInputStream;
21
import java.io.FileOutputStream;
22
import java.io.IOException;
23
import java.io.OutputStream;
24
import java.net.URI;
25
import java.nio.file.DirectoryStream;
26
import java.nio.file.FileSystems;
27
import java.nio.file.FileVisitOption;
28
import java.nio.file.FileVisitResult;
29
import java.nio.file.Files;
30
import java.nio.file.Path;
31
import java.nio.file.PathMatcher;
32
import java.nio.file.SimpleFileVisitor;
33
import java.nio.file.attribute.BasicFileAttributeView;
34
import java.nio.file.attribute.BasicFileAttributes;
92 brianR 35
import java.util.ArrayList;
147 brianR 36
import java.util.EnumSet;
92 brianR 37
import java.util.List;
147 brianR 38
import java.util.zip.ZipEntry;
39
import java.util.zip.ZipOutputStream;
40
 
41
import javax.ws.rs.NotAuthorizedException;
42
import javax.ws.rs.WebApplicationException;
92 brianR 43
import javax.ws.rs.core.GenericEntity;
44
import javax.ws.rs.core.HttpHeaders;
147 brianR 45
import javax.ws.rs.core.MediaType;
92 brianR 46
import javax.ws.rs.core.Response;
147 brianR 47
import javax.ws.rs.core.StreamingOutput;
48
import javax.ws.rs.core.UriInfo;
49
 
50
import net.brutex.xservices.security.DirectoryPermission;
109 brianR 51
import net.brutex.xservices.types.FileInfoType;
147 brianR 52
import net.brutex.xservices.util.FileWalker;
92 brianR 53
 
185 brianR 54
import org.apache.commons.jcs.JCS;
55
import org.apache.commons.jcs.access.CacheAccess;
56
import org.apache.commons.jcs.access.exception.CacheException;
57
import org.apache.logging.log4j.LogManager;
58
import org.apache.logging.log4j.Logger;
147 brianR 59
import org.apache.shiro.SecurityUtils;
60
import org.apache.shiro.authz.UnauthorizedException;
92 brianR 61
 
62
/**
147 brianR 63
 * The Class FileInfoImpl.
64
 *
109 brianR 65
 * @author Brian Rosenberger, bru(at)brutex.de
92 brianR 66
 */
147 brianR 67
public class FileInfoImpl  implements FileInfo {
68
 
69
 
185 brianR 70
	Logger logger = LogManager.getLogger();
147 brianR 71
 
72
 
73
  /* (non-Javadoc)
74
   * @see net.brutex.xservices.ws.rs.FileInfo#getFiles(javax.ws.rs.core.HttpHeaders, java.lang.String, boolean, boolean, int, java.lang.String, int, int)
75
   */
76
  public Response getFiles(HttpHeaders h, UriInfo uriInfo, String dir, boolean withDir, boolean withFiles, int level, String search, int count, int page, boolean useCache)
109 brianR 77
  {
177 brianR 78
	if(dir==null) {
79
		dir = "c:/";
80
		logger.warn("No directory specified. Default is 'c:/'.");
81
		}
147 brianR 82
	isPermitted(dir);
83
 
84
    URI baseuri = URI.create(uriInfo.getBaseUri()+FileInfo.BASE_PATH+"getFile?file=");
85
 
86
    logger.info(String.format("Listing directory '%s'.", dir));
87
    if (level <= 0) level = 1;
92 brianR 88
 
109 brianR 89
    if ((!withDir) && (!withFiles)) withFiles = true;
90
    String cachekey = level + "||" + withFiles + "||" + withDir + "||" + search + "||" + dir;
91
    try {
147 brianR 92
      logger.debug(String.format("Hitting cache with cachekey '%s'", cachekey));
185 brianR 93
      CacheAccess<Object, Object> jcs = JCS.getInstance("FileCache");
94 brianR 94
 
147 brianR 95
      /*Try to retrieve the file list from the cache*/
96
      List<FileInfoType> list = (List<FileInfoType>)jcs.get(cachekey);
97
 
98
      if (list == null || !useCache) {
99
        list = setDirectory(baseuri, dir, withDir, withFiles, level, search);
109 brianR 100
        jcs.put(cachekey, list);
147 brianR 101
        logger.debug("Stored in Cache: " + list.toString());
109 brianR 102
      } else {
147 brianR 103
        logger.debug("Got from Cache: " + list.toString());
109 brianR 104
      }
92 brianR 105
 
109 brianR 106
      int fromIndex = 0;
107
      int toIndex = 0;
108
      fromIndex = (page - 1) * count;
109
      toIndex = page * count;
110
      if (toIndex > list.size()) toIndex = list.size();
111
      if (fromIndex > toIndex) fromIndex = toIndex;
147 brianR 112
      GenericEntity<List<FileInfoType>> sublist = new GenericEntity<List<FileInfoType>>(list.subList(fromIndex, toIndex)) {};
113
      logger.info(String.format("Returning items %s to %s from total of %s items in the list.", fromIndex, toIndex, list.size()));
109 brianR 114
      return Response.ok(sublist).build();
115
    } catch (CacheException e) {
177 brianR 116
      return Response.serverError().build();
109 brianR 117
    }
118
  }
92 brianR 119
 
147 brianR 120
  /**
121
   * Sets the directory.
122
   *
123
   * @param list the list
124
   * @param dir the dir
125
   * @param withDirectories the with directories
126
   * @param withFiles the with files
127
   * @param depth the depth
128
   * @param search the search
129
   */
130
  private void setDirectory(final URI baseuri, final List<FileInfoType> list, File dir, boolean withDirectories, boolean withFiles, final int depth, String search)
109 brianR 131
  {
132
    if (depth <= 0) return;
147 brianR 133
 
134
    	if(search==null || search.equals("") ) {
135
    		search = "*";
136
    		logger.info("No search pattern supplied, using default '*'.");
137
    	}
138
 
139
    	FileWalker finder = new FileWalker(search);
140
    	try {
141
			Files.walkFileTree(dir.toPath(), EnumSet.of(FileVisitOption.FOLLOW_LINKS), depth, finder);
142
			logger.info("FileWalker returned '"+finder.getCount()+"' hits. '" + finder.getTotal() + "' files have been scanned.");
143
			List<Path> result = finder.getResult();
144
	    	for(Path f : result) {
156 brianR 145
	    		if(! withDirectories) {
146
	    			if(f.toFile().isDirectory()) continue;
147
	    		}
148
	    		if(! withFiles) {
149
	    			if(f.toFile().isFile()) continue;
150
	    		}
147 brianR 151
	    		list.add(new FileInfoType(f, baseuri));
152
	    	}
153
		} catch (IOException e2) {
154
			logger.error(e2.getMessage(), e2);;
155
		}
109 brianR 156
  }
147 brianR 157
 
158
  /**
159
   * Sets the directory.
160
   *
161
   * @param dir the dir
162
   * @param withDirectories the with directories
163
   * @param withFiles the with files
164
   * @param depth the depth
165
   * @param search the search
166
   * @return the list
167
   */
168
  private List<FileInfoType> setDirectory(URI baseuri, String dir, boolean withDirectories, boolean withFiles, int depth, String search)
109 brianR 169
  {
147 brianR 170
    List<FileInfoType> list = new ArrayList<FileInfoType>();
171
    setDirectory(baseuri, list, new File(dir), withDirectories, withFiles, depth, search);
109 brianR 172
    return list;
173
  }
147 brianR 174
 
175
@Override
176
public Response getFile(HttpHeaders paramHttpHeaders, String file) {
177
	isPermitted(file);
178
	try {
179
	Path path = FileSystems.getDefault().getPath(file);
180
 
181
	BasicFileAttributeView basicView = Files.getFileAttributeView(path, BasicFileAttributeView.class);
182
	BasicFileAttributes basic;
183
	basic = basicView.readAttributes();
184
 
185
 
186
	//In case this is a directory
187
	//we zip it and return the zip stream
188
	if(basic.isDirectory()) return getDirectoryAsZip(path);
189
 
190
 
191
 
192
	MediaType mime = MediaType.APPLICATION_OCTET_STREAM_TYPE;
193
	try {
194
		mime = MediaType.valueOf(Files.probeContentType(path));
195
	} catch (IllegalArgumentException | IOException e) {
196
		//In case we can not find the media type for some reason
197
		//the default assignment is taken, so we can
198
		//ignore this error.
199
		logger.debug(String.format("Could not probe media type for file '%s'. Default is '%s'", path.toString(), mime.getType()), e);
200
	}
201
	Response r = Response.ok(path.toFile(), mime).build();
202
	String fileName = path.getFileName().toString();
203
	if(mime == MediaType.APPLICATION_OCTET_STREAM_TYPE) r.getHeaders().add("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
204
	return r;
205
		} catch (IOException e1) {
206
			// TODO Auto-generated catch block
207
			logger.error(e1.getMessage(), e1);
208
			return Response.serverError().build();
209
		}
210
}
211
 
212
private Response getDirectoryAsZip(final Path path) {
213
 
214
	StreamingOutput output = new StreamingOutput() {
215
 
216
		@Override
217
		public void write(OutputStream os) throws IOException,
218
				WebApplicationException {
219
			ZipOutputStream zos = new ZipOutputStream(os);
220
 
221
			//read directory content (files only)
222
			try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
223
			    for (Path file: stream) {
224
			    	//skip anything not being a file
225
			        if(! file.toFile().isFile()) continue;
226
 
227
			        //ZipEntry
228
			        String filename = file.getFileName().toString();
229
			        ZipEntry ze = new ZipEntry(filename);
230
			        zos.putNextEntry( ze );
231
 
232
			        //read a file and put it into the output stream
233
			        FileInputStream fis = new FileInputStream(file.toFile());
234
			        byte[] buffer = new byte[1024];
235
			        int len;
236
			        while ((len = fis.read(buffer)) > 0) {
237
			        	zos.write(buffer, 0, len);
238
			        }
239
			        zos.flush();
240
			        fis.close();
241
			    }
242
			    zos.close();
243
			}
244
 
245
		}
246
	};
247
	Response r = Response.ok(output, MediaType.APPLICATION_OCTET_STREAM_TYPE).build();
248
	String zipname = (path.getFileName()==null) ? "null.zip" : path.getFileName().toString()+".zip";
249
	r.getHeaders().add("Content-Disposition", "attachment; filename=\"" + zipname + "\"");
250
	return r;
251
}
252
 
253
private boolean isPermitted(String dir) {
254
	if(! SecurityUtils.getSubject().isPermitted( new DirectoryPermission(dir))) {
255
		logger.warn(String.format("User '%s' does not have permission to access '%s'.",SecurityUtils.getSubject().getPrincipal(), dir ));
256
		throw new NotAuthorizedException(new UnauthorizedException("User does not have permission to access "+ dir));
257
	}
258
	return true;
259
}
177 brianR 260
 
261
//http://stackoverflow.com/questions/3758606/how-to-convert-byte-size-into-human-readable-format-in-java
262
private static String humanReadableByteCount(long bytes, boolean si) {
263
    int unit = si ? 1000 : 1024;
264
    if (bytes < unit) return bytes + " B";
265
    int exp = (int) (Math.log(bytes) / Math.log(unit));
266
    String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
267
    return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
268
}
269
 
109 brianR 270
}