Search in sources :

Example 1 with InvalidPayloadRuntimeException

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

the class BulkRequestAggregationStrategy method aggregate.

@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
    // Don't use getBody(Class<T>) here as we don't want to coerce the body type using a type converter.
    Object objBody = newExchange.getIn().getBody();
    if (!(objBody instanceof ActionRequest)) {
        throw new InvalidPayloadRuntimeException(newExchange, ActionRequest.class);
    }
    ActionRequest newBody = (ActionRequest) objBody;
    BulkRequest request;
    if (oldExchange == null) {
        request = new BulkRequest();
        request.add(newBody);
        newExchange.getIn().setBody(request);
        return newExchange;
    } else {
        request = oldExchange.getIn().getBody(BulkRequest.class);
        request.add(newBody);
        return oldExchange;
    }
}
Also used : ActionRequest(org.elasticsearch.action.ActionRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) InvalidPayloadRuntimeException(org.apache.camel.InvalidPayloadRuntimeException)

Example 2 with InvalidPayloadRuntimeException

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

the class VertxProducer method process.

@Override
public boolean process(Exchange exchange, AsyncCallback callback) {
    EventBus eventBus = getEndpoint().getEventBus();
    if (eventBus == null) {
        exchange.setException(new IllegalStateException("EventBus is not started or not configured"));
        callback.done(true);
        return true;
    }
    String address = getEndpoint().getAddress();
    boolean reply = ExchangeHelper.isOutCapable(exchange);
    boolean pubSub = getEndpoint().isPubSub();
    Object body = getVertxBody(exchange);
    if (body != null) {
        if (reply) {
            LOG.debug("Sending to: {} with body: {}", address, body);
            eventBus.send(address, body, new CamelReplyHandler(exchange, callback));
            return false;
        } else {
            if (pubSub) {
                LOG.debug("Publishing to: {} with body: {}", address, body);
                eventBus.publish(address, body);
            } else {
                LOG.debug("Sending to: {} with body: {}", address, body);
                eventBus.send(address, body);
            }
            callback.done(true);
            return true;
        }
    }
    exchange.setException(new InvalidPayloadRuntimeException(exchange, String.class));
    callback.done(true);
    return true;
}
Also used : EventBus(io.vertx.core.eventbus.EventBus) InvalidPayloadRuntimeException(org.apache.camel.InvalidPayloadRuntimeException)

Example 3 with InvalidPayloadRuntimeException

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

the class BulkRequestAggregationStrategy method aggregate.

@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
    // Don't use getBody(Class<T>) here as we don't want to coerce the body type using a type converter.
    Object objBody = newExchange.getIn().getBody();
    if (!(objBody instanceof ActionRequest)) {
        throw new InvalidPayloadRuntimeException(newExchange, ActionRequest.class);
    }
    ActionRequest newBody = (ActionRequest) objBody;
    BulkRequest request;
    if (oldExchange == null) {
        request = new BulkRequest();
        request.add(newBody);
        newExchange.getIn().setBody(request);
        return newExchange;
    } else {
        request = oldExchange.getIn().getBody(BulkRequest.class);
        request.add(newBody);
        return oldExchange;
    }
}
Also used : ActionRequest(org.elasticsearch.action.ActionRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) InvalidPayloadRuntimeException(org.apache.camel.InvalidPayloadRuntimeException)

Example 4 with InvalidPayloadRuntimeException

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

the class CMProducer method process.

/**
     * Producer is a exchange processor. This process is built in several steps. 1. Validate message receive from client 2. Send validated message to CM endpoints. 3. Process response from CM
     * endpoints.
     */
@Override
public void process(final Exchange exchange) throws Exception {
    // Immutable message receive from clients. Throws camel ' s
    // InvalidPayloadException
    final SMSMessage smsMessage = exchange.getIn().getMandatoryBody(SMSMessage.class);
    // Validates Payload - SMSMessage
    log.trace("Validating SMSMessage instance provided: {}", smsMessage.toString());
    final Set<ConstraintViolation<SMSMessage>> constraintViolations = getValidator().validate(smsMessage);
    if (constraintViolations.size() > 0) {
        final StringBuffer msg = new StringBuffer();
        for (final ConstraintViolation<SMSMessage> cv : constraintViolations) {
            msg.append(String.format("- Invalid value for %s: %s", cv.getPropertyPath().toString(), cv.getMessage()));
        }
        log.debug(msg.toString());
        throw new InvalidPayloadRuntimeException(exchange, SMSMessage.class);
    }
    log.trace("SMSMessage instance is valid: {}", smsMessage.toString());
    // We have a valid (immutable) SMSMessage instance, lets extend to
    // CMMessage
    // This is the instance we will use to build the XML document to be
    // sent to CM SMS GW.
    final CMMessage cmMessage = new CMMessage(smsMessage.getPhoneNumber(), smsMessage.getMessage());
    log.debug("CMMessage instance build from valid SMSMessage instance");
    if (smsMessage.getFrom() == null || smsMessage.getFrom().isEmpty()) {
        String df = getConfiguration().getDefaultFrom();
        cmMessage.setSender(df);
        log.debug("Dynamic sender is set to default dynamic sender: {}", df);
    }
    // Remember, this can be null.
    cmMessage.setIdAsString(smsMessage.getId());
    // Unicode and multipart
    cmMessage.setUnicodeAndMultipart(getConfiguration().getDefaultMaxNumberOfParts());
    // 2. Send a validated sms message to CM endpoints
    //  for abnormal situations.
    sender.send(cmMessage);
    log.debug("Request accepted by CM Host: {}", cmMessage.toString());
}
Also used : SMSMessage(org.apache.camel.component.cm.client.SMSMessage) ConstraintViolation(javax.validation.ConstraintViolation) InvalidPayloadRuntimeException(org.apache.camel.InvalidPayloadRuntimeException)

Aggregations

InvalidPayloadRuntimeException (org.apache.camel.InvalidPayloadRuntimeException)4 ActionRequest (org.elasticsearch.action.ActionRequest)2 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)2 EventBus (io.vertx.core.eventbus.EventBus)1 ConstraintViolation (javax.validation.ConstraintViolation)1 SMSMessage (org.apache.camel.component.cm.client.SMSMessage)1