Example usage for org.apache.commons.vfs2.auth StaticUserAuthenticator StaticUserAuthenticator

List of usage examples for org.apache.commons.vfs2.auth StaticUserAuthenticator StaticUserAuthenticator

Introduction

In this page you can find the example usage for org.apache.commons.vfs2.auth StaticUserAuthenticator StaticUserAuthenticator.

Prototype

public StaticUserAuthenticator(final String domain, final String username, final String password) 

Source Link

Usage

From source file:com.sludev.commons.vfs2.provider.azure.AzFileProviderTest.java

@Test
public void A002_downloadFile() throws Exception {
    String currAccountStr = testProperties.getProperty("azure.account.name");
    String currKey = testProperties.getProperty("azure.account.key");
    String currContainerStr = testProperties.getProperty("azure.test0001.container.name");
    String currHost = testProperties.getProperty("azure.host"); // <account>.blob.core.windows.net
    String currFileNameStr;/*from   w  w w.  j a  va2  s  . c o m*/

    File temp = File.createTempFile("downloadFile01", ".tmp");

    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    currMan.addProvider(AzConstants.AZSBSCHEME, new AzFileProvider());
    currMan.addProvider("file", new DefaultLocalFileProvider());
    currMan.init();

    StaticUserAuthenticator auth = new StaticUserAuthenticator("", currAccountStr, currKey);
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    currFileNameStr = "test01.tmp";
    String currUriStr = String.format("%s://%s/%s/%s", AzConstants.AZSBSCHEME, currHost, currContainerStr,
            currFileNameStr);
    FileObject currFile = currMan.resolveFile(currUriStr, opts);

    String destStr = String.format("file://%s", temp.getAbsolutePath());
    FileObject currFile2 = currMan.resolveFile(destStr);

    log.info(String.format("copying '%s' to '%s'", currUriStr, destStr));

    currFile2.copyFrom(currFile, Selectors.SELECT_SELF);
}

From source file:com.sludev.commons.vfs2.provider.s3.SS3FileProviderTest.java

/**
 * Download a previously uploaded file from the test bucket.
 * @throws Exception //  w  ww .j  a  va2 s.  c  om
 */
@Test
public void A002_downloadFile() throws Exception {
    String currAccountStr = testProperties.getProperty("s3.access.id");
    String currKey = testProperties.getProperty("s3.access.secret");
    String currContainerStr = testProperties.getProperty("s3.test0001.bucket.name");
    String currHost = testProperties.getProperty("s3.host");
    String currRegion = testProperties.getProperty("s3.region");
    String currFileNameStr;

    SS3FileProvider currSS3 = new SS3FileProvider();

    // Optional set endpoint
    //currSS3.setEndpoint(currHost);

    // Optional set region
    //currSS3.setRegion(currRegion);

    File temp = File.createTempFile("downloadFile01", ".tmp");

    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    currMan.addProvider(SS3Constants.S3SCHEME, currSS3);
    currMan.addProvider("file", new DefaultLocalFileProvider());
    currMan.init();

    StaticUserAuthenticator auth = new StaticUserAuthenticator("", currAccountStr, currKey);
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    currFileNameStr = "test01.tmp";
    String currUriStr = String.format("%s://%s/%s/%s", SS3Constants.S3SCHEME, currHost, currContainerStr,
            currFileNameStr);
    FileObject currFile = currMan.resolveFile(currUriStr, opts);

    String destStr = String.format("file://%s", temp.getAbsolutePath());
    FileObject currFile2 = currMan.resolveFile(destStr);

    log.info(String.format("copying '%s' to '%s'", currUriStr, destStr));

    currFile2.copyFrom(currFile, Selectors.SELECT_SELF);
}

From source file:com.sludev.commons.vfs2.provider.azure.AzFileProviderTest.java

