1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.sftp;
18
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.Collections;
22
23 import org.apache.commons.vfs2.Capability;
24 import org.apache.commons.vfs2.FileName;
25 import org.apache.commons.vfs2.FileSystem;
26 import org.apache.commons.vfs2.FileSystemConfigBuilder;
27 import org.apache.commons.vfs2.FileSystemException;
28 import org.apache.commons.vfs2.FileSystemOptions;
29 import org.apache.commons.vfs2.UserAuthenticationData;
30 import org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider;
31 import org.apache.commons.vfs2.provider.GenericFileName;
32 import org.apache.commons.vfs2.util.UserAuthenticatorUtils;
33
34 import com.jcraft.jsch.Session;
35
36
37
38
39 public class SftpFileProvider extends AbstractOriginatingFileProvider {
40
41
42 public static final String ATTR_USER_INFO = "UI";
43
44
45 public static final UserAuthenticationData.Type[] AUTHENTICATOR_TYPES = new UserAuthenticationData.Type[] {
46 UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD };
47
48
49 protected static final Collection<Capability> capabilities = Collections.unmodifiableCollection(Arrays.asList(Capability.CREATE, Capability.DELETE, Capability.RENAME, Capability.GET_TYPE,
50 Capability.LIST_CHILDREN, Capability.READ_CONTENT, Capability.URI, Capability.WRITE_CONTENT,
51 Capability.GET_LAST_MODIFIED, Capability.SET_LAST_MODIFIED_FILE, Capability.RANDOM_ACCESS_READ,
52 Capability.APPEND_CONTENT));
53
54
55
56
57
58
59 static Session createSession(final GenericFileName rootName, final FileSystemOptions fileSystemOptions)
60 throws FileSystemException {
61 UserAuthenticationData authData = null;
62 try {
63 authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES);
64
65 return SftpClientFactory.createConnection(rootName.getHostName(), rootName.getPort(),
66 UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME,
67 UserAuthenticatorUtils.toChar(rootName.getUserName())),
68 UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD,
69 UserAuthenticatorUtils.toChar(rootName.getPassword())),
70 fileSystemOptions);
71 } catch (final Exception e) {
72 throw new FileSystemException("vfs.provider.sftp/connect.error", rootName, e);
73 } finally {
74 UserAuthenticatorUtils.cleanup(authData);
75 }
76 }
77
78
79
80
81 public SftpFileProvider() {
82 setFileNameParser(SftpFileNameParser.getInstance());
83 }
84
85
86
87
88 @Override
89 protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
90 throws FileSystemException {
91
92 return new SftpFileSystem((GenericFileName/apache/commons/vfs2/provider/GenericFileName.html#GenericFileName">GenericFileName) name, createSession((GenericFileName) name, fileSystemOptions),
93 fileSystemOptions);
94 }
95
96 @Override
97 public Collection<Capability> getCapabilities() {
98 return capabilities;
99 }
100
101 @Override
102 public FileSystemConfigBuilder getConfigBuilder() {
103 return SftpFileSystemConfigBuilder.getInstance();
104 }
105 }