public BasicProperties fromMessageProperties( final MessageProperties source, final String charset) { BasicProperties.Builder target = new BasicProperties.Builder(); target .headers(this.convertHeadersIfNecessary(source.getHeaders())) .timestamp(source.getTimestamp()) .messageId(source.getMessageId()) .userId(source.getUserId()) .appId(source.getAppId()) .clusterId(source.getClusterId()) .type(source.getType()); MessageDeliveryMode deliveryMode = source.getDeliveryMode(); if (deliveryMode != null) { target.deliveryMode(MessageDeliveryMode.toInt(deliveryMode)); } target .expiration(source.getExpiration()) .priority(source.getPriority()) .contentType(source.getContentType()) .contentEncoding(source.getContentEncoding()); String correlationId = source.getCorrelationId(); if (StringUtils.hasText(correlationId)) { target.correlationId(correlationId); } String replyTo = source.getReplyTo(); if (replyTo != null) { target.replyTo(replyTo); } return target.build(); }
@Override public Object fromMessage(Message message) throws MessageConversionException { Object content = null; MessageProperties properties = message.getMessageProperties(); if (properties != null) { String contentType = properties.getContentType(); if (contentType != null && contentType.contains("protobuf")) { try { String classId = (String) message.getMessageProperties().getHeaders().get("__TypeId__"); if (classId == null) throw new Exception("no classId found"); Class<?> targetClass = Class.forName(classId); content = converter.deserialize(message.getBody(), targetClass); } catch (Exception e) { throw new MessageConversionException("Failed to convert json-based Message content", e); } } } if (content == null) { content = message.getBody(); } return content; }
@Override public Object fromMessage(Message message) throws MessageConversionException { MessageProperties messageProperties = message.getMessageProperties(); if (messageProperties == null) throw new MessageConversionException("Cannot decode a message with no properties!"); byte[] body = message.getBody(); if (body == null) return null; String messageEncoding = messageProperties.getContentEncoding(); if (messageEncoding == null) messageEncoding = this.encoding; String messageContentType = messageProperties.getContentType(); if (this.contentType != null && !this.contentType.equalsIgnoreCase(messageContentType)) throw new MessageConversionException( "Cannot understand a message of type " + messageContentType); try { return new String(body, messageEncoding); } catch (UnsupportedEncodingException ex) { LOG.error("Cannot dencode strings as {}", this.encoding, ex); throw new MessageConversionException("Cannot dencode strings as " + this.encoding, ex); } }
/** Extract "standard" headers from an AMQP MessageProperties instance. */ @Override protected Map<String, Object> extractStandardHeaders(MessageProperties amqpMessageProperties) { Map<String, Object> headers = new HashMap<String, Object>(); try { String appId = amqpMessageProperties.getAppId(); if (StringUtils.hasText(appId)) { headers.put(AmqpHeaders.APP_ID, appId); } String clusterId = amqpMessageProperties.getClusterId(); if (StringUtils.hasText(clusterId)) { headers.put(AmqpHeaders.CLUSTER_ID, clusterId); } String contentEncoding = amqpMessageProperties.getContentEncoding(); if (StringUtils.hasText(contentEncoding)) { headers.put(AmqpHeaders.CONTENT_ENCODING, contentEncoding); } long contentLength = amqpMessageProperties.getContentLength(); if (contentLength > 0) { headers.put(AmqpHeaders.CONTENT_LENGTH, contentLength); } String contentType = amqpMessageProperties.getContentType(); if (StringUtils.hasText(contentType)) { headers.put(AmqpHeaders.CONTENT_TYPE, contentType); } byte[] correlationId = amqpMessageProperties.getCorrelationId(); if (correlationId != null && correlationId.length > 0) { headers.put(AmqpHeaders.CORRELATION_ID, correlationId); } MessageDeliveryMode deliveryMode = amqpMessageProperties.getDeliveryMode(); if (deliveryMode != null) { headers.put(AmqpHeaders.DELIVERY_MODE, deliveryMode); } long deliveryTag = amqpMessageProperties.getDeliveryTag(); if (deliveryTag > 0) { headers.put(AmqpHeaders.DELIVERY_TAG, deliveryTag); } String expiration = amqpMessageProperties.getExpiration(); if (StringUtils.hasText(expiration)) { headers.put(AmqpHeaders.EXPIRATION, expiration); } Integer messageCount = amqpMessageProperties.getMessageCount(); if (messageCount != null && messageCount > 0) { headers.put(AmqpHeaders.MESSAGE_COUNT, messageCount); } String messageId = amqpMessageProperties.getMessageId(); if (StringUtils.hasText(messageId)) { headers.put(AmqpHeaders.MESSAGE_ID, messageId); } Integer priority = amqpMessageProperties.getPriority(); if (priority != null && priority > 0) { headers.put(MessageHeaders.PRIORITY, priority); } String receivedExchange = amqpMessageProperties.getReceivedExchange(); if (StringUtils.hasText(receivedExchange)) { headers.put(AmqpHeaders.RECEIVED_EXCHANGE, receivedExchange); } String receivedRoutingKey = amqpMessageProperties.getReceivedRoutingKey(); if (StringUtils.hasText(receivedRoutingKey)) { headers.put(AmqpHeaders.RECEIVED_ROUTING_KEY, receivedRoutingKey); } Boolean redelivered = amqpMessageProperties.isRedelivered(); if (redelivered != null) { headers.put(AmqpHeaders.REDELIVERED, redelivered); } String replyTo = amqpMessageProperties.getReplyTo(); if (replyTo != null) { headers.put(AmqpHeaders.REPLY_TO, replyTo); } Date timestamp = amqpMessageProperties.getTimestamp(); if (timestamp != null) { headers.put(AmqpHeaders.TIMESTAMP, timestamp); } String type = amqpMessageProperties.getType(); if (StringUtils.hasText(type)) { headers.put(AmqpHeaders.TYPE, type); } String userId = amqpMessageProperties.getUserId(); if (StringUtils.hasText(userId)) { headers.put(AmqpHeaders.USER_ID, userId); } Object replyCorrelation = amqpMessageProperties.getHeaders().get(AmqpHeaders.STACKED_CORRELATION_HEADER); if (replyCorrelation instanceof String) { if (StringUtils.hasText((String) replyCorrelation)) { headers.put(AmqpHeaders.SPRING_REPLY_CORRELATION, replyCorrelation); } } Object replyToStack = amqpMessageProperties.getHeaders().get(AmqpHeaders.STACKED_REPLY_TO_HEADER); if (replyToStack instanceof String) { if (StringUtils.hasText((String) replyToStack)) { headers.put(AmqpHeaders.SPRING_REPLY_TO_STACK, replyToStack); } } } catch (Exception e) { if (logger.isWarnEnabled()) { logger.warn("error occurred while mapping from AMQP properties to MessageHeaders", e); } } return headers; }