Search in sources :

Example 11 with EncodingRetainingSection

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

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

the class ConsumerTarget_1_0Test method testTTLAdjustedOnSend.

public void testTTLAdjustedOnSend() throws Exception {
    final MessageInstanceConsumer comsumer = mock(MessageInstanceConsumer.class);
    long ttl = 2000L;
    long arrivalTime = System.currentTimeMillis() - 1000L;
    final Header header = new Header();
    header.setTtl(UnsignedInteger.valueOf(ttl));
    final Message_1_0 message = createTestMessage(header, arrivalTime);
    final MessageInstance messageInstance = mock(MessageInstance.class);
    when(messageInstance.getMessage()).thenReturn(message);
    AtomicReference<QpidByteBuffer> payloadRef = new AtomicReference<>();
    doAnswer(invocation -> {
        final Object[] args = invocation.getArguments();
        Transfer transfer = (Transfer) args[0];
        QpidByteBuffer transferPayload = transfer.getPayload();
        QpidByteBuffer payloadCopy = transferPayload.duplicate();
        payloadRef.set(payloadCopy);
        return null;
    }).when(_sendingLinkEndpoint).transfer(any(Transfer.class), anyBoolean());
    _consumerTarget.doSend(comsumer, messageInstance, false);
    verify(_sendingLinkEndpoint, times(1)).transfer(any(Transfer.class), anyBoolean());
    final List<EncodingRetainingSection<?>> sections;
    try (QpidByteBuffer payload = payloadRef.get()) {
        sections = new SectionDecoderImpl(_describedTypeRegistry.getSectionDecoderRegistry()).parseAll(payload);
    }
    Header sentHeader = null;
    for (EncodingRetainingSection<?> section : sections) {
        if (section instanceof HeaderSection) {
            sentHeader = ((HeaderSection) section).getValue();
        }
    }
    assertNotNull("Header is not found", sentHeader);
    assertNotNull("Ttl is not set", sentHeader.getTtl());
    assertTrue("Unexpected ttl", sentHeader.getTtl().longValue() <= 1000);
}
Also used : EncodingRetainingSection(org.apache.qpid.server.protocol.v1_0.type.messaging.EncodingRetainingSection) MessageInstanceConsumer(org.apache.qpid.server.message.MessageInstanceConsumer) AtomicReference(java.util.concurrent.atomic.AtomicReference) HeaderSection(org.apache.qpid.server.protocol.v1_0.type.messaging.HeaderSection) MessageInstance(org.apache.qpid.server.message.MessageInstance) Header(org.apache.qpid.server.protocol.v1_0.type.messaging.Header) Transfer(org.apache.qpid.server.protocol.v1_0.type.transport.Transfer) SectionDecoderImpl(org.apache.qpid.server.protocol.v1_0.messaging.SectionDecoderImpl) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer)

Example 13 with EncodingRetainingSection

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

the class MessageConverter_Internal_to_1_0Test method doTest.

