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

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

Introduction

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

Prototype

public FileSystemException(final String code, final Throwable throwable, final Object... info) 

Source Link

Document

Constructs exception with the specified detail message.

Usage

From source file:SftpClientFactory.java

/**
 * Creates a new connection to the server.
 * @param hostname The name of the host to connect to.
 * @param port The port to use./*from w  w w.ja  va2s .  c om*/
 * @param username The user's id.
 * @param password The user's password.
 * @param fileSystemOptions The FileSystem options.
 * @return A Session.
 * @throws FileSystemException if an error occurs.
 */
public static Session createConnection(String hostname, int port, char[] username, char[] password,
        FileSystemOptions fileSystemOptions) throws FileSystemException {
    JSch jsch = new JSch();

    File sshDir = null;

    // new style - user passed
    File knownHostsFile = SftpFileSystemConfigBuilder.getInstance().getKnownHosts(fileSystemOptions);
    File[] identities = SftpFileSystemConfigBuilder.getInstance().getIdentities(fileSystemOptions);

    if (knownHostsFile != null) {
        try {
            jsch.setKnownHosts(knownHostsFile.getAbsolutePath());
        } catch (JSchException e) {
            throw new FileSystemException("vfs.provider.sftp/known-hosts.error",
                    knownHostsFile.getAbsolutePath(), e);
        }
    } else {
        sshDir = findSshDir();
        // Load the known hosts file
        knownHostsFile = new File(sshDir, "known_hosts");
        if (knownHostsFile.isFile() && knownHostsFile.canRead()) {
            try {
                jsch.setKnownHosts(knownHostsFile.getAbsolutePath());
            } catch (JSchException e) {
                throw new FileSystemException("vfs.provider.sftp/known-hosts.error",
                        knownHostsFile.getAbsolutePath(), e);
            }
        }
    }

    if (identities != null) {
        for (int iterIdentities = 0; iterIdentities < identities.length; iterIdentities++) {
            final File privateKeyFile = identities[iterIdentities];
            try {
                jsch.addIdentity(privateKeyFile.getAbsolutePath());
            } catch (final JSchException e) {
                throw new FileSystemException("vfs.provider.sftp/load-private-key.error", privateKeyFile, e);
            }
        }
    } else {
        if (sshDir == null) {
            sshDir = findSshDir();
        }

        // Load the private key (rsa-key only)
        final File privateKeyFile = new File(sshDir, "id_rsa");
        if (privateKeyFile.isFile() && privateKeyFile.canRead()) {
            try {
                jsch.addIdentity(privateKeyFile.getAbsolutePath());
            } catch (final JSchException e) {
                throw new FileSystemException("vfs.provider.sftp/load-private-key.error", privateKeyFile, e);
            }
        }
    }

    Session session;
    try {
        session = jsch.getSession(new String(username), hostname, port);
        if (password != null) {
            session.setPassword(new String(password));
        }

        Integer timeout = SftpFileSystemConfigBuilder.getInstance().getTimeout(fileSystemOptions);
        if (timeout != null) {
            session.setTimeout(timeout.intValue());
        }

        UserInfo userInfo = SftpFileSystemConfigBuilder.getInstance().getUserInfo(fileSystemOptions);
        if (userInfo != null) {
            session.setUserInfo(userInfo);
        }

        Properties config = new Properties();

        //set StrictHostKeyChecking property
        String strictHostKeyChecking = SftpFileSystemConfigBuilder.getInstance()
                .getStrictHostKeyChecking(fileSystemOptions);
        if (strictHostKeyChecking != null) {
            config.setProperty("StrictHostKeyChecking", strictHostKeyChecking);
        }
        //set PreferredAuthentications property
        String preferredAuthentications = SftpFileSystemConfigBuilder.getInstance()
                .getPreferredAuthentications(fileSystemOptions);
        if (preferredAuthentications != null) {
            config.setProperty("PreferredAuthentications", preferredAuthentications);
        }

        //set compression property
        String compression = SftpFileSystemConfigBuilder.getInstance().getCompression(fileSystemOptions);
        if (compression != null) {
            config.setProperty("compression.s2c", compression);
            config.setProperty("compression.c2s", compression);
        }

        String proxyHost = SftpFileSystemConfigBuilder.getInstance().getProxyHost(fileSystemOptions);
        if (proxyHost != null) {
            int proxyPort = SftpFileSystemConfigBuilder.getInstance().getProxyPort(fileSystemOptions);
            SftpFileSystemConfigBuilder.ProxyType proxyType = SftpFileSystemConfigBuilder.getInstance()
                    .getProxyType(fileSystemOptions);
            Proxy proxy = null;
            if (SftpFileSystemConfigBuilder.PROXY_HTTP.equals(proxyType)) {
                if (proxyPort != 0) {
                    proxy = new ProxyHTTP(proxyHost, proxyPort);
                } else {
                    proxy = new ProxyHTTP(proxyHost);
                }
            } else if (SftpFileSystemConfigBuilder.PROXY_SOCKS5.equals(proxyType)) {
                if (proxyPort != 0) {
                    proxy = new ProxySOCKS5(proxyHost, proxyPort);
                } else {
                    proxy = new ProxySOCKS5(proxyHost);
                }
            }

            if (proxy != null) {
                session.setProxy(proxy);
            }
        }

        //set properties for the session
        if (config.size() > 0) {
            session.setConfig(config);
        }
        session.setDaemonThread(true);
        session.connect();
    } catch (final Exception exc) {
        throw new FileSystemException("vfs.provider.sftp/connect.error", new Object[] { hostname }, exc);
    }

    return session;
}

