brookmiddlewarehandler.pas 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. (*
  2. Brook framework, Middleware Handler Class
  3. Copyright (C) 2014 Silvio Clecio
  4. See the file LICENSE.txt, included in this distribution,
  5. for details about the copyright.
  6. This library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. *)
  10. unit BrookMiddlewareHandler;
  11. {$i brook.inc}
  12. interface
  13. uses
  14. BrookMiddleware, BrookRouter, BrookAction, BrookHttpDefs, BrookUtils;
  15. type
  16. { Handles exceptions for @link(TBrookMiddlewareHandler). }
  17. EBrookMiddlewareHandler = class(EBrookMiddleware);
  18. { Is a metaclass for @link(TBrookMiddlewareHandler) class. }
  19. TBrookMiddlewareHandlerClass = class of TBrookMiddlewareHandler;
  20. { Defines an enumerator to represent the middleware execution modes. }
  21. TBrookMiddlewareExecMode = (emBefore, emAfter);
  22. { Handles the middleware events. }
  23. TBrookMiddlewareHandler = class(TBrookMiddleware)
  24. private
  25. FExecMode: TBrookMiddlewareExecMode;
  26. FOnExecAction: TBrookExecuteActionEvent;
  27. protected
  28. procedure Loaded; override;
  29. procedure DoExecute(ASender: TObject; AAction: TBrookAction;
  30. ARequest: TBrookRequest; AResponse: TBrookResponse; const ANames,
  31. AValues: TBrookArrayOfString; ARoute: TBrookRoute;
  32. var AHandled: Boolean); override;
  33. published
  34. property OnExecute;
  35. { Defines if the middleware will be executed before or after the action
  36. execution. }
  37. property ExecMode: TBrookMiddlewareExecMode read FExecMode write FExecMode
  38. default emBefore;
  39. { Is triggered when the @code(DoExecute) method bound in this class is
  40. executed. }
  41. property OnExecAction: TBrookExecuteActionEvent read FOnExecAction
  42. write FOnExecAction;
  43. end;
  44. implementation
  45. { TBrookMiddlewareHandler }
  46. procedure TBrookMiddlewareHandler.Loaded;
  47. begin
  48. inherited Loaded;
  49. case FExecMode of
  50. emBefore: BindExecution(@TBrookRouter.Service.BeforeExecuteAction);
  51. emAfter: BindExecution(@TBrookRouter.Service.AfterExecuteAction);
  52. end;
  53. end;
  54. procedure TBrookMiddlewareHandler.DoExecute(ASender: TObject;
  55. AAction: TBrookAction; ARequest: TBrookRequest; AResponse: TBrookResponse;
  56. const ANames, AValues: TBrookArrayOfString; ARoute: TBrookRoute;
  57. var AHandled: Boolean);
  58. begin
  59. inherited DoExecute(ASender, AAction, ARequest, AResponse, ANames, AValues,
  60. ARoute, AHandled);
  61. if Assigned(FOnExecAction) then
  62. FOnExecAction(ASender, AAction, ARequest, AResponse, ANames, AValues,
  63. ARoute, AHandled);
  64. end;
  65. end.