Search in sources :

Example 1 with ValidationException

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

the class BeanWithExceptionTest method testInvalidMessage.

public void testInvalidMessage() throws Exception {
    validEndpoint.expectedMessageCount(0);
    invalidEndpoint.expectedMessageCount(1);
    Exchange exchange = template.send("direct:start", new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody("<invalid/>");
            exchange.getIn().setHeader("foo", "notMatchedHeaderValue");
            exchange.setProperty("cheese", "old");
        }
    });
    assertNotNull(exchange.getException());
    ValidationException exception = assertIsInstanceOf(ValidationException.class, exchange.getException());
    assertEquals("Invalid header foo: notMatchedHeaderValue", exception.getMessage());
    assertMockEndpointsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) ValidationException(org.apache.camel.ValidationException) ValidationException(org.apache.camel.ValidationException)

Example 2 with ValidationException

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

the class DefaultExceptionPolicyStrategyTest method testClosetMatch1.

public void testClosetMatch1() {
    setupPolicies();
    OnExceptionDefinition result = strategy.getExceptionPolicy(policies, null, new ValidationException(null, ""));
    assertEquals(type1, result);
    result = strategy.getExceptionPolicy(policies, null, new ExchangeTimedOutException(null, 0));
    assertEquals(type1, result);
}
Also used : ValidationException(org.apache.camel.ValidationException) OnExceptionDefinition(org.apache.camel.model.OnExceptionDefinition) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException)

Example 3 with ValidationException

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

the class ContractAdvice method doValidate.

private void doValidate(Message message, DataType type) throws ValidationException {
    Validator validator = message.getExchange().getContext().resolveValidator(type);
    if (validator != null) {
        LOG.debug("Applying validator: type='{}', validator='{}'", type, validator);
        validator.validate(message, type);
    } else {
        throw new ValidationException(message.getExchange(), String.format("No Validator found for '%s'", type));
    }
}
Also used : ValidationException(org.apache.camel.ValidationException) Validator(org.apache.camel.spi.Validator)

Example 4 with ValidationException

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

the class ProcessorValidator method validate.

/**
     * Perform content validation with specified type using Processor.
     *
     * @param message message to apply validation
     * @param type 'from' data type
     */
@Override
public void validate(Message message, DataType type) throws ValidationException {
    Exchange exchange = message.getExchange();
    LOG.debug("Sending to validate processor '{}'", processor);
    DefaultExchange validateExchange = new DefaultExchange(exchange);
    validateExchange.setIn(message);
    validateExchange.setProperties(exchange.getProperties());
    try {
        processor.process(validateExchange);
    } catch (Exception e) {
        if (e instanceof ValidationException) {
            throw (ValidationException) e;
        } else {
            throw new ValidationException(String.format("Validation failed for '%s'", type), validateExchange, e);
        }
    }
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) ValidationException(org.apache.camel.ValidationException) ValidationException(org.apache.camel.ValidationException)

Aggregations

ValidationException (org.apache.camel.ValidationException)4 Exchange (org.apache.camel.Exchange)2 ExchangeTimedOutException (org.apache.camel.ExchangeTimedOutException)1 Processor (org.apache.camel.Processor)1 DefaultExchange (org.apache.camel.impl.DefaultExchange)1 OnExceptionDefinition (org.apache.camel.model.OnExceptionDefinition)1 Validator (org.apache.camel.spi.Validator)1