//
// Copyright (c) Microsoft and contributors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Module dependencies.
var azureCommon = require('./../../../common/common');
var xmlbuilder = azureCommon.xmlbuilder;
var Constants = azureCommon.Constants;
var HeaderConstants = Constants.HeaderConstants;
function QueueMessageResult(queue, messageid, popreceipt, metadata) {
if (queue) {
this.queue = queue;
}
if (messageid) {
this.messageid = messageid;
}
Iif (popreceipt) {
this.popreceipt = popreceipt;
}
Iif (metadata) {
this.metadata = metadata;
}
}
/**
* Builds an XML representation for a queue message
*
* @param {string} messageJs The queue message.
* @return {string} The XML queue message.
*/
QueueMessageResult.serialize = function (messageJs, encode) {
var doc = xmlbuilder.create();
doc = doc.begin(Constants.QueueConstants.QUEUE_MESSAGE_ELEMENT, { version: '1.0', encoding: 'utf-8' });
if (messageJs) {
var message;
if (encode) {
message = new Buffer(messageJs).toString('base64');
} else {
message = messageJs;
}
doc.ele(Constants.QueueConstants.MESSAGE_TEXT_ELEMENT)
.txt(message)
.up();
} else {
doc.ele(Constants.QueueConstants.MESSAGE_TEXT_ELEMENT).up();
}
return doc.doc().toString();
};
QueueMessageResult.parse = function (messageXml, encode) {
var queueMessageResult = new QueueMessageResult();
for (var property in messageXml) {
if (property === Constants.QueueConstants.MESSAGE_TEXT_ELEMENT) {
if (encode) {
queueMessageResult.messagetext = new Buffer(messageXml[property], 'base64').toString();
} else {
queueMessageResult.messagetext = messageXml[property];
}
} else {
queueMessageResult[property.toLowerCase()] = messageXml[property];
}
}
return queueMessageResult;
};
QueueMessageResult.prototype.getPropertiesFromHeaders = function (headers) {
var self = this;
var setmessagePropertyFromHeaders = function (messageProperty, headerProperty) {
if (!self[messageProperty] && headers[headerProperty.toLowerCase()]) {
self[messageProperty] = headers[headerProperty.toLowerCase()];
}
};
setmessagePropertyFromHeaders('popreceipt', HeaderConstants.POP_RECEIPT);
setmessagePropertyFromHeaders('timenextvisible', HeaderConstants.TIME_NEXT_VISIBLE);
};
module.exports = QueueMessageResult; |