Search in sources :

Example 6 with Header

use of org.apache.qpid.proton.amqp.messaging.Header in project activemq-artemis by apache.

the class CoreAmqpConverter method fromCore.

public static AMQPMessage fromCore(ICoreMessage coreMessage) throws Exception {
    if (coreMessage == null) {
        return null;
    }
    ServerJMSMessage message = ServerJMSMessage.wrapCoreMessage(coreMessage);
    message.decode();
    long messageFormat = 0;
    Header header = null;
    final Properties properties = new Properties();
    Map<Symbol, Object> daMap = null;
    final Map<Symbol, Object> maMap = new HashMap<>();
    Map<String, Object> apMap = null;
    Map<Object, Object> footerMap = null;
    Section body = convertBody(message, maMap, properties);
    if (message.getInnerMessage().isDurable()) {
        if (header == null) {
            header = new Header();
        }
        header.setDurable(true);
    }
    byte priority = (byte) message.getJMSPriority();
    if (priority != javax.jms.Message.DEFAULT_PRIORITY) {
        if (header == null) {
            header = new Header();
        }
        header.setPriority(UnsignedByte.valueOf(priority));
    }
    String type = message.getJMSType();
    if (type != null) {
        properties.setSubject(type);
    }
    String messageId = message.getJMSMessageID();
    if (messageId != null) {
        try {
            properties.setMessageId(AMQPMessageIdHelper.INSTANCE.toIdObject(messageId));
        } catch (ActiveMQAMQPIllegalStateException e) {
            properties.setMessageId(messageId);
        }
    }
    Destination destination = message.getJMSDestination();
    if (destination != null) {
        properties.setTo(toAddress(destination));
        maMap.put(JMS_DEST_TYPE_MSG_ANNOTATION, destinationType(destination));
    }
    Destination replyTo = message.getJMSReplyTo();
    if (replyTo != null) {
        properties.setReplyTo(toAddress(replyTo));
        maMap.put(JMS_REPLY_TO_TYPE_MSG_ANNOTATION, destinationType(replyTo));
    }
    String correlationId = message.getJMSCorrelationID();
    if (correlationId != null) {
        try {
            properties.setCorrelationId(AMQPMessageIdHelper.INSTANCE.toIdObject(correlationId));
        } catch (ActiveMQAMQPIllegalStateException e) {
            properties.setCorrelationId(correlationId);
        }
    }
    long expiration = message.getJMSExpiration();
    if (expiration != 0) {
        long ttl = expiration - System.currentTimeMillis();
        if (ttl < 0) {
            ttl = 1;
        }
        if (header == null) {
            header = new Header();
        }
        header.setTtl(new UnsignedInteger((int) ttl));
        properties.setAbsoluteExpiryTime(new Date(expiration));
    }
    long timeStamp = message.getJMSTimestamp();
    if (timeStamp != 0) {
        properties.setCreationTime(new Date(timeStamp));
    }
    final Set<String> keySet = MessageUtil.getPropertyNames(message.getInnerMessage());
    for (String key : keySet) {
        if (key.startsWith("JMSX")) {
            if (key.equals("JMSXUserID")) {
                String value = message.getStringProperty(key);
                properties.setUserId(new Binary(value.getBytes(StandardCharsets.UTF_8)));
                continue;
            } else if (key.equals("JMSXGroupID")) {
                String value = message.getStringProperty(key);
                properties.setGroupId(value);
                continue;
            } else if (key.equals("JMSXGroupSeq")) {
                UnsignedInteger value = new UnsignedInteger(message.getIntProperty(key));
                properties.setGroupSequence(value);
                continue;
            }
        } else if (key.startsWith(JMS_AMQP_PREFIX)) {
            // AMQP Message Information stored from a conversion to the Core Message
            if (key.equals(JMS_AMQP_NATIVE)) {
                // skip..internal use only
                continue;
            } else if (key.equals(JMS_AMQP_FIRST_ACQUIRER)) {
                if (header == null) {
                    header = new Header();
                }
                header.setFirstAcquirer(message.getBooleanProperty(key));
                continue;
            } else if (key.equals(JMS_AMQP_HEADER)) {
                if (header == null) {
                    header = new Header();
                }
                continue;
            } else if (key.equals(JMS_AMQP_HEADER_DURABLE)) {
                if (header == null) {
                    header = new Header();
                }
                header.setDurable(message.getInnerMessage().isDurable());
                continue;
            } else if (key.equals(JMS_AMQP_HEADER_PRIORITY)) {
                if (header == null) {
                    header = new Header();
                }
                header.setPriority(UnsignedByte.valueOf(priority));
                continue;
            } else if (key.startsWith(JMS_AMQP_PROPERTIES)) {
                continue;
            } else if (key.startsWith(JMS_AMQP_DELIVERY_ANNOTATION_PREFIX)) {
                if (daMap == null) {
                    daMap = new HashMap<>();
                }
                String name = key.substring(JMS_AMQP_DELIVERY_ANNOTATION_PREFIX.length());
                daMap.put(Symbol.valueOf(name), message.getObjectProperty(key));
                continue;
            } else if (key.startsWith(JMS_AMQP_MESSAGE_ANNOTATION_PREFIX)) {
                String name = key.substring(JMS_AMQP_MESSAGE_ANNOTATION_PREFIX.length());
                maMap.put(Symbol.valueOf(name), message.getObjectProperty(key));
                continue;
            } else if (key.equals(JMS_AMQP_CONTENT_TYPE)) {
                properties.setContentType(Symbol.getSymbol(message.getStringProperty(key)));
                continue;
            } else if (key.equals(JMS_AMQP_CONTENT_ENCODING)) {
                properties.setContentEncoding(Symbol.getSymbol(message.getStringProperty(key)));
                continue;
            } else if (key.equals(JMS_AMQP_REPLYTO_GROUP_ID)) {
                properties.setReplyToGroupId(message.getStringProperty(key));
                continue;
            } else if (key.startsWith(JMS_AMQP_FOOTER_PREFIX)) {
                if (footerMap == null) {
                    footerMap = new HashMap<>();
                }
                String name = key.substring(JMS_AMQP_FOOTER_PREFIX.length());
                footerMap.put(name, message.getObjectProperty(key));
                continue;
            }
        } else if (key.equals("_AMQ_GROUP_ID")) {
            String value = message.getStringProperty(key);
            properties.setGroupId(value);
            continue;
        } else if (key.equals(NATIVE_MESSAGE_ID)) {
            // skip..internal use only
            continue;
        } else if (key.endsWith(HDR_SCHEDULED_DELIVERY_TIME.toString())) {
            // skip..remove annotation from previous inbound transformation
            continue;
        }
        if (apMap == null) {
            apMap = new HashMap<>();
        }
        Object objectProperty = message.getObjectProperty(key);
        if (objectProperty instanceof byte[]) {
            objectProperty = new Binary((byte[]) objectProperty);
        }
        apMap.put(key, objectProperty);
    }
    ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(1024);
    try {
        EncoderImpl encoder = TLSEncode.getEncoder();
        encoder.setByteBuffer(new NettyWritable(buffer));
        if (header != null) {
            encoder.writeObject(header);
        }
        if (daMap != null) {
            encoder.writeObject(new DeliveryAnnotations(daMap));
        }
        if (maMap != null) {
            encoder.writeObject(new MessageAnnotations(maMap));
        }
        if (properties != null) {
            encoder.writeObject(properties);
        }
        if (apMap != null) {
            encoder.writeObject(new ApplicationProperties(apMap));
        }
        if (body != null) {
            encoder.writeObject(body);
        }
        if (footerMap != null) {
            encoder.writeObject(new Footer(footerMap));
        }
        byte[] data = new byte[buffer.writerIndex()];
        buffer.readBytes(data);
        AMQPMessage amqpMessage = new AMQPMessage(messageFormat, data);
        amqpMessage.setMessageID(message.getInnerMessage().getMessageID());
        amqpMessage.setReplyTo(coreMessage.getReplyTo());
        return amqpMessage;
    } finally {
        TLSEncode.getEncoder().setByteBuffer((WritableBuffer) null);
        buffer.release();
    }
}
Also used : Destination(javax.jms.Destination) EncoderImpl(org.apache.qpid.proton.codec.EncoderImpl) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Symbol(org.apache.qpid.proton.amqp.Symbol) Properties(org.apache.qpid.proton.amqp.messaging.Properties) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) ByteBuf(io.netty.buffer.ByteBuf) NettyWritable(org.apache.activemq.artemis.protocol.amqp.util.NettyWritable) MessageAnnotations(org.apache.qpid.proton.amqp.messaging.MessageAnnotations) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) ActiveMQAMQPIllegalStateException(org.apache.activemq.artemis.protocol.amqp.exceptions.ActiveMQAMQPIllegalStateException) DeliveryAnnotations(org.apache.qpid.proton.amqp.messaging.DeliveryAnnotations) Section(org.apache.qpid.proton.amqp.messaging.Section) AMQPMessage(org.apache.activemq.artemis.protocol.amqp.broker.AMQPMessage) Date(java.util.Date) Header(org.apache.qpid.proton.amqp.messaging.Header) Footer(org.apache.qpid.proton.amqp.messaging.Footer) Binary(org.apache.qpid.proton.amqp.Binary) UnsignedInteger(org.apache.qpid.proton.amqp.UnsignedInteger) ServerJMSMessage(org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSMessage)

