UNPKG

dl

Version:

DreamLab Libs

103 lines (84 loc) 2.86 kB
/** * @overview OpalServerRequestProcessor * @copyright Dreamlab Onet.pl sp. z. o o 2012 */ var core = require('core'), JsonRpcRequestProcessor = core.jsonrpc.JsonRpcRequestProcessor, Types = core.common.Types, BinaryData = core.data.BinaryData, Request = core.http.Request, JsonRpcRequest = core.jsonrpc.JsonRpcRequest, qs = require('qs'); /** * @class OpalServerRequestProcessor * @classdesc OpalServerRequestProcessor * @extends JsonRpcRequestProcessor * * @requires JsonRpcRequestProcessor * @requires Types * @requires BinaryData * * @param {Server} server * @returns {OpalServerRequestProcessor} */ var OpalServerRequestProcessor = function (server) { JsonRpcRequestProcessor.call(this, server); this._callback = null; }; OpalServerRequestProcessor.prototype = Object.create(JsonRpcRequestProcessor.prototype); /** * Retrieves jsonrpc request from request body or uri * @returns {JsonRpcRequest} * @method * @private */ OpalServerRequestProcessor.prototype._getRequest = function () { var req; switch (this._request.getMethod()) { case Request.GET: //opalowy healthcheck GETem if (this._request.getPath().toLowerCase() == "/healthcheck") { req = new JsonRpcRequest({ id: "healthcheck", method: "_healthcheck" }); break; } var params = (decodeURI(this._request.getQueryString())).substring(1), body; if (!params) { throw new Error("Empty request"); } body = qs.parse(params); this._callback = body.callback; if (!body.hasOwnProperty('body')) { throw new Error("Wrong request"); } req = [new JsonRpcRequest(body.body)]; break; case Request.POST: req = JsonRpcRequestProcessor.prototype._getRequest.call(this); break; default: throw new Error("Unsupported method"); break; } return req; }; /** * Retrieves body from given JsonRpcResponse * In case of JSONP adds wraps response in callback * @method * @private * @param {JsonRpcResponse} response * @return {String} */ OpalServerRequestProcessor.prototype._getBody = function (response) { response = JsonRpcRequestProcessor.prototype._getBody.call(this, response); //opalowy healthcheck GETem if (this._request.getMethod() == Request.GET && this._request.getPath().toLowerCase() != "/healthcheck") { response = this._callback + "(" + response + ");"; } return response; }; exports.OpalServerRequestProcessor = OpalServerRequestProcessor;