Search in sources :

Example 41 with FileSystemManager

use of org.apache.commons.vfs2.FileSystemManager in project pentaho-kettle by pentaho.

the class KettleVFS method startsWithScheme.

/**
 * Check if filename starts with one of the known protocols like file: zip: ram: smb: jar: etc.
 * If yes, return true otherwise return false
 *
 * @param vfsFileName
 * @return boolean
 */
public static boolean startsWithScheme(String vfsFileName) {
    FileSystemManager fsManager = getInstance().getFileSystemManager();
    boolean found = false;
    String[] schemes = fsManager.getSchemes();
    for (int i = 0; i < schemes.length; i++) {
        if (vfsFileName.startsWith(schemes[i] + ":")) {
            found = true;
            break;
        }
    }
    return found;
}
Also used : DefaultFileSystemManager(org.apache.commons.vfs2.impl.DefaultFileSystemManager) FileSystemManager(org.apache.commons.vfs2.FileSystemManager) StandardFileSystemManager(org.apache.commons.vfs2.impl.StandardFileSystemManager)

Example 42 with FileSystemManager

use of org.apache.commons.vfs2.FileSystemManager in project wso2-synapse by wso2.

the class VFSUtils method attachFileSystemOptions.

public static FileSystemOptions attachFileSystemOptions(Map<String, String> options, FileSystemManager fsManager) throws FileSystemException {
    if (options == null) {
        return null;
    }
    FileSystemOptions opts = new FileSystemOptions();
    DelegatingFileSystemOptionsBuilder delegate = new DelegatingFileSystemOptionsBuilder(fsManager);
    // sftp configs
    for (Map.Entry<String, String> entry : options.entrySet()) {
        for (VFSConstants.SFTP_FILE_OPTION option : VFSConstants.SFTP_FILE_OPTION.values()) {
            if (entry.getKey().equals(option.toString()) && entry.getValue() != null) {
                delegate.setConfigString(opts, VFSConstants.SCHEME_SFTP, entry.getKey().toLowerCase(), entry.getValue());
            }
        }
    }
    FtpsFileSystemConfigBuilder configBuilder = FtpsFileSystemConfigBuilder.getInstance();
    // ftp and ftps configs
    String passiveMode = options.get(PASSIVE_MODE);
    if (passiveMode != null) {
        configBuilder.setPassiveMode(opts, Boolean.parseBoolean(passiveMode));
    }
    // ftps configs
    String implicitMode = options.get(IMPLICIT_MODE);
    if (implicitMode != null) {
        if (Boolean.parseBoolean(implicitMode)) {
            configBuilder.setFtpsMode(opts, FtpsMode.IMPLICIT);
        } else {
            configBuilder.setFtpsMode(opts, FtpsMode.EXPLICIT);
        }
    }
    String protectionMode = options.get(PROTECTION_MODE);
    if ("P".equalsIgnoreCase(protectionMode)) {
        configBuilder.setDataChannelProtectionLevel(opts, FtpsDataChannelProtectionLevel.P);
    } else if ("C".equalsIgnoreCase(protectionMode)) {
        configBuilder.setDataChannelProtectionLevel(opts, FtpsDataChannelProtectionLevel.C);
    } else if ("S".equalsIgnoreCase(protectionMode)) {
        configBuilder.setDataChannelProtectionLevel(opts, FtpsDataChannelProtectionLevel.S);
    } else if ("E".equalsIgnoreCase(protectionMode)) {
        configBuilder.setDataChannelProtectionLevel(opts, FtpsDataChannelProtectionLevel.E);
    }
    String keyStore = options.get(KEY_STORE);
    if (keyStore != null) {
        configBuilder.setKeyStore(opts, keyStore);
    }
    String trustStore = options.get(TRUST_STORE);
    if (trustStore != null) {
        configBuilder.setTrustStore(opts, trustStore);
    }
    String keyStorePassword = options.get(KS_PASSWD);
    if (keyStorePassword != null) {
        configBuilder.setKeyStorePW(opts, keyStorePassword);
    }
    String trustStorePassword = options.get(TS_PASSWD);
    if (trustStorePassword != null) {
        configBuilder.setTrustStorePW(opts, trustStorePassword);
    }
    String keyPassword = options.get(KEY_PASSWD);
    if (keyPassword != null) {
        configBuilder.setKeyPW(opts, keyPassword);
    }
    if (options.get(VFSConstants.FILE_TYPE) != null) {
        delegate.setConfigString(opts, options.get(VFSConstants.SCHEME), VFSConstants.FILE_TYPE, options.get(VFSConstants.FILE_TYPE));
    }
    return opts;
}
Also used : FtpsFileSystemConfigBuilder(org.apache.commons.vfs2.provider.ftps.FtpsFileSystemConfigBuilder) DelegatingFileSystemOptionsBuilder(org.apache.commons.vfs2.util.DelegatingFileSystemOptionsBuilder) HashMap(java.util.HashMap) Map(java.util.Map) FileSystemOptions(org.apache.commons.vfs2.FileSystemOptions)