Example 7 with Header

use of org.apache.qpid.proton.amqp.messaging.Header in project activemq-artemis by apache.

the class AMQPMessage method partialDecode.

private synchronized void partialDecode(ByteBuffer buffer) {
    DecoderImpl decoder = TLSEncode.getDecoder();
    decoder.setByteBuffer(buffer);
    buffer.position(0);
    _header = null;
    _deliveryAnnotations = null;
    _messageAnnotations = null;
    _properties = null;
    applicationProperties = null;
    Section section = null;
    try {
        if (buffer.hasRemaining()) {
            section = (Section) decoder.readObject();
        }
        if (section instanceof Header) {
            _header = (Header) section;
            headerEnds = buffer.position();
            messagePaylodStart = headerEnds;
            if (_header.getTtl() != null) {
                this.expiration = System.currentTimeMillis() + _header.getTtl().intValue();
            }
            if (buffer.hasRemaining()) {
                section = (Section) decoder.readObject();
            } else {
                section = null;
            }
        } else {
            // meaning there is no header
            headerEnds = 0;
        }
        if (section instanceof DeliveryAnnotations) {
            _deliveryAnnotations = (DeliveryAnnotations) section;
            // Advance the start beyond the delivery annotations so they are not written
            // out on send of the message.
            messagePaylodStart = buffer.position();
            if (buffer.hasRemaining()) {
                section = (Section) decoder.readObject();
            } else {
                section = null;
            }
        }
        if (section instanceof MessageAnnotations) {
            _messageAnnotations = (MessageAnnotations) section;
            if (buffer.hasRemaining()) {
                section = (Section) decoder.readObject();
            } else {
                section = null;
            }
        }
        if (section instanceof Properties) {
            _properties = (Properties) section;
            if (_properties.getAbsoluteExpiryTime() != null && _properties.getAbsoluteExpiryTime().getTime() > 0) {
                this.expiration = _properties.getAbsoluteExpiryTime().getTime();
            }
            // We don't read the next section on purpose, as we will parse ApplicationProperties
            // lazily
            section = null;
        }
        if (section instanceof ApplicationProperties) {
            applicationProperties = (ApplicationProperties) section;
        } else {
            if (buffer.hasRemaining()) {
                this.appLocation = buffer.position();
            } else {
                this.appLocation = -1;
            }
        }
    } finally {
        decoder.setByteBuffer(null);
    }
}
Also used : DecoderImpl(org.apache.qpid.proton.codec.DecoderImpl) Header(org.apache.qpid.proton.amqp.messaging.Header) MessageAnnotations(org.apache.qpid.proton.amqp.messaging.MessageAnnotations) DeliveryAnnotations(org.apache.qpid.proton.amqp.messaging.DeliveryAnnotations) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) TypedProperties(org.apache.activemq.artemis.utils.collections.TypedProperties) Properties(org.apache.qpid.proton.amqp.messaging.Properties) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) Section(org.apache.qpid.proton.amqp.messaging.Section)

