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