diff --git a/lib/server/handlers/sthGetDataHandler.js b/lib/server/handlers/sthGetDataHandler.js index 7b331d3..69a1383 100644 --- a/lib/server/handlers/sthGetDataHandler.js +++ b/lib/server/handlers/sthGetDataHandler.js @@ -39,7 +39,7 @@ const encodeRFC5987 = require('rfc5987-value-chars').encode; * @param {Object} request The request * @param {Function} reply Hapi's reply function */ -function getRawData(request, reply) { +function getRawData(request, reply, replyHandler) { let response; var query = { @@ -71,7 +71,11 @@ function getRawData(request, reply) { sthLogger.debug(request.sth.context, 'Responding with 500- Internal error'); const error = boom.internal(message); error.output.payload.message = message; - reply(error); + if (reply) { + reply.response(error); + } else { + replyHandler(error); + } } else { // The collection does not exist, reply with en empty response sthLogger.warn( @@ -90,7 +94,11 @@ function getRawData(request, reply) { request.params.attrName, emptyResponse ); - response = reply(ngsiPayload); + if (reply) { + response = reply.response(ngsiPayload); + } else { + response = replyHandler(ngsiPayload); + } sthServerUtils.addFiwareCorrelator(request, response); if (request.query.count) { sthServerUtils.addFiwareTotalCount(0, response); @@ -116,7 +124,11 @@ function getRawData(request, reply) { // Error when getting the raw data sthLogger.error(request.sth.context, 'Error %j when getting raw data with query %j', err, rawQuery); sthLogger.debug(request.sth.context, 'Responding with 500 - Internal Error'); - response = reply(err); + if (reply) { + response = reply.response(err); + } else { + response = replyHandler(err); + } } else if (!result || !(result.length || result instanceof stream)) { // No raw data available for the request sthLogger.info( @@ -127,22 +139,38 @@ function getRawData(request, reply) { ); sthLogger.debug(request.sth.context, 'Responding with no points'); - response = reply( - sthServerUtils.getNGSIPayload( - request.params.version, - request.params.entityId, - request.params.entityType, - request.params.attrName, - sthServerUtils.getEmptyResponse() - ) - ); + if (reply) { + response = reply.response( + sthServerUtils.getNGSIPayload( + request.params.version, + request.params.entityId, + request.params.entityType, + request.params.attrName, + sthServerUtils.getEmptyResponse() + ) + ); + } else { + response = replyHandler( + sthServerUtils.getNGSIPayload( + request.params.version, + request.params.entityId, + request.params.entityType, + request.params.attrName, + sthServerUtils.getEmptyResponse() + ) + ); + } } else if (result instanceof stream) { sthLogger.debug(request.sth.context, 'Responding with a stream of docs'); response = reply(new stream.Readable().wrap(result)); } else if (typeof result === 'string') { sthLogger.debug(request.sth.context, "Responding with file '" + result + "'"); fs.readFile(result, function(err, data) { - response = reply(data); + if (reply) { + response = reply.response(data); + } else { + response = replyHandler(data); + } const fileName = result.substring(result.lastIndexOf(path.sep) + 1); response.header( 'Content-Disposition', @@ -164,15 +192,27 @@ function getRawData(request, reply) { }); // readfile } else { sthLogger.debug(request.sth.context, 'Responding with %s docs', result.length); - response = reply( - sthServerUtils.getNGSIPayload( - request.params.version, - request.params.entityId, - request.params.entityType, - request.params.attrName, - result - ) - ); + if (reply) { + response = reply.response( + sthServerUtils.getNGSIPayload( + request.params.version, + request.params.entityId, + request.params.entityType, + request.params.attrName, + result + ) + ); + } else { + response = replyHandler( + sthServerUtils.getNGSIPayload( + request.params.version, + request.params.entityId, + request.params.entityType, + request.params.attrName, + result + ) + ); + } } sthServerUtils.addFiwareCorrelator(request, response); if (request.query.count) { @@ -221,7 +261,11 @@ function getAggregatedData(request, reply) { sthLogger.debug(request.sth.context, 'Responding with 500- Internal error'); const error = boom.internal(message); error.output.payload.message = message; - reply(error); + if (reply) { + reply.response(error); + } else { + replyHandler(error); + } } else { // The collection does not exist, reply with en empty response sthLogger.warn( @@ -243,7 +287,11 @@ function getAggregatedData(request, reply) { request.params.attrName, emptyResponse ); - response = reply(ngsiPayload); + if (reply) { + response = reply.response(ngsiPayload); + } else { + response = replyHandler(ngsiPayload); + } sthServerUtils.addFiwareCorrelator(request, response); } } else { @@ -274,7 +322,11 @@ function getAggregatedData(request, reply) { aggregatedQuery ); sthLogger.debug(request.sth.context, 'Responding with 500 - Internal Error'); - response = reply(err); + if (reply) { + response = reply.response(err); + } else { + response = replyHandler(err); + } } else if (!result || !result.length) { // No aggregated data available for the request sthLogger.debug( @@ -285,26 +337,50 @@ function getAggregatedData(request, reply) { ); sthLogger.debug(request.sth.context, 'Responding with no points'); - response = reply( - sthServerUtils.getNGSIPayload( - request.params.version, - request.params.entityId, - request.params.entityType, - request.params.attrName, - sthServerUtils.getEmptyResponse() - ) - ); + if (reply) { + response = reply.response( + sthServerUtils.getNGSIPayload( + request.params.version, + request.params.entityId, + request.params.entityType, + request.params.attrName, + sthServerUtils.getEmptyResponse() + ) + ); + } else { + response = replyHandler( + sthServerUtils.getNGSIPayload( + request.params.version, + request.params.entityId, + request.params.entityType, + request.params.attrName, + sthServerUtils.getEmptyResponse() + ) + ); + } } else { sthLogger.debug(request.sth.context, 'Responding with %s docs', result.length); - response = reply( - sthServerUtils.getNGSIPayload( - request.params.version, - request.params.entityId, - request.params.entityType, - request.params.attrName, - result - ) - ); + if (reply) { + response = reply.response( + sthServerUtils.getNGSIPayload( + request.params.version, + request.params.entityId, + request.params.entityType, + request.params.attrName, + result + ) + ); + } else { + response = replyHandler( + sthServerUtils.getNGSIPayload( + request.params.version, + request.params.entityId, + request.params.entityType, + request.params.attrName, + result + ) + ); + } } sthServerUtils.addFiwareCorrelator(request, response); }); @@ -317,7 +393,7 @@ function getAggregatedData(request, reply) { * @param {Object} request The request * @param {Function} reply The reply() function of the hapi server */ -function getDataHandler(request, reply) { +function getDataHandler(request, reply, replyHandler) { request.sth = request.sth || {}; request.sth.context = sthServerUtils.getContext(request); let message; @@ -349,13 +425,17 @@ function getDataHandler(request, reply) { source: 'query', keys: ['lastN', 'hLimit', 'hOffset', 'filetype', 'aggrMethod', 'aggrPeriod', 'count'] }; - return reply(error); + if (reply) { + return reply.response(error); + } else { + return replyHandler(error); + } } } - getRawData(request, reply); + getRawData(request, reply, replyHandler); } else if (request.query.aggrMethod && request.query.aggrPeriod) { // Aggregated data is requested - getAggregatedData(request, reply); + getAggregatedData(request, reply, replyHandler); } else { message = 'A combination of the following query params is required: lastN, hLimit and hOffset, ' + @@ -369,7 +449,11 @@ function getDataHandler(request, reply) { source: 'query', keys: ['lastN', 'hLimit', 'hOffset', 'filetype', 'aggrMethod', 'aggrPeriod', 'count'] }; - return reply(error); + if (reply) { + return reply.response(error); + } else { + return replyHandler(error); + } } } diff --git a/lib/server/handlers/sthGetDataHandlerV2.js b/lib/server/handlers/sthGetDataHandlerV2.js index 163e316..3990c20 100644 --- a/lib/server/handlers/sthGetDataHandlerV2.js +++ b/lib/server/handlers/sthGetDataHandlerV2.js @@ -55,7 +55,7 @@ function getDataHandler(request, reply) { request.params.entityType = request.query.type; request.params.version = 2; - sthGetDataHandlerV1(request, replyHandler); + sthGetDataHandlerV1(request, null, replyHandler); } module.exports = getDataHandler;