private void doTest(final Serializable messageBytes, final String mimeType, final Class<? extends EncodingRetainingSection<?>> expectedBodySection, final Object expectedContent, final Symbol expectedContentType, final Byte expectedJmsTypeAnnotation) throws Exception {
    final InternalMessage sourceMessage = getAmqMessage(messageBytes, mimeType);
    final Message_1_0 convertedMessage = _converter.convert(sourceMessage, mock(NamedAddressSpace.class));
    final QpidByteBuffer content = convertedMessage.getContent();
    List<EncodingRetainingSection<?>> sections = getEncodingRetainingSections(content, 1);
    EncodingRetainingSection<?> encodingRetainingSection = sections.get(0);
    assertEquals("Unexpected section type", expectedBodySection, encodingRetainingSection.getClass());
    if (expectedContent instanceof byte[]) {
        assertArrayEquals("Unexpected content", ((byte[]) expectedContent), ((Binary) encodingRetainingSection.getValue()).getArray());
    } else {
        assertEquals("Unexpected content", expectedContent, encodingRetainingSection.getValue());
    }
    Symbol contentType = getContentType(convertedMessage);
    if (expectedContentType == null) {
        assertNull("Content type should be null", contentType);
    } else {
        assertEquals("Unexpected content type", expectedContentType, contentType);
    }
    Byte jmsMessageTypeAnnotation = getJmsMessageTypeAnnotation(convertedMessage);
    if (expectedJmsTypeAnnotation == null) {
        assertEquals("Unexpected annotation 'x-opt-jms-msg-type'", null, jmsMessageTypeAnnotation);
    } else {
        assertEquals("Unexpected annotation 'x-opt-jms-msg-type'", expectedJmsTypeAnnotation, jmsMessageTypeAnnotation);
    }
}
Also used : InternalMessage(org.apache.qpid.server.message.internal.InternalMessage) EncodingRetainingSection(org.apache.qpid.server.protocol.v1_0.type.messaging.EncodingRetainingSection) Symbol(org.apache.qpid.server.protocol.v1_0.type.Symbol) NamedAddressSpace(org.apache.qpid.server.model.NamedAddressSpace) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer)

Example 14 with EncodingRetainingSection

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

the class MessageConverter_Internal_to_1_0Test method getEncodingRetainingSections.

private List<EncodingRetainingSection<?>> getEncodingRetainingSections(final QpidByteBuffer content, final int expectedNumberOfSections) throws Exception {
    SectionDecoder sectionDecoder = new SectionDecoderImpl(_typeRegistry.getSectionDecoderRegistry());
    final List<EncodingRetainingSection<?>> sections = sectionDecoder.parseAll(content);
    assertEquals("Unexpected number of sections", expectedNumberOfSections, sections.size());
    return sections;
}
Also used : EncodingRetainingSection(org.apache.qpid.server.protocol.v1_0.type.messaging.EncodingRetainingSection) SectionDecoderImpl(org.apache.qpid.server.protocol.v1_0.messaging.SectionDecoderImpl) SectionDecoder(org.apache.qpid.server.protocol.v1_0.messaging.SectionDecoder)

Example 15 with EncodingRetainingSection

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

Aggregations

QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)14 EncodingRetainingSection (org.apache.qpid.server.protocol.v1_0.type.messaging.EncodingRetainingSection)14 Symbol (org.apache.qpid.server.protocol.v1_0.type.Symbol)7 SectionDecoderImpl (org.apache.qpid.server.protocol.v1_0.messaging.SectionDecoderImpl)6 AmqpValueSection (org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpValueSection)6 DataSection (org.apache.qpid.server.protocol.v1_0.type.messaging.DataSection)6 ArrayList (java.util.ArrayList)5 Header (org.apache.qpid.server.protocol.v1_0.type.messaging.Header)5 MessageConversionException (org.apache.qpid.server.protocol.converter.MessageConversionException)4 MessageMetaData_1_0 (org.apache.qpid.server.protocol.v1_0.MessageMetaData_1_0)4 Message_1_0 (org.apache.qpid.server.protocol.v1_0.Message_1_0)4 SectionDecoder (org.apache.qpid.server.protocol.v1_0.messaging.SectionDecoder)4 AmqpErrorException (org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException)4 Binary (org.apache.qpid.server.protocol.v1_0.type.Binary)4 AmqpSequenceSection (org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpSequenceSection)4 ApplicationProperties (org.apache.qpid.server.protocol.v1_0.type.messaging.ApplicationProperties)4 HeaderSection (org.apache.qpid.server.protocol.v1_0.type.messaging.HeaderSection)4 MessageAnnotations (org.apache.qpid.server.protocol.v1_0.type.messaging.MessageAnnotations)4 Date (java.util.Date)3 NamedAddressSpace (org.apache.qpid.server.model.NamedAddressSpace)3