Search in sources :

Example 1 with InvalidPayloadException

use of org.apache.camel.InvalidPayloadException in project camel by apache.

the class IgniteCacheProducer method doQuery.

@SuppressWarnings("unchecked")
private void doQuery(Message in, Message out, Exchange exchange) {
    Query<Object> query = in.getHeader(IgniteConstants.IGNITE_CACHE_QUERY, Query.class);
    if (query == null) {
        try {
            query = in.getMandatoryBody(Query.class);
        } catch (InvalidPayloadException e) {
            exchange.setException(e);
            return;
        }
    }
    final QueryCursor<Object> cursor = cache.query(query);
    out.setBody(cursor.iterator());
    exchange.addOnCompletion(new Synchronization() {

        @Override
        public void onFailure(Exchange exchange) {
            cursor.close();
        }

        @Override
        public void onComplete(Exchange exchange) {
            cursor.close();
        }
    });
}
Also used : Exchange(org.apache.camel.Exchange) Query(org.apache.ignite.cache.query.Query) InvalidPayloadException(org.apache.camel.InvalidPayloadException) Synchronization(org.apache.camel.spi.Synchronization)

Example 2 with InvalidPayloadException

use of org.apache.camel.InvalidPayloadException in project camel by apache.

the class ScpOperations method storeFile.

@Override
public boolean storeFile(String name, Exchange exchange) throws GenericFileOperationFailedException {
    ObjectHelper.notNull(session, "session");
    ScpConfiguration cfg = endpoint.getConfiguration();
    int timeout = cfg.getConnectTimeout();
    if (LOG.isTraceEnabled()) {
        LOG.trace("Opening channel to {} with {} timeout...", cfg.remoteServerInformation(), timeout > 0 ? (Integer.toString(timeout) + " ms") : "no");
    }
    String file = getRemoteFile(name, cfg);
    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 {
        channel = (ChannelExec) session.openChannel("exec");
        channel.setCommand(getScpCommand(cfg, file));
        channel.connect(timeout);
        LOG.trace("Channel connected to {}", cfg.remoteServerInformation());
        try {
            if (is == null) {
                is = exchange.getIn().getMandatoryBody(InputStream.class);
            }
            write(channel, file, is, cfg);
        } catch (InvalidPayloadException e) {
            throw new GenericFileOperationFailedException("Cannot store file: " + name, e);
        } catch (IOException e) {
            throw new GenericFileOperationFailedException("Failed to write file " + file, e);
        } finally {
            // must close stream after usage
            IOHelper.close(is);
        }
    } catch (JSchException e) {
        throw new GenericFileOperationFailedException("Failed to write file " + file, e);
    } finally {
        if (channel != null) {
            LOG.trace("Disconnecting 'exec' scp channel");
            channel.disconnect();
            channel = null;
            LOG.trace("Channel disconnected from {}", cfg.remoteServerInformation());
        }
    }
    return true;
}
Also used : JSchException(com.jcraft.jsch.JSchException) ByteArrayInputStream(java.io.ByteArrayInputStream) GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) InvalidPayloadException(org.apache.camel.InvalidPayloadException) GenericFileEndpoint(org.apache.camel.component.file.GenericFileEndpoint)

Example 3 with InvalidPayloadException

use of org.apache.camel.InvalidPayloadException 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 4 with InvalidPayloadException

use of org.apache.camel.InvalidPayloadException 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 5 with InvalidPayloadException

use of org.apache.camel.InvalidPayloadException in project camel by apache.

the class FreemarkerSetHeaderTest method assertRespondsWith.

protected void assertRespondsWith(final String value, String expectedBody) throws InvalidPayloadException, InterruptedException {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    //mock.expectedHeaderReceived("fruit", value);
    mock.expectedBodiesReceived(expectedBody);
    template.request("direct:start", new Processor() {

        public void process(Exchange exchange) throws Exception {
            Message in = exchange.getIn();
            in.setBody(value);
        }
    });
    mock.assertIsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) InvalidPayloadException(org.apache.camel.InvalidPayloadException)

Aggregations

InvalidPayloadException (org.apache.camel.InvalidPayloadException)26 InputStream (java.io.InputStream)10 Exchange (org.apache.camel.Exchange)9 Message (org.apache.camel.Message)9 Processor (org.apache.camel.Processor)6 IOException (java.io.IOException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 Endpoint (org.apache.camel.Endpoint)3 GenericFileOperationFailedException (org.apache.camel.component.file.GenericFileOperationFailedException)3 Entry (java.util.Map.Entry)2 ServletOutputStream (javax.servlet.ServletOutputStream)2 JAXBException (javax.xml.bind.JAXBException)2 GenericFileEndpoint (org.apache.camel.component.file.GenericFileEndpoint)2 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 StopWatch (org.apache.camel.util.StopWatch)2 Builder (com.google.protobuf.Message.Builder)1 JSchException (com.jcraft.jsch.JSchException)1 SftpException (com.jcraft.jsch.SftpException)1