Example 8 with Header

use of org.apache.qpid.proton.amqp.messaging.Header in project activemq-artemis by apache.

the class AMQPMessage method sendBuffer.

@Override
public void sendBuffer(ByteBuf buffer, int deliveryCount) {
    checkBuffer();
    int amqpDeliveryCount = deliveryCount - 1;
    // otherwise we want to write the original header if present.
    if (amqpDeliveryCount > 0) {
        Header header = getHeader();
        if (header == null) {
            header = new Header();
            header.setDurable(durable);
        }
        synchronized (header) {
            header.setDeliveryCount(UnsignedInteger.valueOf(amqpDeliveryCount));
            TLSEncode.getEncoder().setByteBuffer(new NettyWritable(buffer));
            TLSEncode.getEncoder().writeObject(header);
            TLSEncode.getEncoder().setByteBuffer((WritableBuffer) null);
        }
    } else if (headerEnds > 0) {
        buffer.writeBytes(data, 0, headerEnds);
    }
    buffer.writeBytes(data, messagePaylodStart, data.writerIndex() - messagePaylodStart);
}
Also used : Header(org.apache.qpid.proton.amqp.messaging.Header) NettyWritable(org.apache.activemq.artemis.protocol.amqp.util.NettyWritable)

Example 9 with Header

use of org.apache.qpid.proton.amqp.messaging.Header in project activemq-artemis by apache.

