use of org.apache.qpid.proton.amqp.messaging.MessageAnnotations in project hono by eclipse.
the class MessageHelper method addAnnotation.
/**
* Adds a value for a symbol to an AMQP 1.0 message's <em>annotations</em>.
*
* @param msg the message to add the symbol to.
* @param key the name of the symbol to add a value for.
* @param value the value to add.
*/
public static void addAnnotation(final Message msg, final String key, final Object value) {
MessageAnnotations annotations = msg.getMessageAnnotations();
if (annotations == null) {
annotations = new MessageAnnotations(new HashMap<>());
msg.setMessageAnnotations(annotations);
}
annotations.getValue().put(Symbol.getSymbol(key), value);
}
use of org.apache.qpid.proton.amqp.messaging.MessageAnnotations in project hono by eclipse.
the class RequestResponseApiConstants method getAmqpReply.
/**
* Creates an AMQP message from a response to a service invocation.
*
* @param endpoint The service endpoint that the operation has been invoked on.
* @param response The response message.
* @return The AMQP message.
* @throws NullPointerException if endpoint is {@code null}.
*/
public static final Message getAmqpReply(final String endpoint, final EventBusMessage response) {
Objects.requireNonNull(endpoint);
Objects.requireNonNull(response);
final Object correlationId = response.getCorrelationId();
if (correlationId == null) {
throw new IllegalArgumentException("response must contain correlation ID");
} else {
final String tenantId = response.getTenant();
final String deviceId = response.getDeviceId();
final Integer status = response.getStatus();
final boolean isApplCorrelationId = response.isAppCorrelationId();
final String cacheDirective = response.getCacheDirective();
final JsonObject payload = response.getJsonPayload();
final ResourceIdentifier address = ResourceIdentifier.from(endpoint, tenantId, deviceId);
final Message message = ProtonHelper.message();
message.setMessageId(UUID.randomUUID().toString());
message.setCorrelationId(correlationId);
message.setAddress(address.toString());
final Map<String, Object> map = new HashMap<>();
map.put(MessageHelper.APP_PROPERTY_TENANT_ID, tenantId);
map.put(MessageHelper.APP_PROPERTY_STATUS, status);
if (deviceId != null) {
map.put(MessageHelper.APP_PROPERTY_DEVICE_ID, deviceId);
}
if (cacheDirective != null) {
map.put(MessageHelper.APP_PROPERTY_CACHE_CONTROL, cacheDirective);
}
message.setApplicationProperties(new ApplicationProperties(map));
if (isApplCorrelationId) {
final Map<Symbol, Object> annotations = new HashMap<>();
annotations.put(Symbol.valueOf(MessageHelper.ANNOTATION_X_OPT_APP_CORRELATION_ID), true);
message.setMessageAnnotations(new MessageAnnotations(annotations));
}
if (payload != null) {
message.setContentType(CONTENT_TYPE_APPLICATION_JSON);
message.setBody(new AmqpValue(payload.encode()));
}
return message;
}
}
use of org.apache.qpid.proton.amqp.messaging.MessageAnnotations in project azure-service-bus-java by Azure.
the class MessageConverter method convertAmqpMessageToBrokeredMessage.
public static Message convertAmqpMessageToBrokeredMessage(org.apache.qpid.proton.message.Message amqpMessage, byte[] deliveryTag) {
Message brokeredMessage;
Section body = amqpMessage.getBody();
if (body != null) {
if (body instanceof Data) {
Binary messageData = ((Data) body).getValue();
brokeredMessage = new Message(messageData.getArray());
} else {
// TODO: handle other types of message body
brokeredMessage = new Message();
}
} else {
brokeredMessage = new Message();
}
// Application properties
ApplicationProperties applicationProperties = amqpMessage.getApplicationProperties();
if (applicationProperties != null) {
brokeredMessage.setProperties(applicationProperties.getValue());
}
// Header
brokeredMessage.setTimeToLive(Duration.ofMillis(amqpMessage.getTtl()));
brokeredMessage.setDeliveryCount(amqpMessage.getDeliveryCount());
// Properties
brokeredMessage.setMessageId(amqpMessage.getMessageId().toString());
brokeredMessage.setContentType(amqpMessage.getContentType());
Object correlationId = amqpMessage.getCorrelationId();
if (correlationId != null) {
brokeredMessage.setCorrelationId(amqpMessage.getCorrelationId().toString());
}
brokeredMessage.setLabel(amqpMessage.getSubject());
brokeredMessage.setTo(amqpMessage.getProperties().getTo());
brokeredMessage.setReplyTo(amqpMessage.getReplyTo());
brokeredMessage.setReplyToSessionId(amqpMessage.getReplyToGroupId());
brokeredMessage.setSessionId(amqpMessage.getGroupId());
// Message Annotations
MessageAnnotations messageAnnotations = amqpMessage.getMessageAnnotations();
if (messageAnnotations != null) {
Map<Symbol, Object> messageAnnotationsMap = messageAnnotations.getValue();
if (messageAnnotationsMap != null) {
for (Map.Entry<Symbol, Object> entry : messageAnnotationsMap.entrySet()) {
String entryName = entry.getKey().toString();
switch(entryName) {
case ClientConstants.ENQUEUEDTIMEUTCNAME:
brokeredMessage.setEnqueuedTimeUtc(((Date) entry.getValue()).toInstant());
break;
case ClientConstants.SCHEDULEDENQUEUETIMENAME:
brokeredMessage.setScheduledEnqueuedTimeUtc(((Date) entry.getValue()).toInstant());
break;
case ClientConstants.SEQUENCENUBMERNAME:
brokeredMessage.setSequenceNumber((long) entry.getValue());
break;
case ClientConstants.LOCKEDUNTILNAME:
brokeredMessage.setLockedUntilUtc(((Date) entry.getValue()).toInstant());
break;
case ClientConstants.PARTITIONKEYNAME:
brokeredMessage.setPartitionKey((String) entry.getValue());
break;
case ClientConstants.DEADLETTERSOURCENAME:
brokeredMessage.setDeadLetterSource((String) entry.getValue());
break;
}
}
}
}
if (deliveryTag != null && deliveryTag.length == ClientConstants.LOCKTOKENSIZE) {
UUID lockToken = Util.convertDotNetBytesToUUID(deliveryTag);
brokeredMessage.setLockToken(lockToken);
} else {
brokeredMessage.setLockToken(ClientConstants.ZEROLOCKTOKEN);
}
brokeredMessage.setDeliveryTag(deliveryTag);
return brokeredMessage;
}
use of org.apache.qpid.proton.amqp.messaging.MessageAnnotations in project azure-service-bus-java by Azure.
the class Util method getDataSerializedSize.
// Remove this.. Too many cases, too many types...
public static int getDataSerializedSize(Message amqpMessage) {
if (amqpMessage == null) {
return 0;
}
int payloadSize = getPayloadSize(amqpMessage);
// EventData - accepts only PartitionKey - which is a String & stuffed into MessageAnnotation
MessageAnnotations messageAnnotations = amqpMessage.getMessageAnnotations();
ApplicationProperties applicationProperties = amqpMessage.getApplicationProperties();
int annotationsSize = 0;
int applicationPropertiesSize = 0;
if (messageAnnotations != null) {
annotationsSize += Util.sizeof(messageAnnotations.getValue());
}
if (applicationProperties != null) {
applicationPropertiesSize += Util.sizeof(applicationProperties.getValue());
}
return annotationsSize + applicationPropertiesSize + payloadSize;
}
use of org.apache.qpid.proton.amqp.messaging.MessageAnnotations in project activemq-artemis by apache.
the class AMQPMessageSupportTest method testGetMessageAnnotationWhenMessageHasAnnotationsMap.
// ---------- getMessageAnnotation ----------------------------------------//
@Test
public void testGetMessageAnnotationWhenMessageHasAnnotationsMap() {
Map<Symbol, Object> messageAnnotationsMap = new HashMap<>();
messageAnnotationsMap.put(Symbol.valueOf("x-opt-test"), Boolean.TRUE);
Message message = Proton.message();
message.setMessageAnnotations(new MessageAnnotations(messageAnnotationsMap));
assertNotNull(AMQPMessageSupport.getMessageAnnotation("x-opt-test", message));
}
Aggregations