@Test
public void A003_exist() throws Exception {
    String currAccountStr = testProperties.getProperty("azure.account.name");
    String currKey = testProperties.getProperty("azure.account.key");
    String currContainerStr = testProperties.getProperty("azure.test0001.container.name");
    String currHost = testProperties.getProperty("azure.host"); // <account>.blob.core.windows.net
    String currFileNameStr;//from   w ww.  ja  v  a 2  s  .  co  m

    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    currMan.addProvider(AzConstants.AZSBSCHEME, new AzFileProvider());
    currMan.init();

    StaticUserAuthenticator auth = new StaticUserAuthenticator("", currAccountStr, currKey);
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    currFileNameStr = "test01.tmp";
    String currUriStr = String.format("%s://%s/%s/%s", AzConstants.AZSBSCHEME, currHost, currContainerStr,
            currFileNameStr);
    FileObject currFile = currMan.resolveFile(currUriStr, opts);

    log.info(String.format("exist() file '%s'", currUriStr));

    Boolean existRes = currFile.exists();
    Assert.assertTrue(existRes);

    currFileNameStr = "non-existant-file-8632857264.tmp";
    currUriStr = String.format("%s://%s/%s/%s", AzConstants.AZSBSCHEME, currAccountStr, currContainerStr,
            currFileNameStr);
    currFile = currMan.resolveFile(currUriStr, opts);

    log.info(String.format("exist() file '%s'", currUriStr));

    existRes = currFile.exists();
    Assert.assertFalse(existRes);
}

From source file:com.streamsets.pipeline.stage.origin.remote.RemoteDownloadSource.java

@Override
public List<ConfigIssue> init() {

    List<ConfigIssue> issues = super.init();
    errorRecordHandler = new DefaultErrorRecordHandler(getContext());

    conf.dataFormatConfig.init(getContext(), conf.dataFormat, Groups.REMOTE.getLabel(),
            DATA_FORMAT_CONFIG_PREFIX, issues);

    try {/* ww w.  j  a va 2 s  . c  o  m*/
        this.remoteURI = new URI(conf.remoteAddress);
    } catch (Exception ex) {
        issues.add(getContext().createConfigIssue(Groups.REMOTE.getLabel(), CONF_PREFIX + "remoteAddress",
                Errors.REMOTE_01, conf.remoteAddress));
    }

    try {
        FileSystemManager fsManager = VFS.getManager();
        // If password is not specified, add the username to the URI
        switch (conf.auth) {
        case PRIVATE_KEY:
            String schemeBase = remoteURI.getScheme() + "://";
            remoteURI = new URI(
                    schemeBase + conf.username + "@" + remoteURI.toString().substring(schemeBase.length()));
            File privateKeyFile = new File(conf.privateKey);
            if (!privateKeyFile.exists() || !privateKeyFile.isFile() || !privateKeyFile.canRead()) {
                issues.add(getContext().createConfigIssue(Groups.CREDENTIALS.getLabel(),
                        CONF_PREFIX + "privateKey", Errors.REMOTE_10, conf.privateKey));
            } else {
                if (!remoteURI.getScheme().equals("sftp")) {
                    issues.add(getContext().createConfigIssue(Groups.CREDENTIALS.getLabel(),
                            CONF_PREFIX + "privateKey", Errors.REMOTE_11));
                } else {
                    SftpFileSystemConfigBuilder.getInstance().setIdentities(options,
                            new File[] { privateKeyFile });
                    if (conf.privateKeyPassphrase != null && !conf.privateKeyPassphrase.isEmpty()) {
                        SftpFileSystemConfigBuilder.getInstance().setUserInfo(options,
                                new SDCUserInfo(conf.privateKeyPassphrase));
                    }
                }
            }
            break;
        case PASSWORD:
            StaticUserAuthenticator auth = new StaticUserAuthenticator(remoteURI.getHost(), conf.username,
                    conf.password);
            DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(options, auth);
            break;
        default:
            break;
        }

        if (remoteURI.getScheme().equals("ftp")) {
            FtpFileSystemConfigBuilder.getInstance().setPassiveMode(options, true);
            FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(options, conf.userDirIsRoot);
            if (conf.strictHostChecking) {
                issues.add(getContext().createConfigIssue(Groups.CREDENTIALS.getLabel(),
                        CONF_PREFIX + "strictHostChecking", Errors.REMOTE_12));
            }
        }

        if (remoteURI.getScheme().equals("sftp")) {
            SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(options, conf.userDirIsRoot);
            if (conf.strictHostChecking) {
                if (knownHostsFile != null) {
                    if (knownHostsFile.exists() && knownHostsFile.isFile() && knownHostsFile.canRead()) {
                        SftpFileSystemConfigBuilder.getInstance().setKnownHosts(options, knownHostsFile);
                        SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(options, "yes");
                    } else {
                        issues.add(getContext().createConfigIssue(Groups.CREDENTIALS.getLabel(),
                                CONF_PREFIX + "knownHosts", Errors.REMOTE_06, knownHostsFile));
                    }

                } else {
                    issues.add(getContext().createConfigIssue(Groups.CREDENTIALS.getLabel(),
                            CONF_PREFIX + "strictHostChecking", Errors.REMOTE_07));
                }
            } else {
                SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(options, "no");
            }
        }

        if (issues.isEmpty()) {
            // To ensure we can connect, else we fail validation.
            remoteDir = fsManager.resolveFile(remoteURI.toString(), options);
        }

    } catch (FileSystemException | URISyntaxException ex) {
        issues.add(getContext().createConfigIssue(Groups.REMOTE.getLabel(), CONF_PREFIX + "remoteAddress",
                Errors.REMOTE_08, conf.remoteAddress));
        LOG.error("Error trying to login to remote host", ex);
    }
    return issues;
}

