Search in sources :

Example 11 with ByteArrayInputStream

use of java.io.ByteArrayInputStream in project camel by apache.

the class SftpOperations method doStoreFile.

private boolean doStoreFile(String name, String targetName, Exchange exchange) throws GenericFileOperationFailedException {
    LOG.trace("doStoreFile({})", targetName);
    // if an existing file already exists what should we do?
    if (endpoint.getFileExist() == GenericFileExist.Ignore || endpoint.getFileExist() == GenericFileExist.Fail || endpoint.getFileExist() == GenericFileExist.Move) {
        boolean existFile = existsFile(targetName);
        if (existFile && endpoint.getFileExist() == GenericFileExist.Ignore) {
            // ignore but indicate that the file was written
            LOG.trace("An existing file already exists: {}. Ignore and do not override it.", name);
            return true;
        } else if (existFile && endpoint.getFileExist() == GenericFileExist.Fail) {
            throw new GenericFileOperationFailedException("File already exist: " + name + ". Cannot write new file.");
        } else if (existFile && endpoint.getFileExist() == GenericFileExist.Move) {
            // move any existing file first
            doMoveExistingFile(name, targetName);
        }
    }
    InputStream is = null;
    if (exchange.getIn().getBody() == null) {
        // Do an explicit test for a null body and decide what to do
        if (endpoint.isAllowNullBody()) {
            LOG.trace("Writing empty file.");
            is = new ByteArrayInputStream(new byte[] {});
        } else {
            throw new GenericFileOperationFailedException("Cannot write null body to file: " + name);
        }
    }
    try {
        if (is == null) {
            String charset = endpoint.getCharset();
            if (charset != null) {
                // charset configured so we must convert to the desired
                // charset so we can write with encoding
                is = new ByteArrayInputStream(exchange.getIn().getMandatoryBody(String.class).getBytes(charset));
                LOG.trace("Using InputStream {} with charset {}.", is, charset);
            } else {
                is = exchange.getIn().getMandatoryBody(InputStream.class);
            }
        }
        final StopWatch watch = new StopWatch();
        LOG.debug("About to store file: {} using stream: {}", targetName, is);
        if (endpoint.getFileExist() == GenericFileExist.Append) {
            LOG.trace("Client appendFile: {}", targetName);
            channel.put(is, targetName, ChannelSftp.APPEND);
        } else {
            LOG.trace("Client storeFile: {}", targetName);
            // override is default
            channel.put(is, targetName);
        }
        watch.stop();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Took {} ({} millis) to store file: {} and FTP client returned: true", new Object[] { TimeUtils.printDuration(watch.taken()), watch.taken(), targetName });
        }
        // after storing file, we may set chmod on the file
        String mode = endpoint.getConfiguration().getChmod();
        if (ObjectHelper.isNotEmpty(mode)) {
            // parse to int using 8bit mode
            int permissions = Integer.parseInt(mode, 8);
            LOG.trace("Setting chmod: {} on file: {}", mode, targetName);
            channel.chmod(permissions, targetName);
        }
        return true;
    } catch (SftpException e) {
        throw new GenericFileOperationFailedException("Cannot store file: " + name, e);
    } catch (InvalidPayloadException e) {
        throw new GenericFileOperationFailedException("Cannot store file: " + name, e);
    } catch (UnsupportedEncodingException e) {
        throw new GenericFileOperationFailedException("Cannot store file: " + name, e);
    } finally {
        IOHelper.close(is, "store: " + name, LOG);
    }
}
Also used : GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SftpException(com.jcraft.jsch.SftpException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InvalidPayloadException(org.apache.camel.InvalidPayloadException) GenericFileEndpoint(org.apache.camel.component.file.GenericFileEndpoint) StopWatch(org.apache.camel.util.StopWatch)

Example 12 with ByteArrayInputStream

use of java.io.ByteArrayInputStream in project camel by apache.

the class FtpOperations method doStoreFile.

private boolean doStoreFile(String name, String targetName, Exchange exchange) throws GenericFileOperationFailedException {
    log.trace("doStoreFile({})", targetName);
    // if an existing file already exists what should we do?
    if (endpoint.getFileExist() == GenericFileExist.Ignore || endpoint.getFileExist() == GenericFileExist.Fail || endpoint.getFileExist() == GenericFileExist.Move) {
        boolean existFile = existsFile(targetName);
        if (existFile && endpoint.getFileExist() == GenericFileExist.Ignore) {
            // ignore but indicate that the file was written
            log.trace("An existing file already exists: {}. Ignore and do not override it.", name);
            return true;
        } else if (existFile && endpoint.getFileExist() == GenericFileExist.Fail) {
            throw new GenericFileOperationFailedException("File already exist: " + name + ". Cannot write new file.");
        } else if (existFile && endpoint.getFileExist() == GenericFileExist.Move) {
            // move any existing file first
            doMoveExistingFile(name, targetName);
        }
    }
    InputStream is = null;
    if (exchange.getIn().getBody() == null) {
        // Do an explicit test for a null body and decide what to do
        if (endpoint.isAllowNullBody()) {
            log.trace("Writing empty file.");
            is = new ByteArrayInputStream(new byte[] {});
        } else {
            throw new GenericFileOperationFailedException("Cannot write null body to file: " + name);
        }
    }
    try {
        if (is == null) {
            String charset = endpoint.getCharset();
            if (charset != null) {
                // charset configured so we must convert to the desired
                // charset so we can write with encoding
                is = new ByteArrayInputStream(exchange.getIn().getMandatoryBody(String.class).getBytes(charset));
                log.trace("Using InputStream {} with charset {}.", is, charset);
            } else {
                is = exchange.getIn().getMandatoryBody(InputStream.class);
            }
        }
        final StopWatch watch = new StopWatch();
        boolean answer;
        log.debug("About to store file: {} using stream: {}", targetName, is);
        if (endpoint.getFileExist() == GenericFileExist.Append) {
            log.trace("Client appendFile: {}", targetName);
            answer = client.appendFile(targetName, is);
        } else {
            log.trace("Client storeFile: {}", targetName);
            answer = client.storeFile(targetName, is);
        }
        watch.stop();
        if (log.isDebugEnabled()) {
            log.debug("Took {} ({} millis) to store file: {} and FTP client returned: {}", new Object[] { TimeUtils.printDuration(watch.taken()), watch.taken(), targetName, answer });
        }
        // store client reply information after the operation
        exchange.getIn().setHeader(FtpConstants.FTP_REPLY_CODE, client.getReplyCode());
        exchange.getIn().setHeader(FtpConstants.FTP_REPLY_STRING, client.getReplyString());
        // after storing file, we may set chmod on the file
        String chmod = ((FtpConfiguration) endpoint.getConfiguration()).getChmod();
        if (ObjectHelper.isNotEmpty(chmod)) {
            log.debug("Setting chmod: {} on file: {}", chmod, targetName);
            String command = "chmod " + chmod + " " + targetName;
            log.trace("Client sendSiteCommand: {}", command);
            boolean success = client.sendSiteCommand(command);
            log.trace("Client sendSiteCommand successful: {}", success);
        }
        return answer;
    } catch (IOException e) {
        throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
    } catch (InvalidPayloadException e) {
        throw new GenericFileOperationFailedException("Cannot store file: " + name, e);
    } finally {
        IOHelper.close(is, "store: " + name, log);
    }
}
Also used : GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) InvalidPayloadException(org.apache.camel.InvalidPayloadException) StopWatch(org.apache.camel.util.StopWatch)

