Example usage for org.apache.commons.vfs2 FileSystemOptions FileSystemOptions

List of usage examples for org.apache.commons.vfs2 FileSystemOptions FileSystemOptions

Introduction

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

Prototype

public FileSystemOptions() 

Source Link

Document

Creates a new instance.

Usage

From source file:org.wso2.carbon.connector.util.FileConnectorUtils.java

public static FileSystemOptions init(MessageContext messageContext) {
    String setTimeout = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.SET_TIME_OUT);
    String setPassiveMode = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.SET_PASSIVE_MODE);
    String setSoTimeout = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.SET_SO_TIMEOUT);
    String setStrictHostKeyChecking = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.SET_STRICT_HOST_KEY_CHECKING);
    String setUserDirIsRoot = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.SET_USER_DIRISROOT);

    if (log.isDebugEnabled()) {
        log.debug("File init starts with " + setTimeout + "," + setPassiveMode + "," + "" + setSoTimeout + ","
                + setStrictHostKeyChecking + "," + setUserDirIsRoot);
    }//from  w ww .  ja  v  a 2s  .c  o  m
    FileSystemOptions opts = new FileSystemOptions();
    // SSH Key checking
    try {
        if (StringUtils.isEmpty(setStrictHostKeyChecking)) {
            SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
        } else {
            setStrictHostKeyChecking = setStrictHostKeyChecking.trim();
            SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, setStrictHostKeyChecking);
        }
    } catch (FileSystemException e) {
        throw new SynapseTaskException("Error while configuring a " + "setStrictHostKeyChecking", e);
    }
    // Root directory set to user home
    if (StringUtils.isEmpty(setUserDirIsRoot)) {
        SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, false);
    } else {
        setUserDirIsRoot = setUserDirIsRoot.trim();
        try {
            SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, Boolean.valueOf(setUserDirIsRoot));
        } catch (Exception e) {
            throw new SynapseTaskException("Error while configuring a " + "setUserDirIsRoot", e);
        }
    }
    // Timeout is count by Milliseconds
    if (StringUtils.isEmpty(setTimeout)) {
        SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, FileConstants.TIME_OUT);
    } else {
        setTimeout = setTimeout.trim();
        try {
            SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, Integer.parseInt(setTimeout));
        } catch (NumberFormatException e) {
            throw new SynapseTaskException("Error while configuring a " + "setTimeout", e);
        }
    }
    if (StringUtils.isEmpty(setPassiveMode)) {
        FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true);
        FtpsFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true);
    } else {
        setPassiveMode = setPassiveMode.trim();
        try {
            FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, Boolean.valueOf(setPassiveMode));
            FtpsFileSystemConfigBuilder.getInstance().setPassiveMode(opts, Boolean.valueOf(setPassiveMode));
        } catch (Exception e) {
            throw new SynapseTaskException("Error while configuring a " + "setPassiveMode", e);
        }
    }
    if (StringUtils.isEmpty(setSoTimeout)) {
        FtpFileSystemConfigBuilder.getInstance().setSoTimeout(opts, FileConstants.TIME_OUT);
    } else {
        setSoTimeout = setSoTimeout.trim();
        try {
            FtpFileSystemConfigBuilder.getInstance().setSoTimeout(opts, Integer.parseInt(setSoTimeout));
        } catch (NumberFormatException e) {
            throw new SynapseTaskException("Error while configuring a " + "setSoTimeout", e);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("FileConnector configuration is completed.");
    }
    return opts;
}

From source file:org.wso2.carbon.connector.util.FTPSiteUtils.java

/**
 * Get the default options for File system
 * //  w w w .j  av  a  2s . c om
 * @return
 * @throws FileSystemException
 */
public static FileSystemOptions createDefaultOptions() throws FileSystemException {
    FileSystemOptions opts = new FileSystemOptions();

    // SSH Key checking
    SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");

    // Root directory set to user home
    SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, false);

    // Timeout is count by Milliseconds

    SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 100000);

    FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true);

    FtpFileSystemConfigBuilder.getInstance().setSoTimeout(opts, 100000);

    FtpsFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true);

    return opts;

}

From source file:org.wso2.carbon.transport.file.connector.server.util.FileTransportUtils.java

