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

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

Introduction

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

Prototype

FileName getName();

Source Link

Document

Returns the name of this file.

Usage

From source file:org.wso2.carbon.connector.FileArchives.java

/**
 * @param fileObject   Source fileObject
 * @param file         The file inside source folder
 * @param outputStream ZipOutputStream/*from  w w  w .j  a  va2  s .  com*/
 */
private void addToZip(FileObject fileObject, FileObject file, ZipOutputStream outputStream) {
    InputStream fin = null;
    try {
        fin = file.getContent().getInputStream();
        String name = file.getName().toString();
        String entry = name.substring(fileObject.getName().toString().length() + 1, name.length());
        ZipEntry zipEntry = new ZipEntry(entry);
        outputStream.putNextEntry(zipEntry);
        int length;
        while ((length = fin.read(bytes)) != -1) {
            outputStream.write(bytes, 0, length);
        }
    } catch (IOException e) {
        log.error("Unable to add a file into the zip file directory." + e.getMessage());
    } finally {
        try {
            outputStream.closeEntry();
            if (fin != null) {
                fin.close();
            }
        } catch (IOException e) {
            log.error("Error while closing InputStream: " + e.getMessage(), e);
        }
    }
}

From source file:org.wso2.carbon.connector.FileCopy.java

/**
 * Copy files/*from ww  w  .j  a va 2  s. c  o m*/
 *
 * @param source         Location of the file
 * @param destination    new file location
 * @param filePattern    pattern of the file
 * @param messageContext The message context that is generated for processing the file
 * @param opts           FileSystemOptions
 * @return return a resultStatus
 */
private boolean copyFile(String source, String destination, String filePattern, MessageContext messageContext,
        FileSystemOptions opts) {
    boolean resultStatus = false;
    StandardFileSystemManager manager = null;
    try {
        manager = FileConnectorUtils.getManager();
        FileObject souFile = manager.resolveFile(source, opts);
        FileObject destFile = manager.resolveFile(destination, opts);
        if (StringUtils.isNotEmpty(filePattern)) {
            FileObject[] children = souFile.getChildren();
            for (FileObject child : children) {
                if (child.getType() == FileType.FILE) {
                    copy(source, destination, filePattern, opts);
                } else if (child.getType() == FileType.FOLDER) {
                    String newSource = source + File.separator + child.getName().getBaseName();
                    copyFile(newSource, destination, filePattern, messageContext, opts);
                }
            }
            resultStatus = true;
        } else {
            if (souFile.exists()) {
                if (souFile.getType() == FileType.FILE) {
                    InputStream fileIn = null;
                    OutputStream fileOut = null;
                    try {
                        String name = souFile.getName().getBaseName();
                        FileObject outFile = manager.resolveFile(destination + File.separator + name, opts);
                        //TODO make parameter sense
                        fileIn = souFile.getContent().getInputStream();
                        fileOut = outFile.getContent().getOutputStream();
                        IOUtils.copyLarge(fileIn, fileOut);
                        resultStatus = true;
                    } catch (FileSystemException e) {
                        log.error("Error while copying a file " + e.getMessage());
                    } finally {
                        try {
                            if (fileOut != null) {
                                fileOut.close();
                            }
                        } catch (Exception e) {
                            log.error("Error while closing OutputStream: " + e.getMessage(), e);
                        }
                        try {
                            if (fileIn != null) {
                                fileIn.close();
                            }
                        } catch (Exception e) {
                            log.error("Error while closing InputStream: " + e.getMessage(), e);
                        }
                    }
                } else if (souFile.getType() == FileType.FOLDER) {
                    destFile.copyFrom(souFile, Selectors.SELECT_ALL);
                    resultStatus = true;
                }
                if (log.isDebugEnabled()) {
                    log.debug("File copying completed from " + source + "to" + destination);
                }
            } else {
                log.error("The File Location does not exist.");
                resultStatus = false;
            }
        }
        return resultStatus;
    } catch (IOException e) {
        handleException("Unable to copy a file/folder", e, messageContext);
    } finally {
        if (manager != null) {
            manager.close();
        }
    }
    return resultStatus;
}