Example 13 with ByteArrayInputStream

use of java.io.ByteArrayInputStream in project camel by apache.

the class StreamCacheConverterTest method testConvertToStreamCache.

public void testConvertToStreamCache() throws Exception {
    context.start();
    ByteArrayInputStream inputStream = new ByteArrayInputStream(MESSAGE.getBytes());
    StreamCache streamCache = StreamCacheConverter.convertToStreamCache(new SAXSource(new InputSource(inputStream)), exchange);
    String message = exchange.getContext().getTypeConverter().convertTo(String.class, streamCache);
    assertNotNull(message);
    assertEquals("The converted message is wrong", MESSAGE, message);
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamCache(org.apache.camel.StreamCache)

Example 14 with ByteArrayInputStream

use of java.io.ByteArrayInputStream in project camel by apache.

the class CacheInputStreamInDeadLetterIssue520Test method testSendingInputStream.

public void testSendingInputStream() throws Exception {
    InputStream message = new ByteArrayInputStream("<hello>Willem</hello>".getBytes());
    sendingMessage(message);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 15 with ByteArrayInputStream

use of java.io.ByteArrayInputStream in project camel by apache.

the class RecordableInputStreamTest method testReadAndGetTextsAutoStopRecord.

public void testReadAndGetTextsAutoStopRecord() throws Exception {
    RecordableInputStream ris = new RecordableInputStream(new ByteArrayInputStream(DATA), "utf-8");
    assertEquals(0, ris.size());
    byte[] buf = new byte[64];
    // read 64 bytes
    int n = ris.read(buf, 0, buf.length);
    assertEquals(64, n);
    assertEquals(64, ris.size());
    // consume the 64 bytes
    String text = ris.getText(64);
    assertEquals(new String(DATA, 0, 64, "utf-8"), text);
    assertEquals(0, ris.size());
    // read the next 64 bytes
    n = ris.read(buf, 0, buf.length);
    assertEquals(64, n);
    assertEquals(0, ris.size());
    // turn back on the recording and read the next 64 bytes
    ris.record();
    n = ris.read(buf, 0, buf.length);
    assertEquals(64, n);
    assertEquals(64, ris.size());
    // consume the 64 bytes
    text = ris.getText(64);
    // 64 * 2 = 128
    assertEquals(new String(DATA, 128, 64, "utf-8"), text);
    assertEquals(0, ris.size());
    ris.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)6879 Test (org.junit.Test)2274 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1791 InputStream (java.io.InputStream)1531 IOException (java.io.IOException)1400 DataInputStream (java.io.DataInputStream)600 ObjectInputStream (java.io.ObjectInputStream)597 X509Certificate (java.security.cert.X509Certificate)397 CertificateFactory (java.security.cert.CertificateFactory)355 ObjectOutputStream (java.io.ObjectOutputStream)333 File (java.io.File)279 ArrayList (java.util.ArrayList)270 Certificate (java.security.cert.Certificate)234 HashMap (java.util.HashMap)212 DataOutputStream (java.io.DataOutputStream)200 FileInputStream (java.io.FileInputStream)182 InputStreamReader (java.io.InputStreamReader)180 Test (org.testng.annotations.Test)171 Document (org.w3c.dom.Document)143 Map (java.util.Map)138