Java Code Examples for org.apache.qpid.proton.codec.EncoderImpl#writeObject()

The following examples show how to use org.apache.qpid.proton.codec.EncoderImpl#writeObject() . 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: AmqpCoreConverter.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
private static void encodeUnsupportedMessagePropertyType(ServerJMSMessage jms, String key, Object value) throws JMSException {
   final ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer();
   final EncoderImpl encoder = TLSEncode.getEncoder();

   try {
      encoder.setByteBuffer(new NettyWritable(buffer));
      encoder.writeObject(value);

      final byte[] encodedBytes = new byte[buffer.writerIndex()];

      buffer.readBytes(encodedBytes);

      setProperty(jms, key, encodedBytes);
   } finally {
      encoder.setByteBuffer((WritableBuffer) null);
      buffer.release();
   }
}
 
Example 2
Source File: TestConversions.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
private byte[] encodeObject(Object toEncode) {
   ByteBuf scratch = Unpooled.buffer();
   EncoderImpl encoder = TLSEncode.getEncoder();
   encoder.setByteBuffer(new NettyWritable(scratch));

   try {
      encoder.writeObject(toEncode);
   } finally {
      encoder.setByteBuffer((WritableBuffer) null);
   }

   byte[] result = new byte[scratch.writerIndex()];
   scratch.readBytes(result);

   return result;
}
 
Example 3
Source File: AMQPMessageTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testPartialDecodeIgnoresDeliveryAnnotationsByDefault() {
   Header header = new Header();
   header.setDurable(true);
   header.setPriority(UnsignedByte.valueOf((byte) 6));

   ByteBuf encodedBytes = Unpooled.buffer(1024);
   NettyWritable writable = new NettyWritable(encodedBytes);

   EncoderImpl encoder = TLSEncode.getEncoder();
   encoder.setByteBuffer(writable);
   encoder.writeObject(header);

   // Signal body of AmqpValue but write corrupt underlying type info
   encodedBytes.writeByte(EncodingCodes.DESCRIBED_TYPE_INDICATOR);
   encodedBytes.writeByte(EncodingCodes.SMALLULONG);
   encodedBytes.writeByte(DELIVERY_ANNOTATIONS_DESCRIPTOR.byteValue());
   encodedBytes.writeByte(EncodingCodes.MAP8);
   encodedBytes.writeByte(2);  // Size
   encodedBytes.writeByte(2);  // Elements
   // Use bad encoding code on underlying type of map key which will fail the decode if run
   encodedBytes.writeByte(255);

   ReadableBuffer readable = new NettyReadable(encodedBytes);

   AMQPStandardMessage message = null;
   try {
      message = new AMQPStandardMessage(0, readable, null, null);
   } catch (Exception decodeError) {
      fail("Should not have encountered an exception on partial decode: " + decodeError.getMessage());
   }

   try {
      // This should perform the lazy decode of the DeliveryAnnotations portion of the message
      message.getDeliveryAnnotations();
      fail("Should have thrown an error when attempting to decode the ApplicationProperties which are malformed.");
   } catch (Exception ex) {
      // Expected decode to fail when building full message.
   }
}
 
Example 4
Source File: AMQPMessageTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testPartialDecodeIgnoresBodyByDefault() {
   Header header = new Header();
   header.setDurable(true);
   header.setPriority(UnsignedByte.valueOf((byte) 6));

   ByteBuf encodedBytes = Unpooled.buffer(1024);
   NettyWritable writable = new NettyWritable(encodedBytes);

   EncoderImpl encoder = TLSEncode.getEncoder();
   encoder.setByteBuffer(writable);
   encoder.writeObject(header);

   // Signal body of AmqpValue but write corrupt underlying type info
   encodedBytes.writeByte(EncodingCodes.DESCRIBED_TYPE_INDICATOR);
   encodedBytes.writeByte(EncodingCodes.SMALLULONG);
   encodedBytes.writeByte(AMQPVALUE_DESCRIPTOR.byteValue());
   // Use bad encoding code on underlying type
   encodedBytes.writeByte(255);

   ReadableBuffer readable = new NettyReadable(encodedBytes);

   AMQPStandardMessage message = null;
   try {
      message = new AMQPStandardMessage(0, readable, null, null);
   } catch (Exception decodeError) {
      fail("Should not have encountered an exception on partial decode: " + decodeError.getMessage());
   }

   assertTrue(message.isDurable());

   try {
      // This will decode the body section if present in order to present it as a Proton Message object
      message.getBody();
      fail("Should have thrown an error when attempting to decode the body which is malformed.");
   } catch (Exception ex) {
      // Expected decode to fail when building full message.
   }
}
 
Example 5
Source File: AmqpCodec.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
/**
 * Given an AMQP Section encode it and return the buffer holding the encoded value
 *
 * @param section
 *      the AMQP Section value to encode.
 *
 * @return a buffer holding the encoded bytes of the given AMQP Section object.
 */
public static ByteBuf encode(Section section) {
    if (section == null) {
        return null;
    }

    AmqpWritableBuffer buffer = new AmqpWritableBuffer();

    EncoderImpl encoder = getEncoder();
    encoder.setByteBuffer(buffer);
    encoder.writeObject(section);
    encoder.setByteBuffer((WritableBuffer) null);

    return buffer.getBuffer();
}
 