From source file:org.wso2.carbon.connector.FileCopy.java

/**
 * @param source      file location//  w  w w.j  a v  a2  s.  c  om
 * @param destination target file location
 * @param filePattern pattern of the file
 * @param opts        FileSystemOptions
 * @throws IOException
 */
private void copy(String source, String destination, String filePattern, FileSystemOptions opts)
        throws IOException {
    StandardFileSystemManager manager = FileConnectorUtils.getManager();
    FileObject souFile = manager.resolveFile(source, opts);
    FileObject[] children = souFile.getChildren();
    FilePattenMatcher patternMatcher = new FilePattenMatcher(filePattern);
    for (FileObject child : children) {
        try {
            if (patternMatcher.validate(child.getName().getBaseName())) {
                String name = child.getName().getBaseName();
                FileObject outFile = manager.resolveFile(destination + File.separator + name, opts);
                outFile.copyFrom(child, Selectors.SELECT_FILES);
            }
        } catch (IOException e) {
            log.error("Error occurred while copying a file. " + e.getMessage(), e);
        }
    }
    manager.close();
}

From source file:org.wso2.carbon.connector.FileCopyConnector.java

/**
 * Copy the file or folder from source to destination.
 *
 * @param messageContext The message context that is generated for processing the file.
 * @param opts           FileSystemOptions.
 * @return return true, if file/folder is successfully copied.
 *///w  w w .  j  av a  2  s. com
private boolean copyFile(String source, MessageContext messageContext, FileSystemOptions opts) {
    String destination = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.NEW_FILE_LOCATION);
    String filePattern = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.FILE_PATTERN);
    String includeParentDir = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.INCLUDE_PARENT_DIRECTORY);
    boolean includeParentDirectory = FileConstants.DEFAULT_INCLUDE_PARENT_DIRECTORY;

    if (StringUtils.isNotEmpty(includeParentDir)) {
        includeParentDirectory = Boolean.parseBoolean(includeParentDir);
    }
    boolean resultStatus = false;
    StandardFileSystemManager manager = FileConnectorUtils.getManager();
    FileObject souFile = null;
    FileObject destFile = null;
    try {
        souFile = manager.resolveFile(source, opts);
        destFile = manager.resolveFile(destination, opts);
        if (!souFile.exists()) {
            log.error("The File Location does not exist.");
            return false;
        }
        if (StringUtils.isNotEmpty(filePattern)) {
            FileObject[] children = souFile.getChildren();
            for (FileObject child : children) {
                if (FileType.FILE.equals(child.getType())) {
                    copy(child, destination, filePattern, opts, manager);
                } else if (FileType.FOLDER.equals(child.getType())) {
                    String newSource = source + File.separator + child.getName().getBaseName();
                    copyFile(newSource, messageContext, opts);
                }
            }
        } else {
            if (FileType.FILE.equals(souFile.getType())) {
                String name = souFile.getName().getBaseName();
                FileObject outFile = manager.resolveFile(destination + File.separator + name, opts);
                outFile.copyFrom(souFile, Selectors.SELECT_ALL);
            } else if (FileType.FOLDER.equals(souFile.getType())) {
                if (includeParentDirectory) {
                    destFile = manager
                            .resolveFile(destination + File.separator + souFile.getName().getBaseName(), opts);
                    destFile.createFolder();
                }
                destFile.copyFrom(souFile, Selectors.SELECT_ALL);
            }
            if (log.isDebugEnabled()) {
                log.debug("File copying completed from " + source + "to" + destination);
            }
        }
    } catch (FileSystemException e) {
        throw new SynapseException("Unable to copy a file/folder", e);
    } finally {
        manager.close();
    }
    try {
        souFile.close();
        destFile.close();
    } catch (FileSystemException e) {
        log.error("Error while closing the FileObject", e);
    }
    return true;
}

