Example usage for org.apache.commons.vfs2 FileType FILE

List of usage examples for org.apache.commons.vfs2 FileType FILE

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 FileType FILE.

Prototype

FileType FILE

To view the source code for org.apache.commons.vfs2 FileType FILE.

Click Source Link

Document

A regular file.

Usage

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.
 *///from   www.j a  v a 2 s  .c  o m
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.FileDelete.java

/**
 * Delete the file/*from   w  w  w.jav  a  2s .c om*/
 *
 * @param source         Location of the file
 * @param messageContext The message context that is generated for processing the file
 * @return Return the status
 */
private boolean deleteFile(String source, 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()) {
            if (remoteFile.getType() == FileType.FILE) {
                //delete a file
                remoteFile.delete();
            } else if (remoteFile.getType() == FileType.FOLDER) {
                //delete folder
                remoteFile.delete(Selectors.SELECT_ALL);
            }
            resultStatus = true;
        } else {
            log.error("The file does not exist.");
            resultStatus = false;
        }
        if (log.isDebugEnabled()) {
            log.debug("File delete completed with. " + source);
        }
    } catch (IOException e) {
        handleException("Error occurs while deleting a file.", e, messageContext);
    } finally {
        if (manager != null) {
            manager.close();
        }
    }
    return resultStatus;
}

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

/**
 * Delete an existing file/folder.//from   w  w w  . java 2  s .c  o  m
 *
 * @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//w  w w .ja v a 2 s. co  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/folder from source to destination directory.
 *
 * @param messageContext The message context that is generated for processing the move operation.
 * @return true, if the file/folder is successfully moved.
 *//*from ww w  .  j  av a2s .  co m*/
private boolean move(MessageContext messageContext) throws FileSystemException {
    String source = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.FILE_LOCATION);
    String destination = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.NEW_FILE_LOCATION);
    String filePattern = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.FILE_PATTERN);
    String includeParentDirectory = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.INCLUDE_PARENT_DIRECTORY);
    boolean includeParentDirectoryParameter = FileConstants.DEFAULT_INCLUDE_PARENT_DIRECTORY;

    if (StringUtils.isNotEmpty(includeParentDirectory)) {
        includeParentDirectoryParameter = Boolean.parseBoolean(includeParentDirectory);
    }

    StandardFileSystemManager manager = FileConnectorUtils.getManager();
    FileSystemOptions opts = FileConnectorUtils.init(messageContext);
    // Create remote object
    FileObject remoteFile = manager.resolveFile(source, opts);
    try {
        if (!remoteFile.exists()) {
            log.error("The file/folder location does not exist.");
            return false;
        }
        if (FileType.FILE.equals(remoteFile.getType())) {
            moveFile(destination, remoteFile, manager, opts);
        } else {
            moveFolder(source, destination, filePattern, includeParentDirectoryParameter, manager, opts);
        }
        if (log.isDebugEnabled()) {
            log.debug("File move completed from " + source + " to " + destination);
        }
    } catch (FileSystemException e) {
        throw new SynapseException("Unable to move a file/folder.", e);
    } finally {
        // close the StandardFileSystemManager
        manager.close();
        try {
            remoteFile.close();
        } catch (FileSystemException e) {
            log.error("Error while closing the FileObject", e);
        }
    }
    return true;
}

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 w  w .ja  v a  2s  .  c  o m*/
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.FileRead.java

