use of org.apache.camel.RuntimeCamelException in project camel by apache.
the class UnmarshalProcessor method process.
public boolean process(Exchange exchange, AsyncCallback callback) {
ObjectHelper.notNull(dataFormat, "dataFormat");
InputStream stream = null;
Object result = null;
try {
stream = exchange.getIn().getMandatoryBody(InputStream.class);
// lets setup the out message before we invoke the dataFormat so that it can mutate it if necessary
Message out = exchange.getOut();
out.copyFrom(exchange.getIn());
result = dataFormat.unmarshal(exchange, stream);
if (result instanceof Exchange) {
if (result != exchange) {
// it's not allowed to return another exchange other than the one provided to dataFormat
throw new RuntimeCamelException("The returned exchange " + result + " is not the same as " + exchange + " provided to the DataFormat");
}
} else if (result instanceof Message) {
// the dataformat has probably set headers, attachments, etc. so let's use it as the outbound payload
exchange.setOut((Message) result);
} else {
out.setBody(result);
}
} catch (Throwable e) {
// remove OUT message, as an exception occurred
exchange.setOut(null);
exchange.setException(e);
} finally {
// The Iterator will close the stream itself
if (!(result instanceof Iterator)) {
IOHelper.close(stream, "input stream");
}
}
callback.done(true);
return true;
}
use of org.apache.camel.RuntimeCamelException in project camel by apache.
the class ExceptionBuilderTest method testNPE.
public void testNPE() throws Exception {
MockEndpoint result = getMockEndpoint(RESULT_QUEUE);
result.expectedMessageCount(0);
MockEndpoint mock = getMockEndpoint(ERROR_QUEUE);
mock.expectedMessageCount(1);
mock.expectedHeaderReceived(MESSAGE_INFO, "Damm a NPE");
try {
template.sendBody("direct:a", "Hello NPE");
fail("Should have thrown a NullPointerException");
} catch (RuntimeCamelException e) {
assertTrue(e.getCause() instanceof NullPointerException);
// expected
}
MockEndpoint.assertIsSatisfied(result, mock);
}
use of org.apache.camel.RuntimeCamelException in project camel by apache.
the class ExceptionBuilderTest method testSecurityConfiguredWithExceptionList.
public void testSecurityConfiguredWithExceptionList() throws Exception {
// test that we also handles a configuration with a list of exceptions
MockEndpoint result = getMockEndpoint(RESULT_QUEUE);
result.expectedMessageCount(0);
MockEndpoint mock = getMockEndpoint(ERROR_QUEUE);
mock.expectedMessageCount(1);
mock.expectedHeaderReceived(MESSAGE_INFO, "Damm some access error");
try {
template.sendBody("direct:a", "I am not allowed to access this");
fail("Should have thrown a GeneralSecurityException");
} catch (RuntimeCamelException e) {
assertTrue(e.getCause() instanceof IllegalAccessException);
// expected
}
MockEndpoint.assertIsSatisfied(result, mock);
}
use of org.apache.camel.RuntimeCamelException in project camel by apache.
the class ExceptionBuilderTest method testMyBusinessException.
public void testMyBusinessException() throws Exception {
MockEndpoint result = getMockEndpoint(RESULT_QUEUE);
result.expectedMessageCount(0);
MockEndpoint mock = getMockEndpoint(BUSINESS_ERROR_QUEUE);
mock.expectedMessageCount(1);
mock.expectedHeaderReceived(MESSAGE_INFO, "Damm my business is not going to well");
try {
template.sendBody("direct:a", "Hello business");
fail("Should have thrown a MyBusinessException");
} catch (RuntimeCamelException e) {
assertTrue(e.getCause() instanceof MyBusinessException);
// expected
}
MockEndpoint.assertIsSatisfied(result, mock);
}
use of org.apache.camel.RuntimeCamelException in project camel by apache.
the class ExceptionBuilderWithHandledExceptionTest method testUnhandledException.
public void testUnhandledException() throws Exception {
MockEndpoint result = getMockEndpoint(RESULT_QUEUE);
result.expectedMessageCount(0);
MockEndpoint mock = getMockEndpoint(ERROR_QUEUE);
mock.expectedMessageCount(1);
mock.expectedHeaderReceived(MESSAGE_INFO, "Handled exchange with IOException");
try {
template.sendBodyAndHeader("direct:a", "Hello IOE", "foo", "something that does not match");
fail("Should have thrown a IOException");
} catch (RuntimeCamelException e) {
assertTrue(e.getCause() instanceof IOException);
// expected, failure is not handled because predicate doesn't match
}
MockEndpoint.assertIsSatisfied(result, mock);
}
Aggregations