Search in sources :

Example 1 with ServerJMSMessage

use of org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSMessage 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 2 with ServerJMSMessage

use of org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSMessage in project activemq-artemis by apache.

the class CoreAmqpConverter method convertBody.

private static Section convertBody(ServerJMSMessage message, Map<Symbol, Object> maMap, Properties properties) throws JMSException {
    Section body = null;
    if (message instanceof ServerJMSBytesMessage) {
        Binary payload = getBinaryFromMessageBody((ServerJMSBytesMessage) message);
        maMap.put(AMQPMessageSupport.JMS_MSG_TYPE, AMQPMessageSupport.JMS_BYTES_MESSAGE);
        if (payload == null) {
            payload = EMPTY_BINARY;
        } else {
            body = new AmqpValue(payload);
        }
    } else if (message instanceof ServerJMSTextMessage) {
        body = new AmqpValue(((TextMessage) message).getText());
        maMap.put(AMQPMessageSupport.JMS_MSG_TYPE, AMQPMessageSupport.JMS_TEXT_MESSAGE);
    } else if (message instanceof ServerJMSMapMessage) {
        body = new AmqpValue(getMapFromMessageBody((ServerJMSMapMessage) message));
        maMap.put(AMQPMessageSupport.JMS_MSG_TYPE, AMQPMessageSupport.JMS_MAP_MESSAGE);
    } else if (message instanceof ServerJMSStreamMessage) {
        maMap.put(AMQPMessageSupport.JMS_MSG_TYPE, AMQPMessageSupport.JMS_STREAM_MESSAGE);
        ArrayList<Object> list = new ArrayList<>();
        final ServerJMSStreamMessage m = (ServerJMSStreamMessage) message;
        try {
            while (true) {
                list.add(m.readObject());
            }
        } catch (MessageEOFException e) {
        }
        body = new AmqpSequence(list);
    } else if (message instanceof ServerJMSObjectMessage) {
        properties.setContentType(AMQPMessageSupport.SERIALIZED_JAVA_OBJECT_CONTENT_TYPE);
        maMap.put(AMQPMessageSupport.JMS_MSG_TYPE, AMQPMessageSupport.JMS_OBJECT_MESSAGE);
        Binary payload = getBinaryFromMessageBody((ServerJMSObjectMessage) message);
        if (payload == null) {
            payload = EMPTY_BINARY;
        }
        body = new Data(payload);
        // we are sending it.
        if (!message.propertyExists(JMS_AMQP_CONTENT_TYPE)) {
            message.setStringProperty(JMS_AMQP_CONTENT_TYPE, SERIALIZED_JAVA_OBJECT_CONTENT_TYPE.toString());
        }
    } else if (message instanceof ServerJMSMessage) {
        maMap.put(AMQPMessageSupport.JMS_MSG_TYPE, AMQPMessageSupport.JMS_MESSAGE);
        // If this is not an AMQP message that was converted then the original encoding
        // will be unknown so we check for special cases of messages with special data
        // encoded into the server message body.
        ICoreMessage internalMessage = message.getInnerMessage();
        int readerIndex = internalMessage.getBodyBuffer().readerIndex();
        try {
            Object s = internalMessage.getBodyBuffer().readNullableSimpleString();
            if (s != null) {
                body = new AmqpValue(s.toString());
            }
        } catch (Throwable ignored) {
            logger.debug("Exception ignored during conversion", ignored.getMessage(), ignored);
            body = new AmqpValue("Conversion to AMQP error!");
        } finally {
            internalMessage.getBodyBuffer().readerIndex(readerIndex);
        }
    }
    return body;
}
Also used : MessageEOFException(javax.jms.MessageEOFException) ArrayList(java.util.ArrayList) ServerJMSObjectMessage(org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSObjectMessage) ServerJMSStreamMessage(org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSStreamMessage) Data(org.apache.qpid.proton.amqp.messaging.Data) Section(org.apache.qpid.proton.amqp.messaging.Section) AmqpSequence(org.apache.qpid.proton.amqp.messaging.AmqpSequence) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) ICoreMessage(org.apache.activemq.artemis.api.core.ICoreMessage) ServerJMSBytesMessage(org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSBytesMessage) ServerJMSTextMessage(org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSTextMessage) ServerJMSMapMessage(org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSMapMessage) Binary(org.apache.qpid.proton.amqp.Binary) ServerJMSMessage(org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSMessage)

Example 3 with ServerJMSMessage

use of org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSMessage in project activemq-artemis by apache.

the class AmqpCoreConverter method toCore.

