Java Code Examples for org.apache.qpid.proton.amqp.messaging.Data#getValue()

The following examples show how to use org.apache.qpid.proton.amqp.messaging.Data#getValue() . 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: AmqpMaxFrameSizeTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
private void verifyMessage(final AmqpMessage message, final int payloadSize) {
   MessageImpl wrapped = (MessageImpl) message.getWrappedMessage();

   assertNotNull("Message has no body", wrapped.getBody());
   assertTrue("Unexpected body type: " + wrapped.getBody().getClass(), wrapped.getBody() instanceof Data);

   Data data = (Data) wrapped.getBody();
   Binary binary = data.getValue();
   assertNotNull("Data section has no content", binary);
   assertEquals("Unexpected payload length", payloadSize, binary.getLength());

   byte[] binaryContent = binary.getArray();
   int offset = binary.getArrayOffset();
   for (int i = 0; i < payloadSize; i++) {
      byte expected = (byte) (48 + (i % 7));
      assertEquals("Unexpected content at payload index " + i, expected, binaryContent[i + offset]);
   }
}
 
Example 2
Source File: AmqpLargeMessageTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testMessageWithDataAndEmptyBinaryPreservesBody() throws Exception {
   server.getAddressSettingsRepository().addMatch("#", new AddressSettings().setDefaultAddressRoutingType(RoutingType.ANYCAST));

   AmqpClient client = createAmqpClient();
   AmqpConnection connection = addConnection(client.connect());
   try {
      AmqpSession session = connection.createSession();
      AmqpSender sender = session.createSender(getTestName());

      AmqpMessage message = createAmqpLargeMessageWithNoBody();

      message.getWrappedMessage().setBody(new Data(new Binary(new byte[0])));

      sender.send(message);
      sender.close();

      AmqpReceiver receiver = session.createReceiver(getTestName());
      receiver.flow(1);

      AmqpMessage received = receiver.receive(5, TimeUnit.SECONDS);
      assertNotNull("failed to read large AMQP message", received);
      MessageImpl wrapped = (MessageImpl) received.getWrappedMessage();

      assertTrue(wrapped.getBody() instanceof Data);
      Data body = (Data) wrapped.getBody();
      assertTrue(body.getValue() instanceof Binary);
      Binary payload = (Binary) body.getValue();
      assertEquals(0, payload.getLength());

      received.accept();
      session.close();
   } finally {
      connection.close();
   }
}
 
Example 3
Source File: AmqpJmsTextMessageFacade.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Override
public String getText() throws JMSException {
    Section body = getBody();

    if (body == null) {
        return null;
    } else if (body instanceof Data) {
        Data data = (Data) body;
        if (data.getValue() == null || data.getValue().getLength() == 0) {
            return "";
        } else {
            Binary b = data.getValue();
            ByteBuffer buf = ByteBuffer.wrap(b.getArray(), b.getArrayOffset(), b.getLength());

            try {
                CharBuffer chars = charset.newDecoder().decode(buf);
                return String.valueOf(chars);
            } catch (CharacterCodingException e) {
                throw JmsExceptionSupport.create("Cannot decode String in " + charset.displayName(), e);
            }
        }
    } else if (body instanceof AmqpValue) {
        Object value = ((AmqpValue) body).getValue();

        if (value == null || value instanceof String) {
            return (String) value;
        } else {
            throw new IllegalStateException("Unexpected amqp-value body content type: " + value.getClass().getSimpleName());
        }
    } else {
        throw new IllegalStateException("Unexpected message body type: " + body.getClass().getSimpleName());
    }
}
 
Example 4
Source File: DataType.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Override
protected Binary wrap(Data val)
{
    return val.getValue();
}
 
Example 5
Source File: AmqpLargeMessageTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 60000)
public void testMessageWithDataAndContentTypeOfTextPreservesBodyType() throws Exception {
   server.getAddressSettingsRepository().addMatch("#", new AddressSettings().setDefaultAddressRoutingType(RoutingType.ANYCAST));

   AmqpClient client = createAmqpClient();
   AmqpConnection connection = addConnection(client.connect());
   try {
      AmqpSession session = connection.createSession();
      AmqpSender sender = session.createSender(getTestName());

      AmqpMessage message = createAmqpLargeMessageWithNoBody();

      String messageText = "This text will be in a Data Section";

      message.getWrappedMessage().setContentType("text/plain");
      message.getWrappedMessage().setBody(new Data(new Binary(messageText.getBytes(StandardCharsets.UTF_8))));
      //message.setApplicationProperty("_AMQ_DUPL_ID", "11");

      sender.send(message);
      sender.close();

      AmqpReceiver receiver = session.createReceiver(getTestName());
      receiver.flow(1);

      AmqpMessage received = receiver.receive(10, TimeUnit.SECONDS);
      assertNotNull("failed to read large AMQP message", received);
      MessageImpl wrapped = (MessageImpl) received.getWrappedMessage();

      assertTrue(wrapped.getBody() instanceof Data);
      Data body = (Data) wrapped.getBody();
      assertTrue(body.getValue() instanceof Binary);
      Binary payload = (Binary) body.getValue();
      String reconstitutedString = new String(
         payload.getArray(), payload.getArrayOffset(), payload.getLength(), StandardCharsets.UTF_8);

      assertEquals(messageText, reconstitutedString);

      received.accept();
      session.close();
   } finally {
      connection.close();
   }
}