From source file:org.wso2.carbon.connector.FileCopyConnector.java

/**
 * copy the file for given pattern.// w  w  w.jav a  2  s . com
 *
 * @param source      The source file object.
 * @param destination The target file location.
 * @param filePattern Pattern of the file.
 * @param opts        Configured file system.
 */
private void copy(FileObject source, String destination, String filePattern, FileSystemOptions opts,
        StandardFileSystemManager manager) throws FileSystemException {
    FilePattenMatcher patternMatcher = new FilePattenMatcher(filePattern);
    if (patternMatcher.validate(source.getName().getBaseName())) {
        String name = source.getName().getBaseName();
        FileObject outFile = manager.resolveFile(destination + File.separator + name, opts);
        outFile.copyFrom(source, Selectors.SELECT_ALL);
    }
}

From source file:org.wso2.carbon.connector.FileDeleteConnector.java

/**
 * Delete an existing file/folder./*from   ww w.j  av a 2 s.c om*/
 *
 * @param messageContext The message context that is generated for processing the file.
 * @return true, if the file/folder is successfully deleted.
 */
private boolean deleteFile(MessageContext messageContext) {
    String source = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.FILE_LOCATION);
    StandardFileSystemManager manager = FileConnectorUtils.getManager();
    FileObject remoteFile = null;
    try {
        // create remote fileObject
        remoteFile = manager.resolveFile(source, FileConnectorUtils.init(messageContext));
        if (!remoteFile.exists()) {
            log.error("The file does not exist.");
            return false;
        }
        if (FileType.FILE.equals(remoteFile.getType())) {
            // delete a file
            remoteFile.delete();
        } else if (FileType.FOLDER.equals(remoteFile.getType())) {
            String filePattern = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
                    FileConstants.FILE_PATTERN);

            if (StringUtils.isNotEmpty(filePattern) && !"*".equals(filePattern)) {
                FileObject[] children = remoteFile.getChildren();
                for (FileObject child : children) {
                    if (child.getName().getBaseName().matches(filePattern)) {
                        child.delete();
                    }
                }
            } else {
                // delete folder
                remoteFile.delete(Selectors.SELECT_ALL);
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("File delete completed with " + source);
        }
    } catch (FileSystemException e) {
        throw new SynapseException("Error while deleting file/folder", e);
    } finally {
        // close the StandardFileSystemManager
        manager.close();
        try {
            if (remoteFile != null) {
                // close the file object
                remoteFile.close();
            }
        } catch (FileSystemException e) {
            log.error("Error while closing the FileObject", e);
        }
    }
    return true;
}

From source file:org.wso2.carbon.connector.FileMove.java

/**
 * Move the files//from w  ww  .j  ava2 s .c  o  m
 *
 * @param source      Location of the file
 * @param destination Destination of the file
 * @return return a resultStatus
 */
private boolean moveFile(String source, String destination, MessageContext messageContext) {
    boolean resultStatus = false;
    StandardFileSystemManager manager = null;
    try {
        manager = FileConnectorUtils.getManager();
        // Create remote object
        FileObject remoteFile = manager.resolveFile(source, FileConnectorUtils.init(messageContext));
        if (remoteFile.exists()) {
            FileObject file = manager.resolveFile(destination, FileConnectorUtils.init(messageContext));
            if (!file.exists()) {
                file.createFolder();
            }
            if (remoteFile.getType() == FileType.FOLDER) {
                remoteFile.moveTo(file);
            } else if (remoteFile.getType() == FileType.FILE) {
                FileObject newFile = manager.resolveFile(
                        destination + File.separator + remoteFile.getName().getBaseName(),
                        FileConnectorUtils.init(messageContext));
                remoteFile.moveTo(newFile);
            }
            resultStatus = true;
            if (log.isDebugEnabled()) {
                log.debug("File move completed from " + source + " to " + destination);
            }
        } else {
            log.error("The file/folder location does not exist.");
            resultStatus = false;
        }
    } catch (IOException e) {
        handleException("Unable to move a file/folder.", e, messageContext);
    } finally {
        if (manager != null) {
            manager.close();
        }
    }
    return resultStatus;
}

