|
| 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.embulk.output.sftp.provider.sftp; |
| 18 | + |
| 19 | +import com.jcraft.jsch.Session; |
| 20 | +import org.apache.commons.vfs2.FileName; |
| 21 | +import org.apache.commons.vfs2.FileSystem; |
| 22 | +import org.apache.commons.vfs2.FileSystemException; |
| 23 | +import org.apache.commons.vfs2.FileSystemOptions; |
| 24 | +import org.apache.commons.vfs2.UserAuthenticationData; |
| 25 | +import org.apache.commons.vfs2.provider.GenericFileName; |
| 26 | +import org.apache.commons.vfs2.provider.sftp.SftpClientFactory; |
| 27 | +import org.apache.commons.vfs2.util.UserAuthenticatorUtils; |
| 28 | + |
| 29 | +/* |
| 30 | + * We can remove this file when Apache Commons VFS2 removes permission check logic when remote file renaming. |
| 31 | + * https://github.com/embulk/embulk-output-sftp/issues/40 |
| 32 | + * https://github.com/embulk/embulk-output-sftp/pull/44 |
| 33 | + * https://issues.apache.org/jira/browse/VFS-590 |
| 34 | + */ |
| 35 | +/** |
| 36 | + * A provider for accessing files over SFTP. |
| 37 | + */ |
| 38 | +public class SftpFileProvider extends org.apache.commons.vfs2.provider.sftp.SftpFileProvider |
| 39 | +{ |
| 40 | + /** |
| 41 | + * Constructs a new provider. |
| 42 | + */ |
| 43 | + public SftpFileProvider() |
| 44 | + { |
| 45 | + super(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Creates a {@link FileSystem}. |
| 50 | + */ |
| 51 | + @Override |
| 52 | + protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions) |
| 53 | + throws FileSystemException |
| 54 | + { |
| 55 | + // JSch jsch = createJSch(fileSystemOptions); |
| 56 | + |
| 57 | + // Create the file system |
| 58 | + final GenericFileName rootName = (GenericFileName) name; |
| 59 | + |
| 60 | + Session session; |
| 61 | + UserAuthenticationData authData = null; |
| 62 | + try { |
| 63 | + authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES); |
| 64 | + |
| 65 | + session = SftpClientFactory.createConnection(rootName.getHostName(), rootName.getPort(), |
| 66 | + UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, |
| 67 | + UserAuthenticatorUtils.toChar(rootName.getUserName())), |
| 68 | + UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD, |
| 69 | + UserAuthenticatorUtils.toChar(rootName.getPassword())), |
| 70 | + fileSystemOptions); |
| 71 | + } |
| 72 | + catch (final Exception e) { |
| 73 | + throw new FileSystemException("vfs.provider.sftp/connect.error", name, e); |
| 74 | + } |
| 75 | + finally { |
| 76 | + UserAuthenticatorUtils.cleanup(authData); |
| 77 | + } |
| 78 | + |
| 79 | + return new SftpFileSystem(rootName, session, fileSystemOptions); |
| 80 | + } |
| 81 | +} |
0 commit comments