001: package com.coldcore.coloradoftp.plugin.webcam;
002:
003: import com.coldcore.coloradoftp.filesystem.FailedActionException;
004: import com.coldcore.coloradoftp.filesystem.FailedActionReason;
005: import com.coldcore.coloradoftp.filesystem.FileSystem;
006: import com.coldcore.coloradoftp.plugin.webcam.task.UploadTask;
007: import com.coldcore.coloradoftp.session.Session;
008: import com.coldcore.coloradoftp.session.SessionAttributeName;
009: import org.apache.log4j.Logger;
010:
011: import java.io.IOException;
012: import java.nio.channels.WritableByteChannel;
013: import java.util.HashSet;
014: import java.util.Set;
015:
016: /**
017: * Wraps existing file system and executes plugins for selected users.
018: */
019: public class FileSystemWrapper extends
020: com.coldcore.coloradoftp.filesystem.impl.FileSystemWrapper {
021:
022: private static Logger log = Logger
023: .getLogger(FileSystemWrapper.class);
024: protected Set<UploadInterceptor> interceptors;
025:
026: public FileSystemWrapper(FileSystem fileSystem) {
027: super (fileSystem);
028:
029: interceptors = new HashSet<UploadInterceptor>();
030: }
031:
032: /** Get interceptors
033: * @return Interceptors
034: */
035: public Set<UploadInterceptor> getInterceptors() {
036: return interceptors;
037: }
038:
039: /** Set interceptors
040: * @param interceptors Interceptors
041: */
042: public void setInterceptors(Set<UploadInterceptor> interceptors) {
043: this .interceptors = interceptors;
044: }
045:
046: public WritableByteChannel saveFile(String filename,
047: boolean append, Session userSession)
048: throws FailedActionException {
049: //Test if this operation needs to be intercepted
050: String username = (String) userSession
051: .getAttribute(SessionAttributeName.USERNAME);
052: UploadInterceptor interceptor = null;
053: for (UploadInterceptor i : interceptors)
054: if (username.equalsIgnoreCase(i.getUsername())) {
055: interceptor = i;
056: break;
057: }
058:
059: if (interceptor != null) {
060: log.debug("Upload intercepted, user: "
061: + interceptor.getUsername());
062: if (append)
063: throw new FailedActionException(
064: FailedActionReason.NOT_IMPLEMENTED,
065: "Append is not allowed");
066:
067: UploadTask task = interceptor.getTask();
068: if (task == null) {
069: log.error("No action assigned to interceptor");
070: throw new FailedActionException(
071: FailedActionReason.SYSTEM_ERROR);
072: }
073:
074: //This object will read the file into memory
075: FileGrabber grabber;
076: try {
077: grabber = new FileGrabber();
078: } catch (IOException e) {
079: log.error("Cannot construct file grabber", e);
080: throw new FailedActionException(
081: FailedActionReason.SYSTEM_ERROR);
082: }
083:
084: String lname = filename.indexOf(getFileSeparator()) == -1 ? filename
085: : filename.substring(filename
086: .lastIndexOf(getFileSeparator())
087: + getFileSeparator().length());
088:
089: //Run thread to read the file and execute all interceptor tasks
090: UploadAction action = new UploadAction(lname, grabber, task);
091: Thread thr = new Thread(action);
092: thr.start();
093:
094: return grabber;
095: }
096:
097: //Continue as normal
098: return fileSystem.saveFile(filename, append, userSession);
099: }
100:
101: }
|