public static FileSystemOptions attachFileSystemOptions(Map<String, String> options,
        FileSystemManager fsManager) throws FileServerConnectorException {
    if (options == null) {
        return null; //returning null as this is not an errorneous case.
    }//  w  w w  . ja  v a  2s .  c o  m
    FileSystemOptions opts = new FileSystemOptions();
    DelegatingFileSystemOptionsBuilder delegate = new DelegatingFileSystemOptionsBuilder(fsManager);
    if (Constants.SCHEME_SFTP.equals(options.get(Constants.SCHEME))) {
        Iterator itr = options.entrySet().iterator();

        while (itr.hasNext()) {
            Map.Entry<String, String> entry = (Map.Entry<String, String>) itr.next();
            Constants.SftpFileOption[] array = Constants.SftpFileOption.values();
            int length = array.length;

            for (int i = 0; i < length; ++i) {
                Constants.SftpFileOption option = array[i];
                if (entry.getKey().equals(option.toString()) && null != entry.getValue()) {
                    try {
                        delegate.setConfigString(opts, Constants.SCHEME_SFTP,
                                entry.getKey().toLowerCase(Locale.US), entry.getValue());
                    } catch (FileSystemException e) {
                        throw new FileServerConnectorException(
                                "Failed to set file transport configuration for scheme: "
                                        + Constants.SCHEME_SFTP + " and option: " + option.toString(),
                                e);
                    }
                }
            }
        }
    }
    if (options.get(Constants.FILE_TYPE) != null) {
        try {
            delegate.setConfigString(opts, options.get(Constants.SCHEME), Constants.FILE_TYPE,
                    String.valueOf(getFileType(options.get(Constants.FILE_TYPE))));
        } catch (FileSystemException e) {
            throw new FileServerConnectorException("Failed to set file transport configuration for scheme: "
                    + options.get(Constants.SCHEME) + " and option: " + Constants.FILE_TYPE, e);
        }
    }
    return opts;
}

From source file:org.wso2.carbon.transport.filesystem.connector.server.util.FileTransportUtils.java

/**
 * A utility method for setting the relevant configurations for the file system in question
 *
 * @param options   Options to be used with the file system manager
 * @param fsManager File system manager instance
 * @return A FileSystemOptions instance/*from  w ww. ja  v a 2  s  . c  om*/
 * @throws FileSystemServerConnectorException   Throws an exception if there are any issues in configuring the
 *                                              connector
 */
public static FileSystemOptions attachFileSystemOptions(Map<String, String> options,
        FileSystemManager fsManager) throws FileSystemServerConnectorException {
    if (options == null) {
        return null; //returning null as this is not an errorneous case.
    }
    FileSystemOptions opts = new FileSystemOptions();
    DelegatingFileSystemOptionsBuilder delegate = new DelegatingFileSystemOptionsBuilder(fsManager);
    if (Constants.SCHEME_SFTP.equals(options.get(Constants.SCHEME))) {
        Iterator itr = options.entrySet().iterator();

        while (itr.hasNext()) {
            Map.Entry<String, String> entry = (Map.Entry<String, String>) itr.next();
            Constants.SftpFileOption[] array = Constants.SftpFileOption.values();
            int length = array.length;

            for (int i = 0; i < length; ++i) {
                Constants.SftpFileOption option = array[i];
                if (entry.getKey().equals(option.toString()) && null != entry.getValue()) {
                    try {
                        delegate.setConfigString(opts, Constants.SCHEME_SFTP,
                                entry.getKey().toLowerCase(Locale.US), entry.getValue());
                    } catch (FileSystemException e) {
                        throw new FileSystemServerConnectorException(
                                "Failed to set file transport configuration for scheme: "
                                        + Constants.SCHEME_SFTP + " and option: " + option.toString(),
                                e);
                    }
                }
            }
        }
    }
    if (options.get(Constants.FILE_TYPE) != null) {
        try {
            delegate.setConfigString(opts, options.get(Constants.SCHEME), Constants.FILE_TYPE,
                    String.valueOf(getFileType(options.get(Constants.FILE_TYPE))));
        } catch (FileSystemException e) {
            throw new FileSystemServerConnectorException(
                    "Failed to set file transport configuration for scheme: " + options.get(Constants.SCHEME)
                            + " and option: " + Constants.FILE_TYPE,
                    e);
        }
    }
    return opts;
}

From source file:org.wso2.carbon.transport.remotefilesystem.server.util.FileTransportUtils.java

/**
 * A utility method for setting the relevant configurations for the file system in question
 *
 * @param options   Options to be used with the file system manager
 * @param fsManager File system manager instance
 * @return A FileSystemOptions instance/*w w  w.j  av  a  2  s  . c o m*/
 * @throws RemoteFileSystemConnectorException   Throws an exception if there are any issues in configuring the
 *                                              connector
 */
