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();
}
});
}
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;
}
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);
}
}
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);
}
}
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();
}
Aggregations