Example 6
Source File: AMQPStandardMessage.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Override
protected synchronized void encodeMessage() {
   this.modified = false;
   this.messageDataScanned = MessageDataScanningStatus.NOT_SCANNED.code;
   int estimated = Math.max(1500, data != null ? data.capacity() + 1000 : 0);
   ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(estimated);
   EncoderImpl encoder = TLSEncode.getEncoder();

   try {
      NettyWritable writable = new NettyWritable(buffer);

      encoder.setByteBuffer(writable);
      if (header != null) {
         encoder.writeObject(header);
      }

      // We currently do not encode any delivery annotations but it is conceivable
      // that at some point they may need to be preserved, this is where that needs
      // to happen.

      if (messageAnnotations != null) {
         encoder.writeObject(messageAnnotations);
      }
      if (properties != null) {
         encoder.writeObject(properties);
      }

      // Whenever possible avoid encoding sections we don't need to by
      // checking if application properties where loaded or added and
      // encoding only in that case.
      if (applicationProperties != null) {
         encoder.writeObject(applicationProperties);

         // Now raw write the remainder body and footer if present.
         if (data != null && remainingBodyPosition != VALUE_NOT_PRESENT) {
            writable.put(data.position(remainingBodyPosition));
         }
      } else if (data != null && applicationPropertiesPosition != VALUE_NOT_PRESENT) {
         // Writes out ApplicationProperties, Body and Footer in one go if present.
         writable.put(data.position(applicationPropertiesPosition));
      } else if (data != null && remainingBodyPosition != VALUE_NOT_PRESENT) {
         // No Application properties at all so raw write Body and Footer sections
         writable.put(data.position(remainingBodyPosition));
      }

      byte[] bytes = new byte[buffer.writerIndex()];

      buffer.readBytes(bytes);
      data = ReadableBuffer.ByteBufferReader.wrap(ByteBuffer.wrap(bytes));
   } finally {
      encoder.setByteBuffer((WritableBuffer) null);
      buffer.release();
   }
}
 
Example 7
Source File: AMQPMessageTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testPartialDecodeIgnoresApplicationPropertiesByDefault() {
   Header header = new Header();
   header.setDurable(true);
   header.setPriority(UnsignedByte.valueOf((byte) 6));

   ByteBuf encodedBytes = Unpooled.buffer(1024);
   NettyWritable writable = new NettyWritable(encodedBytes);

   EncoderImpl encoder = TLSEncode.getEncoder();
   encoder.setByteBuffer(writable);
   encoder.writeObject(header);

   // Signal body of AmqpValue but write corrupt underlying type info
   encodedBytes.writeByte(EncodingCodes.DESCRIBED_TYPE_INDICATOR);
   encodedBytes.writeByte(EncodingCodes.SMALLULONG);
   encodedBytes.writeByte(APPLICATION_PROPERTIES_DESCRIPTOR.byteValue());
   encodedBytes.writeByte(EncodingCodes.MAP8);
   encodedBytes.writeByte(2);  // Size
   encodedBytes.writeByte(2);  // Elements
   // Use bad encoding code on underlying type of map key which will fail the decode if run
   encodedBytes.writeByte(255);

   ReadableBuffer readable = new NettyReadable(encodedBytes);

   AMQPStandardMessage message = null;
   try {
      message = new AMQPStandardMessage(0, readable, null, null);
   } catch (Exception decodeError) {
      fail("Should not have encountered an exception on partial decode: " + decodeError.getMessage());
   }

   assertTrue(message.isDurable());

   try {
      // This should perform the lazy decode of the ApplicationProperties portion of the message
      message.getStringProperty("test");
      fail("Should have thrown an error when attempting to decode the ApplicationProperties which are malformed.");
   } catch (Exception ex) {
      // Expected decode to fail when building full message.
   }
}
 
Example 8
Source File: AmqpCodec.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
/**
 * Given a Message instance, encode the Message to the wire level representation
 * of that Message.
 *
 * @param message
 *      the Message that is to be encoded into the wire level representation.
 *
 * @return a buffer containing the wire level representation of the input Message.
 */
public static ByteBuf encodeMessage(AmqpJmsMessageFacade message) {
    EncoderDecoderContext context = TLS_CODEC.get();

    AmqpWritableBuffer buffer = new AmqpWritableBuffer();

    EncoderImpl encoder = context.encoder;
    encoder.setByteBuffer(buffer);

    Header header = message.getHeader();
    DeliveryAnnotations deliveryAnnotations = message.getDeliveryAnnotations();
    MessageAnnotations messageAnnotations = message.getMessageAnnotations();
    Properties properties = message.getProperties();
    ApplicationProperties applicationProperties = message.getApplicationProperties();
    Section body = message.getBody();
    Footer footer = message.getFooter();

    if (header != null) {
        encoder.writeObject(header);
    }
    if (deliveryAnnotations != null) {
        encoder.writeObject(deliveryAnnotations);
    }
    if (messageAnnotations != null) {
        // Ensure annotations contain required message type and destination type data
        AmqpDestinationHelper.setReplyToAnnotationFromDestination(message.getReplyTo(), messageAnnotations);
        AmqpDestinationHelper.setToAnnotationFromDestination(message.getDestination(), messageAnnotations);
        messageAnnotations.getValue().put(AmqpMessageSupport.JMS_MSG_TYPE, message.getJmsMsgType());
        encoder.writeObject(messageAnnotations);
    } else {
        buffer.put(getCachedMessageAnnotationsBuffer(message, context));
    }
    if (properties != null) {
        encoder.writeObject(properties);
    }
    if (applicationProperties != null) {
        encoder.writeObject(applicationProperties);
    }
    if (body != null) {
        encoder.writeObject(body);
    }
    if (footer != null) {
        encoder.writeObject(footer);
    }

    encoder.setByteBuffer((WritableBuffer) null);

    return buffer.getBuffer();
}