Subversion Repositories XServices

Rev

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