Search in sources :

Example 1 with AmqpSequenceSection

use of org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpSequenceSection in project qpid-broker-j by apache.

the class MessageConverter_from_1_0 method convertBodyToObject.

static Object convertBodyToObject(final Message_1_0 serverMessage) {
    SectionDecoderImpl sectionDecoder = new SectionDecoderImpl(MessageConverter_v1_0_to_Internal.TYPE_REGISTRY.getSectionDecoderRegistry());
    Object bodyObject = null;
    List<EncodingRetainingSection<?>> sections = null;
    try {
        try (QpidByteBuffer allData = serverMessage.getContent()) {
            sections = sectionDecoder.parseAll(allData);
        }
        List<EncodingRetainingSection<?>> bodySections = new ArrayList<>(sections.size());
        ListIterator<EncodingRetainingSection<?>> iterator = sections.listIterator();
        EncodingRetainingSection<?> previousSection = null;
        while (iterator.hasNext()) {
            EncodingRetainingSection<?> section = iterator.next();
            if (section instanceof AmqpValueSection || section instanceof DataSection || section instanceof AmqpSequenceSection) {
                if (previousSection != null && (previousSection.getClass() != section.getClass() || section instanceof AmqpValueSection)) {
                    throw new MessageConversionException("Message is badly formed and has multiple body section which are not all Data or not all AmqpSequence");
                } else {
                    previousSection = section;
                }
                bodySections.add(section);
            }
        }
        // In 1.0 of the spec, it is illegal to have message with no body but AMQP-127 asks to have that restriction lifted
        if (!bodySections.isEmpty()) {
            EncodingRetainingSection<?> firstBodySection = bodySections.get(0);
            if (firstBodySection instanceof AmqpValueSection) {
                bodyObject = convertValue(firstBodySection.getValue());
            } else if (firstBodySection instanceof DataSection) {
                int totalSize = 0;
                for (EncodingRetainingSection<?> section : bodySections) {
                    totalSize += ((DataSection) section).getValue().getArray().length;
                }
                byte[] bodyData = new byte[totalSize];
                ByteBuffer buf = ByteBuffer.wrap(bodyData);
                for (EncodingRetainingSection<?> section : bodySections) {
                    buf.put(((DataSection) section).getValue().asByteBuffer());
                }
                bodyObject = bodyData;
            } else {
                ArrayList<Object> totalSequence = new ArrayList<>();
                for (EncodingRetainingSection<?> section : bodySections) {
                    totalSequence.addAll(((AmqpSequenceSection) section).getValue());
                }
                bodyObject = convertValue(totalSequence);
            }
        }
    } catch (AmqpErrorException e) {
        throw new ConnectionScopedRuntimeException(e);
    } finally {
        if (sections != null) {
            sections.forEach(EncodingRetaining::dispose);
        }
    }
    return bodyObject;
}
Also used : MessageConversionException(org.apache.qpid.server.protocol.converter.MessageConversionException) EncodingRetainingSection(org.apache.qpid.server.protocol.v1_0.type.messaging.EncodingRetainingSection) EncodingRetaining(org.apache.qpid.server.protocol.v1_0.type.messaging.codec.EncodingRetaining) 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) ByteBuffer(java.nio.ByteBuffer) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) 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)

Example 2 with AmqpSequenceSection

use of org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpSequenceSection in project qpid-broker-j by apache.

the class MessageFormat_1_0 method createMessageMetaData.

private MessageMetaData_1_0 createMessageMetaData(final List<EncodingRetainingSection<?>> allSections, final List<EncodingRetainingSection<?>> dataSections) {
    long contentSize = 0L;
    HeaderSection headerSection = null;
    PropertiesSection propertiesSection = null;
    DeliveryAnnotationsSection deliveryAnnotationsSection = null;
    MessageAnnotationsSection messageAnnotationsSection = null;
    ApplicationPropertiesSection applicationPropertiesSection = null;
    FooterSection footerSection = null;
    Iterator<EncodingRetainingSection<?>> iter = allSections.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);
    }
    if (s instanceof FooterSection) {
        footerSection = (FooterSection) s;
        s = iter.hasNext() ? iter.next() : null;
    }
    if (s != null) {
        throw new ConnectionScopedRuntimeException(String.format("Encountered unexpected section '%s'", s.getClass().getSimpleName()));
    }
    return new MessageMetaData_1_0(headerSection, deliveryAnnotationsSection, messageAnnotationsSection, propertiesSection, applicationPropertiesSection, footerSection, System.currentTimeMillis(), contentSize);
}
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) ConnectionScopedRuntimeException(org.apache.qpid.server.util.ConnectionScopedRuntimeException) AmqpValueSection(org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpValueSection)

Example 3 with AmqpSequenceSection

use of org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpSequenceSection 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 4 with AmqpSequenceSection

use of org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpSequenceSection 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

AmqpSequenceSection (org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpSequenceSection)4 AmqpValueSection (org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpValueSection)4 DataSection (org.apache.qpid.server.protocol.v1_0.type.messaging.DataSection)4 EncodingRetainingSection (org.apache.qpid.server.protocol.v1_0.type.messaging.EncodingRetainingSection)4 QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)3 ConnectionScopedRuntimeException (org.apache.qpid.server.util.ConnectionScopedRuntimeException)3 ArrayList (java.util.ArrayList)2 SectionDecoderImpl (org.apache.qpid.server.protocol.v1_0.messaging.SectionDecoderImpl)2 AmqpErrorException (org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException)2 ApplicationPropertiesSection (org.apache.qpid.server.protocol.v1_0.type.messaging.ApplicationPropertiesSection)2 DeliveryAnnotationsSection (org.apache.qpid.server.protocol.v1_0.type.messaging.DeliveryAnnotationsSection)2 FooterSection (org.apache.qpid.server.protocol.v1_0.type.messaging.FooterSection)2 HeaderSection (org.apache.qpid.server.protocol.v1_0.type.messaging.HeaderSection)2 MessageAnnotationsSection (org.apache.qpid.server.protocol.v1_0.type.messaging.MessageAnnotationsSection)2 PropertiesSection (org.apache.qpid.server.protocol.v1_0.type.messaging.PropertiesSection)2 ByteBuffer (java.nio.ByteBuffer)1 MessageConversionException (org.apache.qpid.server.protocol.converter.MessageConversionException)1 SectionDecoder (org.apache.qpid.server.protocol.v1_0.messaging.SectionDecoder)1 EncodingRetaining (org.apache.qpid.server.protocol.v1_0.type.messaging.codec.EncodingRetaining)1