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

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

Introduction

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

Prototype

void moveTo(FileObject destFile) throws FileSystemException;

Source Link

Document

Move this file.

Usage

From source file:org.esupportail.portlet.filemanager.services.vfs.VfsAccessImpl.java

@Override
public boolean putFile(String dir, String filename, InputStream inputStream,
        SharedUserPortletParameters userParameters, UploadActionType uploadOption) {

    boolean success = false;
    FileObject newFile = null;

    try {/*from   ww  w. ja v a 2 s . co  m*/
        FileObject folder = cd(dir, userParameters);
        newFile = folder.resolveFile(filename);
        if (newFile.exists()) {
            switch (uploadOption) {
            case ERROR:
                throw new EsupStockFileExistException();
            case OVERRIDE:
                newFile.delete();
                break;
            case RENAME_NEW:
                newFile = folder.resolveFile(this.getUniqueFilename(filename, "-new-"));
                break;
            case RENAME_OLD:
                newFile.moveTo(folder.resolveFile(this.getUniqueFilename(filename, "-old-")));
                break;
            }
        }
        newFile.createFile();

        OutputStream outstr = newFile.getContent().getOutputStream();

        FileCopyUtils.copy(inputStream, outstr);

        success = true;
    } catch (FileSystemException e) {
        log.info("can't upload file : " + e.getMessage(), e);
    } catch (IOException e) {
        log.warn("can't upload file : " + e.getMessage(), e);
    }

    if (!success && newFile != null) {
        // problem when uploading the file -> the file uploaded is corrupted
        // best is to delete it
        try {
            newFile.delete();
            log.debug("delete corrupted file after bad upload ok ...");
        } catch (Exception e) {
            log.debug("can't delete corrupted file after bad upload " + e.getMessage());
        }
    }

    return success;
}

From source file:org.jahia.modules.external.vfs.VFSDataSource.java

@Override
public void move(String oldPath, String newPath) throws RepositoryException {
    if (oldPath.equals(newPath)) {
        return;/* w  ww  .j a  v a  2 s.c om*/
    }
    try {
        FileObject origin = getFile(oldPath);
        if (origin.isContentOpen()) {
            origin.close();
        }
        FileObject destination = getFile(newPath);
        if (destination.exists() && destination.isContentOpen()) {
            destination.close();
        }
        origin.moveTo(destination);
    } catch (FileSystemException e) {
        throw new RepositoryException(oldPath, e);
    }
}

From source file:org.kalypso.commons.io.VFSUtilities.java

/**
 * Moves the complete content of one directory into another.
 *
 * @throws IOException/*from  w w w  .j a v a  2 s. co  m*/
 *           If the move failed.
 */
public static void moveContents(final File sourceDir, final File dest) throws IOException {
    final FileSystemManager vfsManager = VFSUtilities.getManager();
    final FileObject source = vfsManager.toFileObject(sourceDir);
    final FileObject destDir = vfsManager.toFileObject(dest);

    final FileObject[] findFiles = source.findFiles(new AllFileSelector());
    // Might happen, if source does not exists... shouldn't we check this?
    if (findFiles == null)
        return;

    for (final FileObject fileObject : findFiles) {
        if (FileType.FILE.equals(fileObject.getType())) {
            final String relPath = source.getName().getRelativeName(fileObject.getName());
            final FileObject destFile = destDir.resolveFile(relPath, NameScope.DESCENDENT_OR_SELF);
            final FileObject folder = destFile.getParent();
            folder.createFolder();
            fileObject.moveTo(destFile);
        }
    }
}

From source file:org.kalypso.kalypsomodel1d2d.sim.ResultManager.java

