Java Code Examples for org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder

The following examples show how to use org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder. These examples are extracted from open source projects. 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 Project: otroslogviewer   Source File: VFSUtils.java    License: 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 Project: commons-vfs   Source File: AbstractSftpProviderTestCase.java    License: 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
@Test
public void testDelegatingGood() throws Throwable {
    final String[] identityPaths = new String[] { "/file1", "/file2", };

    final FileSystemOptions opts = new FileSystemOptions();
    final DelegatingFileSystemOptionsBuilder delgate = new DelegatingFileSystemOptionsBuilder(fsm);

    delgate.setConfigString(opts, "http", "proxyHost", "proxy");
    delgate.setConfigString(opts, "http", "proxyPort", "8080");
    delgate.setConfigClass(opts, "sftp", "userinfo", TrustEveryoneUserInfo.class);
    delgate.setConfigStrings(opts, "sftp", "identities", identityPaths);

    assertEquals("http.proxyHost", HttpFileSystemConfigBuilder.getInstance().getProxyHost(opts), "proxy");
    assertEquals("http.proxyPort", HttpFileSystemConfigBuilder.getInstance().getProxyPort(opts), 8080);
    assertSame("sftp.userInfo", SftpFileSystemConfigBuilder.getInstance().getUserInfo(opts).getClass(),
            TrustEveryoneUserInfo.class);

    final File identities[] = SftpFileSystemConfigBuilder.getInstance().getIdentities(opts);
    assertNotNull("sftp.identities", identities);
    assertEquals("sftp.identities size", identities.length, identityPaths.length);
    for (int iterIdentities = 0; iterIdentities < identities.length; iterIdentities++) {
        assertEquals("sftp.identities #" + iterIdentities, identities[iterIdentities].getAbsolutePath(),
                new File(identityPaths[iterIdentities]).getAbsolutePath());
    }
}
 
Example 4
@Test
public void recognizesAndSetsIdentityKeyFile() throws Exception {
  File tempFile = File.createTempFile( "KettleSftpFileSystemConfigBuilderTest", ".tmp" );
  tempFile.deleteOnExit();

  final String fullName = "vfs.sftp.identity";
  final String name = fullName.substring( "vfs.sftp.".length() );
  final String vfsInternalName = SftpFileSystemConfigBuilder.class.getName() + ".IDENTITIES";

  final FileSystemOptions opts = new FileSystemOptions();
  KettleSftpFileSystemConfigBuilder builder = KettleSftpFileSystemConfigBuilder.getInstance();
  builder.setParameter( opts, name, tempFile.getAbsolutePath(), fullName, "sftp://fake-url:22" );

  Method getOption = ReflectionUtils.findMethod( opts.getClass(), "getOption", Class.class, String.class );
  getOption.setAccessible( true );
  Object value = ReflectionUtils.invokeMethod( getOption, opts, builder.getConfigClass(), vfsInternalName );
  assertEquals( IdentityInfo[].class, value.getClass() );
  assertEquals( tempFile.getAbsolutePath(), ( (IdentityInfo[]) value )[0].getPrivateKey().getAbsolutePath() );
}
 
Example 5
Source Project: otroslogviewer   Source File: SftpUserAuthenticator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void getAuthenticationData(UserAuthenticationData authenticationData) {
  super.getAuthenticationData(authenticationData);
  authenticationData.setData(UserAuthenticationDataWrapper.SSH_KEY, sshKeyFileField.getText().trim().toCharArray());

  if (StringUtils.isNotBlank(sshKeyFileField.getText())) {
    try {
      SftpFileSystemConfigBuilder.getInstance().setIdentities(getFileSystemOptions(), new File[]{new File(sshKeyFileField.getText())});
      //TODO set user auth data file path
    } catch (FileSystemException e) {
      e.printStackTrace();
    }
  }

}
 
Example 6
Source Project: celos   Source File: JScpWorker.java    License: Apache License 2.0 5 votes vote down vote up
public FileSystemOptions getSftpDefaultOptions() throws FileSystemException {
    FileSystemOptions opts = new FileSystemOptions();
    SftpFileSystemConfigBuilder.getInstance().setPreferredAuthentications(opts, DEFAULT_SECURITY_SETTINGS);
    SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
    SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, false);
    return opts;
}
 
Example 7
@Test
public void recognizesAndSetsUserHomeDirProperty() throws Exception {
  final String fullName = Const.VFS_USER_DIR_IS_ROOT;
  final String name = fullName.substring( "vfs.sftp.".length() );
  final String vfsInternalName = SftpFileSystemConfigBuilder.class.getName() + ".USER_DIR_IS_ROOT";

  final FileSystemOptions opts = new FileSystemOptions();
  KettleSftpFileSystemConfigBuilder builder = KettleSftpFileSystemConfigBuilder.getInstance();
  builder.setParameter( opts, name, "true", fullName, "sftp://fake-url:22" );

  Method getOption = ReflectionUtils.findMethod( opts.getClass(), "getOption", Class.class, String.class );
  getOption.setAccessible( true );
  Object value = ReflectionUtils.invokeMethod( getOption, opts, builder.getConfigClass(), vfsInternalName );
  assertEquals( true, value );
}
 
Example 8
@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://%[email protected]:%d", userInfo, parsedURI.getPort());
    }


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