@SuppressWarnings("unchecked")
public static ICoreMessage toCore(AMQPMessage message, CoreMessageObjectPools coreMessageObjectPools) throws Exception {
    Section body = message.getProtonMessage().getBody();
    ServerJMSMessage result;
    if (body == null) {
        if (isContentType(SERIALIZED_JAVA_OBJECT_CONTENT_TYPE.toString(), message.getProtonMessage())) {
            result = createObjectMessage(message.getMessageID(), coreMessageObjectPools);
        } else if (isContentType(OCTET_STREAM_CONTENT_TYPE, message.getProtonMessage()) || isContentType(null, message.getProtonMessage())) {
            result = createBytesMessage(message.getMessageID(), coreMessageObjectPools);
        } else {
            Charset charset = getCharsetForTextualContent(message.getProtonMessage().getContentType());
            if (charset != null) {
                result = createTextMessage(message.getMessageID(), coreMessageObjectPools);
            } else {
                result = createMessage(message.getMessageID(), coreMessageObjectPools);
            }
        }
    } else if (body instanceof Data) {
        Binary payload = ((Data) body).getValue();
        if (isContentType(SERIALIZED_JAVA_OBJECT_CONTENT_TYPE.toString(), message.getProtonMessage())) {
            result = createObjectMessage(message.getMessageID(), payload.getArray(), payload.getArrayOffset(), payload.getLength(), coreMessageObjectPools);
        } else if (isContentType(OCTET_STREAM_CONTENT_TYPE, message.getProtonMessage())) {
            result = createBytesMessage(message.getMessageID(), payload.getArray(), payload.getArrayOffset(), payload.getLength(), coreMessageObjectPools);
        } else {
            Charset charset = getCharsetForTextualContent(message.getProtonMessage().getContentType());
            if (StandardCharsets.UTF_8.equals(charset)) {
                ByteBuffer buf = ByteBuffer.wrap(payload.getArray(), payload.getArrayOffset(), payload.getLength());
                try {
                    CharBuffer chars = charset.newDecoder().decode(buf);
                    result = createTextMessage(message.getMessageID(), String.valueOf(chars), coreMessageObjectPools);
                } catch (CharacterCodingException e) {
                    result = createBytesMessage(message.getMessageID(), payload.getArray(), payload.getArrayOffset(), payload.getLength(), coreMessageObjectPools);
                }
            } else {
                result = createBytesMessage(message.getMessageID(), payload.getArray(), payload.getArrayOffset(), payload.getLength(), coreMessageObjectPools);
            }
        }
    } else if (body instanceof AmqpSequence) {
        AmqpSequence sequence = (AmqpSequence) body;
        ServerJMSStreamMessage m = createStreamMessage(message.getMessageID(), coreMessageObjectPools);
        for (Object item : sequence.getValue()) {
            m.writeObject(item);
        }
        result = m;
    } else if (body instanceof AmqpValue) {
        Object value = ((AmqpValue) body).getValue();
        if (value == null || value instanceof String) {
            result = createTextMessage(message.getMessageID(), (String) value, coreMessageObjectPools);
        } else if (value instanceof Binary) {
            Binary payload = (Binary) value;
            if (isContentType(SERIALIZED_JAVA_OBJECT_CONTENT_TYPE.toString(), message.getProtonMessage())) {
                result = createObjectMessage(message.getMessageID(), payload, coreMessageObjectPools);
            } else {
                result = createBytesMessage(message.getMessageID(), payload.getArray(), payload.getArrayOffset(), payload.getLength(), coreMessageObjectPools);
            }
        } else if (value instanceof List) {
            ServerJMSStreamMessage m = createStreamMessage(message.getMessageID(), coreMessageObjectPools);
            for (Object item : (List<Object>) value) {
                m.writeObject(item);
            }
            result = m;
        } else if (value instanceof Map) {
            result = createMapMessage(message.getMessageID(), (Map<String, Object>) value, coreMessageObjectPools);
        } else {
            ByteBuf buf = PooledByteBufAllocator.DEFAULT.heapBuffer(1024);
            try {
                TLSEncode.getEncoder().setByteBuffer(new NettyWritable(buf));
                TLSEncode.getEncoder().writeObject(body);
                result = createBytesMessage(message.getMessageID(), buf.array(), 0, buf.writerIndex(), coreMessageObjectPools);
            } finally {
                buf.release();
                TLSEncode.getEncoder().setByteBuffer((WritableBuffer) null);
            }
        }
    } else {
        throw new RuntimeException("Unexpected body type: " + body.getClass());
    }
    TypedProperties properties = message.getExtraProperties();
    if (properties != null) {
        for (SimpleString str : properties.getPropertyNames()) {
            result.getInnerMessage().putBytesProperty(str, properties.getBytesProperty(str));
        }
    }
    populateMessage(result, message.getProtonMessage());
    result.getInnerMessage().setReplyTo(message.getReplyTo());
    result.getInnerMessage().setDurable(message.isDurable());
    result.getInnerMessage().setPriority(message.getPriority());
    result.getInnerMessage().setAddress(message.getAddressSimpleString());
    result.encode();
    return result != null ? result.getInnerMessage() : null;
}
Also used : CharBuffer(java.nio.CharBuffer) Charset(java.nio.charset.Charset) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Data(org.apache.qpid.proton.amqp.messaging.Data) ServerJMSStreamMessage(org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSStreamMessage) CharacterCodingException(java.nio.charset.CharacterCodingException) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ByteBuf(io.netty.buffer.ByteBuf) TypedProperties(org.apache.activemq.artemis.utils.collections.TypedProperties) Section(org.apache.qpid.proton.amqp.messaging.Section) ByteBuffer(java.nio.ByteBuffer) AmqpSequence(org.apache.qpid.proton.amqp.messaging.AmqpSequence) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) NettyWritable(org.apache.activemq.artemis.protocol.amqp.util.NettyWritable) List(java.util.List) Binary(org.apache.qpid.proton.amqp.Binary) ServerJMSMessage(org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSMessage) Map(java.util.Map)

Aggregations

ServerJMSMessage (org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSMessage)3 Binary (org.apache.qpid.proton.amqp.Binary)3 Section (org.apache.qpid.proton.amqp.messaging.Section)3 ByteBuf (io.netty.buffer.ByteBuf)2 ServerJMSStreamMessage (org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSStreamMessage)2 NettyWritable (org.apache.activemq.artemis.protocol.amqp.util.NettyWritable)2 AmqpSequence (org.apache.qpid.proton.amqp.messaging.AmqpSequence)2 AmqpValue (org.apache.qpid.proton.amqp.messaging.AmqpValue)2 Data (org.apache.qpid.proton.amqp.messaging.Data)2 ByteBuffer (java.nio.ByteBuffer)1 CharBuffer (java.nio.CharBuffer)1 CharacterCodingException (java.nio.charset.CharacterCodingException)1 Charset (java.nio.charset.Charset)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Destination (javax.jms.Destination)1