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 = {
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,
50          Capability.RENAME, Capability.GET_TYPE, 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, Capability.APPEND_CONTENT));
52  
53      /**
54       * Creates a new Session.
55       *
56       * @return A Session, never null.
57       */
58      static Session createSession(final GenericFileName rootName, final FileSystemOptions fileSystemOptions)
59              throws FileSystemException {
60          UserAuthenticationData authData = null;
61          try {
62              authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES);
63  
64              return SftpClientFactory.createConnection(rootName.getHostName(), rootName.getPort(),
65                      UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME,
66                              UserAuthenticatorUtils.toChar(rootName.getUserName())),
67                      UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD,
68                              UserAuthenticatorUtils.toChar(rootName.getPassword())),
69                      fileSystemOptions);
70          } catch (final Exception e) {
71              throw new FileSystemException("vfs.provider.sftp/connect.error", rootName, e);
72          } finally {
73              UserAuthenticatorUtils.cleanup(authData);
74          }
75      }
76  
77      /**
78       * Constructs a new provider.
79       */
80      public SftpFileProvider() {
81          setFileNameParser(SftpFileNameParser.getInstance());
82      }
83  
84      /**
85       * Creates a {@link FileSystem}.
86       */
87      @Override
88      protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
89              throws FileSystemException {
90          // Create the file system
91          return new SftpFileSystem((GenericFileName) name, createSession((GenericFileName) name, fileSystemOptions),
92                  fileSystemOptions);
93      }
94  
95      @Override
96      public Collection<Capability> getCapabilities() {
97          return capabilities;
98      }
99  
100     @Override
101     public FileSystemConfigBuilder getConfigBuilder() {
102         return SftpFileSystemConfigBuilder.getInstance();
103     }
104 }