From source file:com.github.songsheng.vfs2.provider.nfs.NfsFileRandomAccessContent.java

public NfsFileRandomAccessContent(final XFile NfsFile, final RandomAccessMode mode) throws FileSystemException {
    super(mode);//from w  ww . j  a v a  2s .  c o m

    try {
        raf = new XRandomAccessFile(NfsFile, mode.getModeString());
        rafis = new InputStream() {
            @Override
            public int available() throws IOException {
                final long available = raf.length() - raf.getFilePointer();
                if (available > Integer.MAX_VALUE) {
                    return Integer.MAX_VALUE;
                }

                return (int) available;
            }

            @Override
            public void close() throws IOException {
                raf.close();
            }

            @Override
            public int read() throws IOException {
                return raf.readByte();
            }

            @Override
            public int read(final byte[] b) throws IOException {
                return raf.read(b);
            }

            @Override
            public int read(final byte[] b, final int off, final int len) throws IOException {
                return raf.read(b, off, len);
            }

            @Override
            public long skip(final long n) throws IOException {
                raf.seek(raf.getFilePointer() + n);
                return n;
            }
        };
    } catch (final MalformedURLException e) {
        throw new FileSystemException("vfs.provider/random-access-open-failed.error", NfsFile, e);
    } catch (final UnknownHostException e) {
        throw new FileSystemException("vfs.provider/random-access-open-failed.error", NfsFile, e);
    } catch (IOException e) {
        throw new FileSystemException("vfs.provider/random-access-open-failed.error", NfsFile, e);
    }
}

From source file:com.adito.networkplaces.vfs2.provider.smb.SmbFileRandomAccessContent.java

SmbFileRandomAccessContent(final SmbFile smbFile, final RandomAccessMode mode) throws FileSystemException {
    super(mode);/*from w w  w.j  av  a  2  s.com*/

    final StringBuilder modes = new StringBuilder(2);
    if (mode.requestRead()) {
        modes.append('r');
    }
    if (mode.requestWrite()) {
        modes.append('w');
    }

    try {
        raf = new SmbRandomAccessFile(smbFile, modes.toString());
        rafis = new InputStream() {
            @Override
            public int read() throws IOException {
                return raf.readByte();
            }

            @Override
            public long skip(long n) throws IOException {
                raf.seek(raf.getFilePointer() + n);
                return n;
            }

            @Override
            public void close() throws IOException {
                raf.close();
            }

            @Override
            public int read(byte b[]) throws IOException {
                return raf.read(b);
            }

            @Override
            public int read(byte b[], int off, int len) throws IOException {
                return raf.read(b, off, len);
            }

            @Override
            public int available() throws IOException {
                long available = raf.length() - raf.getFilePointer();
                if (available > Integer.MAX_VALUE) {
                    return Integer.MAX_VALUE;
                }

                return (int) available;
            }
        };
    } catch (MalformedURLException e) {
        throw new FileSystemException("vfs.provider/random-access-open-failed.error", smbFile, e);
    } catch (SmbException e) {
        throw new FileSystemException("vfs.provider/random-access-open-failed.error", smbFile, e);
    } catch (UnknownHostException e) {
        throw new FileSystemException("vfs.provider/random-access-open-failed.error", smbFile, e);
    }
}

