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

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

Introduction

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

Prototype

public FileSystemOptions() 

Source Link

Usage

From source file:com.adito.networkplaces.store.http.HTTPMount.java

public FileSystemOptions getOptions(URI uri) {
    FileSystemOptions options = new FileSystemOptions();
    return options;
}

From source file:com.adito.networkplaces.store.ftp.FTPMount.java

public FileSystemOptions getOptions(URI uri) {
    FileSystemOptions options = new FileSystemOptions();
    FtpFileSystemConfigBuilder c = FtpFileSystemConfigBuilder.getInstance();
    String mode = Property.getProperty(new ResourceKey("ftp.mode", this.getNetworkPlace().getResourceType(),
            this.getNetworkPlace().getResourceId()));
    c.setPassiveMode(options, mode.equals("passive"));
    int idleTimeout = Property.getPropertyInt(new ResourceKey("ftp.idleTimeout",
            getNetworkPlace().getResourceType(), getNetworkPlace().getResourceId()));
    //NOTE New Apache-commons-vfs does not support setIdleTimeout(options, idleTimeout)
    //c.setIdleTimeout(options, idleTimeout);
    c.setDataTimeout(options, idleTimeout);
    // TODO: Add resource attribute for all these settings.
    c.setUserDirIsRoot(options, true);//ww  w . j a  va  2 s .co m
    String hostType = Property.getProperty(new ResourceKey("ftp.hostType",
            this.getNetworkPlace().getResourceType(), this.getNetworkPlace().getResourceId()));
    if (!"automatic".equals(hostType)) {
        c.setEntryParser(options, hostType);
    }
    return options;
}

From source file:gov.nih.nci.cacis.ip.mirthconnect.ftp.SFTPSender.java

public void sendDocument(InputStream file, String ftpAddress, String extension) {

    StandardFileSystemManager standardFileSystemManager = new StandardFileSystemManager();
    try {//from  w w  w.  j  av  a 2s  .com
        final FTPInfo ftpInfo = ftpMapping.getFTPInfo(ftpAddress);
        if (ftpInfo == null) {
            throw new ApplicationRuntimeException("No server config exists for address[ " + ftpAddress + " ]");
        }

        String sftpURI = "sftp://" + ftpInfo.getUserName() + ":" + ftpInfo.getPassword() + "@"
                + ftpInfo.getSite() + "/" + ftpInfo.getRootDirectory();
        String fileName = "IHEXIPFTP-" + UUID.randomUUID() + extension;

        FileSystemOptions fileSystemOptions = new FileSystemOptions();
        SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(fileSystemOptions, "no");
        SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(fileSystemOptions, true);

        standardFileSystemManager.init();

        FileObject fileObject = standardFileSystemManager.resolveFile(sftpURI + "/" + fileName,
                fileSystemOptions);

        long timestamp = new Date().getTime();
        String tempSftpFile = sftpFileDirectory + "/" + timestamp + ".xml";
        OutputStream out = new FileOutputStream(new File(tempSftpFile));

        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = file.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        file.close();
        out.flush();
        out.close();

        FileObject localFileObject = standardFileSystemManager.resolveFile(tempSftpFile);

        fileObject.copyFrom(localFileObject, Selectors.SELECT_SELF);

        FileUtils.forceDelete(new File(tempSftpFile));

    } catch (Exception e) {
        throw new ApplicationRuntimeException("Error sending SFTP. " + e.getMessage());
    } finally {
        standardFileSystemManager.close();
    }
}

From source file:jfs.sync.vfs.JFSVFSFile.java

/**
 * Creates a new external root file and reads the structure from server.
 * /*from w  ww . ja  va 2 s .  c o  m*/
 * @param fileProducer
 *            The assigned file producer.
 */
