Search in sources :

Example 91 with RuntimeCamelException

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;
}
Also used : Exchange(org.apache.camel.Exchange) Message(org.apache.camel.Message) InputStream(java.io.InputStream) Iterator(java.util.Iterator) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

Example 92 with RuntimeCamelException

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);
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

Example 93 with RuntimeCamelException

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);
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

Example 94 with RuntimeCamelException

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);
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

Example 95 with RuntimeCamelException

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);
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException)

Aggregations

RuntimeCamelException (org.apache.camel.RuntimeCamelException)196 HashMap (java.util.HashMap)52 CamelContextAware (org.apache.camel.CamelContextAware)45 DataFormatFactory (org.apache.camel.spi.DataFormatFactory)45 ConditionalOnBean (org.springframework.boot.autoconfigure.condition.ConditionalOnBean)45 ConditionalOnClass (org.springframework.boot.autoconfigure.condition.ConditionalOnClass)45 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)45 Bean (org.springframework.context.annotation.Bean)45 IOException (java.io.IOException)36 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)32 Test (org.junit.Test)16 ArrayList (java.util.ArrayList)11 Exchange (org.apache.camel.Exchange)11 InputStream (java.io.InputStream)9 GeneralSecurityException (java.security.GeneralSecurityException)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 TimeoutException (java.util.concurrent.TimeoutException)7 QName (javax.xml.namespace.QName)7 Message (org.apache.camel.Message)7 Method (java.lang.reflect.Method)6