use of org.apache.camel.CamelExchangeException in project camel by apache.
the class JcloudsComputeProducer method runScriptOnNode.
/**
* Runs a script on the target node.
*/
protected void runScriptOnNode(Exchange exchange) throws CamelException {
String script = exchange.getIn().getBody(String.class);
String nodeId = getNodeId(exchange);
String user = getUser(exchange);
LoginCredentials credentials = null;
if (ObjectHelper.isNotEmpty(user)) {
credentials = LoginCredentials.builder().user(user).build();
}
ExecResponse execResponse = null;
if (credentials == null) {
execResponse = computeService.runScriptOnNode(nodeId, script);
} else {
execResponse = computeService.runScriptOnNode(nodeId, script, RunScriptOptions.Builder.overrideLoginCredentials(credentials).runAsRoot(false));
}
if (execResponse == null) {
throw new CamelExchangeException("Failed to receive response for run script operation on node: " + nodeId + " using script: " + script, exchange);
}
exchange.setProperty(JcloudsConstants.RUN_SCRIPT_ERROR, execResponse.getError());
exchange.setProperty(JcloudsConstants.RUN_SCRIPT_EXIT_CODE, execResponse.getExitStatus());
exchange.getOut().setBody(execResponse.getOutput());
}
use of org.apache.camel.CamelExchangeException in project camel by apache.
the class JcloudsComputeProducer method createNode.
/**
* Create a node with the specified group.
*/
protected void createNode(Exchange exchange) throws CamelException {
String group = getGroup(exchange);
String imageId = getImageId(exchange);
String locationId = getLocationId(exchange);
String hardwareId = getHardwareId(exchange);
if (ObjectHelper.isEmpty(group)) {
throw new CamelExchangeException("Group must be specific in the URI or as exchange property for the destroy node operation.", exchange);
}
TemplateBuilder builder = computeService.templateBuilder();
builder.any();
if (ObjectHelper.isNotEmpty(locationId)) {
builder.locationId(locationId);
}
if (ObjectHelper.isNotEmpty(imageId)) {
builder.imageId(imageId);
}
if (ObjectHelper.isNotEmpty(hardwareId)) {
builder.hardwareId(hardwareId);
}
try {
Set<? extends NodeMetadata> nodeMetadatas = computeService.createNodesInGroup(group, 1, builder.build());
exchange.getOut().setBody(nodeMetadatas);
exchange.getOut().setHeaders(exchange.getIn().getHeaders());
} catch (RunNodesException e) {
throw new CamelExchangeException("Error creating jclouds node.", exchange, e);
}
}
use of org.apache.camel.CamelExchangeException in project camel by apache.
the class JettyHttpProducerConnectionFailureTest method testHttpGetWithParamsViaURI.
@Test
public void testHttpGetWithParamsViaURI() throws Exception {
// these tests does not run well on Windows
if (isPlatform("windows")) {
return;
}
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(0);
// give Jetty time to startup properly
Thread.sleep(1000);
// use another port with no connection
try {
template.requestBody("jetty://http://localhost:9999/myservice", null, Object.class);
fail("Should have thrown an exception");
} catch (Exception e) {
CamelExchangeException cause = assertIsInstanceOf(CamelExchangeException.class, e.getCause());
assertIsInstanceOf(IOException.class, cause.getCause());
}
assertMockEndpointsSatisfied();
}
use of org.apache.camel.CamelExchangeException in project camel by apache.
the class KestrelProducer method process.
public void process(Exchange exchange) throws Exception {
String msg = exchange.getIn().getBody(String.class);
String queue = endpoint.getQueue();
if (msg != null) {
try {
log.debug("Sending to: {} message: {}", queue, msg);
memcachedClient.set(queue, 0, msg);
} catch (Exception e) {
throw new CamelExchangeException("Error sending to: " + queue, exchange, e);
}
} else {
log.debug("No message body to send to: " + queue);
}
}
use of org.apache.camel.CamelExchangeException in project camel by apache.
the class MinaProducer method doProcess.
protected void doProcess(Exchange exchange) throws Exception {
if (session == null && !lazySessionCreation) {
throw new IllegalStateException("Not started yet!");
}
if (session == null || !session.isConnected()) {
openConnection();
}
// set the exchange encoding property
if (getEndpoint().getConfiguration().getCharsetName() != null) {
exchange.setProperty(Exchange.CHARSET_NAME, IOHelper.normalizeCharset(getEndpoint().getConfiguration().getCharsetName()));
}
Object body = MinaPayloadHelper.getIn(getEndpoint(), exchange);
if (body == null) {
noReplyLogger.log("No payload to send for exchange: " + exchange);
// exit early since nothing to write
return;
}
// if textline enabled then covert to a String which must be used for textline
if (getEndpoint().getConfiguration().isTextline()) {
body = getEndpoint().getCamelContext().getTypeConverter().mandatoryConvertTo(String.class, exchange, body);
}
// if sync is true then we should also wait for a response (synchronous mode)
if (sync) {
// only initialize latch if we should get a response
latch = new CountDownLatch(1);
// reset handler if we expect a response
ResponseHandler handler = (ResponseHandler) session.getHandler();
handler.reset();
}
// log what we are writing
if (LOG.isDebugEnabled()) {
Object out = body;
if (body instanceof byte[]) {
// byte arrays is not readable so convert to string
out = exchange.getContext().getTypeConverter().convertTo(String.class, body);
}
LOG.debug("Writing body : {}", out);
}
// write the body
MinaHelper.writeBody(session, body, exchange);
if (sync) {
// wait for response, consider timeout
LOG.debug("Waiting for response using timeout {} millis.", timeout);
boolean done = latch.await(timeout, TimeUnit.MILLISECONDS);
if (!done) {
throw new ExchangeTimedOutException(exchange, timeout);
}
// did we get a response
ResponseHandler handler = (ResponseHandler) session.getHandler();
if (handler.getCause() != null) {
throw new CamelExchangeException("Error occurred in ResponseHandler", exchange, handler.getCause());
} else if (!handler.isMessageReceived()) {
// no message received
throw new CamelExchangeException("No response received from remote server: " + getEndpoint().getEndpointUri(), exchange);
} else {
// set the result on either IN or OUT on the original exchange depending on its pattern
if (ExchangeHelper.isOutCapable(exchange)) {
MinaPayloadHelper.setOut(exchange, handler.getMessage());
} else {
MinaPayloadHelper.setIn(exchange, handler.getMessage());
}
}
}
}
Aggregations