Java Code Examples for org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder#getInstance()

The following examples show how to use org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder#getInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VFSUtils.java    From otroslogviewer with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a file representation
 *
 * @param filePath The file path
 * @return a file representation
 * @throws FileSystemException
 */
public static FileObject resolveFileObject(String filePath) throws FileSystemException {
  LOGGER.info("Resolving file: {}", filePath);
  if (filePath.startsWith("sftp://")) {
    SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance();
    builder.setStrictHostKeyChecking(opts, "no");
    builder.setUserDirIsRoot(opts, false);
    builder.setCompression(opts, "zlib,none");

  } else if (filePath.startsWith("smb://")) {

  } else if (filePath.startsWith("ftp://")) {
    FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true);
  }
  UserAuthenticatorFactory factory = new UserAuthenticatorFactory();

  OtrosUserAuthenticator authenticator = factory.getUiUserAuthenticator(persistentAuthStore, sessionAuthStore, filePath, opts);

  if (pathContainsCredentials(filePath)) {
    authenticator = null;
  }
  return resolveFileObject(filePath, opts, authenticator, persistentAuthStore, sessionAuthStore);
}
 
Example 2
Source File: AbstractSftpProviderTestCase.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the base folder for tests.
 */
@Override
public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
    String uri = getSystemTestUriOverride();
    if (uri == null) {
        uri = ConnectionUri;
    }

    final FileSystemOptions fileSystemOptions = new FileSystemOptions();
    final SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance();
    builder.setStrictHostKeyChecking(fileSystemOptions, "no");
    builder.setUserInfo(fileSystemOptions, new TrustEveryoneUserInfo());
    builder.setIdentityRepositoryFactory(fileSystemOptions, new TestIdentityRepositoryFactory());
    final FileObject fileObject = manager.resolveFile(uri, fileSystemOptions);
    this.fileSystem = (SftpFileSystem) fileObject.getFileSystem();
    return fileObject;
}
 
Example 3
Source File: SftpProviderStreamProxyModeTestCase.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
@Override
public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
    String uri = getSystemTestUriOverride();
    if (uri == null) {
        uri = ConnectionUri;
    }

    final FileSystemOptions fileSystemOptions = new FileSystemOptions();
    final SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance();
    builder.setStrictHostKeyChecking(fileSystemOptions, "no");
    builder.setUserInfo(fileSystemOptions, new TrustEveryoneUserInfo());
    builder.setIdentityRepositoryFactory(fileSystemOptions, new TestIdentityRepositoryFactory());

    final FileSystemOptions proxyOptions = (FileSystemOptions) fileSystemOptions.clone();

    final URI parsedURI = new URI(uri);
    final String userInfo = parsedURI.getUserInfo();
    final String[] userFields = userInfo == null ? null : userInfo.split(":", 2);

    builder.setProxyType(fileSystemOptions, SftpFileSystemConfigBuilder.PROXY_STREAM);
    if (userFields != null) {
        if (userFields.length > 0) {
            builder.setProxyUser(fileSystemOptions, userFields[0]);
        }
        if (userFields.length > 1) {
            builder.setProxyPassword(fileSystemOptions, userFields[1]);
        }
    }
    builder.setProxyHost(fileSystemOptions, parsedURI.getHost());
    builder.setProxyPort(fileSystemOptions, parsedURI.getPort());
    builder.setProxyCommand(fileSystemOptions, SftpStreamProxy.NETCAT_COMMAND);
    builder.setProxyOptions(fileSystemOptions, proxyOptions);
    builder.setProxyPassword(fileSystemOptions, parsedURI.getAuthority());

    // Set up the new URI
    if (userInfo == null) {
        uri = String.format("sftp://localhost:%d", parsedURI.getPort());
    } else {
        uri = String.format("sftp://%s@localhost:%d", userInfo, parsedURI.getPort());
    }


    final FileObject fileObject = manager.resolveFile(uri, fileSystemOptions);
    this.fileSystem = (SftpFileSystem) fileObject.getFileSystem();
    return fileObject;
}