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.vfs.provider.sftp;
18  
19  import com.jcraft.jsch.Session;
20  import org.apache.commons.vfs.Capability;
21  import org.apache.commons.vfs.FileName;
22  import org.apache.commons.vfs.FileSystem;
23  import org.apache.commons.vfs.FileSystemConfigBuilder;
24  import org.apache.commons.vfs.FileSystemException;
25  import org.apache.commons.vfs.FileSystemOptions;
26  import org.apache.commons.vfs.UserAuthenticationData;
27  import org.apache.commons.vfs.util.UserAuthenticatorUtils;
28  import org.apache.commons.vfs.provider.AbstractOriginatingFileProvider;
29  import org.apache.commons.vfs.provider.GenericFileName;
30  
31  import java.util.Arrays;
32  import java.util.Collection;
33  import java.util.Collections;
34  
35  /***
36   * A provider for accessing files over SFTP.
37   *
38   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
39   * @author Gary D. Gregory
40   * @version $Id: SftpFileProvider.java 480428 2006-11-29 06:15:24Z bayard $
41   */
42  public class SftpFileProvider extends AbstractOriginatingFileProvider
43  {
44      protected final static Collection capabilities = Collections.unmodifiableCollection(Arrays.asList(new Capability[]
45      {
46          Capability.CREATE,
47          Capability.DELETE,
48          Capability.RENAME,
49          Capability.GET_TYPE,
50          Capability.LIST_CHILDREN,
51          Capability.READ_CONTENT,
52          Capability.URI,
53          Capability.WRITE_CONTENT,
54          Capability.GET_LAST_MODIFIED,
55          Capability.SET_LAST_MODIFIED_FILE,
56          Capability.RANDOM_ACCESS_READ
57      }));
58  
59      public final static String ATTR_USER_INFO = "UI";
60  	
61  	public final static UserAuthenticationData.Type[] AUTHENTICATOR_TYPES = new UserAuthenticationData.Type[]
62  		{
63  			UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD
64  		};
65  
66  	// private JSch jSch = new JSch();
67  
68      public SftpFileProvider()
69      {
70          super();
71          setFileNameParser(SftpFileNameParser.getInstance());
72      }
73  
74      /***
75       * Creates a {@link FileSystem}.
76       */
77      protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions) throws FileSystemException
78      {
79          // JSch jsch = createJSch(fileSystemOptions);
80  
81          // Create the file system
82          final GenericFileName rootName = (GenericFileName) name;
83  
84          Session session;
85  		UserAuthenticationData authData = null;
86          try
87          {
88  			authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES);
89  
90  			session = SftpClientFactory.createConnection(
91  				rootName.getHostName(),
92  				rootName.getPort(),
93  				UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName())),
94  				UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(rootName.getPassword())),
95  				fileSystemOptions);
96          }
97          catch (final Exception e)
98          {
99              throw new FileSystemException("vfs.provider.sftp/connect.error",
100                 name,
101                 e);
102         }
103 		finally
104 		{
105 			UserAuthenticatorUtils.cleanup(authData);
106 		}
107 
108 		return new SftpFileSystem(rootName, session, fileSystemOptions);
109     }
110 
111 
112     /***
113      * Returns the JSch.
114      *
115      * @return Returns the jSch.
116      */
117     /*
118     private JSch getJSch()
119     {
120         return this.jSch;
121     }
122     */
123 
124     /***
125      * Initialises the component.
126      */
127     public void init() throws FileSystemException
128     {
129     }
130 
131     public FileSystemConfigBuilder getConfigBuilder()
132     {
133         return SftpFileSystemConfigBuilder.getInstance();
134     }
135 
136     public Collection getCapabilities()
137     {
138         return capabilities;
139     }
140 }