1 var Class = require('../../mootools/mootools-node.js').Class; 2 var UrlRequest = require('./UrlRequest.js').UrlRequest; 3 4 /** 5 * @class OpalRequest 6 * @extends UrlRequest 7 * @requires Class 8 * @requires UrlRequest 9 * 10 */ 11 var OpalRequest = function(){ 12 13 /** @ignore */ 14 this.Extends = UrlRequest; 15 16 /** 17 * @private 18 * @property {String} _remoteMethod 19 */ 20 this._remoteMethod = null; 21 22 /** 23 * @private 24 * @property {Object} _gateway 25 */ 26 this._gateway = {host: null, port: 80}; 27 28 /** @ignore */ 29 this.initialize = function(data){ 30 if(!data.hasOwnProperty('url') || !data.hasOwnProperty('params') || !data.hasOwnProperty('method')){ 31 throw new Error('Given request is incomplete'); 32 } 33 34 this.setHeaders({ 35 'Content-Type': 'application/json-rpc' 36 }); 37 this.setGatewayHost(data.url); 38 39 if(data.url.substring(0, 8) == 'https://'){ 40 this.setGatewayPort(443); 41 this.setHost(data.url.replace('https://', '')); 42 }else{ 43 this.setGatewayPort(80); 44 this.setHost(data.url.replace('http://', '')); 45 } 46 47 this.setRemoteMethod(data.method); 48 this.setMethod(UrlRequest.POST); 49 50 if(data.hasOwnProperty('application')){ 51 this.setApplication(data.application); 52 }else{ 53 this.setApplication('localhost.front.onetapi.pl'); 54 } 55 56 var data = JSON.stringify({ 57 'jsonrpc': '2.0', 58 'id': this.getRemoteMethod() + '-' + +new Date(), 59 'method': this.getRemoteMethod(), 60 'params': data.params 61 }); 62 63 this.setData(data); 64 65 this.setHeaders('Content-Length', data.length); 66 //this.setHeaders('X-Onet-Cache', 'refresh'); 67 }; 68 69 /** 70 * sets hostname of a service endpoint 71 * @param {String} host 72 * @returns {OpalRequest} 73 */ 74 this.setHost = function(host){ 75 this.setHeaders('Host', host); 76 77 return this; 78 }; 79 80 /** 81 * returns hostname of a service endpoint 82 * @returns {OpalRequest} 83 */ 84 this.getHost = function(){ 85 return this.getHeaders()['Host']; 86 }; 87 88 /** 89 * sets the application ident string 90 * @param {String} app 91 * @returns {OpalRequest} 92 */ 93 this.setApplication = function(app){ 94 this.setHeaders('X-Onet-App', app); 95 96 return this; 97 }; 98 99 /** 100 * returns application ident string 101 * @returns {OpalRequest} 102 */ 103 this.getApplication = function(){ 104 return this.getHeaders()['X-Onet-App']; 105 }; 106 107 108 /** 109 * sets method of communicating with a service 110 * @param {String} method 111 * @returns {OpalRequest} 112 */ 113 this.setRemoteMethod = function(method){ 114 this._remoteMethod = method; 115 116 return this; 117 }; 118 119 /** 120 * returns method of communicating with a service 121 * 122 * @returns {OpalRequest} 123 */ 124 this.getRemoteMethod = function(){ 125 return this._remoteMethod; 126 }; 127 128 /** 129 * sets host that will handle request transmission 130 * @param {String} host 131 * @returns {OpalRequest} 132 */ 133 this.setGatewayHost = function(host){ 134 this._gateway.host = host; 135 136 return this; 137 }; 138 139 /** 140 * returns host that will handle request transmission 141 * @returns {OpalRequest} 142 */ 143 this.getGatewayHost = function(){ 144 return this._gateway.host; 145 }; 146 147 /** 148 * sets the port of a gateway host 149 * 150 * @param {Number} port 151 * @returns {OpalRequest} 152 */ 153 this.setGatewayPort = function(port){ 154 this._gateway.port = port; 155 156 return this; 157 }; 158 159 /** 160 * gets the port of a gateway host 161 * 162 * @returns {Number} 163 */ 164 this.getGatewayPort = function(){ 165 return this._gateway.port; 166 }; 167 }; 168 169 exports.OpalRequest = new Class(new OpalRequest());