use of org.apache.qpid.server.protocol.converter.MessageConversionException in project qpid-broker-j by apache.
the class MessageConverter_0_8_to_1_0 method convertMetaData.
@Override
protected MessageMetaData_1_0 convertMetaData(final AMQMessage serverMessage, final EncodingRetainingSection<?> bodySection, SectionEncoder sectionEncoder) {
Header header = new Header();
Properties props = new Properties();
header.setDurable(serverMessage.isPersistent());
BasicContentHeaderProperties contentHeader = serverMessage.getContentHeaderBody().getProperties();
header.setPriority(UnsignedByte.valueOf(contentHeader.getPriority()));
if (contentHeader.hasExpiration()) {
final long expiration = serverMessage.getExpiration();
final long arrivalTime = serverMessage.getArrivalTime();
header.setTtl(UnsignedInteger.valueOf(Math.max(0, expiration - arrivalTime)));
}
if (!GZIPUtils.GZIP_CONTENT_ENCODING.equals(contentHeader.getEncodingAsString()) && bodySection instanceof DataSection) {
props.setContentEncoding(Symbol.valueOf(contentHeader.getEncodingAsString()));
}
Symbol contentType = getContentType(contentHeader.getContentTypeAsString());
props.setContentType(contentType);
final AMQShortString correlationId = contentHeader.getCorrelationId();
if (correlationId != null) {
final byte[] correlationIdAsBytes = correlationId.getBytes();
final String correlationIdAsString = contentHeader.getCorrelationIdAsString();
if (Arrays.equals(correlationIdAsBytes, correlationIdAsString.getBytes(StandardCharsets.UTF_8))) {
props.setCorrelationId(correlationIdAsString);
} else {
props.setCorrelationId(correlationIdAsBytes);
}
}
final AMQShortString messageId = contentHeader.getMessageId();
if (messageId != null) {
props.setMessageId(messageId.toString());
}
if (contentHeader.getReplyTo() != null) {
props.setReplyTo(convertReplyTo(contentHeader.getReplyTo()));
}
if (contentHeader.getUserId() != null) {
props.setUserId(new Binary(contentHeader.getUserId().getBytes()));
}
if (contentHeader.hasTimestamp()) {
props.setCreationTime(new Date(contentHeader.getTimestamp()));
} else {
props.setCreationTime(new Date(serverMessage.getArrivalTime()));
}
if (contentHeader.getType() != null) {
props.setSubject(contentHeader.getType().toString());
}
Map<String, Object> applicationPropertiesMap = new LinkedHashMap<>(FieldTable.convertToMap(contentHeader.getHeaders()));
if (applicationPropertiesMap.containsKey("qpid.subject")) {
props.setSubject(String.valueOf(applicationPropertiesMap.get("qpid.subject")));
applicationPropertiesMap.remove("qpid.subject");
}
if (applicationPropertiesMap.containsKey("JMSXGroupID")) {
props.setGroupId(String.valueOf(applicationPropertiesMap.get("JMSXGroupID")));
applicationPropertiesMap.remove("JMSXGroupID");
}
if (applicationPropertiesMap.containsKey("JMSXGroupSeq")) {
Object jmsxGroupSeq = applicationPropertiesMap.get("JMSXGroupSeq");
if (jmsxGroupSeq instanceof Integer) {
props.setGroupSequence(UnsignedInteger.valueOf((Integer) jmsxGroupSeq));
applicationPropertiesMap.remove("JMSXGroupSeq");
}
}
MessagePublishInfo messagePublishInfo = serverMessage.getMessagePublishInfo();
String to = AMQShortString.toString(messagePublishInfo.getExchange());
if (messagePublishInfo.getRoutingKey() != null) {
String routingKey = AMQShortString.toString(messagePublishInfo.getRoutingKey());
if (to != null && !"".equals(to)) {
to += "/" + routingKey;
} else {
to = routingKey;
}
}
props.setTo(to);
final ApplicationProperties applicationProperties;
try {
applicationProperties = new ApplicationProperties(applicationPropertiesMap);
} catch (IllegalArgumentException e) {
throw new MessageConversionException("Could not convert message from 0-8 to 1.0 because headers conversion failed.", e);
}
MessageAnnotations messageAnnotations = createMessageAnnotation(bodySection, contentHeader.getContentTypeAsString());
return new MessageMetaData_1_0(header.createEncodingRetainingSection(), null, messageAnnotations == null ? null : messageAnnotations.createEncodingRetainingSection(), props.createEncodingRetainingSection(), applicationProperties.createEncodingRetainingSection(), null, serverMessage.getArrivalTime(), bodySection.getEncodedSize());
}
use of org.apache.qpid.server.protocol.converter.MessageConversionException in project qpid-broker-j by apache.
the class MessageConverter_1_0_to_v0_8 method getReplyTo.
private AMQShortString getReplyTo(final Message_1_0 serverMsg, final NamedAddressSpace addressSpace) {
final String replyTo = serverMsg.getMessageHeader().getReplyTo();
if (replyTo != null) {
DestinationAddress destinationAddress = new DestinationAddress(addressSpace, replyTo);
MessageDestination messageDestination = destinationAddress.getMessageDestination();
final String replyToBindingUrl;
if (messageDestination instanceof Exchange) {
Exchange<?> exchange = (Exchange<?>) messageDestination;
final String routingKeyOption = "".equals(destinationAddress.getRoutingKey()) ? "" : String.format("?routingkey='%s'", destinationAddress.getRoutingKey());
replyToBindingUrl = String.format("%s://%s//%s", exchange.getType(), exchange.getName(), routingKeyOption);
} else if (messageDestination instanceof Queue) {
replyToBindingUrl = String.format("%s:////%s", ExchangeDefaults.DIRECT_EXCHANGE_CLASS, messageDestination.getName());
} else {
replyToBindingUrl = String.format("%s:////?routingkey='%s'", ExchangeDefaults.DIRECT_EXCHANGE_CLASS, destinationAddress.getRoutingKey());
}
try {
return AMQShortString.valueOf(replyToBindingUrl);
} catch (IllegalArgumentException e) {
throw new MessageConversionException("Could not convert message from 1.0 to 0-8 because conversion of 'reply-to' failed.", e);
}
}
return null;
}
use of org.apache.qpid.server.protocol.converter.MessageConversionException in project qpid-broker-j by apache.
the class MessageConverter_1_0_to_v0_8 method getCorrelationIdAsShortString.
private AMQShortString getCorrelationIdAsShortString(final Message_1_0 serverMsg) {
Object correlationIdObject = getCorrelationId(serverMsg);
final AMQShortString correlationId;
try {
if (correlationIdObject instanceof Binary) {
correlationId = new AMQShortString(((Binary) correlationIdObject).getArray());
} else if (correlationIdObject instanceof byte[]) {
correlationId = new AMQShortString(((byte[]) correlationIdObject));
} else {
correlationId = AMQShortString.valueOf(correlationIdObject);
}
} catch (IllegalArgumentException e) {
throw new MessageConversionException("Could not convert message from 1.0 to 0-8 because conversion of 'correlation-id' failed.", e);
}
return correlationId;
}
use of org.apache.qpid.server.protocol.converter.MessageConversionException in project qpid-broker-j by apache.
the class MessageConverter_1_0_to_v0_8Test method testAmqpSequenceWithUnsupportedType.
public void testAmqpSequenceWithUnsupportedType() throws Exception {
final List<Object> originalList = Collections.singletonList(new Source());
final AmqpSequence amqpSequence = new AmqpSequence(originalList);
Message_1_0 sourceMessage = createTestMessage(amqpSequence.createEncodingRetainingSection());
try {
_converter.convert(sourceMessage, mock(NamedAddressSpace.class));
fail("expected exception not thrown.");
} catch (MessageConversionException e) {
// pass
}
}
use of org.apache.qpid.server.protocol.converter.MessageConversionException in project qpid-broker-j by apache.
the class MessageConverter_1_0_to_v0_8Test method testAmqpValueWithListContainingUnsupportedType.
public void testAmqpValueWithListContainingUnsupportedType() throws Exception {
final List<Object> originalList = Collections.singletonList(new Source());
final AmqpValue amqpValue = new AmqpValue(originalList);
Message_1_0 sourceMessage = createTestMessage(amqpValue.createEncodingRetainingSection());
try {
_converter.convert(sourceMessage, mock(NamedAddressSpace.class));
fail("expected exception not thrown.");
} catch (MessageConversionException e) {
// pass
}
}
Aggregations