Search in sources :

Example 6 with FileContent

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

the class SSHData method OpenConnection.

public static Connection OpenConnection(String serveur, int port, String username, String password, boolean useKey, String keyFilename, String passPhrase, int timeOut, VariableSpace space, String proxyhost, int proxyport, String proxyusername, String proxypassword) throws KettleException {
    Connection conn = null;
    char[] content = null;
    boolean isAuthenticated = false;
    try {
        // perform some checks
        if (useKey) {
            if (Utils.isEmpty(keyFilename)) {
                throw new KettleException(BaseMessages.getString(SSHMeta.PKG, "SSH.Error.PrivateKeyFileMissing"));
            }
            FileObject keyFileObject = KettleVFS.getFileObject(keyFilename);
            if (!keyFileObject.exists()) {
                throw new KettleException(BaseMessages.getString(SSHMeta.PKG, "SSH.Error.PrivateKeyNotExist", keyFilename));
            }
            FileContent keyFileContent = keyFileObject.getContent();
            CharArrayWriter charArrayWriter = new CharArrayWriter((int) keyFileContent.getSize());
            try (InputStream in = keyFileContent.getInputStream()) {
                IOUtils.copy(in, charArrayWriter);
            }
            content = charArrayWriter.toCharArray();
        }
        // Create a new connection
        conn = createConnection(serveur, port);
        /* We want to connect through a HTTP proxy */
        if (!Utils.isEmpty(proxyhost)) {
            // if the proxy requires basic authentication:
            if (!Utils.isEmpty(proxyusername)) {
                conn.setProxyData(new HTTPProxyData(proxyhost, proxyport, proxyusername, proxypassword));
            } else {
                conn.setProxyData(new HTTPProxyData(proxyhost, proxyport));
            }
        }
        // and connect
        if (timeOut == 0) {
            conn.connect();
        } else {
            conn.connect(null, 0, timeOut * 1000);
        }
        // authenticate
        if (useKey) {
            isAuthenticated = conn.authenticateWithPublicKey(username, content, space.environmentSubstitute(passPhrase));
        } else {
            isAuthenticated = conn.authenticateWithPassword(username, password);
        }
        if (isAuthenticated == false) {
            throw new KettleException(BaseMessages.getString(SSHMeta.PKG, "SSH.Error.AuthenticationFailed", username));
        }
    } catch (Exception e) {
        // do not forget to disconnect if connected
        if (conn != null) {
            conn.close();
        }
        throw new KettleException(BaseMessages.getString(SSHMeta.PKG, "SSH.Error.ErrorConnecting", serveur, username), e);
    }
    return conn;
}
Also used : FileContent(org.apache.commons.vfs2.FileContent) KettleException(org.pentaho.di.core.exception.KettleException) InputStream(java.io.InputStream) Connection(com.trilead.ssh2.Connection) FileObject(org.apache.commons.vfs2.FileObject) HTTPProxyData(com.trilead.ssh2.HTTPProxyData) CharArrayWriter(java.io.CharArrayWriter) KettleException(org.pentaho.di.core.exception.KettleException)

Example 7 with FileContent

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

the class VFSTransportListener method processFile.

/**
 * Process a single file through Axis2
 * @param entry the PollTableEntry for the file (or its parent directory or archive)
 * @param file the file that contains the actual message pumped into Axis2
 * @throws AxisFault on error
 */
