001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2.provider.sftp;
018
019import java.util.Arrays;
020import java.util.Collection;
021import java.util.Collections;
022
023import org.apache.commons.vfs2.Capability;
024import org.apache.commons.vfs2.FileName;
025import org.apache.commons.vfs2.FileSystem;
026import org.apache.commons.vfs2.FileSystemConfigBuilder;
027import org.apache.commons.vfs2.FileSystemException;
028import org.apache.commons.vfs2.FileSystemOptions;
029import org.apache.commons.vfs2.UserAuthenticationData;
030import org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider;
031import org.apache.commons.vfs2.provider.GenericFileName;
032import org.apache.commons.vfs2.util.UserAuthenticatorUtils;
033
034import com.jcraft.jsch.Session;
035
036/**
037 * A provider for accessing files over SFTP.
038 */
039public class SftpFileProvider extends AbstractOriginatingFileProvider {
040    /** User Information. */
041    public static final String ATTR_USER_INFO = "UI";
042
043    /** Authentication types. */
044    public static final UserAuthenticationData.Type[] AUTHENTICATOR_TYPES = new UserAuthenticationData.Type[] {
045            UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD };
046
047    /** The provider's capabilities. */
048    protected static final Collection<Capability> capabilities = Collections.unmodifiableCollection(Arrays
049            .asList(new Capability[] { Capability.CREATE, Capability.DELETE, Capability.RENAME, Capability.GET_TYPE,
050                    Capability.LIST_CHILDREN, Capability.READ_CONTENT, Capability.URI, Capability.WRITE_CONTENT,
051                    Capability.GET_LAST_MODIFIED, Capability.SET_LAST_MODIFIED_FILE, Capability.RANDOM_ACCESS_READ }));
052
053    // private JSch jSch = new JSch();
054
055    /**
056     * Constructs a new provider.
057     */
058    public SftpFileProvider() {
059        super();
060        setFileNameParser(SftpFileNameParser.getInstance());
061    }
062
063    /**
064     * Creates a {@link FileSystem}.
065     */
066    @Override
067    protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
068            throws FileSystemException {
069        // JSch jsch = createJSch(fileSystemOptions);
070
071        // Create the file system
072        final GenericFileName rootName = (GenericFileName) name;
073
074        Session session;
075        UserAuthenticationData authData = null;
076        try {
077            authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES);
078
079            session = SftpClientFactory.createConnection(rootName.getHostName(), rootName.getPort(),
080                    UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME,
081                            UserAuthenticatorUtils.toChar(rootName.getUserName())),
082                    UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD,
083                            UserAuthenticatorUtils.toChar(rootName.getPassword())),
084                    fileSystemOptions);
085        } catch (final Exception e) {
086            throw new FileSystemException("vfs.provider.sftp/connect.error", name, e);
087        } finally {
088            UserAuthenticatorUtils.cleanup(authData);
089        }
090
091        return new SftpFileSystem(rootName, session, fileSystemOptions);
092    }
093
094    /**
095     * Returns the JSch.
096     *
097     * @return Returns the jSch.
098     */
099    /*
100     * private JSch getJSch() { return this.jSch; }
101     */
102
103    /**
104     * Initializes the component.
105     *
106     * @throws FileSystemException if an error occurs.
107     */
108    @Override
109    public void init() throws FileSystemException {
110    }
111
112    @Override
113    public FileSystemConfigBuilder getConfigBuilder() {
114        return SftpFileSystemConfigBuilder.getInstance();
115    }
116
117    @Override
118    public Collection<Capability> getCapabilities() {
119        return capabilities;
120    }
121}