KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > vfs > store > downloads > TempStore


1 package com.sslexplorer.vfs.store.downloads;
2
3 import java.io.File JavaDoc;
4 import java.io.FileFilter JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.Collection JavaDoc;
8 import java.util.List JavaDoc;
9
10 import org.apache.commons.vfs.FileObject;
11
12 import com.sslexplorer.boot.ContextHolder;
13 import com.sslexplorer.core.CoreUtil;
14 import com.sslexplorer.policyframework.LaunchSession;
15 import com.sslexplorer.security.PasswordCredentials;
16 import com.sslexplorer.vfs.AbstractStore;
17 import com.sslexplorer.vfs.AbstractVFSMount;
18 import com.sslexplorer.vfs.VFSMount;
19 import com.sslexplorer.vfs.utils.URI;
20 import com.sslexplorer.vfs.utils.URI.MalformedURIException;
21 import com.sslexplorer.vfs.webdav.DAVException;
22 import com.sslexplorer.vfs.webdav.DAVStatus;
23 import com.sslexplorer.vfs.webdav.DAVUtilities;
24
25 /**
26  * {@link AbstractStore} implementation that creates a mount which acts as a
27  * session store for any files downloaded in a given session, so that once the
28  * download has expired the file is still downloadable.
29  *
30  * @author Lee D Painter <a HREF="mailto: lee@3sp.com">&lt;lee@3sp.com&gt;</a>
31  */

32 public class TempStore extends AbstractStore {
33
34     /**
35      * Constant for the mount name.
36      */

37     public static final String JavaDoc TEMP_DOWNLOAD_MOUNT_NAME = "downloads";
38     
39     /**
40      * Constructor.
41      */

42     public TempStore() {
43         super("temp", "UTF-8");
44     }
45
46     /* (non-Javadoc)
47      * @see com.sslexplorer.vfs.VFSStore#getMountFromString(java.lang.String, com.sslexplorer.policyframework.LaunchSession)
48      */

49     public VFSMount getMountFromString(String JavaDoc mountName, LaunchSession launchSession) throws DAVException {
50         try {
51             File JavaDoc tempDownloadDirectory = CoreUtil.getTempDownloadDirectory(getRepository().getSession());
52             if(!mountName.equals(tempDownloadDirectory.getName())) {
53                 throw new Exception JavaDoc("No permission.");
54             }
55             return new DownloadsStoreMount(launchSession, tempDownloadDirectory);
56         }
57         catch(Exception JavaDoc e) {
58             throw new DAVException(DAVStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
59         }
60     }
61
62     /* (non-Javadoc)
63      * @see com.sslexplorer.vfs.VFSStore#getMountNames()
64      */

65     public Collection JavaDoc<String JavaDoc> getMountNames() throws Exception JavaDoc {
66         File JavaDoc tempDownloadDirectory = new File JavaDoc(ContextHolder.getContext().getTempDirectory(), TempStore.TEMP_DOWNLOAD_MOUNT_NAME);
67         File JavaDoc[] dirs = tempDownloadDirectory.listFiles(new FileFilter JavaDoc() {
68             public boolean accept(File JavaDoc pathname) {
69                 return pathname.isDirectory();
70             }
71         });
72         List JavaDoc<String JavaDoc> l = new ArrayList JavaDoc<String JavaDoc>();
73         if(dirs != null) {
74             for(int i = 0 ; i < dirs.length; i++) {
75                 l.add(dirs[i].getName());
76             }
77         }
78         return l;
79     }
80
81     /*
82      * (non-Javadoc)
83      *
84      * @see com.sslexplorer.vfs.webdav.DAVStore#validateUserEnteredPath(java.lang.String)
85      */

86     public String JavaDoc createURIFromPath(String JavaDoc path) throws IllegalArgumentException JavaDoc {
87         throw new IllegalArgumentException JavaDoc();
88     }
89
90     /**
91      * Class which represents the stores mount.
92      *
93      * @author Lee D Painter <a HREF="mailto:lee@3sp.com">&lt;lee@3sp.com&gt;</a>
94      *
95      */

96     class DownloadsStoreMount extends AbstractVFSMount {
97         
98         private File JavaDoc dir;
99         
100         DownloadsStoreMount(LaunchSession launchSession, File JavaDoc dir) {
101             super(launchSession, TempStore.this, dir.getName(), true);
102             this.dir = dir;
103         }
104
105         /* (non-Javadoc)
106          * @see com.sslexplorer.vfs.AbstractVFSMount#createVFSFileObject(java.lang.String, com.sslexplorer.security.PasswordCredentials)
107          */

108         public FileObject createVFSFileObject(String JavaDoc path, PasswordCredentials credentials) throws IOException JavaDoc {
109             URI uri = new URI(getRootVFSURI());
110             uri.setPath(DAVUtilities.concatenatePaths(uri.getPath(), path));
111             FileObject root = getStore().getRepository().getFileSystemManager().resolveFile(uri.toString());
112             return root;
113         }
114
115         /* (non-Javadoc)
116          * @see com.sslexplorer.vfs.AbstractVFSMount#getRootVFSURI(java.lang.String)
117          */

118         public URI getRootVFSURI(String JavaDoc charset) throws MalformedURIException {
119             return new URI(dir.toURI().toString());
120         }
121     }
122 }
123
Popular Tags