From source file:fr.cls.atoll.motu.library.misc.vfs.provider.gsiftp.GsiFtpClientFactory.java

/**
 * Creates a new connection to the server.
 * /*from  www .j av  a 2  s.  c  o  m*/
 * @param hostname the hostname
 * @param portN the port n
 * @param username the username
 * @param password the password
 * @param fileSystemOptions the file system options
 * 
 * @return the grid ftp client
 * 
 * @throws FileSystemException the file system exception
 */
public static GridFTPClient createConnection(String hostname, int portN, String username, String password,
        FileSystemOptions fileSystemOptions) throws FileSystemException {
    GsiFtpClientFactory.host = hostname;
    GsiFtpClientFactory.port = portN;

    try {
        if (password == null) {
            throw new Exception("Password cannot be null");
        }

        // Create a proxy cert (if missing)
        new ProxyTool().createProxy(password);

        final GridFTPClient client = new GridFTPClient(hostname, port);

        try {

            // Authenticate w/ user credentials defines in $HOME/.globus/cog.properties
            client.authenticate(null);

            // Set binary mode
            // if (!client.setFileType(FTP.BINARY_FILE_TYPE))
            // {
            // throw new FileSystemException("vfs.provider.ftp/set-binary.error", hostname);
            // }

            // Set dataTimeout value
            // Integer dataTimeout =
            // FtpFileSystemConfigBuilder.getInstance().getDataTimeout(fileSystemOptions);
            // if (dataTimeout != null)
            // {
            // client.setDataTimeout(dataTimeout.intValue());
            // }

            // Change to root by default
            // All file operations a relative to the filesystem-root
            // String root = getRoot().getName().getPath();

            // Boolean userDirIsRoot =
            // FtpFileSystemConfigBuilder.getInstance().getUserDirIsRoot(fileSystemOptions);
            // if (workingDirectory != null && (userDirIsRoot == null || !userDirIsRoot.booleanValue()))
            // {
            // if (!client.changeWorkingDirectory(workingDirectory))
            // {
            // throw new FileSystemException("vfs.provider.ftp/change-work-directory.error",
            // workingDirectory);
            // }
            // }
            //
            // Boolean passiveMode =
            // FtpFileSystemConfigBuilder.getInstance().getPassiveMode(fileSystemOptions);
            // if (passiveMode != null && passiveMode.booleanValue())
            // {
            // client.enterLocalPassiveMode();
            // }
        } catch (final IOException e) {
            if (client != null) { // .isConnected())
                client.close();
            }
            throw e;
        }

        return client;
    } catch (final Exception exc) {
        throw new FileSystemException("vfs.provider.gsiftp/connect.error", new Object[] { hostname }, exc);
    }
}

From source file:fr.cls.atoll.motu.library.misc.vfs.provider.gsiftp.GsiFtpFileProvider.java

/**
 * Creates a {@link FileSystem}.//w  w w  . ja va2 s. c  o  m
 * 
 * @param name the name
 * @param fileSystemOptions the file system options
 * 
 * @return the file system
 * 
 * @throws FileSystemException the file system exception
 */
@Override
protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
        throws FileSystemException {
    // Create the file system
    final GenericFileName rootName = (GenericFileName) name;

    String attrHome;
    GridFTPClientWrapper gridFtpClient = null;
    try {
        gridFtpClient = new GridFTPClientWrapper(rootName, fileSystemOptions);
        log.debug("Creating connection to GsiFTP Host:" + gridFtpClient.getRoot().getHostName() + " Port:"
                + gridFtpClient.getRoot().getPort() + " User:" + gridFtpClient.getRoot().getUserName()
                + " Path:" + gridFtpClient.getRoot().getPath());
        attrHome = gridFtpClient.getCurrentDir();
        log.debug("Current directory: " + attrHome);
    } catch (Exception e) {
        throw new FileSystemException("vfs.provider.gsiftp/connect.error", name, e);
    }

    // // Session session;
    // GridFTPClient client;
    // String attrHome;
    // try {
    // log.debug("Creating connection to GsiFTP Host:" + rootName.getHostName() + " Port:" +
    // rootName.getPort() + " User:"
    // + rootName.getUserName() + " Path:" + rootName.getPath());
    //
    // client = GsiFtpClientFactory.createConnection(rootName.getHostName(),
    // rootName.getPort(),
    // rootName.getUserName(),
    // rootName.getPassword(),
    // fileSystemOptions);
    //
    // attrHome = client.getCurrentDir();
    // log.debug("Current directory: " + attrHome);
    // } catch (final Exception e) {
    // throw new FileSystemException("vfs.provider.gsiftp/connect.error", name, e);
    // }

    // set HOME dir attribute
    final GsiFtpFileSystem fs = new GsiFtpFileSystem(rootName, gridFtpClient, fileSystemOptions);
    fs.setAttribute(ATTR_HOME_DIR, attrHome);

    return fs;
}

