Search in sources :

Example 6 with DataSection

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);
    }
}
Also used : EncodingRetainingSection(org.apache.qpid.server.protocol.v1_0.type.messaging.EncodingRetainingSection) ArrayList(java.util.ArrayList) AmqpErrorException(org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException) AmqpSequenceSection(org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpSequenceSection) DataSection(org.apache.qpid.server.protocol.v1_0.type.messaging.DataSection) ConnectionScopedRuntimeException(org.apache.qpid.server.util.ConnectionScopedRuntimeException) SectionDecoderImpl(org.apache.qpid.server.protocol.v1_0.messaging.SectionDecoderImpl) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) AmqpValueSection(org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpValueSection) SectionDecoder(org.apache.qpid.server.protocol.v1_0.messaging.SectionDecoder)

Example 7 with DataSection

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());
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) MessageConversionException(org.apache.qpid.server.protocol.converter.MessageConversionException) Symbol(org.apache.qpid.server.protocol.v1_0.type.Symbol) MessageMetaData_1_0(org.apache.qpid.server.protocol.v1_0.MessageMetaData_1_0) DeliveryProperties(org.apache.qpid.server.protocol.v0_10.transport.DeliveryProperties) CharacterCodingException(java.nio.charset.CharacterCodingException) ApplicationProperties(org.apache.qpid.server.protocol.v1_0.type.messaging.ApplicationProperties) Properties(org.apache.qpid.server.protocol.v1_0.type.messaging.Properties) DeliveryProperties(org.apache.qpid.server.protocol.v0_10.transport.DeliveryProperties) MessageProperties(org.apache.qpid.server.protocol.v0_10.transport.MessageProperties) Date(java.util.Date) ReplyTo(org.apache.qpid.server.protocol.v0_10.transport.ReplyTo) UnsignedInteger(org.apache.qpid.server.protocol.v1_0.type.UnsignedInteger) Header(org.apache.qpid.server.protocol.v1_0.type.messaging.Header) DataSection(org.apache.qpid.server.protocol.v1_0.type.messaging.DataSection) MessageProperties(org.apache.qpid.server.protocol.v0_10.transport.MessageProperties) MessageAnnotations(org.apache.qpid.server.protocol.v1_0.type.messaging.MessageAnnotations) ApplicationProperties(org.apache.qpid.server.protocol.v1_0.type.messaging.ApplicationProperties) Binary(org.apache.qpid.server.protocol.v1_0.type.Binary)

Example 8 with DataSection

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());
}
Also used : MessagePublishInfo(org.apache.qpid.server.protocol.v0_8.transport.MessagePublishInfo) AMQShortString(org.apache.qpid.server.protocol.v0_8.AMQShortString) MessageConversionException(org.apache.qpid.server.protocol.converter.MessageConversionException) Symbol(org.apache.qpid.server.protocol.v1_0.type.Symbol) MessageMetaData_1_0(org.apache.qpid.server.protocol.v1_0.MessageMetaData_1_0) AMQShortString(org.apache.qpid.server.protocol.v0_8.AMQShortString) ApplicationProperties(org.apache.qpid.server.protocol.v1_0.type.messaging.ApplicationProperties) BasicContentHeaderProperties(org.apache.qpid.server.protocol.v0_8.transport.BasicContentHeaderProperties) Properties(org.apache.qpid.server.protocol.v1_0.type.messaging.Properties) BasicContentHeaderProperties(org.apache.qpid.server.protocol.v0_8.transport.BasicContentHeaderProperties) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap) UnsignedInteger(org.apache.qpid.server.protocol.v1_0.type.UnsignedInteger) Header(org.apache.qpid.server.protocol.v1_0.type.messaging.Header) DataSection(org.apache.qpid.server.protocol.v1_0.type.messaging.DataSection) MessageAnnotations(org.apache.qpid.server.protocol.v1_0.type.messaging.MessageAnnotations) ApplicationProperties(org.apache.qpid.server.protocol.v1_0.type.messaging.ApplicationProperties) Binary(org.apache.qpid.server.protocol.v1_0.type.Binary)

Example 9 with DataSection

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;
    }
}
Also used : PropertiesSection(org.apache.qpid.server.protocol.v1_0.type.messaging.PropertiesSection) ApplicationPropertiesSection(org.apache.qpid.server.protocol.v1_0.type.messaging.ApplicationPropertiesSection) DeliveryAnnotationsSection(org.apache.qpid.server.protocol.v1_0.type.messaging.DeliveryAnnotationsSection) EncodingRetainingSection(org.apache.qpid.server.protocol.v1_0.type.messaging.EncodingRetainingSection) MessageAnnotationsSection(org.apache.qpid.server.protocol.v1_0.type.messaging.MessageAnnotationsSection) ApplicationPropertiesSection(org.apache.qpid.server.protocol.v1_0.type.messaging.ApplicationPropertiesSection) AmqpSequenceSection(org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpSequenceSection) HeaderSection(org.apache.qpid.server.protocol.v1_0.type.messaging.HeaderSection) FooterSection(org.apache.qpid.server.protocol.v1_0.type.messaging.FooterSection) DataSection(org.apache.qpid.server.protocol.v1_0.type.messaging.DataSection) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) AmqpValueSection(org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpValueSection)

Aggregations

DataSection (org.apache.qpid.server.protocol.v1_0.type.messaging.DataSection)8 QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)6 EncodingRetainingSection (org.apache.qpid.server.protocol.v1_0.type.messaging.EncodingRetainingSection)5 Binary (org.apache.qpid.server.protocol.v1_0.type.Binary)4 AmqpSequenceSection (org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpSequenceSection)4 AmqpValueSection (org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpValueSection)4 ArrayList (java.util.ArrayList)3 MessageConversionException (org.apache.qpid.server.protocol.converter.MessageConversionException)3 MessageMetaData_1_0 (org.apache.qpid.server.protocol.v1_0.MessageMetaData_1_0)3 AmqpErrorException (org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException)3 ConnectionScopedRuntimeException (org.apache.qpid.server.util.ConnectionScopedRuntimeException)3 Date (java.util.Date)2 SectionDecoderImpl (org.apache.qpid.server.protocol.v1_0.messaging.SectionDecoderImpl)2 Symbol (org.apache.qpid.server.protocol.v1_0.type.Symbol)2 UnsignedInteger (org.apache.qpid.server.protocol.v1_0.type.UnsignedInteger)2 ApplicationProperties (org.apache.qpid.server.protocol.v1_0.type.messaging.ApplicationProperties)2 ApplicationPropertiesSection (org.apache.qpid.server.protocol.v1_0.type.messaging.ApplicationPropertiesSection)2 Data (org.apache.qpid.server.protocol.v1_0.type.messaging.Data)2 DeliveryAnnotationsSection (org.apache.qpid.server.protocol.v1_0.type.messaging.DeliveryAnnotationsSection)2 Footer (org.apache.qpid.server.protocol.v1_0.type.messaging.Footer)2