Subversion Repositories XServices

Rev

Rev 198 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 198 Rev 199
Line 45... Line 45...
45
import javax.ws.rs.core.MediaType;
45
import javax.ws.rs.core.MediaType;
46
import javax.ws.rs.core.Response;
46
import javax.ws.rs.core.Response;
47
import javax.ws.rs.core.StreamingOutput;
47
import javax.ws.rs.core.StreamingOutput;
48
import javax.ws.rs.core.UriInfo;
48
import javax.ws.rs.core.UriInfo;
Line -... Line 49...
-
 
49
 
49
 
50
import lombok.extern.slf4j.Slf4j;
50
import net.brutex.xservices.security.DirectoryPermission;
51
import net.brutex.xservices.security.DirectoryPermission;
51
import net.brutex.xservices.types.FileInfoType;
52
import net.brutex.xservices.types.FileInfoType;
Line 52... Line 53...
52
import net.brutex.xservices.util.FileWalker;
53
import net.brutex.xservices.util.FileWalker;
53
 
54
 
54
import org.apache.commons.jcs.JCS;
55
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;
56
import org.apache.commons.jcs.access.CacheAccess;
58
import org.apache.logging.log4j.Logger;
57
import org.apache.commons.jcs.access.exception.CacheException;
Line 59... Line 58...
59
import org.apache.shiro.SecurityUtils;
58
import org.apache.shiro.SecurityUtils;
60
import org.apache.shiro.authz.UnauthorizedException;
59
import org.apache.shiro.authz.UnauthorizedException;
61
 
60
 
62
/**
61
/**
63
 * The Class FileInfoImpl.
62
 * The Class FileInfoImpl.
-
 
63
 *
64
 *
64
 * @author Brian Rosenberger, bru(at)brutex.de
Line 65... Line 65...
65
 * @author Brian Rosenberger, bru(at)brutex.de
65
 */
66
 */
-
 