From source file:org.wso2.carbon.connector.FileMoveConnector.java

/**
 * Move the file to the target directory.
 *
 * @param destination The target location of the folder to be moved.
 * @param remoteFile  Location of the remote file.
 * @param manager     Standard file system manager.
 * @param opts        Configured file system options.
 * @throws FileSystemException On error parsing the file name, determining if the file exists and creating the
 *                             file/folder.
 *///  w  w  w  .j  a v  a2s  .  com
private void moveFile(String destination, FileObject remoteFile, StandardFileSystemManager manager,
        FileSystemOptions opts) throws FileSystemException {
    FileObject file = manager.resolveFile(destination, opts);
    if (FileConnectorUtils.isFolder(file)) {
        if (!file.exists()) {
            file.createFolder();
        }
        file = manager.resolveFile(destination + File.separator + remoteFile.getName().getBaseName(), opts);
    } else if (!file.exists()) {
        file.createFile();
    }
    remoteFile.moveTo(file);
}

From source file:org.wso2.carbon.connector.FileMoveConnector.java

/**
 * Move the folder to the target directory.
 *
 * @param destination            The target location of the folder to move folder.
 * @param source                 Location of the source folder.
 * @param includeParentDirectory Boolean type to include the parent directory.
 * @param manager                Standard file system manager.
 * @param opts                   Configured file system options.
 *///from w  ww  .java  2  s. c om
private void moveFolder(String source, String destination, String filePattern, boolean includeParentDirectory,
        StandardFileSystemManager manager, FileSystemOptions opts) throws FileSystemException {
    FileObject remoteFile = manager.resolveFile(source, opts);
    FileObject file = manager.resolveFile(destination, opts);
    if (StringUtils.isNotEmpty(filePattern)) {
        FileObject[] children = remoteFile.getChildren();
        for (FileObject child : children) {
            if (FileType.FILE.equals(child.getType())) {
                moveFileWithPattern(child, destination, filePattern, manager, opts);
            } else if (FileType.FOLDER.equals(child.getType())) {
                String newSource = source + File.separator + child.getName().getBaseName();
                moveFolder(newSource, destination, filePattern, includeParentDirectory, manager, opts);
            }
        }
    } else if (includeParentDirectory) {
        FileObject destFile = manager
                .resolveFile(destination + File.separator + remoteFile.getName().getBaseName(), opts);
        destFile.createFolder();
        remoteFile.moveTo(destFile);
    } else {
        if (!file.exists()) {
            file.createFolder();
        }
        remoteFile.moveTo(file);
        remoteFile.createFolder();
    }
}

From source file:org.wso2.carbon.connector.FileMoveConnector.java

/**
 * Move the file for given pattern.//from  w  w w.  j a  va  2 s . co  m
 *
 * @param remoteFile  Location of the remote file.
 * @param destination Location of the target folder.
 * @param filePattern Pattern of the file.
 * @param manager     Standard file system manager.
 * @param opts        Configured file system options.
 */
private void moveFileWithPattern(FileObject remoteFile, String destination, String filePattern,
        StandardFileSystemManager manager, FileSystemOptions opts) {
    FilePattenMatcher patternMatcher = new FilePattenMatcher(filePattern);
    try {
        if (patternMatcher.validate(remoteFile.getName().getBaseName())) {
            FileObject destFile = manager.resolveFile(destination, opts);
            if (!destFile.exists()) {
                destFile.createFolder();
            }
            FileObject newDestFile = manager
                    .resolveFile(destination + File.separator + remoteFile.getName().getBaseName(), opts);
            remoteFile.moveTo(newDestFile);
        }
    } catch (FileSystemException e) {
        throw new SynapseException("Error occurred while moving a file for a given pattern", e);
    }
}