Java Code Examples for org.apache.qpid.proton.message.Message#decode()

The following examples show how to use org.apache.qpid.proton.message.Message#decode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AmqpRawMessageConverter.java    From strimzi-kafka-bridge with Apache License 2.0 6 votes vote down vote up
@Override
public Message toMessage(String address, KafkaConsumerRecord<String, byte[]> record) {

    Message message = Proton.message();
    message.setAddress(address);

    message.decode(record.value(), 0, record.value().length);

    // put message annotations about partition, offset and key (if not null)
    Map<Symbol, Object> map = new HashMap<>();
    map.put(Symbol.valueOf(AmqpBridge.AMQP_PARTITION_ANNOTATION), record.partition());
    map.put(Symbol.valueOf(AmqpBridge.AMQP_OFFSET_ANNOTATION), record.offset());
    map.put(Symbol.valueOf(AmqpBridge.AMQP_KEY_ANNOTATION), record.key());
    map.put(Symbol.valueOf(AmqpBridge.AMQP_TOPIC_ANNOTATION), record.topic());

    MessageAnnotations messageAnnotations = new MessageAnnotations(map);
    message.setMessageAnnotations(messageAnnotations);

    return message;
}
 
Example 2
Source File: AmqpReceiver.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
protected Message decodeIncomingMessage(Delivery incoming) {
   int count;

   byte[] chunk = new byte[2048];
   ByteArrayOutputStream stream = new ByteArrayOutputStream();

   while ((count = getEndpoint().recv(chunk, 0, chunk.length)) > 0) {
      stream.write(chunk, 0, count);
   }

   byte[] messageBytes = stream.toByteArray();

   try {
      Message protonMessage = Message.Factory.create();
      protonMessage.decode(messageBytes, 0, messageBytes.length);
      return protonMessage;
   } finally {
      try {
         stream.close();
      } catch (IOException e) {
      }
   }
}
 
Example 3
Source File: AmqpJmsTextMessageFacadeTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetTextUsingReceivedMessageWithDataSectionContainingStringBytes() throws Exception {
    String encodedString = "myEncodedString";
    byte[] encodedBytes = encodedString.getBytes(Charset.forName("UTF-8"));

    org.apache.qpid.proton.codec.Data payloadData = org.apache.qpid.proton.codec.Data.Factory.create();
    payloadData.putDescribedType(new DataDescribedType(new Binary(encodedBytes)));
    Binary b = payloadData.encode();

    Message message = Message.Factory.create();
    int decoded = message.decode(b.getArray(), b.getArrayOffset(), b.getLength());
    assertEquals(decoded, b.getLength());
    AmqpJmsTextMessageFacade amqpTextMessageFacade = createReceivedTextMessageFacade(createMockAmqpConsumer(), message);

    assertEquals(encodedString, amqpTextMessageFacade.getText());
}
 
Example 4
Source File: AmqpJmsMessageFacadeTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetCorrelationIdBytesOnReceievedMessageWithBinaryId() throws Exception {
    Binary testCorrelationId = createBinaryId();
    byte[] bytes = testCorrelationId.getArray();

    Data payloadData = Data.Factory.create();
    PropertiesDescribedType props = new PropertiesDescribedType();
    props.setCorrelationId(new Binary(bytes));
    payloadData.putDescribedType(props);
    Binary b = payloadData.encode();

    System.out.println("Using encoded AMQP message payload: " + b);

    Message message = Proton.message();
    int decoded = message.decode(b.getArray(), b.getArrayOffset(), b.getLength());
    assertEquals(decoded, b.getLength());

    AmqpJmsMessageFacade amqpMessageFacade = createReceivedMessageFacade(createMockAmqpConsumer(), message);

    assertEquals("Unexpected correlationId value on underlying AMQP message", testCorrelationId, amqpMessageFacade.getProperties().getCorrelationId());
    assertArrayEquals("Expected correlationId bytes not returned", bytes, amqpMessageFacade.getCorrelationIdBytes());
}
 
Example 5
Source File: MessageAnnotationsInjectExtractAdapterTest.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Verifies that the same entries injected via the {@code MessageAnnotationsInjectAdapter} are extracted via the
 * {@code MessageAnnotationsExtractAdapter}.
 * Also verifies that there are no errors during encoding/decoding of the message with the injected entries.
 */