public void connect(MessageContext messageContext) {
    String fileLocation = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.FILE_LOCATION);
    String contentType = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.CONTENT_TYPE);
    String filePattern = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.FILE_PATTERN);
    FileObject fileObj = null;//from w w  w.jav  a 2s.c  o m
    StandardFileSystemManager manager = null;
    try {
        manager = FileConnectorUtils.getManager();
        fileObj = manager.resolveFile(fileLocation, FileConnectorUtils.init(messageContext));
        if (fileObj.exists()) {
            if (fileObj.getType() == FileType.FOLDER) {
                FileObject[] children = fileObj.getChildren();
                if (children == null || children.length == 0) {
                    log.warn("Empty folder.");
                    handleException("Empty folder.", messageContext);
                } else if (filePattern != null && !filePattern.trim().equals("")) {
                    boolean bFound = false;
                    for (FileObject child : children) {
                        if (child.getName().getBaseName().matches(filePattern)) {
                            fileObj = child;
                            bFound = true;
                            break;
                        }
                    }
                    if (!bFound) {
                        log.warn("File does not exists for the mentioned pattern.");
                        handleException("File does not exists for the mentioned pattern.", messageContext);
                    }
                } else {
                    fileObj = children[0];
                }
            } else if (fileObj.getType() != FileType.FILE) {
                log.warn("File does not exists, or an empty folder.");
                handleException("File does not exists, or an empty folder.", messageContext);
            }
        } else {
            log.warn("File/Folder does not exists");
            handleException("File/Folder does not exists", messageContext);
        }
        ResultPayloadCreate.buildFile(fileObj, messageContext, contentType);
        if (log.isDebugEnabled()) {
            log.debug("File read completed." + fileLocation);
        }
    } catch (Exception e) {
        handleException(e.getMessage(), messageContext);
    } finally {
        try {
            // Close the File system if it is not already closed by the finally block of
            // processFile method
            if (fileObj != null && fileObj.getParent() != null && fileObj.getParent().getFileSystem() != null) {
                manager.closeFileSystem(fileObj.getParent().getFileSystem());
            }
        } catch (FileSystemException warn) {
            // ignore the warning, since we handed over the stream close job to
            // AutoCloseInputStream..
        }
        try {
            if (fileObj != null) {
                fileObj.close();
            }
        } catch (Exception e) {
            // ignore the warning, since we handed over the stream close job to
            // AutoCloseInputStream..
        }
    }
}

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

/**
 * Read the file content./*from  w w  w .  j a  v  a 2 s .  co  m*/
 *
 * @param messageContext The message context that is generated for processing the read operation.
 */
private void readFile(MessageContext messageContext) {
    String source = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.FILE_LOCATION);
    String contentType = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.CONTENT_TYPE);
    String filePattern = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.FILE_PATTERN);
    boolean streaming = false;
    String enableStreamingParameter = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.ENABLE_STREAMING);
    if (StringUtils.isNotEmpty(enableStreamingParameter)) {
        streaming = Boolean.parseBoolean(enableStreamingParameter);
    }

    FileObject fileObjectToRead = null;
    StandardFileSystemManager manager = FileConnectorUtils.getManager();
    try {
        FileObject rootFileObject = manager.resolveFile(source, FileConnectorUtils.init(messageContext));
        if (!rootFileObject.exists()) {
            log.error("File/Folder does not exists.");
        }
        if (FileType.FOLDER.equals(rootFileObject.getType())) {
            FileObject[] children = rootFileObject.getChildren();
            if (children == null || children.length == 0) {
                log.error("Empty folder.");
            } else if (StringUtils.isNotEmpty(filePattern)) {
                for (FileObject child : children) {
                    if (child.getName().getBaseName().matches(filePattern)) {
                        fileObjectToRead = child;
                        break;
                    }
                }
                if (fileObjectToRead == null) {
                    log.error("File does not exists for the mentioned pattern.");
                }
            } else {
                fileObjectToRead = children[0];
            }
        } else if (FileType.FILE.equals(rootFileObject.getType())) {
            fileObjectToRead = rootFileObject;
        } else {
            log.error("File does not exists, or an empty folder");
        }
        ResultPayloadCreator.buildFile(fileObjectToRead, messageContext, contentType, streaming);
        if (log.isDebugEnabled()) {
            log.debug("File read completed." + source);
        }
    } catch (FileSystemException e) {
        throw new SynapseException("Error while reading a file", e);
    } finally {
        try {
            // Close the File system if it is not already closed by the finally block of processFile method
            if (fileObjectToRead != null && fileObjectToRead.getParent() != null
                    && fileObjectToRead.getParent().getFileSystem() != null) {
                manager.closeFileSystem(fileObjectToRead.getParent().getFileSystem());
            }
            if (fileObjectToRead != null) {
                fileObjectToRead.close();
            }
        } catch (FileSystemException e) {
            log.error("Error while closing the FileObject", e);
        }
    }
}

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

