-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathuRESTDWServerEvents.pas
More file actions
1133 lines (1066 loc) · 43.7 KB
/
uRESTDWServerEvents.pas
File metadata and controls
1133 lines (1066 loc) · 43.7 KB
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit uRESTDWServerEvents;
{$I ..\Includes\uRESTDW.inc}
{
REST Dataware .
Criado por XyberX (Gilberto Rocha da Silva), o REST Dataware tem como objetivo o uso de REST/JSON
de maneira simples, em qualquer Compilador Pascal (Delphi, Lazarus e outros).
O REST Dataware também tem por objetivo levar componentes compatíveis entre o Delphi e outros Compiladores
Pascal e com compatibilidade entre sistemas operacionais.
Desenvolvido para ser usado de Maneira RAD, o REST Dataware tem como objetivo principal você usuário que precisa
de produtividade e flexibilidade para produção de Serviços REST/JSON, simplificando o processo para você programador.
Maiores informações:
https://github.com/OpenSourceCommunityBrasil/REST-DataWare
}
{$IFNDEF RESTDWLAZARUS}
{$IFDEF FPC}
{$MODE OBJFPC}{$H+}
{$ENDIF}
{$ENDIF}
interface
Uses
SysUtils, Classes, uRESTDWJSONObject, uRESTDWConsts,
uRESTDWBasic, uRESTDWProtoTypes, uRESTDWTools, uRESTDWParams,
uRESTDWJSONInterface, uRESTDWAbout;
Const
TServerEventsConst = '{"typeobject":"%s", "objectdirection":"%s", "objectvalue":"%s", "paramname":"%s", "encoded":"%s", "default":"%s"}';
Type
TDWReplyEvent = Procedure(Var Params : TRESTDWParams;
Const Result : TStringList) Of Object;
PDWReplyEvent = ^TDWReplyEvent;
TDWReplyEventByType = Procedure(Var Params : TRESTDWParams;
Const Result : TStringList;
Const RequestType : TRequestType;
Var StatusCode : Integer;
RequestHeader : TStringList) Of Object;
PDWReplyEventByType = ^TDWReplyEventByType;
TDWAuthRequest = Procedure(Const Params : TRESTDWParams;
Var Rejected : Boolean;
Var ResultError : String;
Var StatusCode : Integer;
RequestHeader : TStringList) Of Object;
TObjectEvent = Procedure(aSelf : TComponent) Of Object;
TObjectExecute = Procedure(Const aSelf : TCollectionItem) Of Object;
TOnBeforeSend = Procedure(aSelf : TComponent) Of Object;
Type
TDWReplyEventData = Class(TComponent)
Private
vReplyEvent : TDWReplyEvent;
vDWReplyEventByType : TDWReplyEventByType;
vDWAuthRequest : TDWAuthRequest;
vBeforeExecute : TObjectExecute;
Public
Property OnReplyEvent : TDWReplyEvent Read vReplyEvent Write vReplyEvent;
Property OnReplyEventByType : TDWReplyEventByType Read vDWReplyEventByType Write vDWReplyEventByType;
Property OnAuthRequest : TDWAuthRequest Read vDWAuthRequest Write vDWAuthRequest;
Property OnBeforeExecute : TObjectExecute Read vBeforeExecute Write vBeforeExecute;
End;
Type
TRESTDWEvent = Class;
TRESTDWEvent = Class(TCollectionItem)
Protected
Private
vDescription : TStrings;
vDWRoutes : TRESTDWRoutes;
vDataMode : TDataMode;
vBaseURL,
vEventName,
vContentType,
FName : String;
vDWParams : TRESTDWParamsMethods;
vOwnerCollection : TCollection;
vCallbackEvent,
vOnlyPreDefinedParams : Boolean;
DWReplyEventData : TDWReplyEventData;
vBeforeExecute : TObjectExecute;
Function GetReplyEvent : TDWReplyEvent;
Procedure SetReplyEvent (Value : TDWReplyEvent);
Function GetReplyEventByType : TDWReplyEventByType;
Procedure SetReplyEventByType(Value : TDWReplyEventByType);
Function GetAuthRequest : TDWAuthRequest;
Procedure SetAuthRequest (Value : TDWAuthRequest);
Procedure SetDescription (Strings : TStrings);
Procedure SetBaseUrl (Value : String);
Procedure SetContentType(Value : String);
Procedure SetDataMode (Value : TDataMode);
Property Name : String Read GetDisplayName Write SetDisplayName;
Public
Function GetDisplayName : String; Override;
Procedure SetDisplayName(Const Value : String); Override;
Procedure CompareParams (Var Dest : TRESTDWParams);
Procedure Assign (Source : TPersistent); Override;
Constructor Create (aCollection : TCollection); Override;
Function GetNamePath : String; Override;
Destructor Destroy; Override;
Published
Property Routes : TRESTDWRoutes Read vDWRoutes Write vDWRoutes;
Property Params : TRESTDWParamsMethods Read vDWParams Write vDWParams;
Property DataMode : TDataMode Read vDataMode Write SetDataMode;
Property EventName : String Read FName Write FName;
Property BaseURL : String Read vBaseURL Write SetBaseURL;
Property DefaultContentType : String Read vContentType Write SetContentType;
Property CallbackEvent : Boolean Read vCallbackEvent Write vCallbackEvent;
Property Description : TStrings Read vDescription Write SetDescription;
Property OnlyPreDefinedParams : Boolean Read vOnlyPreDefinedParams Write vOnlyPreDefinedParams;
Property OnReplyEvent : TDWReplyEvent Read GetReplyEvent Write SetReplyEvent;
Property OnReplyEventByType : TDWReplyEventByType Read GetReplyEventByType Write SetReplyEventByType;
Property OnAuthRequest : TDWAuthRequest Read GetAuthRequest Write SetAuthRequest;
Property OnBeforeExecute : TObjectExecute Read vBeforeExecute Write vBeforeExecute;
End;
Type
TRESTDWEventList = Class;
TRESTDWEventList = Class(TRESTDWOwnedCollection)
Protected
vEditable : Boolean;
Function GetOwner: TPersistent; override;
Private
fOwner : TPersistent;
Function GetRec (Index : Integer) : TRESTDWEvent; Overload;
Procedure PutRec (Index : Integer;
Item : TRESTDWEvent); Overload;
Procedure ClearList;
Function GetRecName(Index : String) : TRESTDWEvent; Overload;
Procedure PutRecName(Index : String;
Item : TRESTDWEvent); Overload;
// Procedure Editable (Value : Boolean);
Public
Function Add : TCollectionItem;
Function AddEvent (Const EventName : String;
BaseURL : String = '/';
OnReplyEvent : TDWReplyEvent = Nil;
ContentType : String = cApplicationJSON;
Datamode : TDataMode = dmRAW) : TRESTDWEvent;Overload;
Function AddEvent (Const EventName : String;
BaseURL : String = '/';
OnReplyEvent : TDWReplyEventByType = Nil;
ContentType : String = cApplicationJSON;
Datamode : TDataMode = dmRAW) : TRESTDWEvent;Overload;
Constructor Create (AOwner : TPersistent;
aItemClass : TCollectionItemClass);
Destructor Destroy; Override;
Function ToJSON : String;
Procedure FromJSON (Value : String );
Procedure Delete (Index : Integer); Overload;
Property Items [Index : Integer] : TRESTDWEvent Read GetRec Write PutRec; Default;
Property EventByName[Index : String ] : TRESTDWEvent Read GetRecName Write PutRecName;
End;
Type
TRESTDWServerEvents = Class(TRESTDWComponent)
Protected
Private
vIgnoreInvalidParams : Boolean;
vEventList : TRESTDWEventList;
vDefaultEvent,
vAccessTag : String;
vOnCreate : TObjectEvent;
Public
Destructor Destroy; Override;
Constructor Create(AOwner : TComponent);Override; //Cria o Componente
Procedure CreateDWParams(EventName : String;
Var DWParams : TRESTDWParams);
Published
Property IgnoreInvalidParams : Boolean Read vIgnoreInvalidParams Write vIgnoreInvalidParams;
Property Events : TRESTDWEventList Read vEventList Write vEventList;
Property AccessTag : String Read vAccessTag Write vAccessTag;
Property DefaultEvent : String Read vDefaultEvent Write vDefaultEvent;
Property OnCreate : TObjectEvent Read vOnCreate Write vOnCreate;
End;
Type
{ TRESTDWClientEvents }
TRESTDWClientEvents = Class(TRESTDWComponent)
Private
vServerEventName : String;
vEditParamList,
vGetEvents : Boolean;
vEventList : TRESTDWEventList;
vRESTClientPooler : TRESTClientPoolerBase;
vOnBeforeSend : TOnBeforeSend;
vCripto : TCripto;
Procedure GetOnlineEvents (Value : Boolean);
Procedure SetEventList (aValue : TRESTDWEventList);
Function GeTRESTClientPoolerBase : TRESTClientPoolerBase;
Procedure SeTRESTClientPoolerBase(Const Value : TRESTClientPoolerBase);
Procedure TokenValidade(Var DWParams : TRESTDWParams;
Var Error : String);
Protected
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
Public
Destructor Destroy; Override;
Constructor Create (AOwner : TComponent);Override; //Cria o Componente
Procedure CreateDWParams(EventName : String;
Var DWParams : TRESTDWParams);
Function SendEvent (EventName : String;
Var DWParams : TRESTDWParams;
Var Error : String;
EventType : TSendEvent = sePOST;
Assyncexec : Boolean = False) : Boolean; Overload;
Function SendEvent (EventName : String;
Var DWParams : TRESTDWParams;
Var Error : String;
Var NativeResult : String;
EventType : TSendEvent = sePOST;
Assyncexec : Boolean = False) : Boolean; Overload;
Procedure ClearEvents;
Property GetEvents : Boolean Read vGetEvents Write GetOnlineEvents;
Published
Property ServerEventName : String Read vServerEventName Write vServerEventName;
Property CriptOptions : TCripto Read vCripto Write vCripto;
Property RESTClientPooler : TRESTClientPoolerBase Read GeTRESTClientPoolerBase Write SeTRESTClientPoolerBase;
Property Events : TRESTDWEventList Read vEventList Write SetEventList;
Property OnBeforeSend : TOnBeforeSend Read vOnBeforeSend Write vOnBeforeSend; // Add Evento por Ico Menezes
End;
implementation
{ TRESTDWEvent }
Uses uRESTDWDataUtils;
Function TRESTDWEvent.GetNamePath: String;
Begin
Result := vOwnerCollection.GetNamePath + FName;
End;
constructor TRESTDWEvent.Create(aCollection: TCollection);
begin
Inherited;
vDWParams := TRESTDWParamsMethods.Create(aCollection, TRESTDWParamMethod);
vDataMode := dmDataware;
DWReplyEventData := TDWReplyEventData.Create(Nil);
vOwnerCollection := aCollection;
FName := 'dwevent' + IntToStr(aCollection.Count);
DWReplyEventData.Name := FName;
vCallbackEvent := False;
vOnlyPreDefinedParams := False;
vEventName := '';
vBaseURL := '/';
vDescription := TStringList.Create;
vDWRoutes := TRESTDWRoutes.Create;
vContentType := cDefaultContentType;
vCallbackEvent := False;
end;
Destructor TRESTDWEvent.Destroy;
Begin
vDWParams.Free;
vDWRoutes.Free;
DWReplyEventData.Free;
vDescription.Free;
Inherited;
End;
Function TRESTDWEvent.GetAuthRequest: TDWAuthRequest;
Begin
Result := DWReplyEventData.OnAuthRequest;
End;
Function TRESTDWEvent.GetDisplayName: String;
Begin
Result := FName;
End;
Procedure TRESTDWEvent.Assign(Source: TPersistent);
begin
If Source is TRESTDWEvent then
Begin
FName := TRESTDWEvent(Source).Name;
vDWParams := TRESTDWEvent(Source).Params;
DWReplyEventData.OnBeforeExecute := TRESTDWEvent(Source).OnBeforeExecute;
DWReplyEventData.OnReplyEvent := TRESTDWEvent(Source).OnReplyEvent;
End
Else
Inherited;
End;
Function TRESTDWEvent.GetReplyEvent: TDWReplyEvent;
Begin
Result := DWReplyEventData.OnReplyEvent;
End;
Function TRESTDWEvent.GetReplyEventByType : TDWReplyEventByType;
Begin
Result := DWReplyEventData.OnReplyEventByType;
End;
Procedure TRESTDWEvent.SetDescription(Strings : TStrings);
Begin
vDescription.Assign(Strings);
End;
Procedure TRESTDWEvent.SetAuthRequest(Value : TDWAuthRequest);
Begin
DWReplyEventData.OnAuthRequest := Value;
End;
Procedure TRESTDWEvent.SetDataMode (Value : TDataMode);
Begin
vDataMode := Value;
If vDataMode = dmDataware Then
vContentType := cDefaultContentType;
End;
Procedure TRESTDWEvent.SetContentType(Value : String);
Begin
If vDataMode = dmDataware Then
vContentType := cDefaultContentType
Else
vContentType := Value;
If Trim(vContentType) = '' Then
vContentType := cDefaultContentType;
End;
Procedure TRESTDWEvent.SetBaseUrl(Value : String);
Var
vTempValue : String;
Begin
vTempValue := Value;
If Trim(vTempValue) = '' Then
vBaseURL := '/'
Else
Begin
If Copy(vTempValue, 1, 1) <> '/' Then
vTempValue := '/' + vTempValue;
If Copy(vTempValue, Length(vTempValue), 1) <> '/' Then
vTempValue := vTempValue + '/';
vBaseURL := vTempValue;
End;
End;
Procedure TRESTDWEvent.SetDisplayName(Const Value: String);
Begin
If Trim(Value) = '' Then
Raise Exception.Create(cInvalidEvent)
Else
Begin
FName := Value;
DWReplyEventData.Name := FName;
If vEventName = '' Then
vEventName := DWReplyEventData.Name;
Inherited SetDisplayName(Value);
End;
End;
procedure TRESTDWEvent.SetReplyEvent(Value: TDWReplyEvent);
begin
DWReplyEventData.OnReplyEvent := Value;
end;
Procedure TRESTDWEvent.SetReplyEventByType(Value: TDWReplyEventByType);
Begin
DWReplyEventData.OnReplyEventByType := Value;
End;
Procedure TRESTDWEvent.CompareParams(Var Dest : TRESTDWParams);
Var
I : Integer;
Begin
If vOnlyPreDefinedParams Then
Begin
If Not Assigned(Dest) Then
Exit;
If vDWParams.Count = 0 Then
Dest.Clear;
For I := Dest.Count -1 DownTo 0 Do
Begin
If (vDWParams.ParamByName[Dest.Items[I].Alias] = Nil) And
(vDWParams.ParamByName[Dest.Items[I].ParamName] = Nil) Then
Dest.Delete(I);
End;
End;
End;
Function TRESTDWEventList.AddEvent(Const EventName : String;
BaseURL : String = '/';
OnReplyEvent : TDWReplyEvent = Nil;
ContentType : String = cApplicationJSON;
Datamode : TDataMode = dmRAW) : TRESTDWEvent;
Var
Event : TRESTDWEvent;
Begin
Event := TRESTDWEvent(Add);
Event.EventName := EventName;
Event.BaseUrl := BaseUrl;
Event.DefaultContentType := ContentType;
Event.DataMode := DataMode;
Event.OnReplyEvent := OnReplyEvent;
Result := Event;
End;
Function TRESTDWEventList.AddEvent(Const EventName : String;
BaseURL : String = '/';
OnReplyEvent : TDWReplyEventByType = Nil;
ContentType : String = cApplicationJSON;
Datamode : TDataMode = dmRAW) : TRESTDWEvent;
Var
Event : TRESTDWEvent;
Begin
Event := TRESTDWEvent(Add);
Event.EventName := EventName;
Event.BaseUrl := BaseUrl;
Event.DefaultContentType := ContentType;
Event.DataMode := DataMode;
Event.OnReplyEventByType := OnReplyEvent;
Result := Event;
End;
Function TRESTDWEventList.Add : TCollectionItem;
Begin
Result := Nil;
If vEditable Then
Result := TRESTDWEvent(Inherited Add);
End;
procedure TRESTDWEventList.ClearList;
Var
I : Integer;
vOldEditable : Boolean;
Begin
vOldEditable := vEditable;
vEditable := True;
Try
For I := Count - 1 Downto 0 Do
Delete(I);
Finally
Self.Clear;
vEditable := vOldEditable;
End;
End;
Constructor TRESTDWEventList.Create(AOwner : TPersistent;
aItemClass : TCollectionItemClass);
Begin
Inherited Create(AOwner, TRESTDWEvent);
Self.fOwner := AOwner;
vEditable := True;
End;
procedure TRESTDWEventList.Delete(Index: Integer);
begin
If (Index < Self.Count) And (Index > -1) And (vEditable) Then
TOwnedCollection(Self).Delete(Index);
end;
destructor TRESTDWEventList.Destroy;
begin
ClearList;
inherited;
end;
Procedure TRESTDWEventList.FromJSON(Value : String);
Var
bJsonOBJBase : TRESTDWJSONInterfaceBase;
bJsonOBJ,
bJsonOBJb,
bJsonOBJc : TRESTDWJSONInterfaceObject;
bJsonArray,
bJsonArrayB,
bJsonArrayC : TRESTDWJSONInterfaceArray;
I, X, Y : Integer;
vDWEvent : TRESTDWEvent;
vDWParamMethod : TRESTDWParamMethod;
vEventName,
vDataMode,
vparams,
vparamname,
vContentType : String;
vneedauth,
vonlypredefparams : Boolean;
Begin
Try
bJsonOBJBase := TRESTDWJSONInterfaceBase(TRESTDWJSONInterfaceObject.Create(Value));
bJsonArray := TRESTDWJSONInterfaceArray(bJsonOBJBase);
For I := 0 to bJsonArray.ElementCount - 1 Do
Begin
bJsonOBJ := TRESTDWJSONInterfaceObject(bJsonArray.GetObject(I));
Try
bJsonArrayB := bJsonOBJ.OpenArray('serverevents'); // Tjsonarray.Create(bJsonOBJ.get('serverevents').tostring);
For X := 0 To bJsonArrayB.ElementCount - 1 Do
Begin
bJsonOBJb := TRESTDWJSONInterfaceObject(bJsonArrayB.GetObject(X));
vEventName := bJsonOBJb.Pairs[0].Value; //eventname
vDataMode := bJsonOBJb.Pairs[1].Value; //DataMode
vparams := bJsonOBJb.Pairs[2].Value; //params
vneedauth := StringToBoolean(bJsonOBJb.Pairs[3].Value); //params
vonlypredefparams := StringToBoolean(bJsonOBJb.Pairs[4].Value); //params
vContentType := DecodeStrings(bJsonOBJb.Pairs[5].Value{$IFDEF FPC}, csUndefined{$ENDIF}); //Final
If EventByName[vEventName] = Nil Then
vDWEvent := TRESTDWEvent(Self.Add)
Else
vDWEvent := EventByName[vEventName];
vDWEvent.DataMode := GetDataModeName(vDataMode);
vDWEvent.DefaultContentType := vContentType;
vDWEvent.Name := vEventName;
vDWEvent.Routes.All.Active := vneedauth;
vDWEvent.OnlyPreDefinedParams := vonlypredefparams;
If vparams <> '' Then
Begin
bJsonArrayC := bJsonOBJb.OpenArray('params');
Try
For Y := 0 To bJsonArrayC.ElementCount - 1 do
Begin
bJsonOBJc := TRESTDWJSONInterfaceObject(bJsonArrayC.GetObject(Y));
vparamname := bJsonOBJc.Pairs[3].Value; // .get('paramname').toString;
If vDWEvent.vDWParams.ParamByName[vparamname] = Nil Then
vDWParamMethod := TRESTDWParamMethod(vDWEvent.vDWParams.Add)
Else
vDWParamMethod := vDWEvent.vDWParams.ParamByName[vparamname];
vDWParamMethod.TypeObject := GetObjectName(bJsonOBJc.Pairs[0].Value); // GetObjectName(bJsonOBJc.get('typeobject').toString);
vDWParamMethod.ObjectDirection := GetDirectionName(bJsonOBJc.Pairs[1].Value); // GetDirectionName(bJsonOBJc.get('objectdirection').toString);
vDWParamMethod.ObjectValue := GetValueType(bJsonOBJc.Pairs[2].Value); // GetValueType(bJsonOBJc.get('objectvalue').toString);
vDWParamMethod.ParamName := vparamname;
If bJsonArrayC.ElementCount > 4 Then
vDWParamMethod.Encoded := StringToBoolean(bJsonOBJc.Pairs[4].Value); // StringToBoolean(bJsonOBJc.get('encoded').toString);
If bJsonArrayC.ElementCount > 5 Then
If Trim(bJsonOBJc.Pairs[5].Value) <> '' Then //Trim(bJsonOBJc.get('default').toString) <> '' Then
vDWParamMethod.DefaultValue := DecodeStrings(bJsonOBJc.Pairs[5].Value{$IFDEF FPC}, csUndefined{$ENDIF}); // bJsonOBJc.get('default').toString{$IFDEF FPC}, csUndefined{$ENDIF});
FreeAndNil(bJsonOBJc);
End;
Finally
FreeAndNil(bJsonArrayC);
End;
End
Else
vDWEvent.vDWParams.ClearList;
FreeAndNil(bJsonOBJb);
End;
Finally
FreeAndNil(bJsonArrayB);
End;
FreeAndNil(bJsonOBJ);
End;
Finally
FreeAndNil(bJsonOBJBase);
End;
End;
Function TRESTDWEventList.GetOwner: TPersistent;
Begin
Result:= fOwner;
End;
function TRESTDWEventList.GetRec(Index: Integer): TRESTDWEvent;
begin
Result := TRESTDWEvent(Inherited GetItem(Index));
end;
function TRESTDWEventList.GetRecName(Index: String): TRESTDWEvent;
Var
X, Z : Integer;
aIndex : String;
vExit : Boolean;
Begin
Result := Nil;
aIndex := Index;
If aIndex <> '' Then
Begin
If (aIndex[Length(aIndex) - FinalStrPos] = '/') Or
(aIndex[Length(aIndex) - FinalStrPos] = '/') Then
DeleteStr(aIndex, Length(aIndex) - FinalStrPos, 1);
End;
X := 0;
Z := Self.Count;
vExit := Z = 0;
If Not vExit Then
Begin
While (X <> Z) Do
Begin
// For I := 0 To Self.Count - 1 Do
If (Uppercase(aIndex) = Uppercase(TRESTDWEvent(Items[X]).EventName)) Or
(Uppercase(aIndex) = Uppercase(TRESTDWEvent(Items[X]).BaseURL + TRESTDWEvent(Items[X]).EventName)) Then
Begin
Result := TRESTDWEvent(Self.Items[X]);
Break;
End;
Dec(Z);
If (Uppercase(aIndex) = Uppercase(TRESTDWEvent(Items[Z]).EventName)) Or
(Uppercase(aIndex) = Uppercase(TRESTDWEvent(Items[Z]).BaseURL + TRESTDWEvent(Items[Z]).EventName)) Then
Begin
Result := TRESTDWEvent(Self.Items[Z]);
Break;
End;
If Z <> X Then
Inc(X);
End;
End;
End;
procedure TRESTDWEventList.PutRec(Index: Integer; Item: TRESTDWEvent);
begin
If (Index < Self.Count) And (Index > -1) And (vEditable) Then
SetItem(Index, Item);
end;
procedure TRESTDWEventList.PutRecName(Index: String; Item: TRESTDWEvent);
Var
I : Integer;
Begin
If (vEditable) Then
Begin
For I := 0 To Self.Count - 1 Do
Begin
If (Uppercase(Index) = Uppercase(TRESTDWEvent(Items[I]).EventName)) Then
Begin
Self.Items[I] := Item;
Break;
End;
End;
End;
End;
Function TRESTDWEventList.ToJSON: String;
Var
A, I : Integer;
vTagEvent,
vParamsLines,
vParamLine,
vParamLine2,
vEventsLines : String;
Begin
Result := '';
vEventsLines := '';
For I := 0 To Count -1 Do
Begin
vParamLine2 := Format('"needauth":"%s", "onlypredefparams":"%s", "ContentType":"%s"', [BooleanToString(Items[I].Routes.All.Active),
BooleanToString(Items[I].OnlyPreDefinedParams),
EncodeStrings(Items[I].DefaultContentType{$IFDEF FPC}, csUndefined{$ENDIF})]);
vTagEvent := Format('{"eventname":"%s"', [TRESTDWEvent(Items[I]).EventName]);
vTagEvent := vTagEvent + Format(', "DataMode":"%s"', [GetDataModeName(Items[I].vDataMode)]);
vTagEvent := vTagEvent + ', "params":[%s], ' + vParamLine2 + '}';
vParamsLines := '';
For A := 0 To Items[I].vDWParams.Count -1 Do
Begin
vParamLine := Format(TServerEventsConst,
[GetObjectName(Items[I].vDWParams[A].TypeObject),
GetDirectionName(Items[I].vDWParams[A].ObjectDirection),
GetValueType(Items[I].vDWParams[A].ObjectValue),
Items[I].vDWParams[A].ParamName,
BooleanToString(Items[I].vDWParams[A].Encoded),
EncodeStrings(Items[I].vDWParams[A].DefaultValue{$IFDEF FPC}, csUndefined{$ENDIF})]);
If vParamsLines = '' Then
vParamsLines := vParamLine
Else
vParamsLines := vParamsLines + ', ' + vParamLine;
End;
If vEventsLines = '' Then
vEventsLines := vEventsLines + Format(vTagEvent, [vParamsLines])
Else
vEventsLines := vEventsLines + Format(', ' + vTagEvent, [vParamsLines]);
End;
Result := Format('{"serverevents":[%s]}', [vEventsLines]);
End;
Procedure TRESTDWServerEvents.CreateDWParams(EventName : String;
Var DWParams : TRESTDWParams);
Var
vParamNameS : String;
dwParam : TRESTDWJSONParam;
I : Integer;
vFound : Boolean;
vEvent : TRESTDWEvent;
Begin
vParamNameS := '';
vEvent := vEventList.EventByName[EventName];
If vEvent <> Nil Then
Begin
If Not Assigned(DWParams) Then
DWParams := TRESTDWParams.Create;
DWParams.DataMode := vEvent.DataMode;
For I := 0 To vEvent.vDWParams.Count -1 Do
Begin
vParamNameS := '';
vFound := (DWParams.ItemsString[vEvent.vDWParams.Items[I].ParamName] <> Nil);
If vFound Then
vParamNameS := vEvent.vDWParams.Items[I].ParamName
Else
Begin
vFound := (DWParams.ItemsString[vEvent.vDWParams.Items[I].Alias] <> Nil);
If vFound Then
vParamNameS := vEvent.vDWParams.Items[I].Alias;
End;
If Not(vFound) Then
Begin
dwParam := TRESTDWJSONParam.Create(DWParams.Encoding);
dwParam.Alias := vEvent.vDWParams.Items[I].Alias;
dwParam.ParamName := vEvent.vDWParams.Items[I].ParamName;
dwParam.ObjectDirection := vEvent.vDWParams.Items[I].ObjectDirection;
dwParam.ObjectValue := vEvent.vDWParams.Items[I].ObjectValue;
dwParam.Encoded := vEvent.vDWParams.Items[I].Encoded;
dwParam.DataMode := DWParams.DataMode;
If (vEvent.vDWParams.Items[I].DefaultValue <> '') And
(Trim(dwParam.AsString) = '') Then
dwParam.Value := vEvent.vDWParams.Items[I].DefaultValue;
DWParams.Add(dwParam);
End
Else
Begin
If (DWParams.ItemsString[vParamNameS].ParamName = '') Or
((DWParams.ItemsString[vParamNameS].ParamName <> '') And
(Lowercase(DWParams.ItemsString[vParamNameS].ParamName) <>
Lowercase(vEvent.vDWParams.Items[I].ParamName))) Then
Begin
DWParams.ItemsString[vParamNameS].Alias := vEvent.vDWParams.Items[I].Alias;
DWParams.ItemsString[vParamNameS].ParamName := vEvent.vDWParams.Items[I].ParamName;
End;
If DWParams.ItemsString[vParamNameS].Alias = '' Then
DWParams.ItemsString[vParamNameS].Alias := vEvent.vDWParams.Items[I].Alias;
End;
End;
End
Else
DWParams := Nil;
End;
Constructor TRESTDWServerEvents.Create(AOwner : TComponent);
Begin
Inherited;
vEventList := TRESTDWEventList.Create(Self, TRESTDWEvent);
vIgnoreInvalidParams := False;
vDefaultEvent := '';
If Assigned(vOnCreate) Then
vOnCreate(Self);
End;
Destructor TRESTDWServerEvents.Destroy;
Begin
vEventList.Free;
Inherited;
End;
{ TRESTDWClientEvents }
constructor TRESTDWClientEvents.Create(AOwner: TComponent);
begin
Inherited;
vEventList := TRESTDWEventList.Create(Self, TRESTDWEvent);
vCripto := TCripto.Create;
vGetEvents := False;
vEditParamList := True;
end;
procedure TRESTDWClientEvents.CreateDWParams(EventName: String;
Var DWParams: TRESTDWParams);
Var
vParamName : String;
dwParam : TRESTDWJSONParam;
I : Integer;
vFound : Boolean;
Begin
vParamName := '';
If vEventList.EventByName[EventName] <> Nil Then
Begin
DWParams := TRESTDWParams.Create;
If Assigned(vRESTClientPooler) Then
DWParams.Encoding := vRESTClientPooler.Encoding;
For I := 0 To vEventList.EventByName[EventName].vDWParams.Count -1 Do
Begin
vParamName := '';
vFound := (DWParams.ItemsString[vEventList.EventByName[EventName].vDWParams.Items[I].ParamName] <> Nil);
If vFound Then
vParamName := vEventList.EventByName[EventName].vDWParams.Items[I].ParamName
Else
Begin
vFound := (DWParams.ItemsString[vEventList.EventByName[EventName].vDWParams.Items[I].Alias] <> Nil);
If vFound Then
vParamName := vEventList.EventByName[EventName].vDWParams.Items[I].Alias;
End;
If Not(vFound) Then
dwParam := TRESTDWJSONParam.Create(DWParams.Encoding)
Else
dwParam := DWParams.ItemsString[vParamName];
dwParam.ParamName := vEventList.EventByName[EventName].vDWParams.Items[I].ParamName;
dwParam.Alias := vEventList.EventByName[EventName].vDWParams.Items[I].Alias;
dwParam.ObjectDirection := vEventList.EventByName[EventName].vDWParams.Items[I].ObjectDirection;
dwParam.ObjectValue := vEventList.EventByName[EventName].vDWParams.Items[I].ObjectValue;
dwParam.Encoded := vEventList.EventByName[EventName].vDWParams.Items[I].Encoded;
dwParam.DataMode := dmDataware;
If (vEventList.EventByName[EventName].vDWParams.Items[I].DefaultValue <> '') And
(Trim(dwParam.AsString) = '') Then
dwParam.Value := vEventList.EventByName[EventName].vDWParams.Items[I].DefaultValue;
If Not(vFound) Then
DWParams.Add(dwParam);
End;
End
Else
DWParams := Nil;
End;
destructor TRESTDWClientEvents.Destroy;
begin
vEventList.Free;
FreeAndNil(vCripto);
Inherited;
end;
procedure TRESTDWClientEvents.GetOnlineEvents(Value: Boolean);
Var
RESTClientPoolerExec : TRESTClientPoolerBase;
vResult,
lResponse : String;
JSONParam : TRESTDWJSONParam;
DWParams : TRESTDWParams;
vRaised : Boolean;
Begin
vRaised := False;
If Assigned(vRESTClientPooler) Then
RESTClientPoolerExec := vRESTClientPooler
Else
Exit;
If Assigned(vRESTClientPooler) Then
If Assigned(vRESTClientPooler.OnBeforeExecute) Then
vRESTClientPooler.OnBeforeExecute(Self);
DWParams := TRESTDWParams.Create;
// DWParams.CriptOptions.Use := CriptOptions.Use;
// DWParams.CriptOptions.Key := CriptOptions.Key;
JSONParam := TRESTDWJSONParam.Create(RESTClientPoolerExec.Encoding);
JSONParam.ParamName := 'dwservereventname';
JSONParam.ObjectDirection := odIn;
JSONParam.AsString := vServerEventName;
DWParams.Add(JSONParam);
JSONParam := TRESTDWJSONParam.Create(RESTClientPoolerExec.Encoding);
JSONParam.ParamName := 'Error';
JSONParam.ObjectDirection := odInOut;
JSONParam.AsBoolean := False;
DWParams.Add(JSONParam);
JSONParam := TRESTDWJSONParam.Create(RESTClientPoolerExec.Encoding);
JSONParam.ParamName := 'MessageError';
JSONParam.ObjectDirection := odInOut;
JSONParam.AsString := '';
DWParams.Add(JSONParam);
JSONParam := TRESTDWJSONParam.Create(RESTClientPoolerExec.Encoding);
JSONParam.ParamName := 'BinaryRequest';
JSONParam.ObjectDirection := odIn;
If Assigned(vRESTClientPooler) Then
JSONParam.AsBoolean := vRESTClientPooler.BinaryRequest
Else
JSONParam.AsBoolean := False;
DWParams.Add(JSONParam);
JSONParam := TRESTDWJSONParam.Create(RESTClientPoolerExec.Encoding);
JSONParam.ParamName := 'Result';
JSONParam.ObjectDirection := odOut;
JSONParam.AsString := '';
DWParams.Add(JSONParam);
Try
Try
RESTClientPoolerExec.NewToken;
lResponse := RESTClientPoolerExec.SendEvent('GetEvents', DWParams, sePOST, dmDataware, vServerEventName);
If (lResponse <> '') And
(Uppercase(lResponse) <> Uppercase(cInvalidAuth)) Then
Begin
If Not DWParams.ItemsString['error'].AsBoolean Then
Begin
// If vRESTClientPooler.CriptOptions.Use Then
// DWParams.ItemsString['Result'].CriptOptions.Use := False;
vResult := DWParams.ItemsString['Result'].AsString;
If Trim(vResult) <> '' Then //Carreta o ParamList
vEventList.FromJSON(Trim(vResult));
End
Else
Begin
vRaised := True;
Raise Exception.Create(DWParams.ItemsString['MessageError'].AsString);
End;
End
Else
Begin
If (lResponse = '') Then
lResponse := Format('Unresolved Host : ''%s''', [RESTClientPoolerExec.Host])
Else If (Uppercase(lResponse) <> Uppercase(cInvalidAuth)) Then
lResponse := cInvalidAuth;
Raise Exception.Create(lResponse);
lResponse := '';
End;
Except
On E : Exception Do
Begin
If Not vRaised Then
Begin
If Trim(vServerEventName) = '' Then
Raise Exception.Create(cInvalidServerEventName)
Else
Raise Exception.Create(cServerEventNotFound);
End;
End;
End;
Finally
If Not Assigned(RESTClientPooler) Then
FreeAndNil(RESTClientPoolerExec);
FreeAndNil(DWParams);
End;
End;
function TRESTDWClientEvents.GeTRESTClientPoolerBase: TRESTClientPoolerBase;
begin
Result := vRESTClientPooler;
end;
procedure TRESTDWClientEvents.Notification(AComponent: TComponent;
Operation: TOperation);
begin
if (Operation = opRemove) and (AComponent = vRESTClientPooler) then
begin
vRESTClientPooler := nil;
end;
inherited Notification(AComponent, Operation);
end;
Function TRESTDWClientEvents.SendEvent(EventName : String;
Var DWParams : TRESTDWParams;
Var Error : String;
Var NativeResult : String;
EventType : TSendEvent = sePOST;
Assyncexec : Boolean = False): Boolean;
Var
vDataMode : TDataMode;
Begin
Error := '';
Result := False;
If vRESTClientPooler <> Nil Then
Begin
If Assigned(vOnBeforeSend) Then
vOnBeforeSend(Self);
If Assigned(vRESTClientPooler.OnBeforeExecute) Then
vRESTClientPooler.OnBeforeExecute(Self);
If vEventList.EventByName[EventName] = Nil Then
Begin
Result := False;
Error := cInvalidEvent;
Exit
End;
TokenValidade(DWParams, Error);
vDataMode := vEventList.EventByName[EventName].vDataMode;
Try
Error := '';
NativeResult := vRESTClientPooler.SendEvent(EventName, DWParams, EventType, vDataMode, vServerEventName);
Result := (NativeResult = TReplyOK) Or (NativeResult = AssyncCommandMSG);
if vDataMode = dmraw then
Begin
if length(Trim(NativeResult)) >0 then
Result:= True
else
Result:= False;
End;
Except
On E : Exception Do
Begin
// Eloy
Error := E.Message;
If Error = cInvalidAuth Then
Begin
Case vRESTClientPooler.AuthenticationOptions.AuthorizationOption Of
rdwAOBearer : Begin
If (TRESTDWAuthOptionBearerClient(vRESTClientPooler.AuthenticationOptions.OptionParams).AutoGetToken) And
(TRESTDWAuthOptionBearerClient(vRESTClientPooler.AuthenticationOptions.OptionParams).AutoRenewToken) And
(TRESTDWAuthOptionBearerClient(vRESTClientPooler.AuthenticationOptions.OptionParams).Token <> '') Then
Begin
TRESTDWAuthOptionBearerClient(vRESTClientPooler.AuthenticationOptions.OptionParams).Token := '';
SendEvent(EventName, DWParams, Error, NativeResult, EventType, Assyncexec);
End;
End;
rdwAOToken : Begin
If (TRESTDWAuthOptionTokenClient(vRESTClientPooler.AuthenticationOptions.OptionParams).AutoGetToken) And
(TRESTDWAuthOptionTokenClient(vRESTClientPooler.AuthenticationOptions.OptionParams).AutoRenewToken) And
(TRESTDWAuthOptionTokenClient(vRESTClientPooler.AuthenticationOptions.OptionParams).Token <> '') Then
Begin
TRESTDWAuthOptionTokenClient(vRESTClientPooler.AuthenticationOptions.OptionParams).Token := '';
SendEvent(EventName, DWParams, Error, NativeResult, EventType, Assyncexec);
End;
End;
End;
End
Else
Begin
Raise Exception.Create(Error);
End;
End;
End;
End;
End;
Procedure TRESTDWClientEvents.TokenValidade(Var DWParams : TRESTDWParams;
Var Error : String);
Var
JSONParam : TRESTDWJSONParam;
vToken : String;
vErrorBoolean : Boolean;
Begin
vErrorBoolean := False;
If Not(Assigned(DWParams)) Then
Begin