private void processSWANTabFile(final FileObject swanResOutTabFile, final FileObject swanResShiftFile) {
    final GM_Position lShiftPosition = SWANDataConverterHelper.readCoordinateShiftValues(swanResShiftFile);
    if (lShiftPosition == null) {
        return;//w  w  w.  j  a  va2  s. com
    }
    try {
        if (swanResOutTabFile.isContentOpen()) {
            swanResOutTabFile.close();
        }
        final FileObject swanResOutTabFileBackUp = swanResOutTabFile.getParent()
                .resolveFile(swanResOutTabFile.getName().getBaseName() + ".bck"); //$NON-NLS-1$
        swanResOutTabFile.moveTo(swanResOutTabFileBackUp);

        // int lIntLinesCounter = 0;
        final OutputStream lOutStream = swanResOutTabFile.getContent().getOutputStream();
        final DataInputStream lInDataStream = new DataInputStream(
                swanResOutTabFileBackUp.getContent().getInputStream());
        BufferedReader streamReader = new BufferedReader(new InputStreamReader(lInDataStream));
        final Formatter lFormatter = new Formatter(lOutStream, Charset.defaultCharset().name(), Locale.US);
        while (lInDataStream.available() != 0) {
            final String lStrTmpLine = streamReader.readLine().trim();
            // ++lIntLinesCounter;
            if (lStrTmpLine.startsWith("%")) { //$NON-NLS-1$
                lFormatter.format("%s\n", lStrTmpLine); //$NON-NLS-1$
                continue;
            }
            final StringTokenizer lStrTokenizer = new StringTokenizer(lStrTmpLine, " "); //$NON-NLS-1$
            int lIntTokenCounter = 0;
            String lStrNewLine = ""; //$NON-NLS-1$
            while (lStrTokenizer.hasMoreTokens()) {
                final String lStrToken = lStrTokenizer.nextToken();
                if (lIntTokenCounter == 1) {
                    lStrNewLine += String.format(Locale.US, "%.5f\t", //$NON-NLS-1$
                            NumberUtils.parseQuietDouble(lStrToken) + lShiftPosition.getX());
                } else if (lIntTokenCounter == 2) {
                    lStrNewLine += String.format(Locale.US, "%.5f\t", //$NON-NLS-1$
                            NumberUtils.parseQuietDouble(lStrToken) + lShiftPosition.getY());
                } else {
                    lStrNewLine += lStrToken + "\t"; //$NON-NLS-1$
                }
                lIntTokenCounter++;
            }
            lFormatter.format("%s\n", lStrNewLine); //$NON-NLS-1$

        }
        lFormatter.close();
        lInDataStream.close();
        lOutStream.close();
    } catch (final Exception e) {
        return;
    }

    return;
}

From source file:org.kalypso.kalypsomodel1d2d.sim.SwanResultProcessor.java

private void processSWANTabFile(final FileObject swanResOutTabFile, final FileObject swanResShiftFile) {
    final GM_Position lShiftPosition = SWANDataConverterHelper.readCoordinateShiftValues(swanResShiftFile);
    if (lShiftPosition == null)
        return;//from   w  w w.j  a va  2s.c  o m

    try {
        // FIXME: why?! should never happen...!
        if (swanResOutTabFile.isContentOpen())
            swanResOutTabFile.close();

        final FileObject swanResOutTabFileBackUp = swanResOutTabFile.getParent()
                .resolveFile(swanResOutTabFile.getName().getBaseName() + ".bck"); //$NON-NLS-1$
        swanResOutTabFile.moveTo(swanResOutTabFileBackUp);

        final OutputStream lOutStream = swanResOutTabFile.getContent().getOutputStream();
        final Formatter lFormatter = new Formatter(lOutStream, Charset.defaultCharset().name(), Locale.US);

        final BufferedReader lInDataStream = new BufferedReader(
                new InputStreamReader(swanResOutTabFileBackUp.getContent().getInputStream()));

        while (lInDataStream.ready()) {
            final String lStrTmpLine = lInDataStream.readLine().trim();
            if (lStrTmpLine.startsWith("%")) //$NON-NLS-1$
            {
                lFormatter.format("%s\n", lStrTmpLine); //$NON-NLS-1$
                continue;
            }

            final StringTokenizer lStrTokenizer = new StringTokenizer(lStrTmpLine, " "); //$NON-NLS-1$
            int lIntTokenCounter = 0;
            String lStrNewLine = ""; //$NON-NLS-1$
            while (lStrTokenizer.hasMoreTokens()) {
                final String lStrToken = lStrTokenizer.nextToken();
                if (lIntTokenCounter == 1) {
                    lStrNewLine += String.format(Locale.US, "%.5f\t", //$NON-NLS-1$
                            NumberUtils.parseQuietDouble(lStrToken) + lShiftPosition.getX());
                } else if (lIntTokenCounter == 2) {
                    lStrNewLine += String.format(Locale.US, "%.5f\t", //$NON-NLS-1$
                            NumberUtils.parseQuietDouble(lStrToken) + lShiftPosition.getY());
                } else {
                    lStrNewLine += lStrToken + "\t"; //$NON-NLS-1$
                }
                lIntTokenCounter++;
            }
            lFormatter.format("%s\n", lStrNewLine); //$NON-NLS-1$
        }

        // FIXME: not closed in a save way!
        lFormatter.close();
        lInDataStream.close();
        lOutStream.close();
    } catch (final Exception e) {
        // FIXME: this is no way to handle an error !
    }
}

