Example usage for org.apache.commons.vfs2 FileObject copyFrom

List of usage examples for org.apache.commons.vfs2 FileObject copyFrom

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 FileObject copyFrom.

Prototype

void copyFrom(FileObject srcFile, FileSelector selector) throws FileSystemException;

Source Link

Document

Copies another file, and all its descendants, to this file.

Usage

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

public static void uploadFile(String accntName, String accntHost, String accntKey, String containerName,
        Path localFile, Path remotePath) throws FileSystemException {
    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    currMan.addProvider(AzConstants.AZSBSCHEME, new AzFileProvider());
    currMan.addProvider("file", new DefaultLocalFileProvider());
    currMan.init();/*from   ww w.  ja v  a  2  s .  co m*/

    StaticUserAuthenticator auth = new StaticUserAuthenticator("", accntName, accntKey);
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    String currUriStr = String.format("%s://%s/%s/%s", AzConstants.AZSBSCHEME, accntHost, containerName,
            remotePath);
    FileObject currFile = currMan.resolveFile(currUriStr, opts);
    FileObject currFile2 = currMan.resolveFile(String.format("file://%s", localFile));

    currFile.copyFrom(currFile2, Selectors.SELECT_SELF);

    currFile.close();
    currMan.close();
}

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

public static void uploadFile(String accntName, String acctHost, String accntKey, String containerName,
        Path localFile, Path remotePath) throws FileSystemException {
    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    currMan.addProvider(SS3Constants.S3SCHEME, new SS3FileProvider());
    currMan.addProvider("file", new DefaultLocalFileProvider());
    currMan.init();//from  w  w  w  .ja  v  a2 s.c  om

    StaticUserAuthenticator auth = new StaticUserAuthenticator("", accntName, accntKey);
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    String currUriStr = String.format("%s://%s/%s/%s", SS3Constants.S3SCHEME, acctHost, containerName,
            remotePath);
    FileObject currFile = currMan.resolveFile(currUriStr, opts);
    FileObject currFile2 = currMan.resolveFile(String.format("file://%s", localFile));

    currFile.copyFrom(currFile2, Selectors.SELECT_SELF);

    currFile.close();
    currMan.close();
}

From source file:fr.cls.atoll.motu.library.misc.ftp.TestFtp.java

public static void testVFS(String user, String pwd, String scheme, String host, String file) {

    StandardFileSystemManager fsManager = null;

    try {/*from  w w  w.  j a v  a  2  s . co  m*/
        fsManager = new StandardFileSystemManager();
        fsManager.setLogger(_LOG);

        StaticUserAuthenticator auth = new StaticUserAuthenticator(null, user, pwd);

        fsManager.setConfiguration(ConfigLoader.getInstance().get(Organizer.getVFSProviderConfig()));
        fsManager.setCacheStrategy(CacheStrategy.ON_RESOLVE);
        // fsManager.addProvider("moi", new DefaultLocalFileProvider());
        fsManager.init();

        FileSystemOptions opts = new FileSystemOptions();
        FileSystemConfigBuilder fscb = fsManager.getFileSystemConfigBuilder(scheme);
        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

        System.out.println(fsManager.getProviderCapabilities(scheme));

        if (fscb instanceof FtpFileSystemConfigBuilder) {
            FtpFileSystemConfigBuilder ftpFscb = (FtpFileSystemConfigBuilder) fscb;
            ftpFscb.setUserDirIsRoot(opts, true);
            ftpFscb.setPassiveMode(opts, true);

        }
        if (fscb instanceof HttpFileSystemConfigBuilder) {
            HttpFileSystemConfigBuilder httpFscb = (HttpFileSystemConfigBuilder) fscb;
            httpFscb.setProxyHost(opts, "proxy.cls.fr");
            httpFscb.setProxyPort(opts, 8080);

        }
        if (fscb instanceof SftpFileSystemConfigBuilder) {
            SftpFileSystemConfigBuilder sftpFscb = (SftpFileSystemConfigBuilder) fscb;
            sftpFscb.setUserDirIsRoot(opts, false);

            // TrustEveryoneUserInfo trustEveryoneUserInfo = new TrustEveryoneUserInfo();
            // trustEveryoneUserInfo.promptYesNo("eddfsdfs");
            // sftpFscb.setUserInfo(opts, new TrustEveryoneUserInfo());
            sftpFscb.setTimeout(opts, 5000);
            // SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
            // SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");

        }
        // FileObject fo =
        // fsManager.resolveFile("ftp://ftp.cls.fr/pub/oceano/AVISO/NRT-SLA/maps/rt/j2/h/msla_rt_j2_err_21564.nc.gz",
        // opts);

        // String uri = String.format("%s://%s/%s", scheme, host, file);
        // String uri = String.format("%s://%s/", scheme, host);
        // FileObject originBase = fsManager.resolveFile(uri, opts);
        // fsManager.setBaseFile(originBase);

        File tempDir = new File("c:/tempVFS");
        // File tempFile = File.createTempFile("AsciiEnvisat", ".txt", tempDir);
        File hostFile = new File(file);
        String fileName = hostFile.getName();
        File newFile = new File(tempDir, fileName);
        newFile.createNewFile();

        DefaultFileReplicator dfr = new DefaultFileReplicator(tempDir);
        fsManager.setTemporaryFileStore(dfr);
        // System.out.println(fsManager.getBaseFile());
        // System.out.println(dfr);
        // System.out.println(fsManager.getTemporaryFileStore());

        // FileObject ff = fsManager.resolveFile("sftp://t:t@CLS-EARITH.pc.cls.fr/AsciiEnvisat.txt",
        // opts);
        String uri = String.format("%s://%s/%s", scheme, host, file);
        FileObject ff = fsManager.resolveFile(uri, opts);
        FileObject dest = fsManager.toFileObject(newFile);

        //ff.getContent().getInputStream();
        dest.copyFrom(ff, Selectors.SELECT_ALL);
        //dest.copyFrom(ff, Selectors.SELECT_ALL);

        //            
        // URL url = ff.getURL();
        //            
        // url.openConnection();
        // URLConnection conn = url.openConnection();
        // InputStream in = conn.getInputStream();
        // in.close();

        // InputStream in = ff.getContent().getInputStream();

    } catch (FileSystemException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MotuException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        // fsManager.close();
        // fsManager.freeUnusedResources();
    }

}