Example 43 with FileSystemManager

use of org.apache.commons.vfs2.FileSystemManager in project wso2-synapse by wso2.

the class VFSUtils method releaseLock.

/**
 * Release a file item lock acquired either by the VFS listener or a sender
 *
 * @param fsManager which is used to resolve the processed file
 * @param fo representing the processed file
 * @param fso represents file system options used when resolving file from file system manager.
 */
public static void releaseLock(FileSystemManager fsManager, FileObject fo, FileSystemOptions fso) {
    String fullPath = fo.getName().getURI();
    try {
        int pos = fullPath.indexOf('?');
        if (pos > -1) {
            fullPath = fullPath.substring(0, pos);
        }
        FileObject lockObject = fsManager.resolveFile(fullPath + LOCK_FILE_SUFFIX, fso);
        if (lockObject.exists()) {
            lockObject.delete();
        }
    } catch (FileSystemException e) {
        log.error("Couldn't release the lock for the file : " + maskURLPassword(fo.getName().getURI()) + " after processing");
    }
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) FileObject(org.apache.commons.vfs2.FileObject)

Example 44 with FileSystemManager

use of org.apache.commons.vfs2.FileSystemManager in project wso2-synapse by wso2.

the class VFSUtils method releaseFail.

public static void releaseFail(FileSystemManager fsManager, FileObject fo, FileSystemOptions fso) {
    try {
        String fullPath = fo.getName().getURI();
        int pos = fullPath.indexOf('?');
        if (pos > -1) {
            fullPath = fullPath.substring(0, pos);
        }
        FileObject failObject = fsManager.resolveFile(fullPath + FAIL_FILE_SUFFIX, fso);
        if (failObject.exists()) {
            failObject.delete();
        }
    } catch (FileSystemException e) {
        log.error("Couldn't release the fail for the file : " + maskURLPassword(fo.getName().getURI()));
    }
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) FileObject(org.apache.commons.vfs2.FileObject)

Example 45 with FileSystemManager

use of org.apache.commons.vfs2.FileSystemManager in project wso2-synapse by wso2.

the class VFSUtils method markFailRecord.

public static synchronized void markFailRecord(FileSystemManager fsManager, FileObject fo, FileSystemOptions fso) {
    // generate a random fail value to ensure that there are no two parties
    // processing the same file
    byte[] failValue = (Long.toString((new Date()).getTime())).getBytes();
    try {
        String fullPath = getFullPath(fo);
        FileObject failObject = fsManager.resolveFile(fullPath + FAIL_FILE_SUFFIX, fso);
        if (!failObject.exists()) {
            failObject.createFile();
        }
        // write a lock file before starting of the processing, to ensure that the
        // item is not processed by any other parties
        OutputStream stream = failObject.getContent().getOutputStream();
        try {
            stream.write(failValue);
            stream.flush();
        } catch (IOException e) {
            failObject.delete();
            log.error("Couldn't create the fail file before processing the file " + maskURLPassword(fullPath), e);
        } finally {
            try {
                stream.close();
            } catch (IOException e) {
                log.debug("Error closing stream", e);
            }
            failObject.close();
        }
    } catch (FileSystemException fse) {
        log.error("Cannot get the lock for the file : " + maskURLPassword(fo.getName().getURI()) + " before processing");
    }
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) OutputStream(java.io.OutputStream) FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) Date(java.util.Date)

Aggregations

FileSystemManager (org.apache.commons.vfs2.FileSystemManager)30 FileObject (org.apache.commons.vfs2.FileObject)29 FileSystemException (org.apache.commons.vfs2.FileSystemException)21 DefaultFileSystemManager (org.apache.commons.vfs2.impl.DefaultFileSystemManager)17 File (java.io.File)12 IOException (java.io.IOException)11 StandardFileSystemManager (org.apache.commons.vfs2.impl.StandardFileSystemManager)7 Test (org.junit.Test)6 FileContent (org.apache.commons.vfs2.FileContent)4 FileSystemOptions (org.apache.commons.vfs2.FileSystemOptions)4 OutputStream (java.io.OutputStream)3 RepositoryException (javax.jcr.RepositoryException)3 FileName (org.apache.commons.vfs2.FileName)3 SoftRefFilesCache (org.apache.commons.vfs2.cache.SoftRefFilesCache)3 FileOutputStream (java.io.FileOutputStream)2 FileWriter (java.io.FileWriter)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 Map (java.util.Map)2