Line 67... Line 66...
67
public class FileInfoImpl  implements FileInfo {
66
@Slf4j
68
	
67
public class FileInfoImpl  implements FileInfo {
69
	
68
	
70
	Logger logger = LogManager.getLogger();
69
 
71
	
70
	
72
 
71
 
73
  /* (non-Javadoc)
72
  /* (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)
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)
75
   */
74
   */
76
  public Response getFiles(HttpHeaders h, UriInfo uriInfo, String dir, boolean withDir, boolean withFiles, int level, String search, int count, int page, boolean useCache)
75
  public Response getFiles(HttpHeaders h, UriInfo uriInfo, String dir, boolean withDir, boolean withFiles, int level, String search, int count, int page, boolean useCache)
Line 77... Line 76...
77
  {
76
  {
Line 78... Line 77...
78
	if(dir==null) {
77
	if(dir==null) {
79
		dir = "c:/"; 
78
		dir = "c:/"; 
Line 80... Line 79...
80
		logger.warn("No directory specified. Default is 'c:/'.");
79
		log.warn("No directory specified. Default is 'c:/'.");
81
		}
80
		}
82
	isPermitted(dir);
81
	isPermitted(dir);
83
	
82
	
84
    URI baseuri = URI.create(uriInfo.getBaseUri()+FileInfo.BASE_PATH+"getFile?file=");
83
    URI baseuri = URI.create(uriInfo.getBaseUri()+FileInfo.BASE_PATH+"getFile?file=");
Line 85... Line 84...
85
    
84
    
86
    logger.info(String.format("Listing directory '%s'.", dir));
85
    log.info(String.format("Listing directory '%s'.", dir));
Line 87... Line 86...
87
    if (level <= 0) level = 1;
86
    if (level <= 0) level = 1;
88
 
87
 
89
    if ((!withDir) && (!withFiles)) withFiles = true;
88
    if ((!withDir) && (!withFiles)) withFiles = true;
90
    String cachekey = level + "||" + withFiles + "||" + withDir + "||" + search + "||" + dir;
89
    String cachekey = level + "||" + withFiles + "||" + withDir + "||" + search + "||" + dir;
91
    try {
90
    try {
92
      logger.debug(String.format("Hitting cache with cachekey '%s'", cachekey));
91
      log.debug(String.format("Hitting cache with cachekey '%s'", cachekey));
93
      CacheAccess<Object, Object> jcs = JCS.getInstance("FileCache");
92
      CacheAccess<Object, Object> jcs = JCS.getInstance("FileCache");
Line 94... Line 93...
94
 
93
 
95
      /*Try to retrieve the file list from the cache*/
94
      /*Try to retrieve the file list from the cache*/
96
      List<FileInfoType> list = (List<FileInfoType>)jcs.get(cachekey);
95
      List<FileInfoType> list = (List<FileInfoType>)jcs.get(cachekey);
97
      
96
      
98
      if (list == null || !useCache) {
97
      if (list == null || !useCache) {
99
        list = setDirectory(baseuri, dir, withDir, withFiles, level, search);
98
        list = setDirectory(baseuri, dir, withDir, withFiles, level, search);
100
        jcs.put(cachekey, list);
99
        jcs.put(cachekey, list);
101
        logger.debug("Stored in Cache: " + list.toString());
100
        log.debug("Stored in Cache: " + list.toString());
102
      } else {
101
      } else {
103
        logger.debug("Got from Cache: " + list.toString());
102
        log.debug("Got from Cache: " + list.toString());
104
      }
103
      }
105
 
104
 
106
      int fromIndex = 0;
105
      int fromIndex = 0;
Line 131... Line 130...
131
  {
130
  {
132
    if (depth <= 0) return;
131
    if (depth <= 0) return;
Line 133... Line 132...
133
    
132
    
134
    	if(search==null || search.equals("") ) {
133
    	if(search==null || search.equals("") ) {
135
    		search = "*";
134
    		search = "*";
136
    		logger.info("No search pattern supplied, using default '*'.");
135
    		log.info("No search pattern supplied, using default '*'.");
Line 137... Line 136...
137
    	}
136
    	}
138
    	
137
    	
139
    	FileWalker finder = new FileWalker(search);
138
    	FileWalker finder = new FileWalker(search);
140
    	try {
139
    	try {
141
			Files.walkFileTree(dir.toPath(), EnumSet.of(FileVisitOption.FOLLOW_LINKS), depth, finder);
140
			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.");
141
			log.info("FileWalker returned '"+finder.getCount()+"' hits. '" + finder.getTotal() + "' files have been scanned.");
143
			List<Path> result = finder.getResult();
142
			List<Path> result = finder.getResult();
144
	    	for(Path f : result) {
143
	    	for(Path f : result) {
145
	    		if(! withDirectories) {
144
	    		if(! withDirectories) {
Line 149... Line 148...
149
	    			if(f.toFile().isFile()) continue;
148
	    			if(f.toFile().isFile()) continue;
150
	    		}
149
	    		}
151
	    		list.add(new FileInfoType(f, baseuri));
150
	    		list.add(new FileInfoType(f, baseuri));
152
	    	}
151
	    	}
153
		} catch (IOException e2) {
152
		} catch (IOException e2) {
154
			logger.error(e2.getMessage(), e2);;
153
			log.error(e2.getMessage(), e2);;
155
		}
154
		}
156
  }
155
  }
Line 157... Line 156...
157
  
156
  
158
  /**
157
  /**
Line 194... Line 193...
194
		mime = MediaType.valueOf(Files.probeContentType(path));
193
		mime = MediaType.valueOf(Files.probeContentType(path));
195
	} catch (IllegalArgumentException | IOException e) {
194
	} catch (IllegalArgumentException | IOException e) {
196
		//In case we can not find the media type for some reason
195
		//In case we can not find the media type for some reason
197
		//the default assignment is taken, so we can
196
		//the default assignment is taken, so we can
198
		//ignore this error.
197
		//ignore this error.
199
		logger.debug(String.format("Could not probe media type for file '%s'. Default is '%s'", path.toString(), mime.getType()), e);
198
		log.debug(String.format("Could not probe media type for file '%s'. Default is '%s'", path.toString(), mime.getType()), e);
200
	}
199
	}
201
	Response r = Response.ok(path.toFile(), mime).build();
200
	Response r = Response.ok(path.toFile(), mime).build();
202
	String fileName = path.getFileName().toString();
201
	String fileName = path.getFileName().toString();
203
	if(mime == MediaType.APPLICATION_OCTET_STREAM_TYPE) r.getHeaders().add("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
202
	if(mime == MediaType.APPLICATION_OCTET_STREAM_TYPE) r.getHeaders().add("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
204
	return r;
203
	return r;
205
		} catch (IOException e1) {
204
		} catch (IOException e1) {
206
			// TODO Auto-generated catch block
205
			// TODO Auto-generated catch block
207
			logger.error(e1.getMessage(), e1);
206
			log.error(e1.getMessage(), e1);
208
			return Response.serverError().build();
207
			return Response.serverError().build();
209
		}
208
		}
210
}
209
}
Line 211... Line 210...
211
 
210
 
Line 250... Line 249...
250
	return r;
249
	return r;
251
}
250
}
Line 252... Line 251...
252
 
251
 
253
private boolean isPermitted(String dir) {
252
private boolean isPermitted(String dir) {
254
	if(! SecurityUtils.getSubject().isPermitted( new DirectoryPermission(dir))) {
253
	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 ));
254
		log.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));
255
		throw new NotAuthorizedException(new UnauthorizedException("User does not have permission to access "+ dir));
257
	}
256
	}
258
	return true;
257
	return true;