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

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

Introduction

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

Prototype

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

Source Link

Document

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

Usage

From source file:egovframework.rte.fdl.filehandling.EgovFileUtil.java

/**
 * <p>//ww w  .  j a v  a 2s.co  m
 *  ? ?? ?  .
 * </p>
 * @param source
 *        <code>String</code>
 * @param target
 *        <code>String</code>
 * @throws Exception
 */
public static void cp(String source, String target) throws Exception {

    try {
        final FileObject src = manager.resolveFile(basefile, source);
        FileObject dest = manager.resolveFile(basefile, target);

        if (dest.exists() && dest.getType() == FileType.FOLDER) {
            dest = dest.resolveFile(src.getName().getBaseName());
        }

        dest.copyFrom(src, Selectors.SELECT_ALL);
    } catch (FileSystemException fse) {
        log.error(fse.toString());
        ;
        throw new FileSystemException(fse);
    }
}

From source file:com.thinkberg.moxo.dav.CopyHandler.java

protected void copyOrMove(FileObject object, FileObject target, int depth) throws FileSystemException {
    target.copyFrom(object, new DepthFileSelector(depth));
}

From source file:com.thinkberg.webdav.CopyHandler.java

protected void copyOrMove(FileObject object, FileObject target, final int depth) throws FileSystemException {
    target.copyFrom(object, new FileSelector() {
        public boolean includeFile(FileSelectInfo fileSelectInfo) throws Exception {
            return fileSelectInfo.getDepth() <= depth;
        }//ww w  .  java2 s.com

        public boolean traverseDescendents(FileSelectInfo fileSelectInfo) throws Exception {
            return fileSelectInfo.getDepth() < depth;
        }
    });
}

From source file:com.thinkberg.moxo.dav.MoveHandler.java

protected void copyOrMove(FileObject object, FileObject target, int depth) throws FileSystemException {
    try {//from  ww w. j av a2  s. c o  m
        object.moveTo(target);
    } catch (FileSystemException ex) {
        ex.printStackTrace();
        target.copyFrom(object, new DepthFileSelector(depth));
        object.delete(new DepthFileSelector());
    }
}

From source file:functionalTests.multiprotocol.TestVFSProviderMultiProtocol.java

/**
 * Testing the File server deployment using multi-protocol
 * @throws Exception//from   w w  w .j  ava  2s  .co  m
 */
@Test
public void testVFSProviderMP() throws Exception {

    logger.info("**************** Testing deploying dataspace server with protocol list : " + protocolsToTest);

    CentralPAPropertyRepository.PA_COMMUNICATION_PROTOCOL.setValue(protocolsToTest.get(0));
    String add_str = protocolsToTest.get(1);
    for (int i = 2; i < protocolsToTest.size(); i++) {
        add_str += "," + protocolsToTest.get(i);
    }
    CentralPAPropertyRepository.PA_COMMUNICATION_ADDITIONAL_PROTOCOLS.setValue(add_str);

    FileSystemServerDeployer deployer = new FileSystemServerDeployer("space name",
            SERVER_PATH.getAbsolutePath(), true);

    try {

        String[] urls = deployer.getVFSRootURLs();
        logger.info("Received urls :" + Arrays.asList(urls));
        Assert.assertEquals(
                "Number of urls of the FileSystemServerDeployer should match the number of protocols + the file protocol",
                protocolsToTest.size() + 1, urls.length);

        // check the file server uris

        URI receiveduri = new URI(urls[0]);
        Assert.assertEquals("protocol of first uri " + receiveduri + " should be file", "file",
                receiveduri.getScheme());

        for (int i = 1; i < urls.length; i++) {
            receiveduri = new URI(urls[i]);
            Assert.assertEquals("protocol of uri " + urls[i] + " should match the expected protocol",
                    "pap" + protocolsToTest.get(i - 1), receiveduri.getScheme());
        }

        // use the file server
        for (int i = 0; i < urls.length; i++) {
            File f = new File(System.getProperty("java.io.tmpdir"), "testfile_" + i);
            f.createNewFile();
            logger.info("Trying to use : " + urls[i]);
            FileObject source = fileSystemManager.resolveFile(f.toURI().toURL().toExternalForm());
            FileObject dest = fileSystemManager.resolveFile(urls[i] + "/" + f.getName());
            dest.copyFrom(source, Selectors.SELECT_SELF);
            Assert.assertTrue("Copy successful of " + source.getURL() + " to " + dest.getURL(), dest.exists());

        }

    } finally {
        deployer.terminate();
    }

}

From source file:com.thinkberg.vfs.s3.tests.S3FileProviderTest.java

public void testCopyShallowFolder() throws FileSystemException {
    FileObject origFolder = ROOT.resolveFile(FOLDER);
    origFolder.delete(ALL_FILE_SELECTOR);
    origFolder.createFolder();/*  w  w  w  . j a v  a  2 s .co m*/

    origFolder.resolveFile("file.0").createFile();
    origFolder.resolveFile("file.1").createFile();
    origFolder.resolveFile("file.2").createFile();

    System.out.println(Arrays.asList(origFolder.getChildren()));
    assertEquals(3, origFolder.getChildren().length);

    FileObject destFolder = ROOT.resolveFile(FOLDER + "_dest");
    assertFalse(destFolder.exists());
    destFolder.copyFrom(origFolder, new DepthFileSelector(1));
    assertTrue(destFolder.exists());

    assertEquals(3, destFolder.getChildren().length);

    FileObject[] origFiles = origFolder.findFiles(new DepthFileSelector(1));
    FileObject[] destFiles = destFolder.findFiles(new DepthFileSelector(1));
    for (int i = 0; i < origFiles.length; i++) {
        assertEquals(origFiles[i].getName().getRelativeName(origFolder.getName()),
                destFiles[i].getName().getRelativeName(destFolder.getName()));
    }

    origFolder.delete(ALL_FILE_SELECTOR);
    destFolder.delete(ALL_FILE_SELECTOR);

    assertFalse(origFolder.exists());
    assertFalse(destFolder.exists());
}