the class AMQPMessage method createCopyWithNewDeliveryCount.

private ByteBuf createCopyWithNewDeliveryCount(int deliveryCount) {
    assert deliveryCount > 1;
    final int amqpDeliveryCount = deliveryCount - 1;
    // If the re-delivering the message then the header must be re-encoded
    // (or created if not previously present).  Any delivery annotations should
    // be skipped as well in the resulting buffer.
    final ByteBuf result = PooledByteBufAllocator.DEFAULT.heapBuffer(getEncodeSize());
    Header header = getHeader();
    if (header == null) {
        header = new Header();
        header.setDurable(durable);
    }
    synchronized (header) {
        // Updates or adds a Header section with the correct delivery count
        header.setDeliveryCount(UnsignedInteger.valueOf(amqpDeliveryCount));
        TLSEncode.getEncoder().setByteBuffer(new NettyWritable(result));
        TLSEncode.getEncoder().writeObject(header);
        TLSEncode.getEncoder().setByteBuffer((WritableBuffer) null);
    }
    // This will skip any existing delivery annotations that might have been present
    // in the original message.
    result.writeBytes(data, messagePaylodStart, data.writerIndex() - messagePaylodStart);
    return result;
}
Also used : Header(org.apache.qpid.proton.amqp.messaging.Header) ByteBuf(io.netty.buffer.ByteBuf) NettyWritable(org.apache.activemq.artemis.protocol.amqp.util.NettyWritable)

Example 10 with Header

use of org.apache.qpid.proton.amqp.messaging.Header in project activemq-artemis by apache.

the class AmqpCoreConverter method populateMessage.

