-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathbrookmiddleware.pas
106 lines (84 loc) · 3.16 KB
/
brookmiddleware.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
(*
Brook for Free Pascal
Copyright (C) 2014-2019 Silvio Clecio
See the file LICENSE.txt, included in this distribution,
for details about the copyright.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*)
{ Middleware classes. }
unit BrookMiddleware;
{$i brook.inc}
interface
uses
BrookClasses, BrookAction, BrookRouter, BrookException, BrookHttpDefs,
BrookUtils;
type
{ Handles exceptions for @link(TBrookMiddleware). }
EBrookMiddleware = class(EBrook);
{ Is a metaclass for @link(TBrookMiddleware) class. }
TBrookMiddlewareClass = class of TBrookMiddleware;
{ Is a type to @code(*Middleware.OnExecute) event. }
TBrookMiddlewareExecuteEvent = procedure(ASender: TObject;
AAction: TBrookAction; ARoute: TBrookRoute) of object;
{ Defines a pointer to the @code(TBrookMiddlewareExecuteEvent) type.}
PBrookMiddlewareExecuteEvent = ^TBrookMiddlewareExecuteEvent;
{ Intermediates two classes through a @code(TBrookExecuteActionEvent) event. }
TBrookMiddleware = class(TBrookComponent)
private
FOldExecute: TBrookExecuteActionEvent;
FOnExecute: TBrookMiddlewareExecuteEvent;
protected
procedure DoExecute(ASender: TObject; AAction: TBrookAction;
ARequest: TBrookRequest; AResponse: TBrookResponse; const ANames,
AValues: TBrookArrayOfString; ARoute: TBrookRoute;
var AHandled: Boolean); virtual;
public
{ Creates an instance of a @link(TBrookMiddleware) class. }
constructor Create(ABoundEvent: PBrookExecuteActionEvent); overload; virtual;
{ Is triggered when the @code(DoExecute) method bound in this class is
executed. }
procedure Execute(ASender: TObject; AAction: TBrookAction;
ARoute: TBrookRoute); virtual;
{ Bindes a @code(TBrookExecuteActionEvent) event to this class keeping the
implementation of a previously declared event. }
procedure BindExecution(AEvent: PBrookExecuteActionEvent);
{ Is triggered when the @code(Execute) method bound in this class is
executed. }
property OnExecute: TBrookMiddlewareExecuteEvent read FOnExecute
write FOnExecute;
end;
implementation
{ TBrookCustomMiddleware }
constructor TBrookMiddleware.Create(ABoundEvent: PBrookExecuteActionEvent);
begin
inherited Create(nil);
BindExecution(ABoundEvent);
end;
procedure TBrookMiddleware.DoExecute(ASender: TObject; AAction: TBrookAction;
ARequest: TBrookRequest; AResponse: TBrookResponse; const ANames,
AValues: TBrookArrayOfString; ARoute: TBrookRoute; var AHandled: Boolean);
begin
Execute(ASender, AAction, ARoute);
if Assigned(FOldExecute) then
FOldExecute(ASender, AAction, ARequest, AResponse, ANames, AValues, ARoute,
AHandled);
end;
{$PUSH}{$WARN 5024 OFF}
procedure TBrookMiddleware.Execute(ASender: TObject; AAction: TBrookAction;
ARoute: TBrookRoute);
begin
if Assigned(FOnExecute) then
FOnExecute(Self, AAction, ARoute);
end;
{$POP}
procedure TBrookMiddleware.BindExecution(AEvent: PBrookExecuteActionEvent);
begin
if Assigned(AEvent) then
begin
FOldExecute := AEvent^;
AEvent^ := @DoExecute;
end;
end;
end.