From source file:com.sludev.commons.vfs2.provider.s3.SS3FileProviderTest.java

@Test
public void A003_exist() throws Exception {
    String currAccountStr = testProperties.getProperty("s3.access.id");
    String currKey = testProperties.getProperty("s3.access.secret");
    String currContainerStr = testProperties.getProperty("s3.test0001.bucket.name");
    String currHost = testProperties.getProperty("s3.host");
    String currRegion = testProperties.getProperty("s3.region");
    String currFileNameStr;//  w  w  w  .jav  a  2  s.  c  o  m

    SS3FileProvider currSS3 = new SS3FileProvider();

    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    currMan.addProvider(SS3Constants.S3SCHEME, currSS3);
    currMan.init();

    StaticUserAuthenticator auth = new StaticUserAuthenticator("", currAccountStr, currKey);
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    currFileNameStr = "test01.tmp";
    String currUriStr = String.format("%s://%s/%s/%s", SS3Constants.S3SCHEME, currHost, currContainerStr,
            currFileNameStr);
    FileObject currFile = currMan.resolveFile(currUriStr, opts);

    log.info(String.format("exist() file '%s'", currUriStr));

    Boolean existRes = currFile.exists();
    Assert.assertTrue(existRes);

    currFileNameStr = "non-existant-file-8632857264.tmp";
    currUriStr = String.format("%s://%s/%s/%s", SS3Constants.S3SCHEME, currHost, currContainerStr,
            currFileNameStr);
    currFile = currMan.resolveFile(currUriStr, opts);

    log.info(String.format("exist() file '%s'", currUriStr));

    existRes = currFile.exists();
    Assert.assertFalse(existRes);
}

From source file:com.sludev.commons.vfs2.provider.azure.AzFileProviderTest.java

@Test
public void A004_getContentSize() throws Exception {
    String currAccountStr = testProperties.getProperty("azure.account.name");
    String currKey = testProperties.getProperty("azure.account.key");
    String currContainerStr = testProperties.getProperty("azure.test0001.container.name");
    String currHost = testProperties.getProperty("azure.host"); // <account>.blob.core.windows.net
    String currFileNameStr;/*from   ww  w .  j  av  a2 s .c  o m*/

    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    currMan.addProvider(AzConstants.AZSBSCHEME, new AzFileProvider());
    currMan.init();

    StaticUserAuthenticator auth = new StaticUserAuthenticator("", currAccountStr, currKey);
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    currFileNameStr = "test01.tmp";
    String currUriStr = String.format("%s://%s/%s/%s", AzConstants.AZSBSCHEME, currHost, currContainerStr,
            currFileNameStr);
    FileObject currFile = currMan.resolveFile(currUriStr, opts);

    log.info(String.format("exist() file '%s'", currUriStr));

    FileContent cont = currFile.getContent();
    long contSize = cont.getSize();

    Assert.assertTrue(contSize > 0);

}

From source file:fr.cls.atoll.motu.library.misc.vfs.VFSManager.java

/**
 * Sets the user info./*from www .j a v  a 2 s . c  om*/
 * 
 * @param user the user
 * @param pwd the pwd
 * @param fileSystemOptions the file system options
 * @return the file system options
 * @throws MotuException the motu exception
 */
public FileSystemOptions setUserInfo(String user, String pwd, FileSystemOptions fileSystemOptions)
        throws MotuException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("setUserInfo(String, String, FileSystemOptions) - start");
    }

    if (fileSystemOptions == null) {
        fileSystemOptions = new FileSystemOptions();
    }
    StaticUserAuthenticator auth = new StaticUserAuthenticator(null, user, pwd);

    FileSystemOptions returnFileSystemOptions = setUserInfo(auth);
    if (LOG.isDebugEnabled()) {
        LOG.debug("setUserInfo(String, String, FileSystemOptions) - end");
    }
    return returnFileSystemOptions;

}

