Search in sources :

Example 31 with Header

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

the class TransferTest method durableTransferWithRejectedOutcome.

@Test
@SpecificationTest(section = "3.2.1", description = "Durable messages MUST NOT be lost even if an intermediary is unexpectedly terminated and " + "restarted. A target which is not capable of fulfilling this guarantee MUST NOT accept messages " + "where the durable header is set to true: if the source allows the rejected outcome then the " + "message SHOULD be rejected with the precondition-failed error, otherwise the link MUST be " + "detached by the receiver with the same error.")
@BrokerSpecific(kind = BrokerAdmin.KIND_BROKER_J)
public void durableTransferWithRejectedOutcome() throws Exception {
    try (FrameTransport transport = new FrameTransport(getBrokerAdmin()).connect()) {
        MessageEncoder messageEncoder = new MessageEncoder();
        final Header header = new Header();
        header.setDurable(true);
        messageEncoder.setHeader(header);
        messageEncoder.addData(getTestName());
        final Disposition receivedDisposition = transport.newInteraction().negotiateOpen().begin().consumeResponse(Begin.class).attachRole(Role.SENDER).attachTargetAddress(BrokerAdmin.TEST_QUEUE_NAME).attachSourceOutcomes(Accepted.ACCEPTED_SYMBOL, Rejected.REJECTED_SYMBOL).attach().consumeResponse(Attach.class).consumeResponse(Flow.class).assertLatestResponse(Flow.class, this::assumeSufficientCredits).transferDeliveryId().transferPayload(messageEncoder.getPayload()).transferRcvSettleMode(ReceiverSettleMode.FIRST).transfer().consumeResponse().getLatestResponse(Disposition.class);
        assertThat(receivedDisposition.getSettled(), is(true));
        assertThat(receivedDisposition.getState(), is(instanceOf(Outcome.class)));
        if (getBrokerAdmin().supportsRestart()) {
            assertThat(((Outcome) receivedDisposition.getState()).getSymbol(), is(Accepted.ACCEPTED_SYMBOL));
        } else {
            assertThat(((Outcome) receivedDisposition.getState()).getSymbol(), is(Rejected.REJECTED_SYMBOL));
        }
    }
}
Also used : FrameTransport(org.apache.qpid.tests.protocol.v1_0.FrameTransport) Header(org.apache.qpid.server.protocol.v1_0.type.messaging.Header) Disposition(org.apache.qpid.server.protocol.v1_0.type.transport.Disposition) Begin(org.apache.qpid.server.protocol.v1_0.type.transport.Begin) MessageEncoder(org.apache.qpid.tests.protocol.v1_0.MessageEncoder) Flow(org.apache.qpid.server.protocol.v1_0.type.transport.Flow) SpecificationTest(org.apache.qpid.tests.protocol.SpecificationTest) Test(org.junit.Test) BrokerSpecific(org.apache.qpid.tests.utils.BrokerSpecific) SpecificationTest(org.apache.qpid.tests.protocol.SpecificationTest)

Example 32 with Header

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

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

the class DecodeErrorTest method illegalMessageFormatPayload.

@Test
@SpecificationTest(section = "3.2", description = "Altogether a message consists of the following sections: Zero or one header," + " Zero or one delivery-annotations, [...]")
public void illegalMessageFormatPayload() throws Exception {
    try (FrameTransport transport = new FrameTransport(_brokerAddress).connect()) {
        List<QpidByteBuffer> payloads = new ArrayList<>();
        final HeaderSection headerSection = new Header().createEncodingRetainingSection();
        payloads.add(headerSection.getEncodedForm());
        headerSection.dispose();
        final StringWriter stringWriter = new StringWriter("string in between annotation sections");
        QpidByteBuffer encodedString = QpidByteBuffer.allocate(stringWriter.getEncodedSize());
        stringWriter.writeToBuffer(encodedString);
        encodedString.flip();
        payloads.add(encodedString);
        final DeliveryAnnotationsSection deliveryAnnotationsSection = new DeliveryAnnotations(Collections.emptyMap()).createEncodingRetainingSection();
        payloads.add(deliveryAnnotationsSection.getEncodedForm());
        deliveryAnnotationsSection.dispose();
        final Detach detachResponse;
        try (QpidByteBuffer combinedPayload = QpidByteBuffer.concatenate(payloads)) {
            detachResponse = transport.newInteraction().negotiateProtocol().consumeResponse().open().consumeResponse(Open.class).begin().consumeResponse(Begin.class).attachRole(Role.SENDER).attachTargetAddress(BrokerAdmin.TEST_QUEUE_NAME).attach().consumeResponse(Attach.class).consumeResponse(Flow.class).transferMessageFormat(UnsignedInteger.ZERO).transferPayload(combinedPayload).transfer().consumeResponse().getLatestResponse(Detach.class);
        }
        payloads.forEach(QpidByteBuffer::dispose);
        assertThat(detachResponse.getError(), is(notNullValue()));
        assertThat(detachResponse.getError().getCondition(), is(equalTo(DECODE_ERROR)));
    }
}
Also used : DeliveryAnnotationsSection(org.apache.qpid.server.protocol.v1_0.type.messaging.DeliveryAnnotationsSection) Attach(org.apache.qpid.server.protocol.v1_0.type.transport.Attach) 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) Open(org.apache.qpid.server.protocol.v1_0.type.transport.Open) Header(org.apache.qpid.server.protocol.v1_0.type.messaging.Header) StringWriter(org.apache.qpid.server.protocol.v1_0.codec.StringWriter) Begin(org.apache.qpid.server.protocol.v1_0.type.transport.Begin) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) Detach(org.apache.qpid.server.protocol.v1_0.type.transport.Detach) Test(org.junit.Test) SpecificationTest(org.apache.qpid.tests.protocol.SpecificationTest) SpecificationTest(org.apache.qpid.tests.protocol.SpecificationTest)

