Search in sources :

Example 41 with TypedProperties

use of org.apache.activemq.artemis.utils.collections.TypedProperties 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)

Example 42 with TypedProperties

use of org.apache.activemq.artemis.utils.collections.TypedProperties in project activemq-artemis by apache.

the class AMQPMessagePersisterV2 method encode.

/**
 * Sub classes must add the first short as the protocol-id
 */
@Override
public void encode(ActiveMQBuffer buffer, Message record) {
    super.encode(buffer, record);
    TypedProperties properties = ((AMQPMessage) record).getExtraProperties();
    if (properties == null) {
        buffer.writeInt(0);
    } else {
        buffer.writeInt(properties.getEncodeSize());
        properties.encode(buffer.byteBuf());
    }
}
Also used : TypedProperties(org.apache.activemq.artemis.utils.collections.TypedProperties)

Example 43 with TypedProperties

use of org.apache.activemq.artemis.utils.collections.TypedProperties in project activemq-artemis by apache.

the class AMQPMessagePersisterV2 method decode.

@Override
public Message decode(ActiveMQBuffer buffer, Message record) {
    AMQPMessage message = (AMQPMessage) super.decode(buffer, record);
    int size = buffer.readInt();
    if (size != 0) {
        TypedProperties properties = new TypedProperties();
        properties.decode(buffer.byteBuf());
        message.setExtraProperties(properties);
    }
    return message;
}
Also used : TypedProperties(org.apache.activemq.artemis.utils.collections.TypedProperties)

Aggregations

TypedProperties (org.apache.activemq.artemis.utils.collections.TypedProperties)43 Notification (org.apache.activemq.artemis.core.server.management.Notification)24 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)23 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)8 Test (org.junit.Test)5 ActiveMQInterruptedException (org.apache.activemq.artemis.api.core.ActiveMQInterruptedException)3 ByteBuf (io.netty.buffer.ByteBuf)2 Channel (io.netty.channel.Channel)2 ServerChannel (io.netty.channel.ServerChannel)2 EpollServerSocketChannel (io.netty.channel.epoll.EpollServerSocketChannel)2 KQueueServerSocketChannel (io.netty.channel.kqueue.KQueueServerSocketChannel)2 LocalServerChannel (io.netty.channel.local.LocalServerChannel)2 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)2 ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)2 ActiveMQIllegalStateException (org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException)2 ActiveMQNonExistentQueueException (org.apache.activemq.artemis.api.core.ActiveMQNonExistentQueueException)2 ClusterConnection (org.apache.activemq.artemis.core.server.cluster.ClusterConnection)2 Connection (org.apache.activemq.artemis.spi.core.remoting.Connection)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1