Java Code Examples for org.apache.qpid.proton.amqp.Binary#getArrayOffset()

The following examples show how to use org.apache.qpid.proton.amqp.Binary#getArrayOffset() . 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: JMSMappingOutboundTransformerTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testConvertTextMessageCreatesBodyUsingOriginalEncodingWithDataSection() throws Exception {
   String contentString = "myTextMessageContent";
   ServerJMSTextMessage outbound = createTextMessage(contentString);
   outbound.setShortProperty(JMS_AMQP_ORIGINAL_ENCODING, AMQP_DATA);
   outbound.encode();

   AMQPMessage amqp = AMQPConverter.getInstance().fromCore(outbound.getInnerMessage(), null);

   assertNotNull(amqp.getBody());
   assertTrue(amqp.getBody() instanceof Data);
   assertTrue(((Data) amqp.getBody()).getValue() instanceof Binary);

   Binary data = ((Data) amqp.getBody()).getValue();
   String contents = new String(data.getArray(), data.getArrayOffset(), data.getLength(), StandardCharsets.UTF_8);
   assertEquals(contentString, contents);
}
 
Example 3
Source File: JMSMappingOutboundTransformerTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testConvertTextMessageContentNotStoredCreatesBodyUsingOriginalEncodingWithDataSection() throws Exception {
   String contentString = "myTextMessageContent";
   ServerJMSTextMessage outbound = createTextMessage(contentString);
   outbound.setShortProperty(JMS_AMQP_ORIGINAL_ENCODING, AMQP_DATA);
   outbound.encode();

   AMQPMessage amqp = AMQPConverter.getInstance().fromCore(outbound.getInnerMessage(), null);

   assertNotNull(amqp.getBody());
   assertTrue(amqp.getBody() instanceof Data);
   assertTrue(((Data) amqp.getBody()).getValue() instanceof Binary);

   Binary data = ((Data) amqp.getBody()).getValue();
   String contents = new String(data.getArray(), data.getArrayOffset(), data.getLength(), StandardCharsets.UTF_8);
   assertEquals(contentString, contents);
}
 
Example 4
Source File: DeliveryImpl.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
void append(Binary payload)
{
    byte[] data = payload.getArray();

    // The Composite buffer cannot handle composites where the array
    // is a view of a larger array so we must copy the payload into
    // an array of the exact size
    if (payload.getArrayOffset() > 0 || payload.getLength() < data.length)
    {
        data = new byte[payload.getLength()];
        System.arraycopy(payload.getArray(), payload.getArrayOffset(), data, 0, payload.getLength());
    }

    getOrCreateDataBuffer().append(data);
}
 
Example 5
Source File: AMQPMessage.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * Before we added AMQP into Artemis the name getUserID was already taken by JMSMessageID.
 * We cannot simply change the names now as it would break the API for existing clients.
 *
 * This is to return and read the proper AMQP userID.
 *
 * @return the UserID value in the AMQP Properties if one is present.
 */
public final Object getAMQPUserID() {
   if (properties != null && properties.getUserId() != null) {
      Binary binary = properties.getUserId();
      return new String(binary.getArray(), binary.getArrayOffset(), binary.getLength(), StandardCharsets.UTF_8);
   } else {
      return null;
   }
}
 
Example 6
Source File: AmqpJmsMessageFacade.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Override
public String getUserId() {
    String userId = null;

    if (properties != null && properties.getUserId() != null) {
        Binary userIdBytes = properties.getUserId();
        if (userIdBytes.getLength() != 0) {
            userId = new String(userIdBytes.getArray(), userIdBytes.getArrayOffset(), userIdBytes.getLength(), StandardCharsets.UTF_8);
        }
    }

    return userId;
}
 
Example 7
Source File: StringUtils.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
/**
 * Converts the Binary to a quoted string.
 *
 * @param bin the Binary to convert
 * @param stringLength the maximum length of stringified content (excluding the quotes, and truncated indicator)
 * @param appendIfTruncated appends "...(truncated)" if not all of the payload is present in the string
 * @return the converted string
 */
public static String toQuotedString(final Binary bin,final int stringLength,final boolean appendIfTruncated)
{
    if(bin == null)
    {
         return "\"\"";
    }

    final byte[] binData = bin.getArray();
    final int binLength = bin.getLength();
    final int offset = bin.getArrayOffset();

    StringBuilder str = new StringBuilder();
    str.append("\"");

    int size = 0;
    boolean truncated = false;
    for (int i = 0; i < binLength; i++)
    {
        byte c = binData[offset + i];

        if (c > 31 && c < 127 && c != '\\')
        {
            if (size + 1 <= stringLength)
            {
                size += 1;
                str.append((char) c);
            }
            else
            {
                truncated = true;
                break;
            }
        }
        else
        {
            if (size + 4 <= stringLength)
            {
                size += 4;
                str.append(String.format("\\x%02x", c));
            }
            else
            {
                truncated = true;
                break;
            }
        }
    }

    str.append("\"");

    if (truncated && appendIfTruncated)
    {
        str.append("...(truncated)");
    }

    return str.toString();
}
 
Example 8
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();
   }
}
 
Example 9
Source File: AmqpProtocolTracer.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
private String formatPayload(TransportFrame frame) {
    Binary payload = frame.getPayload();

    if (payload == null || payload.getLength() == 0 || payloadStringLimit <= 0) {
        return "";
    }

    final byte[] binData = payload.getArray();
    final int binLength = payload.getLength();
    final int offset = payload.getArrayOffset();

    StringBuilder builder = new StringBuilder();

    // Prefix the payload with total bytes which gives insight regardless of truncation.
    builder.append(" (").append(payload.getLength()).append(") ").append("\"");

    int size = 0;
    boolean truncated = false;
    for (int i = 0; i < binLength; i++) {
        byte c = binData[offset + i];

        if (c > 31 && c < 127 && c != '\\') {
            if (size + 1 <= payloadStringLimit) {
                size += 1;
                builder.append((char) c);
            } else {
                truncated = true;
                break;
            }
        } else {
            if (size + 4 <= payloadStringLimit) {
                size += 4;
                builder.append(String.format("\\x%02x", c));
            } else {
                truncated = true;
                break;
            }
        }
    }

    builder.append("\"");

    if (truncated) {
        builder.append("...(truncated)");
    }

    return builder.toString();
}
 
Example 10
Source File: AmqpSupport.java    From activemq-artemis with Apache License 2.0 2 votes vote down vote up
/**
 * Converts a Binary value to a long assuming that the contained value is
 * stored in Big Endian encoding.
 *
 * @param value the Binary object whose payload is converted to a long.
 * @return a long value constructed from the bytes of the Binary instance.
 */
public static long toLong(Binary value) {
   Buffer buffer = new Buffer(value.getArray(), value.getArrayOffset(), value.getLength());
   return buffer.bigEndianEditor().readLong();
}