@SuppressWarnings("unchecked")
protected static ServerJMSMessage populateMessage(ServerJMSMessage jms, org.apache.qpid.proton.message.Message amqp) throws Exception {
    Header header = amqp.getHeader();
    if (header != null) {
        jms.setBooleanProperty(JMS_AMQP_HEADER, true);
        if (header.getDurable() != null) {
            jms.setBooleanProperty(JMS_AMQP_HEADER_DURABLE, true);
            jms.setJMSDeliveryMode(header.getDurable().booleanValue() ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT);
        } else {
            jms.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
        }
        if (header.getPriority() != null) {
            jms.setBooleanProperty(JMS_AMQP_HEADER_PRIORITY, true);
            jms.setJMSPriority(header.getPriority().intValue());
        } else {
            jms.setJMSPriority(javax.jms.Message.DEFAULT_PRIORITY);
        }
        if (header.getFirstAcquirer() != null) {
            jms.setBooleanProperty(JMS_AMQP_FIRST_ACQUIRER, header.getFirstAcquirer());
        }
        if (header.getDeliveryCount() != null) {
            // AMQP Delivery Count counts only failed delivers where JMS
            // Delivery Count should include the original delivery in the count.
            jms.setLongProperty("JMSXDeliveryCount", header.getDeliveryCount().longValue() + 1);
        }
    } else {
        jms.setJMSPriority((byte) javax.jms.Message.DEFAULT_PRIORITY);
        jms.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
    }
    final MessageAnnotations ma = amqp.getMessageAnnotations();
    if (ma != null) {
        for (Map.Entry<?, ?> entry : ma.getValue().entrySet()) {
            String key = entry.getKey().toString();
            if ("x-opt-delivery-time".equals(key) && entry.getValue() != null) {
                long deliveryTime = ((Number) entry.getValue()).longValue();
                jms.setLongProperty(HDR_SCHEDULED_DELIVERY_TIME.toString(), deliveryTime);
            } else if ("x-opt-delivery-delay".equals(key) && entry.getValue() != null) {
                long delay = ((Number) entry.getValue()).longValue();
                if (delay > 0) {
                    jms.setLongProperty(HDR_SCHEDULED_DELIVERY_TIME.toString(), System.currentTimeMillis() + delay);
                }
            }
            setProperty(jms, JMS_AMQP_MESSAGE_ANNOTATION_PREFIX + key, entry.getValue());
        }
    }
    final ApplicationProperties ap = amqp.getApplicationProperties();
    if (ap != null) {
        for (Map.Entry<String, Object> entry : (Set<Map.Entry<String, Object>>) ap.getValue().entrySet()) {
            setProperty(jms, entry.getKey(), entry.getValue());
        }
    }
    final Properties properties = amqp.getProperties();
    if (properties != null) {
        if (properties.getMessageId() != null) {
            jms.setJMSMessageID(AMQPMessageIdHelper.INSTANCE.toMessageIdString(properties.getMessageId()));
        }
        Binary userId = properties.getUserId();
        if (userId != null) {
            jms.setStringProperty("JMSXUserID", new String(userId.getArray(), userId.getArrayOffset(), userId.getLength(), StandardCharsets.UTF_8));
        }
        if (properties.getTo() != null) {
            jms.setJMSDestination(new ServerDestination(properties.getTo()));
        }
        if (properties.getSubject() != null) {
            jms.setJMSType(properties.getSubject());
        }
        if (properties.getReplyTo() != null) {
            jms.setJMSReplyTo(new ServerDestination(properties.getReplyTo()));
        }
        if (properties.getCorrelationId() != null) {
            jms.setJMSCorrelationID(AMQPMessageIdHelper.INSTANCE.toCorrelationIdString(properties.getCorrelationId()));
        }
        if (properties.getContentType() != null) {
            jms.setStringProperty(JMS_AMQP_CONTENT_TYPE, properties.getContentType().toString());
        }
        if (properties.getContentEncoding() != null) {
            jms.setStringProperty(JMS_AMQP_CONTENT_ENCODING, properties.getContentEncoding().toString());
        }
        if (properties.getCreationTime() != null) {
            jms.setJMSTimestamp(properties.getCreationTime().getTime());
        }
        if (properties.getGroupId() != null) {
            jms.setStringProperty("_AMQ_GROUP_ID", properties.getGroupId());
        }
        if (properties.getGroupSequence() != null) {
            jms.setIntProperty("JMSXGroupSeq", properties.getGroupSequence().intValue());
        }
        if (properties.getReplyToGroupId() != null) {
            jms.setStringProperty(JMS_AMQP_REPLYTO_GROUP_ID, properties.getReplyToGroupId());
        }
        if (properties.getAbsoluteExpiryTime() != null) {
            jms.setJMSExpiration(properties.getAbsoluteExpiryTime().getTime());
        }
    }
    // If the jms expiration has not yet been set...
    if (header != null && jms.getJMSExpiration() == 0) {
        // Then lets try to set it based on the message ttl.
        long ttl = javax.jms.Message.DEFAULT_TIME_TO_LIVE;
        if (header.getTtl() != null) {
            ttl = header.getTtl().longValue();
        }
        if (ttl == 0) {
            jms.setJMSExpiration(0);
        } else {
            jms.setJMSExpiration(System.currentTimeMillis() + ttl);
        }
    }
    final Footer fp = amqp.getFooter();
    if (fp != null) {
        for (Map.Entry<Object, Object> entry : (Set<Map.Entry<Object, Object>>) fp.getValue().entrySet()) {
            String key = entry.getKey().toString();
            setProperty(jms, JMS_AMQP_FOOTER_PREFIX + key, entry.getValue());
        }
    }
    return jms;
}
Also used : Set(java.util.Set) ServerDestination(org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerDestination) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) TypedProperties(org.apache.activemq.artemis.utils.collections.TypedProperties) Properties(org.apache.qpid.proton.amqp.messaging.Properties) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) Header(org.apache.qpid.proton.amqp.messaging.Header) MessageAnnotations(org.apache.qpid.proton.amqp.messaging.MessageAnnotations) Footer(org.apache.qpid.proton.amqp.messaging.Footer) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) Binary(org.apache.qpid.proton.amqp.Binary) Map(java.util.Map)

Aggregations

Header (org.apache.qpid.proton.amqp.messaging.Header)19 Test (org.junit.Test)12 AMQPMessage (org.apache.activemq.artemis.protocol.amqp.broker.AMQPMessage)11 MessageImpl (org.apache.qpid.proton.message.impl.MessageImpl)10 Properties (org.apache.qpid.proton.amqp.messaging.Properties)8 ApplicationProperties (org.apache.qpid.proton.amqp.messaging.ApplicationProperties)7 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)5 UnsignedInteger (org.apache.qpid.proton.amqp.UnsignedInteger)5 HashMap (java.util.HashMap)4 NettyWritable (org.apache.activemq.artemis.protocol.amqp.util.NettyWritable)3 TypedProperties (org.apache.activemq.artemis.utils.collections.TypedProperties)3 DeliveryAnnotations (org.apache.qpid.proton.amqp.messaging.DeliveryAnnotations)3 MessageAnnotations (org.apache.qpid.proton.amqp.messaging.MessageAnnotations)3 ByteBuf (io.netty.buffer.ByteBuf)2 URI (java.net.URI)2 Date (java.util.Date)2 Connection (javax.jms.Connection)2 Message (javax.jms.Message)2 MessageConsumer (javax.jms.MessageConsumer)2 Session (javax.jms.Session)2