KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > networkplaces > store > ftp > FTPMount


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.networkplaces.store.ftp;
21
22 import java.io.IOException JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.commons.vfs.FileObject;
27 import org.apache.commons.vfs.FileSystemException;
28 import org.apache.commons.vfs.FileSystemOptions;
29 import org.apache.commons.vfs.provider.ftp.FtpFileSystemConfigBuilder;
30
31 import com.sslexplorer.networkplaces.AbstractNetworkPlaceMount;
32 import com.sslexplorer.policyframework.LaunchSession;
33 import com.sslexplorer.security.PasswordCredentials;
34 import com.sslexplorer.vfs.VFSStore;
35 import com.sslexplorer.vfs.utils.URI;
36 import com.sslexplorer.vfs.webdav.DAVAuthenticationRequiredException;
37 import com.sslexplorer.vfs.webdav.DAVUtilities;
38
39 public class FTPMount extends AbstractNetworkPlaceMount {
40
41     final static Log log = LogFactory.getLog(FTPMount.class);
42
43     public FTPMount(LaunchSession launchSession, VFSStore store) {
44         super(launchSession, store);
45     }
46     
47     public FileSystemOptions getOptions(URI uri) {
48         FileSystemOptions options = new FileSystemOptions();
49         if(uri.getQueryString()==null || uri.getQueryString().indexOf("passive=false")==-1) {
50             FtpFileSystemConfigBuilder c = FtpFileSystemConfigBuilder.getInstance();
51             c.setPassiveMode(options, true);
52             // TODO: Add resource attribute for all these settings.
53
c.setUserDirIsRoot(options, true);
54             //c.setEntryParser(options, "UNIX");
55
}
56         return options;
57     }
58
59     public FileObject createVFSFileObject(String JavaDoc path, PasswordCredentials credentials) throws IOException JavaDoc {
60         try {
61             URI uri = getRootVFSURI();
62             if (credentials != null) {
63                 uri.setUserinfo(DAVUtilities.encodeURIUserInfo(credentials.getUsername() + (credentials.getPassword() != null ? ":" + new String JavaDoc(credentials.getPassword()) : "")));
64             }
65             uri.setPath(uri.getPath() + (uri.getPath().endsWith("/") ? "" : "/") + DAVUtilities.encodePath(path));
66             FileObject fileObject = this.getStore().getRepository().getFileSystemManager().resolveFile(uri.toString(), getOptions(uri));
67             return fileObject;
68         } catch (FileSystemException fse) {
69             if (fse.getCode().equals("vfs.provider.ftp/connect.error")) {
70                 throw new DAVAuthenticationRequiredException(getMountString(), true);
71             }
72             throw fse;
73         }
74     }
75 }
Popular Tags