static toMessage()

in common/transport/amqp/src/amqp_message.ts [161:232]


  static toMessage(amqpMessage: AmqpMessage): Message {
    /*Codes_SRS_NODE_IOTHUB_AMQPMSG_16_001: [The `toMessage` method shall throw if the `amqpMessage` argument is falsy.]*/
    if (!amqpMessage) {
      throw new ReferenceError('amqpMessage cannot be \'' + amqpMessage + '\'');
    }

    /*Codes_SRS_NODE_IOTHUB_AMQPMSG_16_009: [The `toMessage` method shall set the `Message.data` of the message to the content of the `AmqpMessage.body.content` property.]*/
    const msg: Message = ( amqpMessage.body ) ? ( new Message(amqpMessage.body.content) ) : ( new Message(undefined) );

    /*Codes_SRS_NODE_IOTHUB_AMQPMSG_16_005: [The `toMessage` method shall set the `Message.to` property to the `AmqpMessage.to` value if it is present.]*/
    if (amqpMessage.to) {
      msg.to = amqpMessage.to;
    }

    /*Codes_SRS_NODE_IOTHUB_AMQPMSG_16_006: [The `toMessage` method shall set the `Message.expiryTimeUtc` property to the `AmqpMessage.absolute_expiry_time` value if it is present.]*/
    if (amqpMessage.absolute_expiry_time) {
      msg.expiryTimeUtc = amqpMessage.absolute_expiry_time;
    }

    //
    // The rhea library will de-serialize an encoded uuid (0x98) as a 16 byte buffer.
    // Since common messages should only have type string it is safe to decode
    // these as strings.
    //
    /*Codes_SRS_NODE_IOTHUB_AMQPMSG_16_004: [The `toMessage` method shall set the `Message.messageId` property to the `AmqpMessage.message_id` value if it is present.]*/
    if (amqpMessage.message_id) {
      if (((amqpMessage.message_id as any) instanceof Buffer) && (amqpMessage.message_id.length === 16)) {
        msg.messageId = rheaUuidToString(amqpMessage.message_id as any);
      } else {
        msg.messageId = amqpMessage.message_id;
      }
    }

    /*Codes_SRS_NODE_IOTHUB_AMQPMSG_16_003: [The `toMessage` method shall set the `Message.correlationId` property to the `AmqpMessage.correlation_id` value if it is present.]*/
    if (amqpMessage.correlation_id) {
      if (((amqpMessage.correlation_id as any) instanceof Buffer) && (amqpMessage.correlation_id.length === 16)) {
        msg.correlationId = rheaUuidToString(amqpMessage.correlation_id as any);
      } else {
        msg.correlationId = amqpMessage.correlation_id;
      }
    }

    /*Codes_SRS_NODE_IOTHUB_AMQPMSG_16_016: [The `toMessage` method shall set the `Message.contentType` property to the `AmqpMessage.content_type` value if it is present. ]*/
    if (amqpMessage.content_type) {
      msg.contentType = amqpMessage.content_type;
    }

    /*Codes_SRS_NODE_IOTHUB_AMQPMSG_16_017: [The `toMessage` method shall set the `Message.contentEncoding` property to the `AmqpMessage.content_encoding` value if it is present. ]*/
    if (amqpMessage.content_encoding) {
      msg.contentEncoding = amqpMessage.content_encoding;
    }

    /*Codes_SRS_NODE_IOTHUB_AMQPMSG_16_007: [The `toMessage` method shall convert the user-defined `AmqpMessage.applicationProperties` to a `Properties` collection stored in `Message.properties`.]*/
    if (amqpMessage.application_properties) {
      const appProps = amqpMessage.application_properties;
      for (const key in appProps) {
        if (Object.prototype.hasOwnProperty.call(appProps, key)) {
          /*Codes_SRS_NODE_IOTHUB_AMQPMSG_16_008: [The `toMessage` method shall set the `Message.ack` property to the `AmqpMessage.application_properties['iothub-ack']` value if it is present.]*/
          if (key === 'iothub-ack') {
            msg.ack = appProps[key];
          } else {
            msg.properties.add(key, appProps[key]);
          }
        }
      }
    }

    msg.transportObj = amqpMessage;

    /*Codes_SRS_NODE_IOTHUB_AMQPMSG_16_002: [The `toMessage` method shall return a `Message` object.]*/
    return msg;
  }