Search in sources :

Example 1 with AmqpValueSection

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

the class TxnCoordinatorReceivingLinkEndpoint method receiveDelivery.

@Override
protected Error receiveDelivery(Delivery delivery) {
    // Only interested in the amqp-value section that holds the message to the coordinator
    try (QpidByteBuffer payload = delivery.getPayload()) {
        List<EncodingRetainingSection<?>> sections = getSectionDecoder().parseAll(payload);
        boolean amqpValueSectionFound = false;
        for (EncodingRetainingSection section : sections) {
            try {
                if (section instanceof AmqpValueSection) {
                    if (amqpValueSectionFound) {
                        throw new ConnectionScopedRuntimeException("Received more than one AmqpValue sections");
                    }
                    amqpValueSectionFound = true;
                    Object command = section.getValue();
                    Session_1_0 session = getSession();
                    AMQPConnection_1_0<?> connection = session.getConnection();
                    connection.receivedComplete();
                    if (command instanceof Declare) {
                        final IdentifiedTransaction txn = connection.createIdentifiedTransaction();
                        _createdTransactions.put(txn.getId(), txn.getServerTransaction());
                        long notificationRepeatPeriod = getSession().getContextValue(Long.class, Session.TRANSACTION_TIMEOUT_NOTIFICATION_REPEAT_PERIOD);
                        connection.registerTransactionTickers(txn.getServerTransaction(), this::doTimeoutAction, notificationRepeatPeriod);
                        Declared state = new Declared();
                        state.setTxnId(Session_1_0.integerToTransactionId(txn.getId()));
                        updateDisposition(delivery.getDeliveryTag(), state, true);
                    } else if (command instanceof Discharge) {
                        Discharge discharge = (Discharge) command;
                        Error error = discharge(discharge.getTxnId(), Boolean.TRUE.equals(discharge.getFail()));
                        final DeliveryState outcome;
                        if (error == null) {
                            outcome = new Accepted();
                        } else if (Arrays.asList(getSource().getOutcomes()).contains(Rejected.REJECTED_SYMBOL)) {
                            final Rejected rejected = new Rejected();
                            rejected.setError(error);
                            outcome = rejected;
                            error = null;
                        } else {
                            outcome = null;
                        }
                        if (error == null) {
                            updateDisposition(delivery.getDeliveryTag(), outcome, true);
                        }
                        return error;
                    } else {
                        throw new ConnectionScopedRuntimeException(String.format("Received unknown command '%s'", command.getClass().getSimpleName()));
                    }
                }
            } finally {
                section.dispose();
            }
        }
        if (!amqpValueSectionFound) {
            throw new ConnectionScopedRuntimeException("Received no AmqpValue section");
        }
    } catch (AmqpErrorException e) {
        return e.getError();
    }
    return null;
}
Also used : EncodingRetainingSection(org.apache.qpid.server.protocol.v1_0.type.messaging.EncodingRetainingSection) Error(org.apache.qpid.server.protocol.v1_0.type.transport.Error) AmqpError(org.apache.qpid.server.protocol.v1_0.type.transport.AmqpError) TransactionError(org.apache.qpid.server.protocol.v1_0.type.transaction.TransactionError) AmqpErrorException(org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException) Rejected(org.apache.qpid.server.protocol.v1_0.type.messaging.Rejected) Declare(org.apache.qpid.server.protocol.v1_0.type.transaction.Declare) Declared(org.apache.qpid.server.protocol.v1_0.type.transaction.Declared) Discharge(org.apache.qpid.server.protocol.v1_0.type.transaction.Discharge) Accepted(org.apache.qpid.server.protocol.v1_0.type.messaging.Accepted) DeliveryState(org.apache.qpid.server.protocol.v1_0.type.DeliveryState) ConnectionScopedRuntimeException(org.apache.qpid.server.util.ConnectionScopedRuntimeException) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) AmqpValueSection(org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpValueSection)

Example 2 with AmqpValueSection

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

the class MessageConverter_to_1_0 method createMessageAnnotation.