From source file:com.collective.celos.ci.mode.test.TestRunCelosServerModeEmbedded.java

public void copyRemoteDefaultsToLocal(String username, URI defaultsDirUri)
        throws URISyntaxException, FileSystemException {
    JScpWorker worker = new JScpWorker(username);
    if (defaultsDirUri != null) {
        FileObject remoteDefaultsDir = worker.getFileObjectByUri(defaultsDirUri);
        if (remoteDefaultsDir.exists()) {
            FileObject localDefaultsDir = worker.getFileObjectByUri(getCelosDefaultsDir());
            localDefaultsDir.copyFrom(remoteDefaultsDir, Selectors.SELECT_CHILDREN);
        }// w  ww . j  av  a  2  s .co m
    }
}

From source file:binky.reportrunner.engine.utils.impl.FileSystemHandlerImpl.java

public void copyFile(String url, String destinationUrl) throws IOException {

    FileObject dest = fsManager.resolveFile(destinationUrl);
    FileObject src = fsManager.resolveFile(url);
    dest.copyFrom(src, Selectors.SELECT_ALL);
}

From source file:com.collective.celos.ci.deploy.WorkflowFilesDeployer.java

private void deployJSFile(URI dirUri, String fileName) throws URISyntaxException, IOException {
    if (dirUri != null) {
        URI fileUri = getTargetJsFileUri(dirUri);
        File localFile = new File(config.getDeployDir(), fileName);

        if (localFile.exists()) {
            parser.validateJsSyntax(new FileReader(localFile), localFile.getName());

            FileObject sftpFile = jScpWorker.getFileObjectByUri(fileUri);
            FileObject localFileObject = jScpWorker.getFileObjectByUri(localFile.getAbsolutePath());
            sftpFile.copyFrom(localFileObject, Selectors.SELECT_SELF);
        }/*from  w w w  .j a va2  s . c  om*/
    }
}

From source file:com.stehno.sanctuary.core.remote.FileSystemRemoteStore.java

private void storeFile(File rootDirectory, File file, String action) {
    try {/* w w w.  ja v a 2s .co m*/
        FileObject remoteFile = findRemoteFile(rootDirectory, file);

        // TODO: if this fails, it could be a partial copy, save original?
        remoteFile.copyFrom(fileSystemManager.toFileObject(file), new AllFileSelector());

        if (log.isDebugEnabled())
            log.debug(action + ": " + remoteFile);

    } catch (FileSystemException e) {
        // FIXME: error
        e.printStackTrace();
    }
}

From source file:hpmonitoringaudit.sftp.java

public boolean startFTP(String propertiesFilename, String fileToDownload) {

    props = new Properties();
    StandardFileSystemManager manager = new StandardFileSystemManager();

    try {//  w  w w . ja va2s.  c o m

        props.load(new FileInputStream("properties/" + propertiesFilename));
        String serverAddress = props.getProperty("serverAddress").trim();
        String userId = props.getProperty("userId").trim();
        String password = props.getProperty("password").trim();
        String remoteDirectory = props.getProperty("remoteDirectory").trim();
        String localDirectory = props.getProperty("localDirectory").trim();

        //Initializes the file manager
        manager.init();

        //Setup our SFTP configuration
        FileSystemOptions opts = new FileSystemOptions();
        SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
        SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
        SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);

        //Create the SFTP URI using the host name, userid, password,  remote path and file name
        String sftpUri = "sftp://" + userId + ":" + password + "@" + serverAddress + "/" + remoteDirectory
                + fileToDownload;

        // Create local file object
        String filepath = localDirectory + fileToDownload;
        File file = new File(filepath);
        FileObject localFile = manager.resolveFile(file.getAbsolutePath());

        // Create remote file object
        FileObject remoteFile = manager.resolveFile(sftpUri, opts);

        // Copy local file to sftp server
        localFile.copyFrom(remoteFile, Selectors.SELECT_SELF);
        System.out.println("File download successful");

    } catch (Exception ex) {
        ex.printStackTrace();
        return false;
    } finally {
        manager.close();
    }

    return true;
}

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;/* w  ww.  jav a  2s  .c  om*/

    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.azure.AzFileProviderTest.java

/**
 * /*from   www  .  j a  va2  s.c o m*/
 */
@Test
public void A001_uploadFile() 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;

    File temp = File.createTempFile("uploadFile01", ".tmp");
    try (FileWriter fw = new FileWriter(temp)) {
        BufferedWriter bw = new BufferedWriter(fw);
        bw.append("testing...");
        bw.flush();
    }

    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);
    FileObject currFile2 = currMan.resolveFile(String.format("file://%s", temp.getAbsolutePath()));

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