use of org.apache.qpid.server.protocol.v1_0.type.messaging.DataSection in project qpid-broker-j by apache.
the class Message_1_0 method getContent.
@Override
public QpidByteBuffer getContent(final int offset, final int length) {
if (getMessageMetaData().getVersion() == 0) {
SectionDecoder sectionDecoder = new SectionDecoderImpl(DESCRIBED_TYPE_REGISTRY.getSectionDecoderRegistry());
try {
List<EncodingRetainingSection<?>> sections;
// not just #getSize()
try (QpidByteBuffer allSectionsContent = super.getContent(0, Integer.MAX_VALUE)) {
sections = sectionDecoder.parseAll(allSectionsContent);
}
List<QpidByteBuffer> bodySectionContent = new ArrayList<>();
for (final EncodingRetainingSection<?> section : sections) {
if (section instanceof DataSection || section instanceof AmqpValueSection || section instanceof AmqpSequenceSection) {
bodySectionContent.add(section.getEncodedForm());
}
section.dispose();
}
try (QpidByteBuffer bodyContent = QpidByteBuffer.concatenate(bodySectionContent)) {
bodySectionContent.forEach(QpidByteBuffer::dispose);
return bodyContent.view(offset, length);
}
} catch (AmqpErrorException e) {
throw new ConnectionScopedRuntimeException(e);
}
} else {
return super.getContent(offset, length);
}
}
use of org.apache.qpid.server.protocol.v1_0.type.messaging.DataSection in project qpid-broker-j by apache.
the class MessageConverter_0_10_to_1_0 method convertMetaData.
@Override
protected MessageMetaData_1_0 convertMetaData(MessageTransferMessage serverMessage, final EncodingRetainingSection<?> bodySection, SectionEncoder sectionEncoder) {
Properties props = new Properties();
props.setCreationTime(new Date(serverMessage.getArrivalTime()));
final MessageProperties msgProps = serverMessage.getHeader().getMessageProperties();
final DeliveryProperties deliveryProps = serverMessage.getHeader().getDeliveryProperties();
Header header = new Header();
if (deliveryProps != null) {
header.setDurable(deliveryProps.hasDeliveryMode() && deliveryProps.getDeliveryMode() == MessageDeliveryMode.PERSISTENT);
if (deliveryProps.hasPriority()) {
header.setPriority(UnsignedByte.valueOf((byte) deliveryProps.getPriority().getValue()));
}
if (deliveryProps.hasTtl()) {
header.setTtl(UnsignedInteger.valueOf(deliveryProps.getTtl()));
} else if (deliveryProps.hasExpiration()) {
long ttl = Math.max(0, deliveryProps.getExpiration() - serverMessage.getArrivalTime());
header.setTtl(UnsignedInteger.valueOf(ttl));
}
if (deliveryProps.hasTimestamp()) {
props.setCreationTime(new Date(deliveryProps.getTimestamp()));
}
String to = deliveryProps.getExchange();
if (deliveryProps.getRoutingKey() != null) {
String routingKey = deliveryProps.getRoutingKey();
if (to != null && !"".equals(to)) {
to += "/" + routingKey;
} else {
to = routingKey;
}
}
props.setTo(to);
}
ApplicationProperties applicationProperties = null;
String originalContentMimeType = null;
if (msgProps != null) {
if (msgProps.hasContentEncoding() && !GZIPUtils.GZIP_CONTENT_ENCODING.equals(msgProps.getContentEncoding()) && bodySection instanceof DataSection) {
props.setContentEncoding(Symbol.valueOf(msgProps.getContentEncoding()));
}
if (msgProps.hasCorrelationId()) {
CharsetDecoder charsetDecoder = StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT);
try {
String correlationIdAsString = charsetDecoder.decode(ByteBuffer.wrap(msgProps.getCorrelationId())).toString();
props.setCorrelationId(correlationIdAsString);
} catch (CharacterCodingException e) {
props.setCorrelationId(new Binary(msgProps.getCorrelationId()));
}
}
if (msgProps.hasMessageId()) {
props.setMessageId(msgProps.getMessageId());
}
if (msgProps.hasReplyTo()) {
ReplyTo replyTo = msgProps.getReplyTo();
String to = null;
if (replyTo.hasExchange() && !"".equals(replyTo.getExchange())) {
to = replyTo.getExchange();
}
if (replyTo.hasRoutingKey()) {
if (to != null) {
to += "/" + replyTo.getRoutingKey();
} else {
to = replyTo.getRoutingKey();
}
}
props.setReplyTo(to);
}
if (msgProps.hasContentType()) {
originalContentMimeType = msgProps.getContentType();
final Symbol contentType = MessageConverter_to_1_0.getContentType(originalContentMimeType);
props.setContentType(contentType);
}
if (msgProps.hasUserId()) {
props.setUserId(new Binary(msgProps.getUserId()));
}
Map<String, Object> applicationPropertiesMap = msgProps.getApplicationHeaders();
if (applicationPropertiesMap != null) {
applicationPropertiesMap = new LinkedHashMap<>(applicationPropertiesMap);
if (applicationPropertiesMap.containsKey("x-jms-type")) {
props.setSubject(String.valueOf(applicationPropertiesMap.get("x-jms-type")));
applicationPropertiesMap.remove("x-jms-type");
}
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");
}
}
try {
applicationProperties = new ApplicationProperties(applicationPropertiesMap);
} catch (IllegalArgumentException e) {
throw new MessageConversionException("Could not convert message from 0-10 to 1.0 because application headers conversion failed.", e);
}
}
}
final MessageAnnotations messageAnnotation = MessageConverter_to_1_0.createMessageAnnotation(bodySection, originalContentMimeType);
return new MessageMetaData_1_0(header.createEncodingRetainingSection(), null, messageAnnotation == null ? null : messageAnnotation.createEncodingRetainingSection(), props.createEncodingRetainingSection(), applicationProperties == null ? null : applicationProperties.createEncodingRetainingSection(), null, serverMessage.getArrivalTime(), bodySection.getEncodedSize());
}
use of org.apache.qpid.server.protocol.v1_0.type.messaging.DataSection 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.v1_0.type.messaging.DataSection in project qpid-broker-j by apache.
the class MessageDecoder method parse.
public void parse() throws AmqpErrorException {
if (!_parsed) {
List<EncodingRetainingSection<?>> sections;
try (QpidByteBuffer combined = QpidByteBuffer.concatenate(_fragments)) {
sections = _sectionDecoder.parseAll(combined);
}
_fragments.forEach(QpidByteBuffer::dispose);
Iterator<EncodingRetainingSection<?>> iter = sections.iterator();
EncodingRetainingSection<?> s = iter.hasNext() ? iter.next() : null;
if (s instanceof HeaderSection) {
_headerSection = (HeaderSection) s;
s = iter.hasNext() ? iter.next() : null;
}
if (s instanceof DeliveryAnnotationsSection) {
_deliveryAnnotationsSection = (DeliveryAnnotationsSection) s;
s = iter.hasNext() ? iter.next() : null;
}
if (s instanceof MessageAnnotationsSection) {
_messageAnnotationsSection = (MessageAnnotationsSection) s;
s = iter.hasNext() ? iter.next() : null;
}
if (s instanceof PropertiesSection) {
_propertiesSection = (PropertiesSection) s;
s = iter.hasNext() ? iter.next() : null;
}
if (s instanceof ApplicationPropertiesSection) {
_applicationPropertiesSection = (ApplicationPropertiesSection) s;
s = iter.hasNext() ? iter.next() : null;
}
if (s instanceof AmqpValueSection) {
_contentSize = s.getEncodedSize();
_dataSections.add(s);
s = iter.hasNext() ? iter.next() : null;
} else if (s instanceof DataSection) {
do {
_contentSize += s.getEncodedSize();
_dataSections.add(s);
s = iter.hasNext() ? iter.next() : null;
} while (s instanceof DataSection);
} else if (s instanceof AmqpSequenceSection) {
do {
_contentSize += s.getEncodedSize();
_dataSections.add(s);
s = iter.hasNext() ? iter.next() : null;
} while (s instanceof AmqpSequenceSection);
} else {
throw new IllegalStateException("Application data sections are not found");
}
if (s instanceof FooterSection) {
_footerSection = (FooterSection) s;
s = iter.hasNext() ? iter.next() : null;
}
if (s != null) {
throw new IllegalStateException(String.format("Encountered unexpected section '%s'", s.getClass().getSimpleName()));
}
_parsed = true;
}
}
Aggregations