private void processFile(PollTableEntry entry, FileObject file) throws AxisFault {
    try {
        FileContent content = file.getContent();
        String fileName = file.getName().getBaseName();
        String filePath = file.getName().getPath();
        String fileURI = file.getName().getURI();
        metrics.incrementBytesReceived(content.getSize());
        Map<String, Object> transportHeaders = new HashMap<String, Object>();
        transportHeaders.put(VFSConstants.FILE_PATH, filePath);
        transportHeaders.put(VFSConstants.FILE_NAME, fileName);
        transportHeaders.put(VFSConstants.FILE_URI, fileURI);
        try {
            transportHeaders.put(VFSConstants.FILE_LENGTH, content.getSize());
            transportHeaders.put(VFSConstants.LAST_MODIFIED, content.getLastModifiedTime());
        } catch (FileSystemException ignore) {
        }
        MessageContext msgContext = entry.createMessageContext();
        String contentType = entry.getContentType();
        if (BaseUtils.isBlank(contentType)) {
            if (file.getName().getExtension().toLowerCase().endsWith(".xml")) {
                contentType = "text/xml";
            } else if (file.getName().getExtension().toLowerCase().endsWith(".txt")) {
                contentType = "text/plain";
            }
        } else {
            // Extract the charset encoding from the configured content type and
            // set the CHARACTER_SET_ENCODING property as e.g. SOAPBuilder relies on this.
            String charSetEnc = null;
            try {
                if (contentType != null) {
                    charSetEnc = new ContentType(contentType).getParameter("charset");
                }
            } catch (ParseException ex) {
            // ignore
            }
            msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc);
        }
        // if the content type was not found, but the service defined it.. use it
        if (contentType == null) {
            if (entry.getContentType() != null) {
                contentType = entry.getContentType();
            } else if (VFSUtils.getProperty(content, BaseConstants.CONTENT_TYPE) != null) {
                contentType = VFSUtils.getProperty(content, BaseConstants.CONTENT_TYPE);
            }
        }
        // does the service specify a default reply file URI ?
        String replyFileURI = entry.getReplyFileURI();
        if (replyFileURI != null) {
            msgContext.setProperty(Constants.OUT_TRANSPORT_INFO, new VFSOutTransportInfo(replyFileURI, entry.isFileLockingEnabled()));
        }
        // Determine the message builder to use
        Builder builder;
        if (contentType == null) {
            if (log.isDebugEnabled()) {
                log.debug("No content type specified. Using SOAP builder.");
            }
            builder = new SOAPBuilder();
        } else {
            int index = contentType.indexOf(';');
            String type = index > 0 ? contentType.substring(0, index) : contentType;
            builder = BuilderUtil.getBuilderFromSelector(type, msgContext);
            if (builder == null) {
                if (log.isDebugEnabled()) {
                    log.debug("No message builder found for type '" + type + "'. Falling back to SOAP.");
                }
                builder = new SOAPBuilder();
            }
        }
        // set the message payload to the message context
        InputStream in;
        ManagedDataSource dataSource;
        if (builder instanceof DataSourceMessageBuilder && entry.isStreaming()) {
            in = null;
            dataSource = ManagedDataSourceFactory.create(new FileObjectDataSource(file, contentType));
        } else {
            in = new AutoCloseInputStream(content.getInputStream());
            dataSource = null;
        }
        try {
            OMElement documentElement;
            if (in != null) {
                documentElement = builder.processDocument(in, contentType, msgContext);
            } else {
                documentElement = ((DataSourceMessageBuilder) builder).processDocument(dataSource, contentType, msgContext);
            }
            msgContext.setEnvelope(TransportUtils.createSOAPEnvelope(documentElement));
            handleIncomingMessage(msgContext, transportHeaders, // * SOAP Action - not applicable *//
            null, contentType);
        } finally {
            if (dataSource != null) {
                dataSource.destroy();
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Processed file : " + VFSUtils.maskURLPassword(file.toString()) + " of Content-type : " + contentType);
        }
    } catch (FileSystemException e) {
        handleException("Error reading file content or attributes : " + VFSUtils.maskURLPassword(file.toString()), e);
    } finally {
        try {
            if (file != null) {
                if (fsManager != null && file.getName() != null && file.getName().getScheme() != null && file.getName().getScheme().startsWith("file")) {
                    fsManager.closeFileSystem(file.getParent().getFileSystem());
                }
                file.close();
            }
        } catch (FileSystemException warn) {
        // ignore the warning,  since we handed over the stream close job to
        // AutocloseInputstream..
        }
    }
}
Also used : ContentType(javax.mail.internet.ContentType) HashMap(java.util.HashMap) AutoCloseInputStream(org.apache.commons.io.input.AutoCloseInputStream) InputStream(java.io.InputStream) DataSourceMessageBuilder(org.apache.axis2.format.DataSourceMessageBuilder) Builder(org.apache.axis2.builder.Builder) SOAPBuilder(org.apache.axis2.builder.SOAPBuilder) OMElement(org.apache.axiom.om.OMElement) FileContent(org.apache.commons.vfs2.FileContent) FileSystemException(org.apache.commons.vfs2.FileSystemException) FileObject(org.apache.commons.vfs2.FileObject) SOAPBuilder(org.apache.axis2.builder.SOAPBuilder) FileObjectDataSource(org.apache.synapse.commons.vfs.FileObjectDataSource) MessageContext(org.apache.axis2.context.MessageContext) ParseException(javax.mail.internet.ParseException) VFSOutTransportInfo(org.apache.synapse.commons.vfs.VFSOutTransportInfo) AutoCloseInputStream(org.apache.commons.io.input.AutoCloseInputStream) ManagedDataSource(org.apache.axis2.format.ManagedDataSource) DataSourceMessageBuilder(org.apache.axis2.format.DataSourceMessageBuilder)

