Search in sources :

Example 6 with ServerJMSStreamMessage

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

ServerJMSStreamMessage (org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSStreamMessage)6 AmqpSequence (org.apache.qpid.proton.amqp.messaging.AmqpSequence)6 ServerJMSMessage (org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSMessage)5 ServerJMSBytesMessage (org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSBytesMessage)4 ServerJMSMapMessage (org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSMapMessage)4 ServerJMSObjectMessage (org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSObjectMessage)4 ServerJMSTextMessage (org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSTextMessage)4 Test (org.junit.Test)4 List (java.util.List)3 CoreMessage (org.apache.activemq.artemis.core.message.impl.CoreMessage)3 Message (org.apache.qpid.proton.message.Message)3 ICoreMessage (org.apache.activemq.artemis.api.core.ICoreMessage)2 Binary (org.apache.qpid.proton.amqp.Binary)2 AmqpValue (org.apache.qpid.proton.amqp.messaging.AmqpValue)2 Data (org.apache.qpid.proton.amqp.messaging.Data)2 Section (org.apache.qpid.proton.amqp.messaging.Section)2 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBuffer (java.nio.ByteBuffer)1 CharBuffer (java.nio.CharBuffer)1 CharacterCodingException (java.nio.charset.CharacterCodingException)1