@Test
public void testInjectAndExtract() {
    final Map<String, String> testEntries = new HashMap<>();
    testEntries.put("key1", "value1");
    testEntries.put("key2", "value2");

    final Message message = ProtonHelper.message();
    // inject the properties
    final MessageAnnotationsInjectAdapter injectAdapter = new MessageAnnotationsInjectAdapter(message, propertiesMapName);
    testEntries.forEach((key, value) -> {
        injectAdapter.put(key, value);
    });

    // encode the message
    final WritableBuffer.ByteBufferWrapper buffer = WritableBuffer.ByteBufferWrapper.allocate(100);
    message.encode(buffer);

    // decode the message
    final Message decodedMessage = ProtonHelper.message();
    decodedMessage.decode(buffer.toReadableBuffer());
    // extract the properties from the decoded message
    final MessageAnnotationsExtractAdapter extractAdapter = new MessageAnnotationsExtractAdapter(decodedMessage, propertiesMapName);
    extractAdapter.iterator().forEachRemaining(extractedEntry -> {
        assertThat(extractedEntry.getValue()).isEqualTo(testEntries.get(extractedEntry.getKey()));
    });
}
 
Example 6
Source File: AmqpMessageCoder.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public Message decode(InputStream inStream) throws CoderException, IOException {
  Message message = Message.Factory.create();
  int bytesToRead = VarInt.decodeInt(inStream);
  byte[] encodedMessage = new byte[bytesToRead];
  ByteStreams.readFully(inStream, encodedMessage);
  message.decode(encodedMessage, 0, encodedMessage.length);
  return message;
}
 
Example 7
Source File: Recv.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public void onDelivery(Event event) {
    Receiver recv = (Receiver)event.getLink();
    Delivery delivery = recv.current();
    if (delivery.isReadable() && !delivery.isPartial()) {
        int size = delivery.pending();
        byte[] buffer = new byte[size];
        int read = recv.recv(buffer, 0, buffer.length);
        recv.advance();

        Message msg = Proton.message();
        msg.decode(buffer, 0, read);
        System.out.println(((AmqpValue)msg.getBody()).getValue());
    }
}
 
Example 8
Source File: DeferredSettlementTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
private Delivery receiveMessageFromServer(String deliveryTag, int count)
{
    Delivery delivery = getClient().getConnection().getWorkHead();
    Receiver clientReceiver = getClient().getReceiver();

    assertTrue(Arrays.equals(deliveryTag.getBytes(StandardCharsets.UTF_8), delivery.getTag()));
    assertEquals("The received delivery should be on our receiver",
            clientReceiver, delivery.getLink());

    assertNull(delivery.getLocalState());
    assertNull(delivery.getRemoteState());

    assertFalse(delivery.isPartial());
    assertTrue(delivery.isReadable());

    int size = delivery.available();
    byte[] received = new byte[size];

    int len = clientReceiver.recv(received, 0, size);

    assertEquals("Should have received " + size + " bytes", size, len);
    assertEquals("Should be no bytes left", 0, delivery.available());

    Message m = Proton.message();
    m.decode(received, 0, len);

    Object messageBody = ((AmqpValue)m.getBody()).getValue();
    assertEquals("Unexpected message content", count, messageBody);

    boolean receiverAdvanced = clientReceiver.advance();
    assertTrue("receiver has not advanced", receiverAdvanced);

    delivery.setContext(count);

    return delivery;
}
 
Example 9
Source File: LinkTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
private Delivery receiveMessageFromServer(String deliveryTag, String messageContent)
{
    Delivery delivery = getClient().connection.getWorkHead();

    assertTrue(Arrays.equals(deliveryTag.getBytes(StandardCharsets.UTF_8), delivery.getTag()));
    assertEquals("The received delivery should be on our receiver",
                        getClient().receiver, delivery.getLink());

    assertNull(delivery.getLocalState());
    assertNull(delivery.getRemoteState());

    assertFalse(delivery.isPartial());
    assertTrue(delivery.isReadable());

    byte[] received = new byte[BUFFER_SIZE];
    int len = getClient().receiver.recv(received, 0, BUFFER_SIZE);

    assertTrue("given array was too small", len < BUFFER_SIZE);

    Message m = Proton.message();
    m.decode(received, 0, len);

    Object messageBody = ((AmqpValue)m.getBody()).getValue();
    assertEquals("Unexpected message content", messageContent, messageBody);

    boolean receiverAdvanced = getClient().receiver.advance();
    assertTrue("receiver has not advanced", receiverAdvanced);

    return delivery;
}
 
