forked from CPsoftBE/BackupOfCromis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cromis.MVC.Sessions.pas
512 lines (439 loc) · 14.4 KB
/
Cromis.MVC.Sessions.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
unit Cromis.MVC.Sessions;
interface
uses
Windows, SysUtils, Classes, Contnrs, DateUtils,
// library units
Cromis.MVC.Controller,
Cromis.MVC.Routing,
Cromis.MVC.Common,
// other cromis units
Cromis.Cryptography,
Cromis.Scheduler,
Cromis.Threading,
HashList;
type
IAuthenticationRoute = Interface(IInterface)
['{EE9B20C3-D8C5-469D-919C-B4B6970339FA}']
function GetController: string;
function GetAuthCommand: string;
function GetLoginCommand: string;
procedure SetController(const Value: string);
procedure SetAuthCommand(const Value: string);
procedure SetLoginCommand(const Value: string);
property Controller: string read GetController write SetController;
property AuthCommand: string read GetAuthCommand write SetAuthCommand;
property LoginCommand: string read GetLoginCommand write SetLoginCommand;
end;
IMVCSession = Interface(IInterface)
['{BE3A976A-9B80-4606-8D84-E4748E11F536}']
function GetID: string;
function GetMVCFactory: IMVCFactory;
function GetLastRequest: TDateTime;
function GetSessionData: ISessionData;
function GetAuthenticationRoute: IAuthenticationRoute;
procedure DispatchRequest(const WebRoute: IMVCRoute; const Data :IMVCData);
procedure InitializeRequest;
procedure FinalizeRequest;
procedure Initialize;
procedure Finalize;
// properties of the MVCSession interface
property AuthenticationRoute: IAuthenticationRoute read GetAuthenticationRoute;
property SessionData: ISessionData read GetSessionData;
property LastRequest: TDateTime read GetLastRequest;
property MVCFactory: IMVCFactory read GetMVCFactory;
property ID: string read GetID;
end;
IDispatchData = Interface(IInterface)
['{DBEFDBB4-04F4-45E0-9249-405A7B4BA050}']
function GetSession: IMVCSession;
function GetDispatchData: IMVCData;
property DispatchData: IMVCData read GetDispatchData;
property Session: IMVCSession read GetSession;
end;
// on redirect login action
TOnSessionDispatch = procedure(const DispatchInfo: IDispatchData) of Object;
ISessionEvents = Interface(IInterface)
['{EED2B578-F936-4D5F-8063-AAC7918C4554}']
function GetOnSessionDispatch: TOnSessionDispatch;
procedure SetOnSessionDispatch(const Value: TOnSessionDispatch);
property OnSessionDispatch: TOnSessionDispatch read GetOnSessionDispatch write SetOnSessionDispatch;
end;
TMVCSession = class(TInterfacedObject, IMVCSession)
private
FID: string;
FTaskQueue: ITaskQueue;
FMVCFactory: IMVCFactory;
FLastRequest: TDateTime;
FSessionData: ISessionData;
FSessionEvents: ISessionEvents;
FAuthenticationRoute: IAuthenticationRoute;
function GetAuthenticationRoute: IAuthenticationRoute;
function GetSessionData: ISessionData;
function GetMVCFactory: IMVCFactory;
function GetLastRequest: TDateTime;
function GetID: string;
public
destructor Destroy; override;
constructor Create(const ID: string; const SessionEvents: ISessionEvents = nil);
procedure DispatchRequest(const WebRoute: IMVCRoute; const Data :IMVCData);
procedure InitializeRequest; virtual;
procedure FinalizeRequest; virtual;
procedure Initialize; virtual;
procedure Finalize; virtual;
// properties of the MVCSession interface
property AuthenticationRoute: IAuthenticationRoute read GetAuthenticationRoute;
property SessionData: ISessionData read GetSessionData;
property LastRequest: TDateTime read GetLastRequest;
property MVCFactory: IMVCFactory read GetMVCFactory;
property ID: string read GetID;
end;
// the family of MVC sessions
TMVCSessionClass = class of TMVCSession;
TMVCSessionManager = class
private
FSessions: THashList;
FIdleEvent: TScheduledEvent;
FSessionCS: TRTLCriticalSection;
FMVCFactory: IMVCFactory;
FSessionEvents: ISessionEvents;
procedure OnIdleSchedule(Sender: TScheduledEvent);
function OnIdleCleanup(AUserData: Pointer; const AStr: string; var APtr: Pointer): Boolean;
function CreateNewSession(const ID: string; const SessionClass: TClass): IMVCSession;
function GetSessionEvents: ISessionEvents;
function GetMVCFactory: IMVCFactory;
public
constructor Create;
destructor Destroy; override;
function SessionCount: Integer;
procedure ReleaseSession(const ID: string);
function AcquireSession(const ID: string; const SessionClass: TClass): IMVCSession;
property SessionEvents: ISessionEvents read GetSessionEvents;
property MVCFactory: IMVCFactory read GetMVCFactory;
end;
implementation
type
TSessionWrapper = class
private
FSession: IMVCSession;
public
destructor Destroy; override;
constructor Create(const Session: IMVCSession);
property Session: IMVCSession read FSession;
end;
TAuthenticationRoute = class(TInterfacedObject, IAuthenticationRoute)
private
FController: string;
FAuthCommand: string;
FLoginCommand: string;
function GetController: string;
function GetAuthCommand: string;
function GetLoginCommand: string;
procedure SetController(const Value: string);
procedure SetAuthCommand(const Value: string);
procedure SetLoginCommand(const Value: string);
public
property Controller: string read GetController write SetController;
property AuthCommand: string read GetAuthCommand write SetAuthCommand;
property LoginCommand: string read GetLoginCommand write SetLoginCommand;
end;
TDispatchData = class(TInterfacedObject, IDispatchData)
private
FSession: IMVCSession;
FDispatchData: IMVCData;
function GetSession: IMVCSession;
function GetDispatchData: IMVCData;
public
constructor Create(const DispatchData: IMVCData; const Session: IMVCSession);
property DispatchData: IMVCData read GetDispatchData;
property Session: IMVCSession read GetSession;
end;
TSessionEvents = class(TInterfacedObject, ISessionEvents)
private
FOnSessionDispatch: TOnSessionDispatch;
function GetOnSessionDispatch: TOnSessionDispatch;
procedure SetOnSessionDispatch(const Value: TOnSessionDispatch);
public
property OnSessionDispatch: TOnSessionDispatch read GetOnSessionDispatch write SetOnSessionDispatch;
end;
{ TMVCSessionManager }
function TMVCSessionManager.AcquireSession(const ID: string; const SessionClass: TClass): IMVCSession;
var
SessionWrapper: TSessionWrapper;
begin
SessionWrapper := TSessionWrapper(FSessions.Data[ID]);
// session was not found
if SessionWrapper = nil then
begin
EnterCriticalSection(FSessionCS);
try
Result := CreateNewSession(ID, SessionClass);
Result.Initialize;
finally
LeaveCriticalSection(FSessionCS);
end;
end
else
Result := SessionWrapper.Session;
end;
constructor TMVCSessionManager.Create;
begin
InitializeCriticalSection(FSessionCS);
FSessionEvents := TSessionEvents.Create;
FMVCFactory := AcquireMVCFactory;
FSessions := THashList.Create(CaseInsensitiveTraits, 1000);
FSessions.OwnsObjets := True;
FIdleEvent := TScheduledEvent.Create('OnIdleCleanup');
FIdleEvent.Schedule.SetEventInterval(0,0,0,0,5,0);
FIdleEvent.OnSchedule := OnIdleSchedule;
FIdleEvent.Run;
end;
function TMVCSessionManager.CreateNewSession(const ID: string; const SessionClass: TClass): IMVCSession;
var
SessionWrapper: TSessionWrapper;
begin
SessionWrapper := TSessionWrapper.Create(TMVCSessionClass(SessionClass).Create(GetNewFormatedGUID, FSessionEvents));
FSessions.Add(SessionWrapper.Session.ID, SessionWrapper);
SessionWrapper.Session.MVCFactory.Assign(FMVCFactory);
Result := SessionWrapper.Session;
end;
destructor TMVCSessionManager.Destroy;
begin
DeleteCriticalSection(FSessionCS);
FreeAndNil(FIdleEvent);
FreeAndNil(FSessions);
FMVCFactory.Cleanup;
FMVCFactory := nil;
inherited;
end;
function TMVCSessionManager.GetMVCFactory: IMVCFactory;
begin
Result := FMVCFactory;
end;
function TMVCSessionManager.GetSessionEvents: ISessionEvents;
begin
Result := FSessionEvents;
end;
function TMVCSessionManager.SessionCount: Integer;
begin
EnterCriticalSection(FSessionCS);
try
Result := FSessions.Count;
finally
LeaveCriticalSection(FSessionCS);
end;
end;
function TMVCSessionManager.OnIdleCleanup(AUserData: Pointer; const AStr: string; var APtr: Pointer): Boolean;
begin
Result := True;
try
if SecondsBetween(TSessionWrapper(APtr).Session.LastRequest, Now) > 300 then
TObjectList(AUserData).Add(TSessionWrapper(APtr));
except
Result := False;
end;
end;
procedure TMVCSessionManager.OnIdleSchedule(Sender: TScheduledEvent);
var
I: Integer;
IdleCleanList: TObjectList;
SessionWrapper: TSessionWrapper;
begin
EnterCriticalSection(FSessionCS);
try
IdleCleanList := TObjectList.Create(False);
try
FSessions.IterateMethod(IdleCleanList, OnIdleCleanup);
finally
try
for I := IdleCleanList.Count - 1 downto 0 do
begin
SessionWrapper := TSessionWrapper(IdleCleanList[I]);
FSessions.Remove(SessionWrapper.Session.ID);
IdleCleanList[I].Free;
end;
finally
IdleCleanList.Free;
end;
end;
finally
LeaveCriticalSection(FSessionCS);
end;
end;
procedure TMVCSessionManager.ReleaseSession(const ID: string);
var
SessionWrapper: TSessionWrapper;
begin
EnterCriticalSection(FSessionCS);
try
SessionWrapper := FSessions.Data[ID];
FSessions.Remove(ID);
SessionWrapper.Free;
finally
LeaveCriticalSection(FSessionCS);
end;
end;
{ TMVCWebSession }
constructor TMVCSession.Create(const ID: string; const SessionEvents: ISessionEvents);
begin
FAuthenticationRoute := TAuthenticationRoute.Create;
FSessionData := AcquireSessionData(ID);
FTaskQueue := AcquireTaskQueue;
// create the MVC factory with session data attached
FMVCFactory := AcquireMVCFactory(FSessionData);
// passed on data from manager
FSessionEvents := SessionEvents;
FID := ID;
end;
destructor TMVCSession.Destroy;
begin
FMVCFactory.Cleanup;
FMVCFactory := nil;
inherited;
end;
procedure TMVCSession.Finalize;
begin
// virtual stub
end;
procedure TMVCSession.FinalizeRequest;
begin
SessionData.SessionContent.Encoding := '';
SessionData.SessionContent.Clear;
end;
function TMVCSession.GetAuthenticationRoute: IAuthenticationRoute;
begin
Result := FAuthenticationRoute;
end;
function TMVCSession.GetID: string;
begin
Result := FID;
end;
function TMVCSession.GetLastRequest: TDateTime;
begin
Result := FLastRequest;
end;
function TMVCSession.GetMVCFactory: IMVCFactory;
begin
Result := FMVCFactory;
end;
function TMVCSession.GetSessionData: ISessionData;
begin
Result := FSessionData;
end;
procedure TMVCSession.Initialize;
begin
FLastRequest := Now;
end;
procedure TMVCSession.InitializeRequest;
begin
SessionData.SessionContent.Encoding := 'text/html';
end;
procedure TMVCSession.DispatchRequest(const WebRoute: IMVCRoute; const Data: IMVCData);
var
DispatchData: IDispatchData;
MVCController: IMVCController;
AuthController: IMVCController;
begin
FTaskQueue.EnqueueTask.WaitFor;
try
SessionData.SessionContent.ExitCode := 200;
SessionData.DispatchResult := drSuccess;
FLastRequest := Now;
if not FSessionData.Authenticated then
begin
if SameText(FAuthenticationRoute.Controller, WebRoute.Controller) and
SameText(FAuthenticationRoute.AuthCommand, WebRoute.Command) then
begin
SessionData.DispatchResult := drDoLogin;
AuthController := MVCFactory.GetControler(AuthenticationRoute.Controller);
AuthController.GetModel.ExecuteCommand(AuthenticationRoute.AuthCommand, WebRoute.ID, Data);
end
else
begin
SessionData.DispatchResult := drRedirectToLogin;
AuthController := MVCFactory.GetControler(AuthenticationRoute.Controller);
AuthController.GetModel.ExecuteCommand(AuthenticationRoute.LoginCommand, WebRoute.ID, Data);
end;
end
else
begin
// try to get the controller if it exists
MVCController := MVCFactory.GetControler(WebRoute.Controller);
if MVCController = nil then
MVCController := MVCFactory.DefaultControler;
if MVCController = nil then
begin
SessionData.DispatchResult := drFailed;
Exit;
end;
// normal MVC action for the session
MVCController.GetModel.ExecuteCommand(WebRoute.Command, WebRoute.ID, Data);
end;
// inform the listener of the session dispatch
if (FSessionEvents <> nil) and Assigned(FSessionEvents.OnSessionDispatch) then
begin
DispatchData := TDispatchData.Create(Data, Self);
FSessionEvents.OnSessionDispatch(DispatchData);
end;
finally
FTaskQueue.DequeueTask;
end;
end;
{ TSessionWrapper }
constructor TSessionWrapper.Create(const Session: IMVCSession);
begin
FSession := Session;
end;
destructor TSessionWrapper.Destroy;
begin
FSession := nil;
inherited;
end;
{ TAuthenticationRoute }
function TAuthenticationRoute.GetAuthCommand: string;
begin
Result := FAuthCommand;
end;
function TAuthenticationRoute.GetController: string;
begin
Result := FController;
end;
function TAuthenticationRoute.GetLoginCommand: string;
begin
Result := FLoginCommand;
end;
procedure TAuthenticationRoute.SetAuthCommand(const Value: string);
begin
FAuthCommand := Value;
end;
procedure TAuthenticationRoute.SetController(const Value: string);
begin
FController := Value;
end;
procedure TAuthenticationRoute.SetLoginCommand(const Value: string);
begin
FLoginCommand := Value;
end;
{ TSessionEvents }
function TSessionEvents.GetOnSessionDispatch: TOnSessionDispatch;
begin
Result := FOnSessionDispatch;
end;
procedure TSessionEvents.SetOnSessionDispatch(const Value: TOnSessionDispatch);
begin
FOnSessionDispatch := Value;
end;
{ TDispatchData }
constructor TDispatchData.Create(const DispatchData: IMVCData; const Session: IMVCSession);
begin
FDispatchData := DispatchData;
FSession := Session;
end;
function TDispatchData.GetDispatchData: IMVCData;
begin
Result := FDispatchData;
end;
function TDispatchData.GetSession: IMVCSession;
begin
Result := FSession;
end;
end.