Example 8 with FileContent

use of org.apache.commons.vfs2.FileContent in project javautils by jiadongpo.

the class FtpVFS method testsftp.

/**
 * 测试末通过
 *
 * @throws Exception
 */
public void testsftp() throws Exception {
    FileSystemManager fsManager = VFS.getManager();
    FileSystemOptions opts = new FileSystemOptions();
    SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
    // 显示目录
    FileObject fo = fsManager.resolveFile("sftp://ci:Zj4xyBkgjd@10.151.30.10:22/apps/tomcat7-40-tomcat-air-ticket-merchant/logs/", opts);
    // 得到远程文件列表
    FileObject[] children = fo.getChildren();
    for (int i = 0; i < children.length; i++) {
        FileObject f = children[i];
        FileContent c = f.getContent();
        File localFile = new File(f.getName().getBaseName());
        FileOutputStream out = new FileOutputStream(localFile);
        // 写入本地
        org.apache.commons.io.IOUtils.copy(c.getInputStream(), out);
        // 或使用写入
        FileObject obj = fsManager.resolveFile(this.getTargetResourceURL() + f.getName().getBaseName());
        if (!obj.exists()) {
            obj.createFile();
            obj.copyFrom(f, Selectors.SELECT_SELF);
        }
        final long size = (f.getType() == FileType.FILE) ? c.getSize() : -1;
        final long date = (f.getType() == FileType.FILE) ? c.getLastModifiedTime() : -1;
        System.out.println(f.getName().getPath() + " date:" + date + " Size:" + size);
    }
}
Also used : FileContent(org.apache.commons.vfs2.FileContent) FileOutputStream(java.io.FileOutputStream) FileObject(org.apache.commons.vfs2.FileObject) FileSystemManager(org.apache.commons.vfs2.FileSystemManager) File(java.io.File) FileSystemOptions(org.apache.commons.vfs2.FileSystemOptions)

Example 9 with FileContent

use of org.apache.commons.vfs2.FileContent in project mycore by MyCoRe-Org.

the class MCRVFSContent method getInputStream.

@Override
public InputStream getInputStream() throws IOException {
    final FileContent content = fo.getContent();
    LOGGER.debug(() -> getDebugMessage("{}: returning InputStream of {}"));
    return new FilterInputStream(content.getInputStream()) {

        @Override
        public void close() throws IOException {
            LOGGER.debug(() -> getDebugMessage("{}: closing InputStream of {}"));
            super.close();
            content.close();
        }
    };
}
Also used : FileContent(org.apache.commons.vfs2.FileContent) FilterInputStream(java.io.FilterInputStream)

Example 10 with FileContent

use of org.apache.commons.vfs2.FileContent in project mycore by MyCoRe-Org.

the class MCRVFSContent method getETag.

@Override
public String getETag() throws IOException {
    try (FileContent content = fo.getContent()) {
        String systemId = getSystemId();
        if (systemId == null) {
            systemId = fo.getName().getURI();
        }
        long length = content.getSize();
        long lastModified = content.getLastModifiedTime();
        String eTag = getSimpleWeakETag(systemId, length, lastModified);
        if (eTag == null) {
            return null;
        }
        // remove weak
        return eTag.substring(2);
    }
}
Also used : FileContent(org.apache.commons.vfs2.FileContent)

Aggregations

FileContent (org.apache.commons.vfs2.FileContent)20 FileObject (org.apache.commons.vfs2.FileObject)16 IOException (java.io.IOException)6 InputStream (java.io.InputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 File (java.io.File)4 FileSystemException (org.apache.commons.vfs2.FileSystemException)4 FileSystemManager (org.apache.commons.vfs2.FileSystemManager)4 Test (org.junit.Test)4 FileOutputStream (java.io.FileOutputStream)3 OutputStream (java.io.OutputStream)3 URL (java.net.URL)2 FileName (org.apache.commons.vfs2.FileName)2 KettleFileException (org.pentaho.di.core.exception.KettleFileException)2 LogChannelInterface (org.pentaho.di.core.logging.LogChannelInterface)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 Connection (com.trilead.ssh2.Connection)1 HTTPProxyData (com.trilead.ssh2.HTTPProxyData)1 CharArrayWriter (java.io.CharArrayWriter)1