Example 10
Source File: DeliveryTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
private Delivery receiveMessageFromServer(String deliveryTag, String messageContent)
{
    Delivery delivery = getClient().connection.getWorkHead();

    assertTrue(Arrays.equals(deliveryTag.getBytes(StandardCharsets.UTF_8), delivery.getTag()));
    assertEquals("The received delivery should be on our receiver",
                        getClient().receiver, delivery.getLink());

    assertNull(delivery.getLocalState());
    assertNull(delivery.getRemoteState());

    assertFalse(delivery.isPartial());
    assertTrue(delivery.isReadable());

    final int BUFFER_SIZE = messageContent.length() * 4;
    byte[] received = new byte[BUFFER_SIZE];
    int len = getClient().receiver.recv(received, 0, BUFFER_SIZE);

    assertTrue("given array was too small", len < BUFFER_SIZE);

    Message m = Proton.message();
    m.decode(received, 0, len);

    Object messageBody = ((AmqpValue)m.getBody()).getValue();
    assertEquals("Unexpected message content", messageContent, messageBody);

    boolean receiverAdvanced = getClient().receiver.advance();
    assertTrue("receiver has not advanced", receiverAdvanced);

    return delivery;
}
 
Example 11
Source File: DefaultDeliveryStateTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
private Delivery receiveMessageFromServer(String deliveryTag, String messageContent)
{
    Delivery delivery = getClient().connection.getWorkHead();

    assertTrue(Arrays.equals(deliveryTag.getBytes(StandardCharsets.UTF_8), delivery.getTag()));
    assertEquals("The received delivery should be on our receiver",
                        getClient().receiver, delivery.getLink());

    assertNull(delivery.getLocalState());
    assertNull(delivery.getRemoteState());

    assertFalse(delivery.isPartial());
    assertTrue(delivery.isReadable());

    byte[] received = new byte[BUFFER_SIZE];
    int len = getClient().receiver.recv(received, 0, BUFFER_SIZE);

    assertTrue("given array was too small", len < BUFFER_SIZE);

    Message m = Proton.message();
    m.decode(received, 0, len);

    Object messageBody = ((AmqpValue)m.getBody()).getValue();
    assertEquals("Unexpected message content", messageContent, messageBody);

    boolean receiverAdvanced = getClient().receiver.advance();
    assertTrue("receiver has not advanced", receiverAdvanced);

    return delivery;
}
 
Example 12
Source File: InteropTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
Message decodeMessage(String name) throws IOException
{
    byte[] data = getBytes(name);
    Message m = Proton.message();
    m.decode(data, 0, data.length);
    return m;
}
 
Example 13
Source File: AmqpJmsTextMessageFacadeTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetTextUsingReceivedMessageWithZeroLengthDataSectionReturnsEmptyString() throws Exception {
    org.apache.qpid.proton.codec.Data payloadData = org.apache.qpid.proton.codec.Data.Factory.create();
    payloadData.putDescribedType(new DataDescribedType(new Binary(new byte[0])));
    Binary b = payloadData.encode();

    Message message = Message.Factory.create();
    int decoded = message.decode(b.getArray(), b.getArrayOffset(), b.getLength());
    assertEquals(decoded, b.getLength());
    AmqpJmsTextMessageFacade amqpTextMessageFacade = createReceivedTextMessageFacade(createMockAmqpConsumer(), message);

    assertEquals("expected zero-length string", "", amqpTextMessageFacade.getText());
}
 
Example 14
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 3 votes vote down vote up
private Delivery verifyDelivery(Receiver receiver, String deliveryTag, String messageContent)
{
    Delivery delivery = receiver.current();

    assertTrue(Arrays.equals(deliveryTag.getBytes(StandardCharsets.UTF_8), delivery.getTag()));

    assertNull(delivery.getLocalState());
    assertNull(delivery.getRemoteState());

    assertFalse(delivery.isPartial());
    assertTrue(delivery.isReadable());

    byte[] received = new byte[BUFFER_SIZE];
    int len = receiver.recv(received, 0, BUFFER_SIZE);

    assertTrue("given array was too small", len < BUFFER_SIZE);

    Message m = Proton.message();
    m.decode(received, 0, len);

    Object messageBody = ((AmqpValue)m.getBody()).getValue();
    assertEquals("Unexpected message content", messageContent, messageBody);

    boolean receiverAdvanced = receiver.advance();
    assertTrue("receiver has not advanced", receiverAdvanced);

    return delivery;
}
 
Example 15
Source File: AmqpMessageSupport.java    From qpid-jms with Apache License 2.0 3 votes vote down vote up
/**
 * Given a byte buffer that represents an encoded AMQP Message instance,
 * decode and return the Message.
 *
 * @param encodedBytes
 *      the bytes that represent an encoded AMQP Message.
 *
 * @return a new Message instance with the decoded data.
 */
public static Message decodeMessage(ByteBuf encodedBytes) {
    // For now we must fully decode the message to get at the annotations.
    Message protonMessage = Message.Factory.create();
    protonMessage.decode(encodedBytes.array(), 0, encodedBytes.readableBytes());
    return protonMessage;
}