use of com.rabbitmq.stream.Properties in project rabbitmq-stream-java-client by rabbitmq.
the class QpidProtonCodec method encode.
@Override
public EncodedMessage encode(Message message) {
org.apache.qpid.proton.message.Message qpidMessage;
if (message instanceof QpidProtonAmqpMessageWrapper) {
qpidMessage = ((QpidProtonAmqpMessageWrapper) message).message;
} else {
qpidMessage = org.apache.qpid.proton.message.Message.Factory.create();
if (message.getProperties() != null) {
Properties headers = message.getProperties();
org.apache.qpid.proton.amqp.messaging.Properties properties = new org.apache.qpid.proton.amqp.messaging.Properties();
boolean propertiesSet = false;
if (headers.getMessageId() != null) {
if (headers.getMessageId() instanceof String) {
properties.setMessageId(headers.getMessageIdAsString());
} else if (headers.getMessageId().getClass().isPrimitive() || headers.getMessageId() instanceof Long) {
properties.setMessageId(new UnsignedLong(headers.getMessageIdAsLong()));
} else if (headers.getMessageId().getClass().isArray()) {
properties.setMessageId(new Binary(headers.getMessageIdAsBinary()));
} else if (headers.getMessageId() instanceof UUID) {
properties.setMessageId(headers.getMessageIdAsUuid());
} else {
throw new IllegalStateException("Type not supported for message ID:" + properties.getMessageId().getClass());
}
propertiesSet = true;
}
if (headers.getUserId() != null) {
properties.setUserId(new Binary(headers.getUserId()));
propertiesSet = true;
}
if (headers.getTo() != null) {
properties.setTo(headers.getTo());
propertiesSet = true;
}
if (headers.getSubject() != null) {
properties.setSubject(headers.getSubject());
propertiesSet = true;
}
if (headers.getReplyTo() != null) {
properties.setReplyTo(headers.getReplyTo());
propertiesSet = true;
}
if (headers.getCorrelationId() != null) {
if (headers.getCorrelationId() instanceof String) {
properties.setCorrelationId(headers.getCorrelationIdAsString());
} else if (headers.getCorrelationId().getClass().isPrimitive() || headers.getCorrelationId() instanceof Long) {
properties.setCorrelationId(new UnsignedLong(headers.getCorrelationIdAsLong()));
} else if (headers.getCorrelationId().getClass().isArray()) {
properties.setCorrelationId(new Binary(headers.getCorrelationIdAsBinary()));
} else if (headers.getCorrelationId() instanceof UUID) {
properties.setCorrelationId(headers.getCorrelationIdAsUuid());
} else {
throw new IllegalStateException("Type not supported for correlation ID:" + properties.getCorrelationId().getClass());
}
propertiesSet = true;
}
if (headers.getContentType() != null) {
properties.setContentType(Symbol.getSymbol(headers.getContentType()));
propertiesSet = true;
}
if (headers.getContentEncoding() != null) {
properties.setContentEncoding(Symbol.getSymbol(headers.getContentEncoding()));
propertiesSet = true;
}
if (headers.getAbsoluteExpiryTime() > 0) {
properties.setAbsoluteExpiryTime(new Date(headers.getAbsoluteExpiryTime()));
propertiesSet = true;
}
if (headers.getCreationTime() > 0) {
properties.setCreationTime(new Date(headers.getCreationTime()));
propertiesSet = true;
}
if (headers.getGroupId() != null) {
properties.setGroupId(headers.getGroupId());
propertiesSet = true;
}
if (headers.getGroupSequence() >= 0) {
properties.setGroupSequence(UnsignedInteger.valueOf(headers.getGroupSequence()));
propertiesSet = true;
}
if (headers.getReplyToGroupId() != null) {
properties.setReplyToGroupId(headers.getReplyToGroupId());
propertiesSet = true;
}
if (propertiesSet) {
qpidMessage.setProperties(properties);
}
}
if (message.getApplicationProperties() != null && !message.getApplicationProperties().isEmpty()) {
Map<String, Object> applicationProperties = new LinkedHashMap<>(message.getApplicationProperties().size());
for (Map.Entry<String, Object> entry : message.getApplicationProperties().entrySet()) {
applicationProperties.put(entry.getKey(), convertToQpidType(entry.getValue()));
}
qpidMessage.setApplicationProperties(new ApplicationProperties(applicationProperties));
}
if (message.getMessageAnnotations() != null && !message.getMessageAnnotations().isEmpty()) {
Map<Symbol, Object> messageAnnotations = new LinkedHashMap<>(message.getMessageAnnotations().size());
for (Map.Entry<String, Object> entry : message.getMessageAnnotations().entrySet()) {
messageAnnotations.put(Symbol.getSymbol(entry.getKey()), convertToQpidType(entry.getValue()));
}
qpidMessage.setMessageAnnotations(new MessageAnnotations(messageAnnotations));
}
if (message.getBodyAsBinary() != null) {
qpidMessage.setBody(new Data(new Binary(message.getBodyAsBinary())));
}
}
int bufferSize;
if (qpidMessage.getBody() instanceof Data) {
bufferSize = ((Data) qpidMessage.getBody()).getValue().getLength() * 2;
} else {
bufferSize = 8192;
}
ByteArrayWritableBuffer writableBuffer = new ByteArrayWritableBuffer(bufferSize);
qpidMessage.encode(writableBuffer);
return new EncodedMessage(writableBuffer.getArrayLength(), writableBuffer.getArray());
}
use of com.rabbitmq.stream.Properties in project spring-amqp by spring-projects.
the class DefaultStreamMessageConverter method toMessageProperties.
private void toMessageProperties(com.rabbitmq.stream.Message streamMessage, StreamMessageProperties mProps) {
Properties properties = streamMessage.getProperties();
if (properties != null) {
JavaUtils.INSTANCE.acceptIfNotNull(properties.getMessageIdAsString(), mProps::setMessageId).acceptIfNotNull(properties.getUserId(), usr -> mProps.setUserId(new String(usr, this.charset))).acceptIfNotNull(properties.getTo(), mProps::setTo).acceptIfNotNull(properties.getSubject(), mProps::setSubject).acceptIfNotNull(properties.getReplyTo(), mProps::setReplyTo).acceptIfNotNull(properties.getCorrelationIdAsString(), mProps::setCorrelationId).acceptIfNotNull(properties.getContentType(), mProps::setContentType).acceptIfNotNull(properties.getContentEncoding(), mProps::setContentEncoding).acceptIfNotNull(properties.getAbsoluteExpiryTime(), exp -> mProps.setExpiration(Long.toString(exp))).acceptIfNotNull(properties.getCreationTime(), mProps::setCreationTime).acceptIfNotNull(properties.getGroupId(), mProps::setGroupId).acceptIfNotNull(properties.getGroupSequence(), mProps::setGroupSequence).acceptIfNotNull(properties.getReplyToGroupId(), mProps::setReplyToGroupId);
}
Map<String, Object> applicationProperties = streamMessage.getApplicationProperties();
if (applicationProperties != null) {
mProps.getHeaders().putAll(applicationProperties);
}
}
use of com.rabbitmq.stream.Properties in project spring-amqp by spring-projects.
the class DefaultStreamMessageConverterTests method toAndFrom.
@Test
void toAndFrom() {
StreamMessageProperties smp = new StreamMessageProperties(mock(Context.class));
smp.setMessageId("test");
smp.setUserId("user");
smp.setTo("to");
smp.setSubject("subject");
smp.setReplyTo("replyTo");
smp.setCorrelationId("correlation");
smp.setContentType("application/json");
smp.setContentEncoding("UTF-8");
smp.setExpiration("42");
smp.setCreationTime(43L);
smp.setGroupId("groupId");
smp.setGroupSequence(44L);
smp.setReplyToGroupId("replyGroupId");
smp.setHeader("foo", "bar");
DefaultStreamMessageConverter converter = new DefaultStreamMessageConverter();
Message msg = new Message("foo".getBytes(), smp);
com.rabbitmq.stream.Message streamMessage = converter.fromMessage(msg);
Properties props = streamMessage.getProperties();
assertThat(props.getMessageIdAsString()).isEqualTo("test");
assertThat(props.getUserId()).isEqualTo("user".getBytes());
assertThat(props.getTo()).isEqualTo("to");
assertThat(props.getSubject()).isEqualTo("subject");
assertThat(props.getReplyTo()).isEqualTo("replyTo");
assertThat(props.getCorrelationIdAsString()).isEqualTo("correlation");
assertThat(props.getContentType()).isEqualTo("application/json");
assertThat(props.getContentEncoding()).isEqualTo("UTF-8");
assertThat(props.getAbsoluteExpiryTime()).isEqualTo(42L);
assertThat(props.getCreationTime()).isEqualTo(43L);
assertThat(props.getGroupId()).isEqualTo("groupId");
assertThat(props.getGroupSequence()).isEqualTo(44L);
assertThat(props.getReplyToGroupId()).isEqualTo("replyGroupId");
assertThat(streamMessage.getApplicationProperties().get("foo")).isEqualTo("bar");
StreamMessageProperties smp2 = new StreamMessageProperties(mock(Context.class));
msg = converter.toMessage(streamMessage, smp2);
assertThat(msg.getMessageProperties()).isSameAs(smp2);
assertThat(smp2).isEqualTo(smp);
}
Aggregations