Example usage for org.apache.commons.vfs2.impl StandardFileSystemManager resolveFile

List of usage examples for org.apache.commons.vfs2.impl StandardFileSystemManager resolveFile

Introduction

In this page you can find the example usage for org.apache.commons.vfs2.impl StandardFileSystemManager resolveFile.

Prototype


@Override
public FileObject resolveFile(final String uri, final FileSystemOptions fileSystemOptions)
        throws FileSystemException 

Source Link

Document

Locate a file by URI, use the FileSystemOptions for file-system creation.

Usage

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

/**
 * Decompress the compressed file into the given directory.
 *
 * @param messageContext The message context that is generated for processing unzip operation.
 * @return true, if zip file successfully extracts and false, if not.
 * @throws FileSystemException On error parsing the file name, determining if the file exists and creating the
 * folder.//  ww  w  .  ja va  2  s  .co  m
 */
private boolean unzip(MessageContext messageContext) throws FileSystemException {
    String source = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.FILE_LOCATION);
    String destination = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.NEW_FILE_LOCATION);
    StandardFileSystemManager manager = FileConnectorUtils.getManager();
    FileSystemOptions opts = FileConnectorUtils.init(messageContext);
    FileObject remoteFile = manager.resolveFile(source, opts);
    FileObject remoteDesFile = manager.resolveFile(destination, opts);

    if (!remoteFile.exists()) {
        log.error("File does not exist.");
        return false;
    }
    if (!remoteDesFile.exists()) {
        //create a folder
        remoteDesFile.createFolder();
    }
    //open the zip file
    ZipInputStream zipIn = new ZipInputStream(remoteFile.getContent().getInputStream());
    try {
        ZipEntry entry = zipIn.getNextEntry();

        // iterates over entries in the zip file
        while (entry != null) {
            String filePath = destination + File.separator + entry.getName();
            // create remote object
            FileObject remoteFilePath = manager.resolveFile(filePath, opts);
            if (log.isDebugEnabled()) {
                log.debug("The created path is " + remoteFilePath.toString());
            }
            try {
                if (!entry.isDirectory()) {
                    // if the entry is a file, extracts it
                    extractFile(zipIn, remoteFilePath);
                } else {
                    // if the entry is a directory, make the directory
                    remoteFilePath.createFolder();
                }
            } finally {
                try {
                    zipIn.closeEntry();
                    entry = zipIn.getNextEntry();
                } catch (IOException e) {
                    log.error("Error while closing the ZipInputStream", e);
                }
            }
        }
    } catch (IOException e) {
        throw new SynapseException("Error while reading the next ZIP file entry", e);
    } finally {
        // close the zip file
        try {
            zipIn.close();
        } catch (IOException e) {
            log.error("Error while closing the ZipInputStream", e);
        }
        // close the StandardFileSystemManager
        manager.close();
        try {
            remoteFile.close();
            remoteDesFile.close();
        } catch (FileSystemException e) {
            log.error("Error while closing the FileObject", e);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("File extracted to" + destination);
    }
    return true;
}

From source file:sftpexamples.SftpUtility.java

public static boolean move(String hostName, String username, String password, String remoteSrcFilePath,
        String remoteDestFilePath) {
    StandardFileSystemManager manager = new StandardFileSystemManager();

    try {/*from  w  w w . j a  v a  2 s.  c om*/
        manager.init();

        // Create remote object
        FileObject remoteFile = manager.resolveFile(
                createConnectionString(hostName, username, password, remoteSrcFilePath),
                createDefaultOptions());
        FileObject remoteDestFile = manager.resolveFile(
                createConnectionString(hostName, username, password, remoteDestFilePath),
                createDefaultOptions());

        if (remoteFile.exists()) {
            remoteFile.moveTo(remoteDestFile);
            ;
            System.out.println("Move remote file success");
            return true;
        } else {
            System.out.println("Source file doesn't exist");
            return false;
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        manager.close();
    }
}

From source file:sftpexamples.SftpUtility.java

/**
 * Method to delete the specified file from the remote system
 *
 * @param hostName HostName of the server
 * @param username UserName to login//from  ww w  .  j a  v a  2  s.  c om
 * @param password Password to login
 * @param localFilePath LocalFilePath. Should contain the entire local file
 * path - Directory and Filename with \\ as separator
 * @param remoteFilePath remoteFilePath. Should contain the entire remote
 * file path - Directory and Filename with / as separator
 */
public static void delete(String hostName, String username, String password, String remoteFilePath) {
    StandardFileSystemManager manager = new StandardFileSystemManager();

    try {
        manager.init();

        // Create remote object
        FileObject remoteFile = manager.resolveFile(
                createConnectionString(hostName, username, password, remoteFilePath), createDefaultOptions());

        if (remoteFile.exists()) {
            remoteFile.delete();
            System.out.println("Delete remote file success");
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        manager.close();
    }
}

From source file:sftpexamples.SftpUtility.java

/**
 * Method to check if the remote file exists in the specified remote
 * location/*from   w w w  . j  a  va  2 s  .  c  o  m*/
 *
 * @param hostName HostName of the server
 * @param username UserName to login
 * @param password Password to login
 * @param remoteFilePath remoteFilePath. Should contain the entire remote
 * file path - Directory and Filename with / as separator
 * @return Returns if the file exists in the specified remote location
 */
public static boolean exist(String hostName, String username, String password, String remoteFilePath) {
    StandardFileSystemManager manager = new StandardFileSystemManager();

    try {
        manager.init();

        // Create remote object
        FileObject remoteFile = manager.resolveFile(
                createConnectionString(hostName, username, password, remoteFilePath), createDefaultOptions());

        System.out.println("File exist: " + remoteFile.exists());

        return remoteFile.exists();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        manager.close();
    }
}