Search in sources :

Example 11 with MessageMetaData_1_0

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

use of org.apache.qpid.server.protocol.v1_0.MessageMetaData_1_0 in project qpid-broker-j by apache.

the class MessageConverter_v1_0_to_InternalTest method createTestMessage.

private Message_1_0 createTestMessage(final Header header, final DeliveryAnnotations deliveryAnnotations, final MessageAnnotations messageAnnotations, final Properties properties, final ApplicationProperties applicationProperties, final long arrivalTime, final EncodingRetainingSection section) {
    final StoredMessage<MessageMetaData_1_0> storedMessage = mock(StoredMessage.class);
    MessageMetaData_1_0 metaData = new MessageMetaData_1_0(header.createEncodingRetainingSection(), deliveryAnnotations.createEncodingRetainingSection(), messageAnnotations.createEncodingRetainingSection(), properties.createEncodingRetainingSection(), applicationProperties.createEncodingRetainingSection(), new Footer(Collections.emptyMap()).createEncodingRetainingSection(), arrivalTime, 0);
    when(storedMessage.getMetaData()).thenReturn(metaData);
    if (section != null) {
        // TODO this is leaking QBBs
        final QpidByteBuffer combined = section.getEncodedForm();
        when(storedMessage.getContentSize()).thenReturn((int) section.getEncodedSize());
        final ArgumentCaptor<Integer> offsetCaptor = ArgumentCaptor.forClass(Integer.class);
        final ArgumentCaptor<Integer> sizeCaptor = ArgumentCaptor.forClass(Integer.class);
        when(storedMessage.getContent(offsetCaptor.capture(), sizeCaptor.capture())).then(invocation -> combined.view(offsetCaptor.getValue(), sizeCaptor.getValue()));
    } else {
        when(storedMessage.getContent(0, 0)).thenReturn(QpidByteBuffer.emptyQpidByteBuffer());
    }
    return new Message_1_0(storedMessage);
}
Also used : Footer(org.apache.qpid.server.protocol.v1_0.type.messaging.Footer) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer)

Example 13 with MessageMetaData_1_0

use of org.apache.qpid.server.protocol.v1_0.MessageMetaData_1_0 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 14 with MessageMetaData_1_0

use of org.apache.qpid.server.protocol.v1_0.MessageMetaData_1_0 in project qpid-broker-j by apache.

the class MessageConverter_1_0_to_v0_10 method getApplicationHeaders.

private Map<String, Object> getApplicationHeaders(final Message_1_0 serverMsg) {
    final MessageMetaData_1_0.MessageHeader_1_0 origHeader = serverMsg.getMessageHeader();
    final Map<String, Object> applicationProperties = serverMsg.getMessageHeader().getHeadersAsMap();
    for (String key : applicationProperties.keySet()) {
        ensureStr8("application-properties[\"" + key + "\"]", key);
    }
    Map<String, Object> appHeaders = new LinkedHashMap((Map<String, Object>) convertValue(applicationProperties));
    if (origHeader.getSubject() != null) {
        if (!appHeaders.containsKey("qpid.subject")) {
            appHeaders.put("qpid.subject", origHeader.getSubject());
        }
        if (!appHeaders.containsKey("x-jms-type")) {
            appHeaders.put("x-jms-type", origHeader.getSubject());
        }
    }
    String groupId = getGroupId(serverMsg);
    if (groupId != null && !appHeaders.containsKey("JMSXGroupID")) {
        appHeaders.put("JMSXGroupID", groupId);
    }
    UnsignedInteger groupSequence = getGroupSequence(serverMsg);
    if (groupSequence != null && !appHeaders.containsKey("JMSXGroupSeq")) {
        appHeaders.put("JMSXGroupSeq", groupSequence.intValue());
    }
    return appHeaders;
}
Also used : MessageMetaData_1_0(org.apache.qpid.server.protocol.v1_0.MessageMetaData_1_0) UnsignedInteger(org.apache.qpid.server.protocol.v1_0.type.UnsignedInteger) LinkedHashMap(java.util.LinkedHashMap)

Example 15 with MessageMetaData_1_0

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

Aggregations

MessageMetaData_1_0 (org.apache.qpid.server.protocol.v1_0.MessageMetaData_1_0)9 Footer (org.apache.qpid.server.protocol.v1_0.type.messaging.Footer)7 QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)6 Date (java.util.Date)5 Binary (org.apache.qpid.server.protocol.v1_0.type.Binary)5 DataSection (org.apache.qpid.server.protocol.v1_0.type.messaging.DataSection)5 MessageConversionException (org.apache.qpid.server.protocol.converter.MessageConversionException)4 Message_1_0 (org.apache.qpid.server.protocol.v1_0.Message_1_0)4 UnsignedInteger (org.apache.qpid.server.protocol.v1_0.type.UnsignedInteger)4 ApplicationProperties (org.apache.qpid.server.protocol.v1_0.type.messaging.ApplicationProperties)4 MessageAnnotations (org.apache.qpid.server.protocol.v1_0.type.messaging.MessageAnnotations)4 Properties (org.apache.qpid.server.protocol.v1_0.type.messaging.Properties)4 LinkedHashMap (java.util.LinkedHashMap)3 Symbol (org.apache.qpid.server.protocol.v1_0.type.Symbol)3 Header (org.apache.qpid.server.protocol.v1_0.type.messaging.Header)3 DeliveryProperties (org.apache.qpid.server.protocol.v0_10.transport.DeliveryProperties)2 MessageProperties (org.apache.qpid.server.protocol.v0_10.transport.MessageProperties)2 AMQShortString (org.apache.qpid.server.protocol.v0_8.AMQShortString)2 BasicContentHeaderProperties (org.apache.qpid.server.protocol.v0_8.transport.BasicContentHeaderProperties)2 MessagePublishInfo (org.apache.qpid.server.protocol.v0_8.transport.MessagePublishInfo)2