Example usage for org.apache.commons.vfs2 Selectors SELECT_ALL

List of usage examples for org.apache.commons.vfs2 Selectors SELECT_ALL

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 Selectors SELECT_ALL.

Prototype

FileSelector SELECT_ALL

To view the source code for org.apache.commons.vfs2 Selectors SELECT_ALL.

Click Source Link

Document

A FileSelector that selects the base file/folder, plus all its descendants.

Usage

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

/**
 * Delete the file/*from ww  w . j av  a 2s .co  m*/
 *
 * @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./*  w w  w.  j  a  v 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.transport.file.connector.sender.VFSClientConnector.java

@Override
public boolean send(CarbonMessage carbonMessage, CarbonCallback carbonCallback, Map<String, String> map)
        throws ClientConnectorException {
    FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true);
    String fileURI = map.get(Constants.FILE_URI);
    String action = map.get(Constants.ACTION);
    FileType fileType;//from   w  ww  .j a  v a2  s .c o  m
    ByteBuffer byteBuffer;
    InputStream inputStream = null;
    OutputStream outputStream = null;
    try {
        FileSystemManager fsManager = VFS.getManager();
        FileObject path = fsManager.resolveFile(fileURI, opts);
        fileType = path.getType();
        switch (action) {

        case Constants.CREATE:
            boolean isFolder = Boolean.parseBoolean(map.getOrDefault("create-folder", "false"));
            if (path.exists()) {
                throw new ClientConnectorException("File already exists: " + path.getName().getURI());
            }
            if (isFolder) {
                path.createFolder();
            } else {
                path.createFile();
            }
            break;
        case Constants.WRITE:
            if (!path.exists()) {
                path.createFile();
                path.refresh();
                fileType = path.getType();
            }
            if (fileType == FileType.FILE) {
                if (carbonMessage instanceof BinaryCarbonMessage) {
                    BinaryCarbonMessage binaryCarbonMessage = (BinaryCarbonMessage) carbonMessage;
                    byteBuffer = binaryCarbonMessage.readBytes();
                } else {
                    throw new ClientConnectorException("Carbon message received is not a BinaryCarbonMessage");
                }
                byte[] bytes = byteBuffer.array();
                if (map.get(Constants.APPEND) != null) {
                    outputStream = path.getContent()
                            .getOutputStream(Boolean.parseBoolean(map.get(Constants.APPEND)));
                } else {
                    outputStream = path.getContent().getOutputStream();
                }
                outputStream.write(bytes);
                outputStream.flush();
            }
            break;
        case Constants.DELETE:
            if (path.exists()) {
                int filesDeleted = path.delete(Selectors.SELECT_ALL);
                if (logger.isDebugEnabled()) {
                    logger.debug(filesDeleted + " files successfully deleted");
                }
            } else {
                throw new ClientConnectorException(
                        "Failed to delete file: " + path.getName().getURI() + " not found");
            }
            break;
        case Constants.COPY:
            if (path.exists()) {
                String destination = map.get("destination");
                FileObject dest = fsManager.resolveFile(destination, opts);
                dest.copyFrom(path, Selectors.SELECT_ALL);
            } else {
                throw new ClientConnectorException(
                        "Failed to copy file: " + path.getName().getURI() + " not found");
            }
            break;
        case Constants.MOVE:
            if (path.exists()) {
                //TODO: Improve this to fix issue #331
                String destination = map.get("destination");
                FileObject newPath = fsManager.resolveFile(destination, opts);
                FileObject parent = newPath.getParent();
                if (parent != null && !parent.exists()) {
                    parent.createFolder();
                }
                if (!newPath.exists()) {
                    path.moveTo(newPath);
                } else {
                    throw new ClientConnectorException("The file at " + newPath.getURL().toString()
                            + " already exists or it is a directory");
                }
            } else {
                throw new ClientConnectorException(
                        "Failed to move file: " + path.getName().getURI() + " not found");
            }
            break;
        case Constants.READ:
            if (path.exists()) {
                //TODO: Do not assume 'path' always refers to a file
                inputStream = path.getContent().getInputStream();
                byte[] bytes = toByteArray(inputStream);
                BinaryCarbonMessage message = new BinaryCarbonMessage(ByteBuffer.wrap(bytes), true);
                message.setProperty(org.wso2.carbon.messaging.Constants.DIRECTION,
                        org.wso2.carbon.messaging.Constants.DIRECTION_RESPONSE);
                carbonMessageProcessor.receive(message, carbonCallback);
            } else {
                throw new ClientConnectorException(
                        "Failed to read file: " + path.getName().getURI() + " not found");
            }
            break;
        case Constants.EXISTS:
            TextCarbonMessage message = new TextCarbonMessage(Boolean.toString(path.exists()));
            message.setProperty(org.wso2.carbon.messaging.Constants.DIRECTION,
                    org.wso2.carbon.messaging.Constants.DIRECTION_RESPONSE);
            carbonMessageProcessor.receive(message, carbonCallback);
            break;
        default:
            return false;
        }
    } catch (RuntimeException e) {
        throw new ClientConnectorException("Runtime Exception occurred : " + e.getMessage(), e);
    } catch (Exception e) {
        throw new ClientConnectorException("Exception occurred while processing file: " + e.getMessage(), e);
    } finally {
        closeQuietly(inputStream);
        closeQuietly(outputStream);
    }
    return true;
}

From source file:org.wso2.carbon.transport.remotefilesystem.client.connector.contractimpl.VFSClientConnectorImpl.java

@Override
public void send(RemoteFileSystemMessage message) {
    FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true);
    String fileURI = connectorConfig.get(Constants.URI);
    String action = connectorConfig.get(Constants.ACTION);
    FileType fileType;/*from  ww  w.j a va2s  .co m*/
    ByteBuffer byteBuffer;
    InputStream inputStream = null;
    OutputStream outputStream = null;
    FileObject path = null;
    try {
        FileSystemManager fsManager = VFS.getManager();
        path = fsManager.resolveFile(fileURI, opts);
        fileType = path.getType();
        switch (action) {
        case Constants.CREATE:
            boolean isFolder = Boolean
                    .parseBoolean(connectorConfig.getOrDefault(Constants.CREATE_FOLDER, "false"));
            if (path.exists()) {
                throw new RemoteFileSystemConnectorException("File already exists: " + path.getName().getURI());
            }
            if (isFolder) {
                path.createFolder();
            } else {
                path.createFile();
            }
            break;
        case Constants.WRITE:
            if (!path.exists()) {
                path.createFile();
                path.refresh();
                fileType = path.getType();
            }
            if (fileType == FileType.FILE) {
                byteBuffer = message.getBytes();
                byte[] bytes = byteBuffer.array();
                if (connectorConfig.get(Constants.APPEND) != null) {
                    outputStream = path.getContent()
                            .getOutputStream(Boolean.parseBoolean(connectorConfig.get(Constants.APPEND)));
                } else {
                    outputStream = path.getContent().getOutputStream();
                }
                outputStream.write(bytes);
                outputStream.flush();
            }
            break;
        case Constants.DELETE:
            if (path.exists()) {
                int filesDeleted = path.delete(Selectors.SELECT_ALL);
                if (logger.isDebugEnabled()) {
                    logger.debug(filesDeleted + " files successfully deleted");
                }
            } else {
                throw new RemoteFileSystemConnectorException(
                        "Failed to delete file: " + path.getName().getURI() + " not found");
            }
            break;
        case Constants.COPY:
            if (path.exists()) {
                String destination = connectorConfig.get(Constants.DESTINATION);
                FileObject dest = fsManager.resolveFile(destination, opts);
                dest.copyFrom(path, Selectors.SELECT_ALL);
            } else {
                throw new RemoteFileSystemConnectorException(
                        "Failed to copy file: " + path.getName().getURI() + " not found");
            }
            break;
        case Constants.MOVE:
            if (path.exists()) {
                //TODO: Improve this to fix issue #331
                String destination = connectorConfig.get(Constants.DESTINATION);
                FileObject newPath = fsManager.resolveFile(destination, opts);
                FileObject parent = newPath.getParent();
                if (parent != null && !parent.exists()) {
                    parent.createFolder();
                }
                if (!newPath.exists()) {
                    path.moveTo(newPath);
                } else {
                    throw new RemoteFileSystemConnectorException("The file at " + newPath.getURL().toString()
                            + " already exists or it is a directory");
                }
            } else {
                throw new RemoteFileSystemConnectorException(
                        "Failed to move file: " + path.getName().getURI() + " not found");
            }
            break;
        case Constants.READ:
            if (path.exists()) {
                //TODO: Do not assume 'path' always refers to a file
                inputStream = path.getContent().getInputStream();
                byte[] bytes = toByteArray(inputStream);
                RemoteFileSystemMessage fileContent = new RemoteFileSystemMessage(ByteBuffer.wrap(bytes));
                remoteFileSystemListener.onMessage(fileContent);
            } else {
                throw new RemoteFileSystemConnectorException(
                        "Failed to read file: " + path.getName().getURI() + " not found");
            }
            break;
        case Constants.EXISTS:
            RemoteFileSystemMessage fileContent = new RemoteFileSystemMessage(Boolean.toString(path.exists()));
            remoteFileSystemListener.onMessage(fileContent);
            break;
        default:
            break;
        }
        remoteFileSystemListener.done();
    } catch (RemoteFileSystemConnectorException | IOException e) {
        remoteFileSystemListener.onError(e);
    } finally {
        if (path != null) {
            try {
                path.close();
            } catch (FileSystemException e) {
                //Do nothing.
            }
        }
        closeQuietly(inputStream);
        closeQuietly(outputStream);
    }
}