From source file:com.thinkberg.vfs.s3.tests.S3FileProviderTest.java

public void testCopyFile() throws FileSystemException {
    FileObject srcObject = ROOT.resolveFile(FILE);
    srcObject.createFile();//w ww.  ja v  a 2 s  .  c  om
    assertTrue("source object should exist", srcObject.exists());
    FileObject dstObject = ROOT.resolveFile(FILE + ".dst");
    assertFalse("destination should not exist", dstObject.exists());
    dstObject.copyFrom(srcObject, ALL_FILE_SELECTOR);
    assertTrue("destination should exist after copy", dstObject.exists());

    srcObject.delete();
    dstObject.delete();
}

From source file:com.thinkberg.vfs.s3.tests.S3FileProviderTest.java

public void testCopyFileWithAttribute() throws FileSystemException {
    if (!BUCKETID.startsWith("s3:")) {
        FileObject srcObject = ROOT.resolveFile(FILE);
        srcObject.createFile();// w w  w .  j  av a 2s. c om
        srcObject.getContent().setAttribute(ATTR_TESTKEY, ATTR_TESTVALUE);
        assertTrue("source object should exist", srcObject.exists());
        assertEquals("source object attribute missing", ATTR_TESTVALUE,
                srcObject.getContent().getAttribute(ATTR_TESTKEY));
        FileObject dstObject = ROOT.resolveFile(FILE + ".dst");
        assertFalse("destination should not exist", dstObject.exists());
        dstObject.copyFrom(srcObject, ALL_FILE_SELECTOR);
        assertTrue("destination should exist after copy", dstObject.exists());
        assertEquals("destination object attribute missing", ATTR_TESTVALUE,
                dstObject.getContent().getAttribute(ATTR_TESTKEY));

        srcObject.delete();
        dstObject.delete();
    } else {
        LogFactory.getLog(S3FileProviderTest.class)
                .info(String.format("ignoring property test for '%s'", ROOT));
    }
}

From source file:com.newatlanta.appengine.junit.vfs.provider.GaeProviderTestCase.java

/**
  * Returns the base folder for tests. Copies test files from the local file
  * system to GaeVFS. Note that SVN (.svn) folders are not copied; if the are,
  * then the size of the LRUFilesCache created within GaeFileSystemManager.prepare()
  * must be increased to avoid testcase failures.
  *///ww w. j a  v  a2 s .  co  m
@Override
public FileObject getBaseTestFolder(FileSystemManager manager) throws Exception {
    FileObject gaeTestBaseDir = manager.getBaseFile().resolveFile("test-data");
    if (!gaeTestBaseDir.exists()) {
        FileObject localTestBaseDir = manager
                .resolveFile("file://" + GaeFileNameParser.getRootPath(manager.getBaseFile().getName())
                        + gaeTestBaseDir.getName().getPath());
        gaeTestBaseDir.copyFrom(localTestBaseDir, new TestFileSelector());
        // confirm that the correct number of files were copied
        FileObject[] testFiles = localTestBaseDir.findFiles(new TestFileSelector());
        FileObject[] gaeFiles = gaeTestBaseDir.findFiles(Selectors.SELECT_FILES);
        assertEquals(testFiles.length, gaeFiles.length);
    }
    return gaeTestBaseDir;
}

From source file:gov.nih.nci.cacis.ip.mirthconnect.ftp.SFTPSender.java

public void sendDocument(InputStream file, String ftpAddress, String extension) {

    StandardFileSystemManager standardFileSystemManager = new StandardFileSystemManager();
    try {/*from www  .  j a  va  2 s.  c o m*/
        final FTPInfo ftpInfo = ftpMapping.getFTPInfo(ftpAddress);
        if (ftpInfo == null) {
            throw new ApplicationRuntimeException("No server config exists for address[ " + ftpAddress + " ]");
        }

        String sftpURI = "sftp://" + ftpInfo.getUserName() + ":" + ftpInfo.getPassword() + "@"
                + ftpInfo.getSite() + "/" + ftpInfo.getRootDirectory();
        String fileName = "IHEXIPFTP-" + UUID.randomUUID() + extension;

        FileSystemOptions fileSystemOptions = new FileSystemOptions();
        SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(fileSystemOptions, "no");
        SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(fileSystemOptions, true);

        standardFileSystemManager.init();

        FileObject fileObject = standardFileSystemManager.resolveFile(sftpURI + "/" + fileName,
                fileSystemOptions);

        long timestamp = new Date().getTime();
        String tempSftpFile = sftpFileDirectory + "/" + timestamp + ".xml";
        OutputStream out = new FileOutputStream(new File(tempSftpFile));

        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = file.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        file.close();
        out.flush();
        out.close();

        FileObject localFileObject = standardFileSystemManager.resolveFile(tempSftpFile);

        fileObject.copyFrom(localFileObject, Selectors.SELECT_SELF);

        FileUtils.forceDelete(new File(tempSftpFile));

    } catch (Exception e) {
        throw new ApplicationRuntimeException("Error sending SFTP. " + e.getMessage());
    } finally {
        standardFileSystemManager.close();
    }
}