From source file:com.sludev.commons.vfs2.provider.s3.SS3FileProviderTest.java

@Test
public void A004_getContentSize() throws Exception {
    String currAccountStr = testProperties.getProperty("s3.access.id");
    String currKey = testProperties.getProperty("s3.access.secret");
    String currContainerStr = testProperties.getProperty("s3.test0001.bucket.name");
    String currHost = testProperties.getProperty("s3.host");
    String currRegion = testProperties.getProperty("s3.region");
    String currFileNameStr;/*w  ww .  j  a v  a 2  s.c o m*/

    SS3FileProvider currSS3 = new SS3FileProvider();

    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    currMan.addProvider(SS3Constants.S3SCHEME, currSS3);
    currMan.init();

    StaticUserAuthenticator auth = new StaticUserAuthenticator("", currAccountStr, currKey);
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    currFileNameStr = "test01.tmp";
    String currUriStr = String.format("%s://%s/%s/%s", SS3Constants.S3SCHEME, currHost, currContainerStr,
            currFileNameStr);
    FileObject currFile = currMan.resolveFile(currUriStr, opts);

    log.info(String.format("getContent() file '%s'", currUriStr));

    FileContent cont = currFile.getContent();
    long contSize = cont.getSize();

    Assert.assertTrue(contSize > 0);

}

From source file:com.sludev.commons.vfs2.provider.azure.AzFileProviderTest.java

/**
 * By default FileObject.getChildren() will use doListChildrenResolved() if available
 * /*w w  w .  j  a  v a  2  s  .c  o  m*/
 * @throws Exception 
 */
@Test
public void A005_listChildren() throws Exception {
    String currAccountStr = testProperties.getProperty("azure.account.name");
    String currKey = testProperties.getProperty("azure.account.key");
    String currContainerStr = testProperties.getProperty("azure.test0001.container.name");
    String currHost = testProperties.getProperty("azure.host"); // <account>.blob.core.windows.net

    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    currMan.addProvider(AzConstants.AZSBSCHEME, new AzFileProvider());
    currMan.init();

    StaticUserAuthenticator auth = new StaticUserAuthenticator("", currAccountStr, currKey);
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    String currFileNameStr = "uploadFile02";
    String currUriStr = String.format("%s://%s/%s/%s", AzConstants.AZSBSCHEME, currHost, currContainerStr,
            currFileNameStr);
    FileObject currFile = currMan.resolveFile(currUriStr, opts);

    FileObject[] currObjs = currFile.getChildren();
    for (FileObject obj : currObjs) {
        FileName currName = obj.getName();
        Boolean res = obj.exists();
        FileType ft = obj.getType();

        log.info(String.format("\nNAME.PATH : '%s'\nEXISTS : %b\nTYPE : %s\n\n", currName.getPath(), res, ft));
    }
}

From source file:com.sludev.commons.vfs2.provider.s3.SS3FileProviderTest.java

@Test
public void A005_testContent() throws Exception {
    String currAccountStr = testProperties.getProperty("s3.access.id");
    String currKey = testProperties.getProperty("s3.access.secret");
    String currContainerStr = testProperties.getProperty("s3.test0001.bucket.name");
    String currHost = testProperties.getProperty("s3.host");
    String currRegion = testProperties.getProperty("s3.region");
    String currFileNameStr;// w  ww .  jav  a2s  .  c o m

    SS3FileProvider currSS3 = new SS3FileProvider();

    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    currMan.addProvider(SS3Constants.S3SCHEME, currSS3);
    currMan.init();

    StaticUserAuthenticator auth = new StaticUserAuthenticator("", currAccountStr, currKey);
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    currFileNameStr = "file05";
    String currUriStr = String.format("%s://%s/%s/%s", SS3Constants.S3SCHEME, currHost, currContainerStr,
            currFileNameStr);
    FileObject currFile = currMan.resolveFile(currUriStr, opts);

    FileContent content = currFile.getContent();
    long size = content.getSize();
    Assert.assertTrue(size >= 0);

    long modTime = content.getLastModifiedTime();
    Assert.assertTrue(modTime > 0);
}