From source file:tain.kr.test.vfs.v01.Shell.java

/**
 * Does a 'cp' command.//from  w  ww .j a v a 2  s. co  m
 */
private void cp(final String[] cmd) throws Exception {

    if (cmd.length < 3) {
        throw new Exception("USAGE: cp <src> <dest>");
    }

    final FileObject src = mgr.resolveFile(cwd, cmd[1]);
    FileObject dest = mgr.resolveFile(cwd, cmd[2]);

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

    dest.copyFrom(src, Selectors.SELECT_ALL);
}

From source file:tw.edu.sinica.iis.SSHadoop.SSHSftp.java

public boolean sftpUpload(final String localDir, final String remoteDir) {
    try {/*from  w w w.  j  ava 2s . co m*/
        manager.init();

        FileObject local = manager.resolveFile(localDir);
        FileObject remote = manager.resolveFile(genConnectString() + remoteDir, opts.getOptions());

        remote.copyFrom(local, Selectors.SELECT_ALL);

    } catch (FileSystemException e) {
        e.printStackTrace();
        return false;
    } finally {
        manager.close();
    }
    return true;
}

From source file:tw.edu.sinica.iis.SSHadoop.SSHSftp.java

public void sftpDownload(final String remoteDir, final String localDir) {
    try {//from   w  w  w.  j ava2s  .  c o m
        manager.init();

        FileObject local = manager.resolveFile(localDir);
        FileObject remote = manager.resolveFile(genConnectString() + remoteDir, opts.getOptions());

        local.copyFrom(remote, Selectors.SELECT_ALL);

    } catch (FileSystemException e) {
        e.printStackTrace();
    } finally {
        manager.close();
    }
}

From source file:tw.edu.sinica.iis.SSHadoop.SSHSftp.java

public void sftpDelete(final String remoteDir) {
    try {/*from  w  ww  . j  ava 2  s .  com*/
        manager.init();

        FileObject remote = manager.resolveFile(genConnectString() + remoteDir, opts.getOptions());

        if (remote.exists()) {
            remote.delete(Selectors.SELECT_ALL);
        }

    } catch (FileSystemException e) {
        e.printStackTrace();
    } finally {
        manager.close();
    }
}