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: */
017: package org.apache.commons.vfs.provider.sftp;
018:
019: import com.jcraft.jsch.Session;
020: import org.apache.commons.vfs.Capability;
021: import org.apache.commons.vfs.FileName;
022: import org.apache.commons.vfs.FileSystem;
023: import org.apache.commons.vfs.FileSystemConfigBuilder;
024: import org.apache.commons.vfs.FileSystemException;
025: import org.apache.commons.vfs.FileSystemOptions;
026: import org.apache.commons.vfs.UserAuthenticationData;
027: import org.apache.commons.vfs.util.UserAuthenticatorUtils;
028: import org.apache.commons.vfs.provider.AbstractOriginatingFileProvider;
029: import org.apache.commons.vfs.provider.GenericFileName;
030:
031: import java.util.Arrays;
032: import java.util.Collection;
033: import java.util.Collections;
034:
035: /**
036: * A provider for accessing files over SFTP.
037: *
038: * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
039: * @author Gary D. Gregory
040: * @version $Id: SftpFileProvider.java 480428 2006-11-29 06:15:24Z bayard $
041: */
042: public class SftpFileProvider extends AbstractOriginatingFileProvider {
043: protected final static Collection capabilities = Collections
044: .unmodifiableCollection(Arrays.asList(new Capability[] {
045: Capability.CREATE, Capability.DELETE,
046: Capability.RENAME, Capability.GET_TYPE,
047: Capability.LIST_CHILDREN, Capability.READ_CONTENT,
048: Capability.URI, Capability.WRITE_CONTENT,
049: Capability.GET_LAST_MODIFIED,
050: Capability.SET_LAST_MODIFIED_FILE,
051: Capability.RANDOM_ACCESS_READ }));
052:
053: public final static String ATTR_USER_INFO = "UI";
054:
055: public final static UserAuthenticationData.Type[] AUTHENTICATOR_TYPES = new UserAuthenticationData.Type[] {
056: UserAuthenticationData.USERNAME,
057: UserAuthenticationData.PASSWORD };
058:
059: // private JSch jSch = new JSch();
060:
061: public SftpFileProvider() {
062: super ();
063: setFileNameParser(SftpFileNameParser.getInstance());
064: }
065:
066: /**
067: * Creates a {@link FileSystem}.
068: */
069: protected FileSystem doCreateFileSystem(final FileName name,
070: final FileSystemOptions fileSystemOptions)
071: throws FileSystemException {
072: // JSch jsch = createJSch(fileSystemOptions);
073:
074: // Create the file system
075: final GenericFileName rootName = (GenericFileName) name;
076:
077: Session session;
078: UserAuthenticationData authData = null;
079: try {
080: authData = UserAuthenticatorUtils.authenticate(
081: fileSystemOptions, AUTHENTICATOR_TYPES);
082:
083: session = SftpClientFactory
084: .createConnection(rootName.getHostName(), rootName
085: .getPort(), UserAuthenticatorUtils.getData(
086: authData, UserAuthenticationData.USERNAME,
087: UserAuthenticatorUtils.toChar(rootName
088: .getUserName())),
089: UserAuthenticatorUtils.getData(authData,
090: UserAuthenticationData.PASSWORD,
091: UserAuthenticatorUtils
092: .toChar(rootName
093: .getPassword())),
094: fileSystemOptions);
095: } catch (final Exception e) {
096: throw new FileSystemException(
097: "vfs.provider.sftp/connect.error", name, e);
098: } finally {
099: UserAuthenticatorUtils.cleanup(authData);
100: }
101:
102: return new SftpFileSystem(rootName, session, fileSystemOptions);
103: }
104:
105: /**
106: * Returns the JSch.
107: *
108: * @return Returns the jSch.
109: */
110: /*
111: private JSch getJSch()
112: {
113: return this.jSch;
114: }
115: */
116:
117: /**
118: * Initialises the component.
119: */
120: public void init() throws FileSystemException {
121: }
122:
123: public FileSystemConfigBuilder getConfigBuilder() {
124: return SftpFileSystemConfigBuilder.getInstance();
125: }
126:
127: public Collection getCapabilities() {
128: return capabilities;
129: }
130: }
|