static fromMessage()

in src/common-amqp/amqp_message.ts [56:153]


  static fromMessage(message: Message): AmqpMessage {
    if (!message) throw new ReferenceError('message is \'' + message + '\'');

    /*Codes_SRS_NODE_IOTHUB_AMQPMSG_05_001: [The fromMessage method shall create a new instance of AmqpMessage.]*/
    const amqpMessage = new AmqpMessage();

    /*Codes_SRS_NODE_IOTHUB_AMQPMSG_05_003: [If the message argument has a to property, the AmqpMessage object shall have a property named to with the same value.]*/
    if (message.to) {
      amqpMessage.to = message.to;
    }
    /*Codes_SRS_NODE_IOTHUB_AMQPMSG_05_004: [If the message argument has an expiryTimeUtc property, the AmqpMessage object shall have a property named absolute_expiry_time with the same value.]*/
    if (message.expiryTimeUtc) {
      amqpMessage.absolute_expiry_time = message.expiryTimeUtc;
    }

    /*Codes_SRS_NODE_IOTHUB_AMQPMSG_05_007: [If the message argument has a messageId property, the AmqpMessage object shall have a property named messageId with the same value.]*/
    if (message.messageId) {
      amqpMessage.message_id = encodeUuid(message.messageId);
    }

    /*Codes_SRS_NODE_IOTHUB_AMQPMSG_16_010: [If the `message` argument has a `correlationId` property, the `AmqpMessage` object shall have a property named `correlation_id` with the same value.]*/
    if (message.correlationId) {
      /*Codes_SRS_NODE_IOTHUB_AMQPMSG_16_012: [If the `Message.correlationId` property is a UUID, the AMQP type of the `AmqpMessage.correlation_id` property shall be forced to Buffer[16].]*/
      amqpMessage.correlation_id = encodeUuid(message.correlationId);
    }

    /*Codes_SRS_NODE_IOTHUB_AMQPMSG_16_014: [If the `message` argument has a `contentEncoding` property, the `AmqpMessage` object shall have a property named `content_encoding` with the same value.]*/
    if (message.contentEncoding) {
      amqpMessage.content_encoding = message.contentEncoding;
    }

    /*Codes_SRS_NODE_IOTHUB_AMQPMSG_16_015: [If the `message` argument has a `contentType` property, the `AmqpMessage` object shall have a property named `content_type` with the same value.]*/
    if (message.contentType) {
      amqpMessage.content_type = message.contentType;
    }

    if (message.interfaceId) {
      if (!amqpMessage.message_annotations) {
        amqpMessage.message_annotations = {
          'iothub-interface-id': message.interfaceId
        };
      } else {
        amqpMessage.message_annotations['iothub-interface-id'] = message.interfaceId;
      }
    }

    /*Codes_SRS_NODE_IOTHUB_AMQPMSG_05_008: [If needed, the created AmqpMessage object shall have a property of type Object named application_properties.]*/
    function ensureApplicationPropertiesCreated(): void {
      if (!amqpMessage.application_properties) {
        amqpMessage.application_properties = {};
      }
    }

    /*Codes_SRS_NODE_IOTHUB_AMQPMSG_05_009: [If the message argument has an ack property, the application_properties property of the AmqpMessage object shall have a property named iothub-ack with the same value.]*/
    if (message.ack) {
      ensureApplicationPropertiesCreated();
      amqpMessage.application_properties['iothub-ack'] = message.ack;
    }

    /*Codes_SRS_NODE_IOTHUB_AMQPMSG_13_001: [ If message.properties is truthy, then all the properties in it shall be copied to the application_properties property of the AmqpMessage object. ]*/
    if (message.properties) {
      const props = message.properties;
      const propsCount = props.count();
      if (propsCount > 0) {
        const DT_SUBJECT = 'dt-subject';
        for (let index = 0; index < propsCount; index++) {
          const item = props.getItem(index);
          if (item) {
            if (item.key === DT_SUBJECT) {
              if (!amqpMessage.message_annotations) {
                amqpMessage.message_annotations = {
                  [DT_SUBJECT]: item.value
                };
              } else {
                amqpMessage.message_annotations[DT_SUBJECT] = item.value;
              }
            } else {
              if (!amqpMessage.application_properties) {
                ensureApplicationPropertiesCreated();
              }
              /*Codes_SRS_NODE_IOTHUB_AMQPMSG_16_013: [If one of the property key is `IoThub-status`, this property is reserved and shall be forced to an `int` `rhea` type.]*/
              const val = (item.key === 'IoThub-status') ? rheaTypes.wrap_int(parseInt(item.value)) : item.value;
              amqpMessage.application_properties[item.key] = val;
            }
          }
        }
      }
    }

    /*Codes_SRS_NODE_IOTHUB_AMQPMSG_05_005: [If message.getData() is truthy, the AmqpMessage object shall have a property named body with the value returned from message.getData().]*/
    const body = message.getData();
    if (body !== undefined) {
      amqpMessage.body = rheaMessage.data_section(message.getBytes());
    }

    /*Codes_SRS_NODE_IOTHUB_AMQPMSG_05_006: [The generated AmqpMessage object shall be returned to the caller.]*/
    return amqpMessage;
  }