private void populateRoutingInfoHeaders(final Message message, final Envelope envelope) {
   if (envelope != null) {
     message.setHeader(RabbitMQConstants.ROUTING_KEY, envelope.getRoutingKey());
     message.setHeader(RabbitMQConstants.EXCHANGE_NAME, envelope.getExchange());
     message.setHeader(RabbitMQConstants.DELIVERY_TAG, envelope.getDeliveryTag());
   }
 }
 public MessageProperties toMessageProperties(
     final BasicProperties source, final Envelope envelope, final String charset) {
   MessageProperties target = new MessageProperties();
   Map<String, Object> headers = source.getHeaders();
   if (!CollectionUtils.isEmpty(headers)) {
     for (Map.Entry<String, Object> entry : headers.entrySet()) {
       String key = entry.getKey();
       if (MessageProperties.X_DELAY.equals(key)) {
         Object value = entry.getValue();
         if (value instanceof Integer) {
           target.setReceivedDelay((Integer) value);
         }
       } else {
         target.setHeader(key, convertLongStringIfNecessary(entry.getValue(), charset));
       }
     }
   }
   target.setTimestamp(source.getTimestamp());
   target.setMessageId(source.getMessageId());
   target.setReceivedUserId(source.getUserId());
   target.setAppId(source.getAppId());
   target.setClusterId(source.getClusterId());
   target.setType(source.getType());
   Integer deliveryMode = source.getDeliveryMode();
   if (deliveryMode != null) {
     target.setReceivedDeliveryMode(MessageDeliveryMode.fromInt(deliveryMode));
   }
   target.setDeliveryMode(null);
   target.setExpiration(source.getExpiration());
   target.setPriority(source.getPriority());
   target.setContentType(source.getContentType());
   target.setContentEncoding(source.getContentEncoding());
   String correlationId = source.getCorrelationId();
   if (StringUtils.hasText(correlationId)) {
     target.setCorrelationId(source.getCorrelationId());
   }
   String replyTo = source.getReplyTo();
   if (replyTo != null) {
     target.setReplyTo(replyTo);
   }
   if (envelope != null) {
     target.setReceivedExchange(envelope.getExchange());
     target.setReceivedRoutingKey(envelope.getRoutingKey());
     target.setRedelivered(envelope.isRedeliver());
     target.setDeliveryTag(envelope.getDeliveryTag());
   }
   return target;
 }
Esempio n. 3
0
  public void setRabbitExchange(
      Exchange camelExchange, Envelope envelope, AMQP.BasicProperties properties, byte[] body) {
    Message message;
    if (camelExchange.getIn() != null) {
      // Use the existing message so we keep the headers
      message = camelExchange.getIn();
    } else {
      message = new DefaultMessage();
      camelExchange.setIn(message);
    }

    if (envelope != null) {
      message.setHeader(RabbitMQConstants.ROUTING_KEY, envelope.getRoutingKey());
      message.setHeader(RabbitMQConstants.EXCHANGE_NAME, envelope.getExchange());
      message.setHeader(RabbitMQConstants.DELIVERY_TAG, envelope.getDeliveryTag());
    }

    Map<String, Object> headers = properties.getHeaders();
    if (headers != null) {
      for (Map.Entry<String, Object> entry : headers.entrySet()) {
        // Convert LongStrings to String.
        if (entry.getValue() instanceof LongString) {
          message.setHeader(entry.getKey(), entry.getValue().toString());
        } else {
          message.setHeader(entry.getKey(), entry.getValue());
        }
      }
    }

    if (hasSerializeHeader(properties)) {
      Object messageBody = null;
      try (InputStream b = new ByteArrayInputStream(body);
          ObjectInputStream o = new ObjectInputStream(b); ) {
        messageBody = o.readObject();
      } catch (IOException | ClassNotFoundException e) {
        LOG.warn("Could not deserialize the object");
      }
      if (messageBody instanceof Throwable) {
        LOG.debug("Reply was an Exception. Setting the Exception on the Exchange");
        camelExchange.setException((Throwable) messageBody);
      } else {
        message.setBody(messageBody);
      }
    } else {
      // Set the body as a byte[] and let the type converter deal with it
      message.setBody(body);
    }
  }