public static FileSystemOptions attachFileSystemOptions(Map<String, String> options,
        FileSystemManager fsManager) throws RemoteFileSystemConnectorException {
    if (options == null) {
        return null; //returning null as this is not an erroneous case.
    }
    FileSystemOptions opts = new FileSystemOptions();
    DelegatingFileSystemOptionsBuilder delegate = new DelegatingFileSystemOptionsBuilder(fsManager);
    if (Constants.SCHEME_SFTP.equals(options.get(Constants.SCHEME))) {
        Constants.SftpFileOption[] array = Constants.SftpFileOption.values();
        outer: for (Constants.SftpFileOption fileOption : array) {
            for (Map.Entry<String, String> entry : options.entrySet()) {
                if (entry.getValue() != null && entry.getKey().equals(fileOption.toString())) {
                    try {
                        delegate.setConfigString(opts, Constants.SCHEME_SFTP,
                                entry.getKey().toLowerCase(Locale.getDefault()), entry.getValue());
                        continue outer;
                    } catch (FileSystemException e) {
                        throw new RemoteFileSystemConnectorException(
                                "Failed to set file transport configuration for scheme: "
                                        + Constants.SCHEME_SFTP + " and option: " + fileOption.toString(),
                                e);
                    }
                }
            }
        }
    }
    if (options.get(Constants.FILE_TYPE) != null) {
        try {
            delegate.setConfigString(opts, options.get(Constants.SCHEME), Constants.FILE_TYPE,
                    String.valueOf(getFileType(options.get(Constants.FILE_TYPE))));
        } catch (FileSystemException e) {
            throw new RemoteFileSystemConnectorException(
                    "Failed to set file transport configuration for scheme: " + options.get(Constants.SCHEME)
                            + " and option: " + Constants.FILE_TYPE,
                    e);
        }
    }
    return opts;
}

From source file:sftpexamples.GetMyFiles.java

public boolean startFTP(String propertiesFilename, String fileToDownload) {

    props = new Properties();
    StandardFileSystemManager manager = new StandardFileSystemManager();

    try {//www. ja  v a2s. com

        props.load(new FileInputStream(System.getProperty("user.home") + propertiesFilename));
        String serverAddress = props.getProperty("serverAddress").trim();
        String userId = props.getProperty("userId").trim();
        String password = props.getProperty("password").trim();
        String remoteDirectory = props.getProperty("remoteDirectory").trim();
        String localDirectory = props.getProperty("localDirectory").trim();

        //Initializes the file manager
        manager.init();

        //Setup our SFTP configuration
        FileSystemOptions opts = new FileSystemOptions();
        SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
        SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
        SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);

        //Create the SFTP URI using the host name, userid, password,  remote path and file name
        //String sftpUri = "sftp://" + userId + ":" + password + "@" + serverAddress 
        //      + "/" + remoteDirectory + fileToDownload;
        String sftpUri = "sftp://" + userId + ":" + password + "@" + serverAddress + "/" + fileToDownload;
        // Create local file object
        String filepath = localDirectory + fileToDownload;
        File file = new File(filepath);
        FileObject localFile = manager.resolveFile(file.getAbsolutePath());

        // Create remote file object
        FileObject remoteFile = manager.resolveFile(sftpUri, opts);

        // Copy local file to sftp server
        localFile.copyFrom(remoteFile, Selectors.SELECT_SELF);
        System.out.println("File download successful");

    } catch (Exception ex) {
        ex.printStackTrace();
        return false;
    } finally {
        manager.close();
    }

    return true;
}

From source file:sftpexamples.SendMyFiles.java