public JFSVFSFile(JFSVFSFileProducer fileProducer) {
    super(fileProducer, "");
    try {
        FileSystemOptions opts = new FileSystemOptions();

        // Avoid using known hosts file if SFTP is used:
        if (fileProducer.getScheme().equals("sftp")) {
            SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
        }

        // Get user name and password, if not specified:
        try {
            URI uriObject = new URI(fileProducer.getUri());
            String userInfo = uriObject.getUserInfo();
            if (userInfo == null || !userInfo.contains(":")) {
                JFSUserAuthentication userAuth = JFSUserAuthentication.getInstance();
                userAuth.setResource(fileProducer.getUri());
                JFSUserAuthenticationInterface userInterface = userAuth.getUserInterface();

                StaticUserAuthenticator auth = new StaticUserAuthenticator(null, userInterface.getUserName(),
                        userInterface.getPassword());
                DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
            }
        } catch (URISyntaxException e) {
            JFSLog.getErr().getStream().println(e.getLocalizedMessage());
        }

        file = VFS.getManager().resolveFile(fileProducer.getUri(), opts);
    } catch (FileSystemException e) {
        JFSLog.getErr().getStream().println(e.getLocalizedMessage());
    }
}

From source file:com.thinkberg.webdav.servlet.MoxoWebDAVServlet.java

public void init(ServletConfig servletConfig) throws ServletException {
    super.init(servletConfig);
    String rootUri = servletConfig.getInitParameter("vfs.uri");
    String authDomain = servletConfig.getInitParameter("vfs.auth.domain");
    String authUser = servletConfig.getInitParameter("vfs.auth.user");
    String authPass = servletConfig.getInitParameter("vfs.auth.password");
    try {/*from w w w  .ja  v  a 2 s  .  c  o  m*/
        StaticUserAuthenticator userAuthenticator = new StaticUserAuthenticator(authDomain, authUser, authPass);
        FileSystemOptions options = new FileSystemOptions();
        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(options, userAuthenticator);

        VFSBackend.initialize(rootUri, options);
    } catch (FileSystemException e) {
        LOG.error(String.format("can't create file system backend for '%s'", rootUri));
    }
}

From source file:com.learningobjects.community.abgm.logic.ControllerJob.java

private void download(String sourceUrl, File destinationFile) throws IOException {
    logger.log(Level.INFO, "Downloading or copying file from " + sourceUrl + " to " + destinationFile);
    FileObject sourceFileObject = null;
    OutputStream outputStream = null;
    try {/* www  .j  av  a2 s.c  o m*/
        // special case sftp so that new hosts work out of the box. other options could go here too
        SftpFileSystemConfigBuilder sftpFileSystemConfigBuilder = SftpFileSystemConfigBuilder.getInstance();
        FileSystemOptions fileSystemOptions = new FileSystemOptions();
        sftpFileSystemConfigBuilder.setStrictHostKeyChecking(fileSystemOptions, "no");
        // actually try to get the file
        FileSystemManager fsManager = VFS.getManager();
        sourceFileObject = fsManager.resolveFile(sourceUrl, fileSystemOptions);
        FileContent sourceFileContent = sourceFileObject.getContent();
        InputStream inputStream = sourceFileContent.getInputStream();
        outputStream = new FileOutputStream(destinationFile);
        // do the copy - this is probably a dupe of commons io, and many others
        byte[] buffer = new byte[8192];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
        }
    } finally {
        if (outputStream != null) {
            outputStream.close();
        }
        if (sourceFileObject != null) {
            sourceFileObject.close(); // this will close the fileContent object, too
        }
    }
}

From source file:de.ecclesia.kipeto.RepositoryResolver.java

/**
 * Ld die Config vom Default-Repository herunter.
 *//* w  w w .ja v  a2 s  .  c om*/
private Properties loadVfsConfig() throws IOException {
    String configUrl = String.format("%s/%s/%s", defaultRepositoryUrl, DIST_DIR, RESOLVE_CONFIG_FILE);
    log.info("Looking for repository-config at {}", configUrl);
    Properties properties = new Properties();
    FileSystemOptions fso = new FileSystemOptions();
    if (isSftp()) {
        Assert.isTrue(this.keyFile.isFile(), "Keyfile is not a file");
        File[] files = { this.keyFile };
        SftpFileSystemConfigBuilder.getInstance().setIdentities(fso, files);
        SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(fso, "yes");
    }

    FileObject fo;
    try {
        fo = VFS.getManager().resolveFile(configUrl, fso);

        BufferedInputStream inputStream = new BufferedInputStream(fo.getContent().getInputStream());

        properties.load(inputStream);
    } catch (FileSystemException e) {
        log.info("No repository-config found at {}", configUrl);
        throw new RuntimeException(e);
    }

    return properties;
}

