Search in sources :

Example 6 with AmqpSequence

use of org.apache.qpid.proton.amqp.messaging.AmqpSequence 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 7 with AmqpSequence

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

the class JMSMappingInboundTransformerTest method testCreateAmqpStreamMessageFromAmqpSequence.

/**
 * Test that an amqp-sequence body containing a list results in an StreamMessage when not
 * otherwise annotated to indicate the type of JMS message it is.
 *
 * @throws Exception
 *         if an error occurs during the test.
 */
@Test
public void testCreateAmqpStreamMessageFromAmqpSequence() throws Exception {
    Message message = Proton.message();
    List<String> list = new ArrayList<>();
    message.setBody(new AmqpSequence(list));
    javax.jms.Message jmsMessage = ServerJMSMessage.wrapCoreMessage(new AMQPMessage(message).toCore());
    assertNotNull("Message should not be null", jmsMessage);
    assertEquals("Unexpected message class type", ServerJMSStreamMessage.class, jmsMessage.getClass());
}
Also used : ServerJMSTextMessage(org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSTextMessage) AMQPMessage(org.apache.activemq.artemis.protocol.amqp.broker.AMQPMessage) ServerJMSObjectMessage(org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSObjectMessage) Message(org.apache.qpid.proton.message.Message) TextMessage(javax.jms.TextMessage) ServerJMSMessage(org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSMessage) ServerJMSStreamMessage(org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSStreamMessage) ICoreMessage(org.apache.activemq.artemis.api.core.ICoreMessage) ServerJMSBytesMessage(org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSBytesMessage) ServerJMSMapMessage(org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSMapMessage) ArrayList(java.util.ArrayList) AmqpSequence(org.apache.qpid.proton.amqp.messaging.AmqpSequence) AMQPMessage(org.apache.activemq.artemis.protocol.amqp.broker.AMQPMessage) Test(org.junit.Test)

Example 8 with AmqpSequence

use of org.apache.qpid.proton.amqp.messaging.AmqpSequence in project azure-service-bus-java by Azure.

the class Util method getPayloadSize.

private static int getPayloadSize(Message msg) {
    if (msg == null || msg.getBody() == null) {
        return 0;
    }
    Section bodySection = msg.getBody();
    if (bodySection instanceof AmqpValue) {
        return Util.sizeof(((AmqpValue) bodySection).getValue());
    } else if (bodySection instanceof AmqpSequence) {
        return Util.sizeof(((AmqpSequence) bodySection).getValue());
    } else if (bodySection instanceof Data) {
        Data payloadSection = (Data) bodySection;
        Binary payloadBytes = payloadSection.getValue();
        return Util.sizeof(payloadBytes);
    } else {
        return 0;
    }
}
Also used : Data(org.apache.qpid.proton.amqp.messaging.Data) Binary(org.apache.qpid.proton.amqp.Binary) Section(org.apache.qpid.proton.amqp.messaging.Section) AmqpSequence(org.apache.qpid.proton.amqp.messaging.AmqpSequence) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue)

Aggregations

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