From source file:org.pentaho.di.core.vfs.SftpFileObjectWithWindowsSupport.java

@Override
public boolean isReadable() throws FileSystemException {
    try {//from w ww  .j  a  v  a  2  s .  c  o m
        if (!this.sftpFileSystemWindows.isRemoteHostWindows()) {
            return sftpFileObject.isReadable();
        } else {
            return this.exists() && this.doIsReadable();
        }

    } catch (Exception var) {
        throw new FileSystemException("vfs.provider/check-is-readable.error", sftpFileObject.getName(), var);
    }
}

From source file:org.pentaho.di.core.vfs.SftpFileObjectWithWindowsSupport.java

@Override
public boolean isWriteable() throws FileSystemException {
    try {/*from   w  w  w .  j  av  a2 s  .  c  o  m*/
        if (!this.sftpFileSystemWindows.isRemoteHostWindows()) {
            return sftpFileObject.isWriteable();
        } else {
            return this.exists() && this.doIsWriteable();
        }

    } catch (Exception var) {
        throw new FileSystemException("vfs.provider/check-is-writeable.error", sftpFileObject.getName(), var);
    }
}

From source file:org.pentaho.di.core.vfs.SftpFileSystemWindows.java

/**
 * {@link  org.apache.commons.vfs2.provider.sftp.SftpFileSystem#getChannel() }
 *
 *///from   ww  w  .  j a  v a2  s .co m
private void ensureSession() throws FileSystemException {
    if (this.session == null || !this.session.isConnected()) {
        this.doCloseCommunicationLink();
        UserAuthenticationData authData = null;

        Session session;
        try {
            GenericFileName e = (GenericFileName) this.getRootName();
            authData = UserAuthenticatorUtils.authenticate(this.getFileSystemOptions(),
                    SftpFileProvider.AUTHENTICATOR_TYPES);
            session = SftpClientFactory.createConnection(e.getHostName(), e.getPort(),
                    UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME,
                            UserAuthenticatorUtils.toChar(e.getUserName())),
                    UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD,
                            UserAuthenticatorUtils.toChar(e.getPassword())),
                    this.getFileSystemOptions());
        } catch (Exception var7) {
            throw new FileSystemException("vfs.provider.sftp/connect.error", this.getRootName(), var7);
        } finally {
            UserAuthenticatorUtils.cleanup(authData);
        }

        this.session = session;
    }

}

From source file:org.ysb33r.groovy.vfsplugin.cpio.CpioFileSystem.java

private void recreateCpioFile() throws FileSystemException {
    if (this.cpioFile != null) {
        try {/*from ww w  . java2s.c  om*/
            this.cpioFile.close();
        } catch (final IOException e) {
            throw new FileSystemException("vfs.provider.Cpio/close-Cpio-file.error", file, e);
        }
        cpioFile = null;
    }

    final CpioArchiveInputStream CpioFile = createCpioFile(this.file);
    this.cpioFile = CpioFile;
}

From source file:org.ysb33r.groovy.vfsplugin.cpio.CpioFileSystem.java

protected CpioArchiveInputStream createCpioFile(final File file) throws FileSystemException {
    try {/* ww w  .j a  v a 2  s .com*/
        if ("cpiogz".equalsIgnoreCase(getRootName().getScheme())) {
            return new CpioArchiveInputStream(new GZIPInputStream(new FileInputStream(file)));
        } else if ("cpiobz2".equalsIgnoreCase(getRootName().getScheme())) {
            return new CpioArchiveInputStream(
                    Bzip2FileObject.wrapInputStream(file.getAbsolutePath(), new FileInputStream(file)));
        }
        return new CpioArchiveInputStream(new FileInputStream(file));
    } catch (final IOException ioe) {
        throw new FileSystemException("vfs.provider.Cpio/open-Cpio-file.error", file, ioe);
    }
}