View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * A provider for accessing files over SFTP.
38   */
39  public class SftpFileProvider extends AbstractOriginatingFileProvider {
40  
41      /** User Information. */
42      public static final String ATTR_USER_INFO = "UI";
43  
44      /** Authentication types. */
45      public static final UserAuthenticationData.Type[] AUTHENTICATOR_TYPES = new UserAuthenticationData.Type[] {
46              UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD };
47  
48      /** The provider's capabilities. */
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       * Creates a new Session.
56       *
57       * @return A Session, never null.
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       * Constructs a new provider.
80       */
81      public SftpFileProvider() {
82          setFileNameParser(SftpFileNameParser.getInstance());
83      }
84  
85      /**
86       * Creates a {@link FileSystem}.
87       */
88      @Override
89      protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
90              throws FileSystemException {
91          // Create the file system
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 }