public boolean startFTP(String propertiesFilename, String fileToFTP) {

    props = new Properties();
    StandardFileSystemManager manager = new StandardFileSystemManager();
    try {/*  w w w . j a  va2s.c  o  m*/
        props.load(new FileInputStream(System.getProperty("user.home") + propertiesFilename));
        //props.setProperty("serverAddress", "127.0.0.1");
        String serverAddress = props.getProperty("serverAddress").trim();
        String userId = props.getProperty("userId").trim();
        String password = props.getProperty("password").trim();
        String remoteDirectory = props.getProperty("remoteDirectory").trim();
        String localDirectory = props.getProperty("localDirectory").trim();
        System.out.println("properties are fetched:serverAddress=" + serverAddress);
        System.out.println("userId=" + userId);
        System.out.println("password=" + password);
        System.out.println("remoteDirectory=" + remoteDirectory);
        System.out.println("localDirectory=" + localDirectory);
        //check if the file exists
        String filepath = localDirectory + fileToFTP;
        System.out.println("filepath:" + filepath);
        File file = new File(filepath);
        if (!file.exists()) {
            throw new RuntimeException("Error. Local file not found");
        }

        //Initializes the file manager
        manager.init();

        //Setup our SFTP configuration
        FileSystemOptions opts = new FileSystemOptions();
        SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
        SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
        SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);

        //Create the SFTP URI using the host name, userid, password,  remote path and file name
        String sftpUri = "sftp://" + userId + ":" + password + "@" + serverAddress + "/" + fileToFTP;
        System.out.println("uri: " + sftpUri);
        // Create local file object
        FileObject localFile = manager.resolveFile(file.getAbsolutePath());

        // Create remote file object
        FileObject remoteFile = manager.resolveFile(sftpUri, opts);

        // Copy local file to sftp server
        remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
        System.out.println("File upload successful");
    } catch (Exception ex) {
        ex.printStackTrace();
        return false;
    } finally {
        manager.close();
    }
    return true;
}

From source file:sftpexamples.SftpUtility.java

/**
 * Method to setup default SFTP config/*from ww w.  j a  v a2  s .com*/
 *
 * @return the FileSystemOptions object containing the specified
 * configuration options
 * @throws FileSystemException
 */
public static FileSystemOptions createDefaultOptions() throws FileSystemException {
    // Create SFTP options
    FileSystemOptions opts = new FileSystemOptions();

    // SSH Key checking
    SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");

    /*
     * Using the following line will cause VFS to choose File System's Root
     * as VFS's root. If I wanted to use User's home as VFS's root then set
     * 2nd method parameter to "true"
     */
    // Root directory set to user home
    SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, false);

    // Timeout is count by Milliseconds
    SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);

    return opts;
}

From source file:si.matjazcerkvenik.dtools.tools.ftp.VfsFtpSftpClient.java

public void upload(String localFile, String remoteFile) {

    StandardFileSystemManager manager = new StandardFileSystemManager();

    try {//from   w  ww  .  j  a v a  2s .  c  o m

        // check if the file exists
        File file = new File(localFile);
        if (!file.exists())
            throw new RuntimeException("Error. Local file not found");

        // Initializes the file manager
        manager.init();

        // Setup our SFTP configuration
        FileSystemOptions opts = new FileSystemOptions();
        SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
        SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
        SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);

        System.out.println("copy " + file.getAbsolutePath() + " to");
        // Create the SFTP URI using the host name, userid, password, remote
        // path and file name
        String sftpUri = protocol + "://" + username + ":" + password + "@" + hostname + ":" + port
                + remoteFile;
        System.out.println(sftpUri);

        // Create local file object
        FileObject localFileObject = manager.resolveFile(file.getAbsolutePath());

        // Create remote file object
        FileObject remoteFileObject = manager.resolveFile(sftpUri, opts);

        // Copy local file to sftp server
        remoteFileObject.copyFrom(localFileObject, Selectors.SELECT_SELF);
        System.out.println("File upload successful");

    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        manager.close();
    }

}

From source file:si.matjazcerkvenik.dtools.tools.ftp.VfsFtpSftpClient.java

public void download(String localFile, String remoteFile) {

    StandardFileSystemManager manager = new StandardFileSystemManager();
    // Initializes the file manager
    try {/*w  ww. java2 s . c o  m*/
        manager.init();
        // Setup our SFTP configuration
        FileSystemOptions opts = new FileSystemOptions();
        SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
        SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
        SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);

        // Create the SFTP URI using the host name, userid, password, remote
        // path and file name
        String sftpUri = protocol + "://" + username + ":" + password + "@" + hostname + ":" + port
                + remoteFile;
        System.out.println(sftpUri);

        // Create local file object
        File file = new File(localFile);
        FileObject localFileObject = manager.resolveFile(file.getAbsolutePath());

        // Create remote file object
        FileObject remoteFileObject = manager.resolveFile(sftpUri, opts);

        // Copy local file to sftp server
        localFileObject.copyFrom(remoteFileObject, Selectors.SELECT_SELF);
        System.out.println("File download successful");
    } catch (FileSystemException e) {
        e.printStackTrace();
    } finally {
        manager.close();
    }

}