public static MessageAnnotations createMessageAnnotation(final EncodingRetainingSection<?> bodySection, final String contentMimeType) {
    MessageAnnotations messageAnnotations = null;
    final Symbol key = Symbol.valueOf("x-opt-jms-msg-type");
    if (contentMimeType != null) {
        if (TEXT_CONTENT_TYPES.matcher(contentMimeType).matches()) {
            messageAnnotations = new MessageAnnotations(Collections.singletonMap(key, TEXT_MESSAGE.getType()));
        } else if (BYTES_MESSAGE_CONTENT_TYPES.matcher(contentMimeType).matches()) {
            messageAnnotations = new MessageAnnotations(Collections.singletonMap(key, BYTES_MESSAGE.getType()));
        } else if (MAP_MESSAGE_CONTENT_TYPES.matcher(contentMimeType).matches()) {
            if (isSectionValidForJmsMap(bodySection)) {
                messageAnnotations = new MessageAnnotations(Collections.singletonMap(key, MAP_MESSAGE.getType()));
            }
        } else if (LIST_MESSAGE_CONTENT_TYPES.matcher(contentMimeType).matches()) {
            if (isSectionValidForJmsList(bodySection)) {
                messageAnnotations = new MessageAnnotations(Collections.singletonMap(key, STREAM_MESSAGE.getType()));
            }
        } else if (OBJECT_MESSAGE_CONTENT_TYPES.matcher(contentMimeType).matches()) {
            messageAnnotations = new MessageAnnotations(Collections.singletonMap(key, OBJECT_MESSAGE.getType()));
        }
    } else if (bodySection instanceof AmqpValueSection && bodySection.getValue() == null) {
        messageAnnotations = new MessageAnnotations(Collections.singletonMap(key, MESSAGE.getType()));
    }
    return messageAnnotations;
}
Also used : MessageAnnotations(org.apache.qpid.server.protocol.v1_0.type.messaging.MessageAnnotations) Symbol(org.apache.qpid.server.protocol.v1_0.type.Symbol) AmqpValueSection(org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpValueSection)

Example 3 with AmqpValueSection

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

the class Interaction method transferPayload.

private void transferPayload(final Transfer transfer, final Object payload) {
    AmqpValue amqpValue = new AmqpValue(payload);
    final AmqpValueSection section = amqpValue.createEncodingRetainingSection();
    try (QpidByteBuffer encodedForm = section.getEncodedForm()) {
        transfer.setPayload(encodedForm);
    } finally {
        section.dispose();
    }
}
Also used : QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) AmqpValueSection(org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpValueSection) AmqpValue(org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpValue)

Example 4 with AmqpValueSection

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

the class DecodeErrorTest method buildInvalidMessage.

private QpidByteBuffer buildInvalidMessage() {
    final List<QpidByteBuffer> payloads = new ArrayList<>();
    try {
        final Header header = new Header();
        header.setTtl(UnsignedInteger.valueOf(10000L));
        final HeaderSection headerSection = header.createEncodingRetainingSection();
        try {
            payloads.add(headerSection.getEncodedForm());
        } finally {
            headerSection.dispose();
        }
        final StringWriter stringWriter = new StringWriter("string in between message sections");
        final QpidByteBuffer encodedString = QpidByteBuffer.allocate(stringWriter.getEncodedSize());
        stringWriter.writeToBuffer(encodedString);
        encodedString.flip();
        payloads.add(encodedString);
        final Map<Symbol, Object> annotationMap = Collections.singletonMap(Symbol.valueOf("foo"), "bar");
        final DeliveryAnnotations annotations = new DeliveryAnnotations(annotationMap);
        final DeliveryAnnotationsSection deliveryAnnotationsSection = annotations.createEncodingRetainingSection();
        try {
            payloads.add(deliveryAnnotationsSection.getEncodedForm());
        } finally {
            deliveryAnnotationsSection.dispose();
        }
        final AmqpValueSection payload = new AmqpValue(getTestName()).createEncodingRetainingSection();
        try {
            payloads.add(payload.getEncodedForm());
        } finally {
            payload.dispose();
        }
        return QpidByteBuffer.concatenate(payloads);
    } finally {
        payloads.forEach(QpidByteBuffer::dispose);
    }
}
Also used : DeliveryAnnotationsSection(org.apache.qpid.server.protocol.v1_0.type.messaging.DeliveryAnnotationsSection) Symbol(org.apache.qpid.server.protocol.v1_0.type.Symbol) ArrayList(java.util.ArrayList) DeliveryAnnotations(org.apache.qpid.server.protocol.v1_0.type.messaging.DeliveryAnnotations) HeaderSection(org.apache.qpid.server.protocol.v1_0.type.messaging.HeaderSection) AmqpValue(org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpValue) Header(org.apache.qpid.server.protocol.v1_0.type.messaging.Header) StringWriter(org.apache.qpid.server.protocol.v1_0.codec.StringWriter) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) AmqpValueSection(org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpValueSection)

Example 5 with AmqpValueSection

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

the class MessageConverter_from_1_0 method convertBodyToObject.

static Object convertBodyToObject(final Message_1_0 serverMessage) {
    final 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);
        }
        final int size = sections == null ? 0 : sections.size();
        final List<EncodingRetainingSection<?>> bodySections = new ArrayList<>(size);
        final ListIterator<EncodingRetainingSection<?>> iterator = sections == null ? Collections.emptyListIterator() : sections.listIterator();
        EncodingRetainingSection<?> previousSection = null;
        while (iterator.hasNext()) {
            final 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()) {
            final 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;
                }
                final byte[] bodyData = new byte[totalSize];
                final ByteBuffer buf = ByteBuffer.wrap(bodyData);
                for (EncodingRetainingSection<?> section : bodySections) {
                    buf.put(((DataSection) section).getValue().asByteBuffer());
                }
                bodyObject = bodyData;
            } else {
                final 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)

Aggregations

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