From source file:org.obiba.opal.fs.DecoratedFileObjectTest.java

@Test
public void testMoveTo() throws FileSystemException {

    FileObject file_1 = root.resolveFile("file_1");
    file_1.createFile();//from   w  w w . j a  v a  2  s .  c o  m
    FileObject file_2 = root.resolveFile("file_2");
    FileObject decorate_1 = new DFO(file_1);
    FileObject decorate_2 = new DFO(file_2);
    System.out.println(decorate_1);
    System.out.println(decorate_2);
    decorate_1.moveTo(decorate_2);

    //    System.out.println(file_1);
    //    log.debug("file_1: {}", file_1);
}

From source file:org.obiba.opal.fs.security.SecuredFileObject.java

@Override
public void moveTo(FileObject destFile) throws FileSystemException {
    FileObject sourceFile = getDecoratedFileObject();

    if (isPermitted(sourceFile, "DELETE")) {
        if (!(destFile instanceof SecuredFileObject)) {
            super.moveTo(destFile);
            return;
        }// ww w  .j  av a  2  s . c o  m

        sourceFile.moveTo(((DecoratedFileObject) destFile).getDecoratedFileObject());
        return;
    }
    throw new FileSystemException("vfs.provider.local/delete-file.error", getName());
}

From source file:org.obiba.opal.shell.commands.ImportCommand.java

private void archive(FileObject file) throws IOException {
    if (!options.isArchive()) {
        return;//from   w ww. j a  va 2  s.c  o m
    }

    String archivePath = options.getArchive();
    try {
        FileObject archiveDir = getFile(archivePath);
        if (archiveDir == null) {
            throw new IOException("Cannot archive file " + file.getName().getPath()
                    + ". Archive directory is null: " + archivePath);
        }
        archiveDir.createFolder();
        FileObject archiveFile = archiveDir.resolveFile(file.getName().getBaseName());
        file.moveTo(archiveFile);
    } catch (FileSystemException ex) {
        throw new IOException("Failed to archive file " + file.getName().getPath() + " to " + archivePath);
    }
}

From source file:org.obiba.opal.web.FilesResource.java

private Response moveTo(FileObject destinationFolder, Iterable<String> sourcesPath) throws IOException {
    // destination check
    String destinationPath = destinationFolder.getName().getPath();
    if (destinationFolder.getType() != FileType.FOLDER)
        return Response.status(Status.BAD_REQUEST).entity("Destination must be a folder: " + destinationPath)
                .build();//from   w w  w.j a  v a 2 s . co m

    // sources check
    for (String sourcePath : sourcesPath) {
        FileObject sourceFile = resolveFileInFileSystem(sourcePath);
        if (!sourceFile.exists())
            getPathNotExistResponse(sourcePath);
        if (!sourceFile.isReadable()) {
            return Response.status(Status.FORBIDDEN).entity("Source file is not readable: " + sourcePath)
                    .build();
        }
        if (!sourceFile.isWriteable()) {
            return Response.status(Status.FORBIDDEN).entity("Source file cannot be moved: " + sourcePath)
                    .build();
        }
    }

    // do action
    for (String sourcePath : sourcesPath) {
        FileObject sourceFile = resolveFileInFileSystem(sourcePath);
        FileObject destinationFile = resolveFileInFileSystem(
                destinationPath + "/" + sourceFile.getName().getBaseName());
        sourceFile.moveTo(destinationFile);
    }

    return Response.ok().build();
}

From source file:org.pentaho.di.plugins.fileopensave.providers.vfs.VFSFileProvider.java

/**
 * @param file//from  w w w . ja v a  2  s . c  o  m
 * @param newPath
 * @param overwrite
 * @return
 */
private VFSFile doMove(VFSFile file, String newPath, Boolean overwrite) {
    try {
        FileObject fileObject = KettleVFS.getFileObject(file.getPath(), new Variables(),
                VFSHelper.getOpts(file.getPath(), file.getConnection()));
        FileObject renameObject = KettleVFS.getFileObject(newPath, new Variables(),
                VFSHelper.getOpts(file.getPath(), file.getConnection()));
        if (overwrite) {
            if (renameObject.exists()) {
                renameObject.delete();
            }
        }
        fileObject.moveTo(renameObject);
        if (file instanceof VFSDirectory) {
            return VFSDirectory.create(renameObject.getParent().getPublicURIString(), renameObject,
                    file.getConnection());
        } else {
            return VFSFile.create(renameObject.getParent().getPublicURIString(), renameObject,
                    file.getConnection());
        }
    } catch (KettleFileException | FileSystemException e) {
        return null;
    }
}