From source file:com.naryx.tagfusion.cfm.file.vfs.cfVFSData.java

private void setFileObject(String fileUrl) throws Exception {

    // Check to see if this is a native file
    boolean bFoundScheme = false;
    String[] schemes = VFS.getManager().getSchemes();
    for (int x = 0; x < schemes.length; x++) {
        if (fileUrl.startsWith(schemes[x] + "://")) {
            bFoundScheme = true;//from   w w  w  . j  a  v a2  s . c om
            break;
        }
    }

    if (!bFoundScheme) {
        file = new File(fileUrl);
        return;
    }

    if (fileUrl.toLowerCase().startsWith("s3://")) {
        /* This is an S3 object, so we pull out the details here */
        int c1 = fileUrl.indexOf("@");
        if (c1 == -1)
            throw new Exception(
                    "provider S3 authentication 's3://[accesskey]@[secretkey]/[bucket]' or 's3://@[datasource]/[bucket]'");

        int c2 = fileUrl.indexOf("/", c1 + 1);

        String accessKey = fileUrl.substring(5, c1);
        String secretKey = fileUrl.substring(c1 + 1, c2);
        String domain = null;

        if (accessKey.length() == 0) {
            //This is an Amazon datasource format
            org.alanwilliamson.amazon.AmazonKey amazonKey = org.alanwilliamson.amazon.AmazonKeyFactory
                    .getDS(secretKey);
            if (amazonKey == null)
                throw new Exception("The Amazon Datasource [" + secretKey
                        + "] has not been registered; use AmazonRegisterDataSource()");

            accessKey = amazonKey.getKey();
            secretKey = amazonKey.getSecret();
            domain = amazonKey.getS3Host();
        } else
            domain = "http://s3.amazonaws.com/";

        fileUrl = "s3://" + fileUrl.substring(c2);

        FileSystemOptions opts = new FileSystemOptions();
        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts,
                new StaticUserAuthenticator(domain, accessKey, secretKey));

        this.fileUrl = fileUrl;

        fileObject = VFS.getManager().resolveFile(this.fileUrl, opts).resolveFile(null);
    } else {
        this.fileUrl = fileUrl;
        fileObject = VFS.getManager().resolveFile(this.fileUrl);
    }
}

From source file:egovframework.rte.fdl.filehandling.FilehandlingServiceTest.java

@Test
public void testUserAuthentication() throws Exception {
    StaticUserAuthenticator auth = new StaticUserAuthenticator(null, "username", "password");
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    FileSystemManager manager = VFS.getManager();

    FileObject baseDir = manager.resolveFile(System.getProperty("user.dir"));
    FileObject file = manager.resolveFile(baseDir, "testfolder/file1.txt");
    FileObject fo = manager.resolveFile("d:" + file.getName().getPath(), opts);

    fo.createFile();/*  w w w  .j a  v  a 2s .  co m*/

}

From source file:org.efaps.webdav4vfs.WebDAVServlet.java

@Override()
public void init(final ServletConfig servletConfig) throws ServletException {
    super.init(servletConfig);
    String rootUri = servletConfig.getInitParameter("vfs.uri");
    String authDomain = servletConfig.getInitParameter("vfs.auth.domain");
    String authUser = servletConfig.getInitParameter("vfs.auth.user");
    String authPass = servletConfig.getInitParameter("vfs.auth.password");
    try {/*from  w  w  w  .j av  a  2  s.c o  m*/
        StaticUserAuthenticator userAuthenticator = new StaticUserAuthenticator(authDomain, authUser, authPass);
        FileSystemOptions options = new FileSystemOptions();
        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(options, userAuthenticator);

        VFSBackend.initialize(rootUri, options);
    } catch (FileSystemException e) {
        LOG.error(String.format("can't create file system backend for '%s'", rootUri));
    }
}