Example 34 with Header

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

the class AMQPConnection_1_0Impl method processProtocolHeader.

private void processProtocolHeader(final QpidByteBuffer msg) {
    if (msg.remaining() >= 8) {
        byte[] header = new byte[8];
        msg.get(header);
        final AuthenticationProvider<?> authenticationProvider = getPort().getAuthenticationProvider();
        if (Arrays.equals(header, SASL_HEADER)) {
            if (_saslComplete) {
                throw new ConnectionScopedRuntimeException("SASL Layer header received after SASL already established");
            }
            try (QpidByteBuffer protocolHeader = QpidByteBuffer.wrap(SASL_HEADER)) {
                getSender().send(protocolHeader);
            }
            SaslMechanisms mechanisms = new SaslMechanisms();
            ArrayList<Symbol> mechanismsList = new ArrayList<>();
            for (String name : authenticationProvider.getAvailableMechanisms(getTransport().isSecure())) {
                mechanismsList.add(Symbol.valueOf(name));
            }
            mechanisms.setSaslServerMechanisms(mechanismsList.toArray(new Symbol[mechanismsList.size()]));
            send(new SASLFrame(mechanisms));
            _connectionState = ConnectionState.AWAIT_SASL_INIT;
            _frameHandler = getFrameHandler(true);
        } else if (Arrays.equals(header, AMQP_HEADER)) {
            if (!_saslComplete) {
                final List<String> mechanisms = authenticationProvider.getAvailableMechanisms(getTransport().isSecure());
                if (mechanisms.contains(ExternalAuthenticationManagerImpl.MECHANISM_NAME) && getNetwork().getPeerPrincipal() != null) {
                    setUserPrincipal(new AuthenticatedPrincipal(getNetwork().getPeerPrincipal()));
                } else if (mechanisms.contains(AnonymousAuthenticationManager.MECHANISM_NAME)) {
                    setUserPrincipal(new AuthenticatedPrincipal(((AnonymousAuthenticationManager) authenticationProvider).getAnonymousPrincipal()));
                } else {
                    LOGGER.warn("{} : attempt to initiate AMQP connection without correctly authenticating", getLogSubject());
                    _connectionState = ConnectionState.CLOSED;
                    getNetwork().close();
                }
            }
            try (QpidByteBuffer protocolHeader = QpidByteBuffer.wrap(AMQP_HEADER)) {
                getSender().send(protocolHeader);
            }
            _connectionState = ConnectionState.AWAIT_OPEN;
            _frameHandler = getFrameHandler(false);
        } else {
            LOGGER.warn("{} : unknown AMQP header {}", getLogSubject(), Functions.str(header));
            _connectionState = ConnectionState.CLOSED;
            getNetwork().close();
        }
    }
}
Also used : Symbol(org.apache.qpid.server.protocol.v1_0.type.Symbol) SASLFrame(org.apache.qpid.server.protocol.v1_0.framing.SASLFrame) ArrayList(java.util.ArrayList) SaslMechanisms(org.apache.qpid.server.protocol.v1_0.type.security.SaslMechanisms) AuthenticatedPrincipal(org.apache.qpid.server.security.auth.AuthenticatedPrincipal) AnonymousAuthenticationManager(org.apache.qpid.server.security.auth.manager.AnonymousAuthenticationManager) ConnectionScopedRuntimeException(org.apache.qpid.server.util.ConnectionScopedRuntimeException) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) Futures.allAsList(com.google.common.util.concurrent.Futures.allAsList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 35 with Header

use of org.apache.qpid.server.protocol.v1_0.type.messaging.Header 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(convertMessageId(correlationId));
        } else {
            props.setCorrelationId(correlationIdAsBytes);
        }
    }
    final AMQShortString messageId = contentHeader.getMessageId();
    if (messageId != null) {
        props.setMessageId(convertMessageId(messageId));
    }
    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<>(contentHeader.getHeadersAsMap());
    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)

Aggregations

Header (org.apache.qpid.server.protocol.v1_0.type.messaging.Header)45 Test (org.junit.Test)44 Message_1_0 (org.apache.qpid.server.protocol.v1_0.Message_1_0)32 DeliveryProperties (org.apache.qpid.server.protocol.v0_10.transport.DeliveryProperties)19 MessageTransferMessage (org.apache.qpid.server.protocol.v0_10.MessageTransferMessage)18 Properties (org.apache.qpid.server.protocol.v1_0.type.messaging.Properties)17 QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)16 ApplicationProperties (org.apache.qpid.server.protocol.v1_0.type.messaging.ApplicationProperties)14 BasicContentHeaderProperties (org.apache.qpid.server.protocol.v0_8.transport.BasicContentHeaderProperties)13 MessageAnnotations (org.apache.qpid.server.protocol.v1_0.type.messaging.MessageAnnotations)13 DeliveryAnnotations (org.apache.qpid.server.protocol.v1_0.type.messaging.DeliveryAnnotations)12 MessageProperties (org.apache.qpid.server.protocol.v0_10.transport.MessageProperties)11 AMQMessage (org.apache.qpid.server.protocol.v0_8.AMQMessage)11 Date (java.util.Date)9 InternalMessage (org.apache.qpid.server.message.internal.InternalMessage)8 MessageMetaData_1_0 (org.apache.qpid.server.protocol.v1_0.MessageMetaData_1_0)8 Binary (org.apache.qpid.server.protocol.v1_0.type.Binary)8 Footer (org.apache.qpid.server.protocol.v1_0.type.messaging.Footer)7 ArrayList (java.util.ArrayList)6 Symbol (org.apache.qpid.server.protocol.v1_0.type.Symbol)6