/**
 * Generate the file search// w  w  w .  j  av  a 2 s  .co m
 *
 * @param source         Location fo the file
 * @param filePattern    Pattern of the file
 * @param recursiveSearch check whether recursively search or not
 * @param messageContext The message context that is processed by a handler in the handle method
 */
private void search(String source, String filePattern, String recursiveSearch, MessageContext messageContext) {
    ResultPayloadCreate resultPayload = new ResultPayloadCreate();
    StandardFileSystemManager manager = null;
    if (StringUtils.isEmpty(filePattern)) {
        log.error("FilePattern should not be null");
    } else {
        try {
            manager = FileConnectorUtils.getManager();
            FileSystemOptions opt = FileConnectorUtils.init(messageContext);
            FileObject remoteFile = manager.resolveFile(source, opt);
            if (remoteFile.exists()) {
                FileObject[] children = remoteFile.getChildren();
                OMFactory factory = OMAbstractFactory.getOMFactory();
                String outputResult;
                OMNamespace ns = factory.createOMNamespace(FileConstants.FILECON, FileConstants.NAMESPACE);
                OMElement result = factory.createOMElement(FileConstants.RESULT, ns);
                resultPayload.preparePayload(messageContext, result);
                FilePattenMatcher fpm = new FilePattenMatcher(filePattern);
                recursiveSearch = recursiveSearch.trim();

                for (FileObject child : children) {
                    try {
                        if (child.getType() == FileType.FILE
                                && fpm.validate(child.getName().getBaseName().toLowerCase())) {
                            outputResult = child.getName().getPath();
                            OMElement messageElement = factory.createOMElement(FileConstants.FILE, ns);
                            messageElement.setText(outputResult);
                            result.addChild(messageElement);
                        } else if (child.getType() == FileType.FOLDER && "true".equals(recursiveSearch)) {
                            searchSubFolders(child, filePattern, messageContext, factory, result, ns);
                        }
                    } catch (IOException e) {
                        handleException("Unable to search a file.", e, messageContext);
                    } finally {
                        try {
                            if (child != null) {
                                child.close();
                            }
                        } catch (IOException e) {
                            log.error("Error while closing Directory: " + e.getMessage(), e);
                        }
                    }
                }
                messageContext.getEnvelope().getBody().addChild(result);
            } else {
                log.error("File location does not exist.");
            }
        } catch (IOException e) {
            handleException("Unable to search a file.", e, messageContext);
        } finally {
            if (manager != null) {
                manager.close();
            }
        }
    }
}

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

/**
 *
 * @param child sub folder//from w w w . j  a v  a 2 s .co m
 * @param filePattern pattern of the file to be searched
 * @param messageContext the message context that is generated for processing the file
 * @param factory OMFactory
 * @param result OMElement
 * @param ns OMNamespace
 * @throws IOException
 */
private void searchSubFolders(FileObject child, String filePattern, MessageContext messageContext,
        OMFactory factory, OMElement result, OMNamespace ns) throws IOException {
    List<FileObject> fileList = new ArrayList<FileObject>();
    getAllFiles(child, fileList, messageContext);
    FilePattenMatcher fpm = new FilePattenMatcher(filePattern);
    String outputResult;
    try {
        for (FileObject file : fileList) {
            if (file.getType() == FileType.FILE) {
                if (fpm.validate(file.getName().getBaseName().toLowerCase())) {
                    outputResult = file.getName().getPath();
                    OMElement messageElement = factory.createOMElement(FileConstants.FILE, ns);
                    messageElement.setText(outputResult);
                    result.addChild(messageElement);
                }
            } else if (file.getType() == FileType.FOLDER) {
                searchSubFolders(file, filePattern, messageContext, factory, result, ns);
            }
        }
    } catch (IOException e) {
        handleException("Unable to search a file in sub folder.", e, messageContext);
    } finally {
        try {
            if (child != null) {
                child.close();
            }
        } catch (IOException e) {
            log.error("Error while closing Directory: " + e.getMessage(), e);
        }
    }
}