UNPKG

12.8 kBJavaScriptView Raw
1/**
2 * @license
3 * MOST Web Framework 2.0 Codename Blueshift
4 * Copyright (c) 2017, THEMOST LP All rights reserved
5 *
6 * Use of this source code is governed by an BSD-3-Clause license that can be
7 * found in the LICENSE file at https://themost.io/license
8 */
9///
10var Args = require('@themost/common/utils').Args;
11var LangUtils = require('@themost/common/utils').LangUtils;
12var SequentialEventEmitter = require('@themost/common/emitter').SequentialEventEmitter;
13var AbstractMethodError = require('@themost/common/errors').AbstractMethodError;
14var AbstractClassError = require('@themost/common/errors').AbstractClassError;
15var Symbol = require('symbol');
16var applicationProperty = Symbol('application');
17var IApplicationService = require('@themost/common/app').IApplicationService;
18
19/**
20 * @abstract
21 * @class
22 * @constructor
23 * @param {HttpApplication} app
24 * @augments IApplicationService
25 */
26function HttpApplicationService(app) {
27 if (this.constructor === HttpApplicationService.prototype.constructor) {
28 throw new AbstractClassError();
29 }
30 Args.notNull(app, 'HTTP Application');
31 this[applicationProperty] = app;
32}
33LangUtils.inherits(HttpApplicationService,IApplicationService);
34/**
35 * @returns {HttpApplication}
36 */
37HttpApplicationService.prototype.getApplication = function() {
38 return this[applicationProperty];
39};
40
41/**
42 * Abstract view engine class
43 * @class HttpViewEngine
44 * @param {HttpContext} context
45 * @constructor
46 * @augments {SequentialEventEmitter}
47 */
48function HttpViewEngine(context) {
49 if (this.constructor === HttpViewEngine.prototype.constructor) {
50 throw new AbstractClassError();
51 }
52 /**
53 * @name HttpViewEngine#context
54 * @type HttpContext
55 * @description Gets or sets an instance of HttpContext that represents the current HTTP context.
56 */
57 /**
58 * @type {HttpContext}
59 */
60 var ctx = context;
61 Object.defineProperty(this,'context', {
62 get: function() {
63 return ctx;
64 },
65 set: function(value) {
66 ctx = value;
67 },
68 configurable:false,
69 enumerable:false
70 });
71}
72LangUtils.inherits(HttpViewEngine, SequentialEventEmitter);
73/**
74 * @returns {HttpContext}
75 */
76HttpViewEngine.prototype.getContext = function() {
77 return this.context;
78};
79
80/**
81 * @abstract
82 * @description Renders the specified view with the options provided
83 * @param {string} file
84 * @param {*} data
85 * @param {Function} callback
86 */
87// eslint-disable-next-line no-unused-vars
88HttpViewEngine.prototype.render = function(file, data, callback) {
89 throw new AbstractMethodError();
90};
91
92/**
93 * @class
94 * @abstract
95 */
96// eslint-disable-next-line no-unused-vars
97function BeginRequestHandler() {
98 if (this.constructor === BeginRequestHandler.prototype.constructor) {
99 throw new AbstractClassError();
100 }
101}
102/**
103 * Occurs as the first event in the HTTP execution
104 * @param {HttpContext} context
105 * @param {Function} callback
106 */
107BeginRequestHandler.prototype.beginRequest = function (context, callback) {
108 callback = callback || function () { };
109 return callback();
110};
111
112/**
113 * @class
114 * @abstract
115 */
116// eslint-disable-next-line no-unused-vars
117function ValidateRequestHandler() {
118 if (this.constructor === ValidateRequestHandler.prototype.constructor) {
119 throw new AbstractClassError();
120 }
121}
122/**
123 * Occurs when a handler is going to validate current HTTP request.
124 * @param {HttpContext} context
125 * @param {Function} callback
126 */
127ValidateRequestHandler.prototype.validateRequest = function (context, callback) {
128 callback = callback || function () { };
129 return callback();
130};
131
132/**
133 * @class
134 * @abstract
135 */
136// eslint-disable-next-line no-unused-vars
137function AuthenticateRequestHandler() {
138 if (this.constructor === AuthenticateRequestHandler.prototype.constructor) {
139 throw new AbstractClassError();
140 }
141}
142/**
143 * Occurs when a handler is going to set current user identity.
144 * @param {HttpContext} context
145 * @param {Function} callback
146 */
147AuthenticateRequestHandler.prototype.authenticateRequest = function (context, callback) {
148 callback = callback || function () { };
149 return callback();
150};
151
152/**
153 * @class
154 * @abstract
155 */
156// eslint-disable-next-line no-unused-vars
157function AuthorizeRequestHandler() {
158 if (this.constructor === AuthorizeRequestHandler.prototype.constructor) {
159 throw new AbstractClassError();
160 }
161}
162/**
163 * Occurs when a handler has verified user authorization.
164 * @param {HttpContext} context
165 * @param {Function} callback
166 */
167AuthorizeRequestHandler.prototype.authorizeRequest = function (context, callback) {
168 callback = callback || function () { };
169 return callback();
170};
171/**
172 * @class
173 * @abstract
174 */
175// eslint-disable-next-line no-unused-vars
176function MapRequestHandler() {
177 if (this.constructor === MapRequestHandler.prototype.constructor) {
178 throw new AbstractClassError();
179 }
180}
181/**
182 * Occurs when the handler is selected to respond to the request.
183 * @param {HttpContext} context
184 * @param {Function} callback
185 */
186MapRequestHandler.prototype.mapRequest = function (context, callback) {
187 callback = callback || function () { };
188 return callback();
189};
190/**
191 * @class
192 * @abstract
193 */
194// eslint-disable-next-line no-unused-vars
195function PostMapRequestHandler() {
196 if (this.constructor === PostMapRequestHandler.prototype.constructor) {
197 throw new AbstractClassError();
198 }
199}
200
201/**
202 * Occurs when application has mapped the current request to the appropriate handler.
203 * @param {HttpContext} context
204 * @param {Function} callback
205 */
206PostMapRequestHandler.prototype.postMapRequest = function(context, callback) {
207 callback = callback || function () { };
208 return callback();
209};
210
211/**
212 * @class
213 * @abstract
214 */
215// eslint-disable-next-line no-unused-vars
216function ProcessRequestHandler() {
217 if (this.constructor === ProcessRequestHandler.prototype.constructor) {
218 throw new AbstractClassError();
219 }
220}
221/**
222 * Occurs when application starts processing current HTTP request.
223 * @param {HttpContext} context
224 * @param {Function} callback
225 */
226ProcessRequestHandler.prototype.processRequest = function (context, callback) {
227 callback = callback || function () { };
228 return callback();
229};
230
231/**
232 * @class
233 * @abstract
234 */
235// eslint-disable-next-line no-unused-vars
236function PreExecuteResultHandler() {
237 if (this.constructor === PreExecuteResultHandler.prototype.constructor) {
238 throw new AbstractClassError();
239 }
240}
241/**
242 * Occurs when application starts executing an HTTP Result.
243 * @param {HttpContext} context
244 * @param {Function} callback
245 */
246PreExecuteResultHandler.prototype.preExecuteResult = function (context, callback) {
247 callback = callback || function () { };
248 return callback();
249};
250/**
251 * @class
252 * @abstract
253 */
254// eslint-disable-next-line no-unused-vars
255function PostExecuteResultHandler() {
256 if (this.constructor === PreExecuteResultHandler.prototype.constructor) {
257 throw new AbstractClassError();
258 }
259}
260/**
261 * Occurs when application was successfully executes an HTTP Result.
262 * @param {HttpContext} context
263 * @param {Function} callback
264 */
265PostExecuteResultHandler.prototype.postExecuteResult = function (context, callback) {
266 callback = callback || function () { };
267 return callback();
268};
269
270
271/**
272 * @class
273 * @abstract
274 */
275// eslint-disable-next-line no-unused-vars
276function EndRequestHandler() {
277 if (this.constructor === EndRequestHandler.prototype.constructor) {
278 throw new AbstractClassError();
279 }
280}
281/**
282 * Occurs as the first event in the HTTP execution
283 * @param {HttpContext} context
284 * @param {Function} callback
285 */
286EndRequestHandler.prototype.beginRequest = function (context, callback) {
287 callback = callback || function () { };
288 return callback();
289};
290
291/**
292 * @classdesc An abstract class that represents an HTTP Handler
293 * @class
294 * @abstract
295 * @constructor
296 */
297function HttpHandler() {
298 if (this.constructor === HttpHandler.prototype.constructor) {
299 throw new AbstractClassError();
300 }
301}
302
303/**
304 * Gets an array of strings which represent the collection of events of an HttpHandler instance
305 * @type {Array.<string>}
306 */
307HttpHandler.Events = [
308 'beginRequest',
309 'validateRequest',
310 'authenticateRequest',
311 'authorizeRequest',
312 'mapRequest',
313 'postMapRequest',
314 'preExecuteResult',
315 'postExecuteResult',
316 'endRequest'
317];
318
319/**
320 * Occurs as the first event in the HTTP execution
321 * @param {HttpContext} context
322 * @param {Function} callback
323 */
324HttpHandler.prototype.beginRequest = function (context, callback) {
325 callback = callback || function () { };
326 return callback();
327};
328
329/**
330 * Occurs when a handler is going to validate current HTTP request.
331 * @param {HttpContext} context
332 * @param {Function} callback
333 */
334HttpHandler.prototype.validateRequest = function (context, callback) {
335 callback = callback || function () { };
336 return callback();
337};
338
339/**
340 * Occurs when a handler is going to set current user identity.
341 * @param {HttpContext} context
342 * @param {Function} callback
343 */
344HttpHandler.prototype.authenticateRequest = function (context, callback) {
345 callback = callback || function () { };
346 return callback();
347};
348
349/**
350 * Occurs when a handler has established the identity of the current user.
351 * @param {HttpContext} context
352 * @param {Function} callback
353 */
354/*HttpHandler.prototype.postAuthenticateRequest = function(context, callback) {
355 callback = callback || function() {};
356 callback.call(context);
357 };*/
358
359
360/**
361 * Occurs when a handler has verified user authorization.
362 * @param {HttpContext} context
363 * @param {Function} callback
364 */
365HttpHandler.prototype.authorizeRequest = function (context, callback) {
366 callback = callback || function () { };
367 return callback();
368};
369
370/**
371 * Occurs when the handler is selected to respond to the request.
372 * @param {HttpContext} context
373 * @param {Function} callback
374 */
375HttpHandler.prototype.mapRequest = function (context, callback) {
376 callback = callback || function () { };
377 return callback();
378};
379
380/**
381 * Occurs when application has mapped the current request to the appropriate handler.
382 * @param {HttpContext} context
383 * @param {Function} callback
384 */
385HttpHandler.prototype.postMapRequest = function(context, callback) {
386 callback = callback || function () { };
387 return callback();
388};
389
390/**
391 * Occurs just before application starts executing a handler.
392 * @param {HttpContext} context
393 * @param {Function} callback
394 */
395/*HttpHandler.prototype.preRequestHandlerExecute = function(context, callback) {
396 callback = callback || function() {};
397 callback.call(context);
398 };*/
399
400/**
401 * Occurs when application starts processing current HTTP request.
402 * @param {HttpContext} context
403 * @param {Function} callback
404 */
405HttpHandler.prototype.processRequest = function (context, callback) {
406 callback = callback || function () { };
407 return callback();
408};
409
410/**
411 * Occurs when application starts executing an HTTP Result.
412 * @param {HttpContext} context
413 * @param {Function} callback
414 */
415HttpHandler.prototype.preExecuteResult = function (context, callback) {
416 callback = callback || function () { };
417 return callback();
418};
419
420/**
421 * Occurs when application was successfully executes an HTTP Result.
422 * @param {HttpContext} context
423 * @param {Function} callback
424 */
425HttpHandler.prototype.postExecuteResult = function (context, callback) {
426 callback = callback || function () { };
427 return callback();
428};
429
430/**
431 * Occurs when the handler finishes execution.
432 * @param {HttpContext} context
433 * @param {Function} callback
434 */
435/*HttpHandler.prototype.postRequestHandlerExecute = function(context, callback) {
436 callback = callback || function() {};
437 callback.call(context);
438 };*/
439
440/**
441 * Occurs as the last event in the HTTP execution
442 * @param {HttpContext} context
443 * @param {Function} callback
444 */
445HttpHandler.prototype.endRequest = function (context, callback) {
446 callback = callback || function () { };
447 return callback();
448};
449
450
451if (typeof exports !== 'undefined')
452{
453 module.exports.HttpApplicationService = HttpApplicationService;
454 module.exports.HttpViewEngine = HttpViewEngine;
455 module.exports.BeginRequestHandler = BeginRequestHandler;
456 module.exports.AuthenticateRequestHandler = AuthenticateRequestHandler;
457 module.exports.AuthorizeRequestHandler = AuthorizeRequestHandler;
458 module.exports.MapRequestHandler = MapRequestHandler;
459 module.exports.PostMapRequestHandler = PostMapRequestHandler;
460 module.exports.ProcessRequestHandler = ProcessRequestHandler;
461 module.exports.PreExecuteResultHandler = PreExecuteResultHandler;
462 module.exports.PostExecuteResultHandler = PostExecuteResultHandler;
463 module.exports.EndRequestHandler = EndRequestHandler;
464 module.exports.HttpHandler = HttpHandler;
465}
466