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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
|
unit fpjsonrpc;
{$mode objfpc}{$H+}
{$inline on}
interface
uses
Classes, SysUtils, fpjson;
Type
{ ---------------------------------------------------------------------
JSON-RPC Handler support
---------------------------------------------------------------------}
{ TJSONParamDef }
TJSONParamDef = Class(TCollectionItem)
private
FName: TJSONStringType;
FRequired: Boolean;
FType: TJSONtype;
procedure SetName(const AValue: TJSONStringType);
protected
function GetDisplayName: string; override;
public
Constructor Create(ACollection : TCollection); override;
Procedure Assign(Source : TPersistent); override;
Published
Property Name : TJSONStringType Read FName Write SetName;
Property DataType : TJSONtype Read FType Write FType default jtString;
Property Required : Boolean Read FRequired Write FRequired default True;
end;
{ TJSONParamDefs }
TJSONParamDefs = Class(TCollection)
private
function GetP(AIndex : Integer): TJSONParamDef;
procedure SetP(AIndex : Integer; const AValue: TJSONParamDef);
Public
Function AddParamDef(Const AName : TJSONStringType; AType : TJSONType = jtString) : TJSONParamDef;
Function IndexOfParamDef(Const AName : TJSONStringType) : Integer;
Function FindParamDef(Const AName : TJSONStringType) : TJSONParamDef;
Function ParamDefByName(Const AName : TJSONStringType) : TJSONParamDef;
Property ParamDefs[AIndex : Integer] : TJSONParamDef Read GetP Write SetP; default;
end;
{ TCustomJSONRPCHandler }
TJSONParamErrorEvent = Procedure (Sender : TObject; Const E : Exception; Var Fatal : boolean) of Object;
TJSONRPCOption = (jroCheckParams,jroObjectParams,jroArrayParams);
TJSONRPCOptions = set of TJSONRPCOption;
{ TJSONRPCCallContext }
TJSONRPCCallContext = Class(TObject)
private
FClassName: String;
FMethod: String;
FTID: String;
Public
// Action used to call handler.
Property ClassName : String Read FClassName Write FClassName;
// Method used to call handler.
Property Method : String Read FMethod Write FMethod;
// Transaction in which handler is called.
Property TID : String Read FTID Write FTID;
end;
TCustomJSONRPCHandler = Class(TComponent)
private
FAfterExecute: TNotifyEvent;
FBeforeExecute: TNotifyEvent;
FOnParamError: TJSONParamErrorEvent;
FOptions: TJSONRPCOptions;
FParamDefs: TJSONParamDefs;
FExecParams : TJSONData;
procedure SetParamDefs(const AValue: TJSONParamDefs);
Protected
function CreateParamDefs: TJSONParamDefs; virtual;
Procedure DoCheckParams(Const Params : TJSONData); virtual;
Function DoExecute(Const Params : TJSONData; AContext : TJSONRPCCallContext): TJSONData; virtual;
Property BeforeExecute : TNotifyEvent Read FBeforeExecute Write FBeforeExecute;
Property AfterExecute : TNotifyEvent Read FAfterExecute Write FAfterExecute;
Property OnParamError :TJSONParamErrorEvent Read FOnParamError Write FONParamError;
Property Options : TJSONRPCOptions Read FOptions Write FOptions;
Public
Constructor Create(AOwner : TComponent); override;
Destructor Destroy; override;
Procedure CheckParams(Const Params : TJSONData);
Function ParamByName(Const AName : String) : TJSONData;
Function Execute(Const Params : TJSONData; AContext : TJSONRPCCallContext = Nil) : TJSONData;
Property ParamDefs : TJSONParamDefs Read FParamDefs Write SetParamDefs;
end;
TCustomJSONRPCHandlerClass = Class of TCustomJSONRPCHandler;
TJSONRPCEvent = Procedure (Sender : TObject; Const Params : TJSONData; Out Res : TJSONData) of object;
{ TJSONRPCHandler }
TJSONRPCHandler = Class(TCustomJSONRPCHandler)
private
FOnExecute: TJSONRPCEvent;
protected
Function DoExecute(Const Params : TJSONData; AContext : TJSONRPCCallContext): TJSONData; override;
Published
Property OnExecute : TJSONRPCEvent Read FOnExecute Write FOnExecute;
Property BeforeExecute;
Property AfterExecute;
Property OnParamError;
Property Options;
Property ParamDefs;
end;
{ TJSONRPCEcho }
TJSONRPCEcho = Class(TCustomJSONRPCHandler)
Protected
Function DoExecute(Const Params : TJSONData;AContext : TJSONRPCCallContext): TJSONData; override;
end;
{ ---------------------------------------------------------------------
JSON-RPC dispatcher support
---------------------------------------------------------------------}
TJSONRPCDispatchOption = (jdoSearchRegistry, // Check JSON Handler registry
jdoSearchOwner, // Check owner (usually webmodule) for request handler
jdoJSONRPC1, // Allow JSON RPC-1
jdoJSONRPC2, // Allow JSON RPC-2
jdoRequireClass, // Require class name (as in Ext.Direct)
jdoNotifications, // Allow JSON Notifications
jdoStrictNotifications // Error if notification returned result. Default is to discard result.
);
TJSONRPCDispatchOptions = set of TJSONRPCDispatchOption;
Const
DefaultDispatchOptions = [jdoSearchOwner,jdoJSONRPC1,jdoJSONRPC2,jdoNotifications];
Type
TDispatchRequestEvent = Procedure(Sender : TObject; Const AClassName,AMethod : TJSONStringType; Const Params : TJSONData) of object;
TFindRPCHandlerEvent = Procedure(Sender : TObject; Const AClassName,AMethod : TJSONStringType; Out Handler : TCustomJSONRPCHandler) of object;
{ TCustomJSONRPCDispatcher }
TCustomJSONRPCDispatcher = Class(TComponent)
private
FFindHandler: TFindRPCHandlerEvent;
FOnDispatchRequest: TDispatchRequestEvent;
FOnEndBatch: TNotifyEvent;
FOnStartBatch: TNotifyEvent;
FOptions: TJSONRPCDispatchOptions;
Protected
// Find handler. If none found, nil is returned. Executes OnFindHandler if needed.
// On return 'DoFree' must be set to indicate that the hand
Function FindHandler(Const AClassName,AMethodName : TJSONStringType;AContext : TJSONRPCCallContext; Out FreeObject : TComponent) : TCustomJSONRPCHandler; virtual;
// Execute method. Finds handler, and returns response.
Function ExecuteMethod(Const AClassName, AMethodName : TJSONStringType; Params,ID : TJSONData; AContext : TJSONRPCCallContext) : TJSONData; virtual;
// Check and Execute a single request. Exceptions are caught and converted to request error object.
function ExecuteRequest(ARequest: TJSONData;AContext : TJSONRPCCallContext): TJSONData;
// Execute requests, returns responses in same format as requests (single or array)
Function DoExecute(Requests : TJSONData;AContext : TJSONRPCCallContext) : TJSONData; virtual;
// Check if single request corresponds to specs.
// Returns an error object if an error was found.
// if request is OK, then transaction id, classname, method and params *must* be returned.
// The returned transaction id, method, classname and params will be ignored if there is an error.
function CheckRequest(Request: TJSONData; Out AClassName, AMethodName : TJSONStringType; Out ID, Params : TJSONData): TJSONData; virtual;
// Check if requests are OK (check if JSON2 is allowed for array).
Function CheckRequests(Requests : TJSONData) : TJSONData; virtual;
// Format result of a single request. Result is returned to the client, possibly in an array if multiple requests were received in batch.
Function FormatResult(const AClassName, AMethodName: TJSONStringType; const Params, ID, Return: TJSONData) : TJSONData; virtual;
// Format error of a single request.
function CreateJSON2Error(Const AMessage : String; Const ACode : Integer; ID : TJSONData = Nil; idname : TJSONStringType = 'id' ) : TJSONObject; virtual;
function CreateJSON2Error(Const AFormat : String; Args : Array of const; Const ACode : Integer; ID : TJSONData = Nil; idname : TJSONStringType = 'id') : TJSONObject;
// Hooks for user.
Property OnStartBatch : TNotifyEvent Read FOnStartBatch Write FOnStartBatch;
Property OnDispatchRequest : TDispatchRequestEvent Read FOnDispatchRequest Write FOnDispatchRequest;
Property OnFindHandler : TFindRPCHandlerEvent Read FFindHandler Write FFindHandler;
Property OnEndBatch : TNotifyEvent Read FOnEndBatch Write FOnEndBatch;
Property Options : TJSONRPCDispatchOptions Read FOptions Write FOptions default DefaultDispatchOptions;
Class Function MethodProperty : String; virtual;
Class Function ClassNameProperty : String; virtual;
Class Function ParamsProperty : String; virtual;
Public
Constructor Create(AOwner : TComponent); override;
Class Function TransactionProperty : String; virtual;
Function Execute(Requests : TJSONData;AContext : TJSONRPCCallContext = Nil) : TJSONData;
end;
TJSONRPCDispatcher = Class(TCustomJSONRPCDispatcher)
Published
Property OnStartBatch;
Property OnDispatchRequest;
Property OnFindHandler;
Property OnEndBatch;
Property Options;
end;
{ ---------------------------------------------------------------------
Factory support
---------------------------------------------------------------------}
{ TJSONRPCHandlerDef }
TDataModuleClass = Class of TDataModule; // For the time being. As of rev 15343 it is in classes unit
TBeforeCreateJSONRPCHandlerEvent = Procedure (Sender : TObject; Var AClass : TCustomJSONRPCHandlerClass) of object;
TJSONRPCHandlerEvent = Procedure (Sender : TObject; AHandler : TCustomJSONRPCHandler) of object;
TJSONRPCHandlerDef = Class(TCollectionItem)
private
FAfterCreate: TJSONRPCHandlerEvent;
FArgumentCount: Integer;
FBeforeCreate: TBeforeCreateJSONRPCHandlerEvent;
FParamDefs: TJSONParamDefs;
FPClass: TCustomJSONRPCHandlerClass;
FDataModuleClass : TDataModuleClass;
FHandlerMethodName: TJSONStringType;
FHandlerClassName: TJSONStringType;
procedure CheckNames(const AClassName, AMethodName: TJSONStringType);
function GetParamDefs: TJSONParamDefs;
procedure SetFPClass(const AValue: TCustomJSONRPCHandlerClass);
procedure SetHandlerClassName(const AValue: TJSONStringType);
procedure SetHandlerMethodName(const AValue: TJSONStringType);
procedure SetParamDefs(AValue: TJSONParamDefs);
protected
Function CreateInstance(AOwner : TComponent; Out AContainer : TComponent) : TCustomJSONRPCHandler; virtual;
Property DataModuleClass : TDataModuleClass Read FDataModuleClass;
Public
Destructor Destroy; override;
Function HaveParamDefs : Boolean;
Property HandlerClassName : TJSONStringType Read FHandlerClassName Write SetHandlerClassName;
Property HandlerMethodName : TJSONStringType Read FHandlerMethodName Write SetHandlerMethodName;
Property HandlerClass : TCustomJSONRPCHandlerClass Read FPClass Write SetFPClass;
Property BeforeCreate : TBeforeCreateJSONRPCHandlerEvent Read FBeforeCreate Write FBeforeCreate;
Property AfterCreate : TJSONRPCHandlerEvent Read FAfterCreate Write FAfterCreate;
Property ArgumentCount : Integer Read FArgumentCount Write FArgumentCount;
Property ParamDefs : TJSONParamDefs Read GetParamDefs Write SetParamDefs;
end;
{ TJSONRPCHandlerDefs }
TJSONRPCHandlerDefs = Class(TCollection)
private
function GetH(Index : Integer): TJSONRPCHandlerDef;
procedure SetH(Index : Integer; const AValue: TJSONRPCHandlerDef);
Public
Function IndexOfHandler(Const AClassName,AMethodName : TJSONStringType) : Integer;
Function AddHandler(Const AClassName,AMethodName : TJSONStringType) : TJSONRPCHandlerDef; overload;
Function AddHandler(Const AClassName,AMethodName : TJSONStringType; AClass : TCustomJSONRPCHandlerClass) : TJSONRPCHandlerDef; overload;
Property HandlerDefs[Index : Integer] : TJSONRPCHandlerDef Read GetH Write SetH; default;
end;
{ TCustomJSONRPCHandlerManager }
TCustomJSONRPCHandlerManager = Class(TComponent)
Private
FRegistering: Boolean;
Protected
procedure Initialize; virtual;
// Handler support
Procedure RemoveHandlerDef(Const Index : Integer); virtual; abstract;
function AddHandlerDef(Const AClassName,AMethodName : TJSONStringType) : TJSONRPCHandlerDef; virtual; abstract;
function IndexOfHandlerDef(Const AClassName,AMethodName : TJSONStringType) : Integer; virtual; abstract;
function GetHandlerDef(Index : Integer): TJSONRPCHandlerDef; virtual; abstract;
function GetHandlerDefCount: Integer; virtual; abstract;
Public
// Handler support
Procedure UnregisterHandler(Const AClassName, AMethodName : TJSONStringType);
Procedure RegisterDatamodule(Const AClass : TDatamoduleClass; Const AHandlerClassName : TJSONStringType);
Function RegisterHandler(Const AMethodName : TJSONStringType; AClass : TCustomJSONRPCHandlerClass; AArgumentCount : Integer = 0) : TJSONRPCHandlerDef; overload;
Function RegisterHandler(Const AClassName,AMethodName : TJSONStringType; AClass : TCustomJSONRPCHandlerClass; AArgumentCount : Integer = 0) : TJSONRPCHandlerDef; overload;
Function FindHandlerDefByName(Const AClassName,AMethodName : TJSONStringType) : TJSONRPCHandlerDef;
Function GetHandlerDefByName(Const AClassName,AMethodName : TJSONStringType) : TJSONRPCHandlerDef;
Function GetHandler(Const ADef : TJSONRPCHandlerDef; AOwner : TComponent; Out AContainer : TComponent): TCustomJSONRPCHandler;
Function GetHandler(Const AClassName,AMethodName : TJSONStringType; AOwner : TComponent; Out AContainer : TComponent): TCustomJSONRPCHandler;
Procedure GetClassNames (List : TStrings); // Should be a stringlist of TJSONStringType
Procedure GetMethodsOfClass(Const AClassName : TJSONStringType; List : TStrings); // Should be a stringlist of TJSONStringType
// properties
Property Registering : Boolean Read FRegistering;
Property HandlerCount : Integer Read GetHandlerDefCount;
Property HandlerDefs[Index : Integer] : TJSONRPCHandlerDef Read GetHandlerDef;
end;
TCustomJSONRPCHandlerManagerClass = Class of TCustomJSONRPCHandlerManager;
{ TJSONRPCHandlerManager }
TJSONRPCHandlerManager = Class(TCustomJSONRPCHandlerManager)
Private
FHandlerDefs : TJSONRPCHandlerDefs;
Protected
Procedure RemoveHandlerDef(Const Index : Integer); override;
function AddHandlerDef(Const AClassName,AMethodName : TJSONStringType) : TJSONRPCHandlerDef; override;
function IndexOfHandlerDef(Const AClassName,AMethodName : TJSONStringType) : Integer; override;
function GetHandlerDef(Index : Integer): TJSONRPCHandlerDef; override;
function GetHandlerDefCount: Integer; override;
Public
Constructor Create(AOwner : TComponent); override;
Destructor Destroy; override;
end;
{ ---------------------------------------------------------------------
Auxiliary stuff
---------------------------------------------------------------------}
EJSONRPC = Class(Exception);
TJSONErrorObject = Class(TJSONObject);
// Raise EJSONRPC exceptions.
Procedure JSONRPCError(Msg : String);
Procedure JSONRPCError(Fmt : String; Args : Array of const);
// Create an 'Error' object for an error response.
function CreateJSONErrorObject(Const AMessage : String; Const ACode : Integer) : TJSONObject;
// Create a JSON RPC 2 error response object containing an 'Error' object.
// Result is of type TJSONErrorObject
function CreateJSON2ErrorResponse(Const AMessage : String; Const ACode : Integer; ID : TJSONData = Nil; idname : TJSONStringType = 'id' ) : TJSONObject;
function CreateJSON2ErrorResponse(Const AFormat : String; Args : Array of const; Const ACode : Integer; ID : TJSONData = Nil; idname : TJSONStringType = 'id') : TJSONObject;
// Examines Req (request) and returns Error or an array of clones of Error)
Function CreateErrorForRequest(Const Req,Error : TJSONData) : TJSONData;
// Return TCustomJSONRPCHandlerManager instance to use for managing JSON-RPC handler.
Function JSONRPCHandlerManager : TCustomJSONRPCHandlerManager;
// Class that will be created. Must be set before first call to JSONRPCHandlerManager.
Var
JSONRPCHandlerManagerClass : TCustomJSONRPCHandlerManagerClass = TJSONRPCHandlerManager;
Const
// JSON RPC 2.0 error codes
EJSONRPCParseError = -32700;
EJSONRPCInvalidRequest = -32600;
EJSONRPCMethodNotFound = -32601;
EJSONRPCInvalidParams = -32602;
EJSONRPCInternalError = -32603;
resourcestring
SErrDuplicateParam = 'Duplicate JSON-RPC Parameter name';
SErrUnknownParamDef = 'Unknown parameter definition: "%s"';
SErrParams = 'Error checking JSON-RPC parameters: "%s"';
SErrParamsMustBeArrayorObject = 'Parameters must be passed in an object or an array.';
SErrParamsMustBeObject = 'Parameters must be passed in an object.';
SErrParamsMustBeArray = 'Parameters must be passed in an array.';
SErrRequestMustBeObject = 'JSON-RPC Request must be an object.';
SErrNoIDProperty = 'No "id" property found in request.';
SErrInvalidIDProperty = 'Type of "id" property is not correct.';
SErrNoJSONRPCProperty = 'No "jsonrpc" property in request.';
SErrInvalidJSONRPCProperty = 'Type or value of "jsonrpc" property is not correct.';
SErrNoMethodName = 'Cannot determine method: No "%s" property found in request.';
SErrNoClassName = 'Cannot determine class: No "%s" property found in request.';
SErrNoParams = 'Cannot determine parameters: No "%s" property found in request.';
SErrInvalidMethodType = 'Type of "%s" property in request is not correct.';
SErrInvalidClassNameType = 'Type of "%s" property in request is not correct.';
SErrJSON2NotAllowed = 'JSON RPC 2 calls are not allowed.';
SErrJSON1NotAllowed = 'JSON RPC 1 calls are not allowed.';
SErrNoResponse = 'No response received from non-notification method "%s".';
SErrResponseFromNotification = 'A response was received from a notification method "%s".';
SErrInvalidMethodName = 'No method "%s" was found.';
SErrInvalidClassMethodName = 'No class "%s" with method "%s" was found.';
SErrDuplicateJSONRPCClassHandlerName = 'Duplicate JSON-RPC handler for class "%s" with method "%s".';
SErrDuplicateJSONRPCHandlerName = 'Duplicate JSON-RPC handler for method "%s".';
SErrUnknownJSONRPCClassMethodHandler = 'Unknown JSON-RPC handler for class "%s", method "%s".';
SErrUnknownJSONRPCMethodHandler = 'Unknown JSON-RPC handler for method "%s".';
SErrDuplicateRPCCLassMethodHandler = 'Duplicate JSON-RPC handler for class "%s", method "%s".';
SErrDuplicateRPCMethodHandler = 'Duplicate JSON-RPC handler for method "%s".';
SErrNoDispatcher = 'No method dispatcher available to handle request.';
implementation
{$IFDEF WMDEBUG}
uses dbugintf;
{$ENDIF}
function CreateJSONErrorObject(Const AMessage : String; Const ACode : Integer) : TJSONObject;
begin
Result:=TJSONErrorObject.Create(['code',ACode,'message',AMessage])
end;
function CreateJSON2ErrorResponse(Const AMessage : String; Const ACode : Integer; ID : TJSONData = Nil; idname : TJSONStringType = 'id' ) : TJSONObject;
begin
If (ID=Nil) then
ID:=TJSONNull.Create;
Result:=TJSONErrorObject.Create(['jsonrpc','2.0','error',CreateJSONErrorObject(AMessage,ACode),idname,ID]);
end;
function CreateJSON2ErrorResponse(Const AFormat : String; Args : Array of const; Const ACode : Integer; ID : TJSONData = Nil; idname : TJSONStringType = 'id' ) : TJSONObject;
begin
If (ID=Nil) then
ID:=TJSONNull.Create;
Result:=TJSONErrorObject.Create(['jsonrpc','2.0','error',CreateJSONErrorObject(Format(AFormat,Args),ACode),idname,ID]);
end;
Function CreateErrorForRequest(Const Req,Error : TJSONData) : TJSONData;
Var
I : Integer;
begin
if Req is TJSONArray then
begin
Result:=TJSONArray.Create;
TJSONArray(Result).Add(Error);
For I:=1 to TJSONArray(Req).Count-1 do
TJSONArray(Result).Add(Error.Clone);
end
else
Result:=Error;
end;
Var
TheHandler : TCustomJSONRPCHandlerManager;
function JSONRPCHandlerManager: TCustomJSONRPCHandlerManager;
begin
If (TheHandler=Nil) then
TheHandler:=JSONRPCHandlerManagerClass.Create(Nil);
JSONRPCHandlerManager:=TheHandler;
end;
Procedure JSONRPCError(Msg : String);
begin
Raise EJSONRPC.Create(Msg);
end;
Procedure JSONRPCError(Fmt : String; Args : Array of const);
begin
Raise EJSONRPC.CreateFmt(Fmt,Args);
end;
{ TJSONParamDef }
procedure TJSONParamDef.SetName(const AValue: TJSONStringType);
Var
D: TJSONParamDef;
begin
if FName=AValue then exit;
If Assigned(Collection) and (Collection is TJSONParamDefs) then
begin
D:=(Collection as TJSONParamDefs).FindParamDef(AValue);
If (D<>Nil) and (D<>Self) then
JSONRPCError(SErrDuplicateParam,[AValue]);
end;
FName:=AValue;
end;
function TJSONParamDef.GetDisplayName: string;
begin
Result:=FName;
If (Result='') then
Result:=Inherited GetDisplayName;
end;
constructor TJSONParamDef.Create(ACollection: TCollection);
begin
inherited Create(ACollection);
FType:=jtString;
FRequired:=True;
end;
procedure TJSONParamDef.Assign(Source: TPersistent);
Var
P : TJSONParamDef;
begin
If Source is TJSONParamDef then
begin
P:=TJSONParamDef(Source);
FType:=P.DataType;
FName:=P.Name;
FRequired:=P.Required;
end
else
inherited Assign(Source);
end;
{ TJSONParamDefs }
function TJSONParamDefs.GetP(AIndex : Integer): TJSONParamDef;
begin
Result:=TJSONParamDef(Items[AIndex]);
end;
procedure TJSONParamDefs.SetP(AIndex : Integer; const AValue: TJSONParamDef);
begin
Items[AIndex]:=AValue;
end;
function TJSONParamDefs.AddParamDef(const AName: TJSONStringType; AType: TJSONType
): TJSONParamDef;
begin
Result:=Add as TJSONParamDef;
try
Result.Name:=AName;
Result.DataType:=Atype;
except
FReeAndNil(Result);
Raise;
end;
end;
function TJSONParamDefs.IndexOfParamDef(const AName: TJSONStringType): Integer;
begin
Result:=Count-1;
While (Result>=0) and (CompareText(AName,GetP(result).Name)<>0) do
Dec(Result);
end;
function TJSONParamDefs.FindParamDef(const AName: TJSONStringType): TJSONParamDef;
Var
I : integer;
begin
I:=IndexOfParamDef(AName);
If (I=-1) then
Result:=Nil
else
Result:=GetP(I);
end;
function TJSONParamDefs.ParamDefByName(const AName: TJSONStringType): TJSONParamDef;
begin
Result:=FindParamDef(AName);
If (Result=Nil) then
JSONRPCError(SErrUnknownParamDef,[AName]);
end;
{ TCustomJSONRPCHandler }
procedure TCustomJSONRPCHandler.CheckParams(const Params: TJSONData);
Var
B : Boolean;
begin
Try
DoCheckParams(Params);
Except
On E : Exception do
begin
B:=True;
If Assigned(FonParamError) then
FonParamError(Self,E,B);
If B then
Raise;
end;
end;
end;
function TCustomJSONRPCHandler.ParamByName(const AName: String): TJSONData;
Var
I : Integer;
N : String;
begin
If (FExecParams=Nil) or Not (FExecParams.JSONType in [jtArray,jtObject]) then
Result:=Nil
else
begin
I:=ParamDefs.IndexOfParamDef(AName);
If (I=-1) then
N:=AName
else
N:=ParamDefs[i].Name; // Search with original defined name.
If (FExecParams is TJSONObject) then
Result:=TJSONObject(FExecParams).Elements[N]
else if (FExecParams is TJSONArray) then
begin
If (I=-1) or (I>=FExecParams.Count) then
JSONRPCError(SErrUnknownParamDef,[AName]);
Result:=TJSONArray(FExecParams).Items[i];
end;
end;
end;
procedure TCustomJSONRPCHandler.SetParamDefs(const AValue: TJSONParamDefs);
begin
if FParamDefs=AValue then exit;
FParamDefs.Assign(AValue);
end;
procedure TCustomJSONRPCHandler.DoCheckParams(const Params: TJSONData);
begin
If (jroObjectParams in Options) and Not (Params is TJSONobject) then
JSONRPCError(SErrParams,[SErrParamsMustBeObject]);
If (jroArrayParams in Options) and Not (Params is TJSONArray) then
JSONRPCError(SErrParams,[SErrParamsMustBeArray]);
end;
function TCustomJSONRPCHandler.DoExecute(Const Params: TJSONData;AContext : TJSONRPCCallContext): TJSONData;
begin
Result:=Nil;
end;
constructor TCustomJSONRPCHandler.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FParamDefs:=CreateParamDefs;
end;
destructor TCustomJSONRPCHandler.Destroy;
begin
FreeAndNil(FParamDefs);
inherited Destroy;
end;
function TCustomJSONRPCHandler.CreateParamDefs : TJSONParamDefs;
begin
Result:=TJSONParamDefs.Create(TJSONParamDef);
end;
function TCustomJSONRPCHandler.Execute(Const Params: TJSONData;AContext : TJSONRPCCallContext = Nil): TJSONData;
begin
If Assigned(FBeforeExecute) then
FBeforeExecute(Self);
if (jroCheckParams in Options) then
CheckParams(Params);
FExecParams:=Params;
try
Result:=DoExecute(Params,AContext);
finally
FExecParams:=Nil;
end;
If Assigned(FAfterExecute) then
FAfterExecute(Self);
end;
{ TJSONRPCHandler }
function TJSONRPCHandler.DoExecute(const Params: TJSONData;AContext : TJSONRPCCallContext): TJSONData;
begin
Result:=Nil;
If Assigned(FOnExecute) then
FOnExecute(Self,Params,Result);
end;
{ TJSONRPCEcho }
function TJSONRPCEcho.DoExecute(const Params: TJSONData;AContext : TJSONRPCCallContext): TJSONData;
Var
I : Integer;
A : TJSONArray;
S : TJSONStringType;
begin
If Params is TJSONArray then
begin
A:=Params as TJSONArray;
S:='';
For I:=0 to A.Count-1 do
begin
if (S<>'') then
S:=S+' ';
S:=S+A.Items[i].AsString;
end;
end
else If Params.JSONType in [jtObject,jtNumber] then
S:=Params.AsJSON
else
S:=Params.AsString;
Result:=TJSONString.Create(S);
end;
{ TCustomJSONRPCDispatcher }
function TCustomJSONRPCDispatcher.FindHandler(const AClassName, AMethodName: TJSONStringType;AContext : TJSONRPCCallContext;Out FreeObject : TComponent): TCustomJSONRPCHandler;
Var
C : TComponent;
D : TJSONRPCHandlerDef;
begin
Result:=Nil;
FreeObject:=Nil;
If Assigned(Owner) and ((AClassName='') or (CompareText(AClassName,Owner.name)=0)) then
begin
C:=Owner.FindComponent(AMethodName);
If C is TCustomJSONRPCHandler then
Result:=TCustomJSONRPCHandler(C);
end;
If (Result=Nil) and (jdoSearchRegistry in Options) then
begin
D:=JSONRPCHandlerManager.FindHandlerDefByName(AClassName,AMethodName);
If Assigned(D) then
Result:=JSONRPCHandlerManager.GetHandler(D,Self,FreeObject);
end;
If (Result=Nil) and Assigned(FFindHandler) then
begin
FFindhandler(Self,AClassName,AMethodName,Result);
FreeObject:=Nil
end;
end;
function TCustomJSONRPCDispatcher.ExecuteMethod(Const AClassName,AMethodName: TJSONStringType;
Params,ID: TJSONData;AContext : TJSONRPCCallContext): TJSONData;
Var
H : TCustomJSONRPCHandler;
FreeObject : TComponent;
begin
H:=FindHandler(AClassName,AMethodName,AContext,FreeObject);
If (H=Nil) then
if (AClassName='') then
Exit(CreateJSON2Error(SErrInvalidMethodName,[AMethodName],EJSONRPCMethodNotFound,ID.Clone,transactionProperty))
else
Exit(CreateJSON2Error(SErrInvalidClassMethodName,[AClassName,AMethodName],EJSONRPCMethodNotFound,ID.Clone,transactionProperty));
try
If Assigned(FOndispatchRequest) then
FOndispatchRequest(Self,AClassName,AMethodName,Params);
Result:=H.Execute(Params,AContext);
finally
If Assigned(FreeObject) then
FreeAndNil(FreeObject);
end;
end;
function TCustomJSONRPCDispatcher.FormatResult(Const AClassName, AMethodName: TJSONStringType;
Const Params,ID, Return : TJSONData) : TJSONData;
begin
Result:=TJSONObject.Create(['result',Return,'error',TJSonNull.Create,transactionproperty,ID.Clone]);
if jdoJSONRPC2 in options then
TJSONObject(Result).Add('jsonrpc','2.0');
end;
function TCustomJSONRPCDispatcher.CreateJSON2Error(const AMessage: String;
const ACode: Integer; ID: TJSONData; idname: TJSONStringType): TJSONObject;
begin
Result:=CreateJSON2ErrorResponse(AMessage,ACode,ID,IDName);
end;
function TCustomJSONRPCDispatcher.CreateJSON2Error(const AFormat: String;
Args: array of const; const ACode: Integer; ID: TJSONData;
idname: TJSONStringType): TJSONObject;
begin
Result:=CreateJSON2Error(Format(AFormat,Args),ACode,ID,IDName);
end;
function TCustomJSONRPCDispatcher.ExecuteRequest(ARequest: TJSONData;AContext : TJSONRPCCallContext): TJSONData;
Var
C,M : TJSONStringType;
Id,P : TJSONData;
begin
Result:=Nil;
try
Result:=CheckRequest(ARequest,C,M,ID,P);
If (Result=Nil) then
begin
If Assigned(AContext) then
begin
AContext.ClassName:=C;
AContext.Method:=M;
if Assigned(ID) then
AContext.TID:=ID.AsJSON;
end;
Result:=ExecuteMethod(C,M,P,ID,AContext);
// Do some sanity checks.
If (Result=Nil) then
begin
// No response, and a response was expected.
if (ID<>Nil) or not (jdoNotifications in Options) then
Result:=CreateJSON2Error(SErrNoResponse,[M],EJSONRPCInternalError,ID.Clone,transactionProperty);
end
else
begin
// A response was received, and no response was expected.
if ((ID=Nil) or (ID is TJSONNull)) and (jdoStrictNotifications in Options) then
Result:=CreateJSON2Error(SErrResponseFromNotification,[M],EJSONRPCInternalError,Nil,transactionProperty);
If (ID=Nil) or (ID is TJSONNull) then // Notification method, discard result.
FreeAndNil(Result);
end;
end;
If Assigned(Result) and not (Result is TJSONErrorObject) then
Result:=FormatResult(C,M,P,ID,Result)
except
// Something went really wrong if we end up here.
On E : Exception do
begin
If (Result<>Nil) then
FreeAndNil(Result);
If Assigned(ID) and not (ID is TJSONNull) then
Result:=CreateJSON2Error(E.Message,EJSONRPCInternalError,ID.Clone,transactionproperty)
else
Result:=CreateJSON2Error(E.Message,EJSONRPCInternalError,Nil,transactionproperty);
end;
end;
end;
function TCustomJSONRPCDispatcher.DoExecute(Requests: TJSONData;AContext : TJSONRPCCallContext): TJSONData;
Var
A : TJSONArray;
Res : TJSONData;
I : Integer;
begin
Result:=Nil;
If Requests is TJSONArray then
begin
A:=Requests as TJSONArray;
Result:=TJSONArray.Create();
For I:=0 to A.Count-1 do
begin
Res:=ExecuteRequest(A[i],AContext);
If (Res<>Nil) then
TJSONArray(Result).Add(Res);
end;
end
else
Result:=ExecuteRequest(Requests,ACOntext);
end;
function TCustomJSONRPCDispatcher.CheckRequest(Request: TJSONData; Out AClassName,AMethodName : TJSONStringType; Out ID, Params : TJSONData): TJSONData;
Var
O : TJSONObject;
I : Integer;
D : TJSONData;
OJ2 : Boolean;
begin
AMethodName:='';
AClassName:='';
ID:=Nil;
Params:=Nil;
Result:=Nil;
If Not (Request is TJSONObject) then
Exit(CreateJSON2Error(SErrRequestMustBeObject,EJSONRPCInvalidRequest,Nil,transactionproperty));
O:=TJSONObject(Request);
// Get ID object, if it exists.
I:=O.IndexOfName(TransactionProperty);
If (I<>-1) then
ID:=O.Items[i];
// Check ID
If (ID=Nil) and not (jdoNotifications in Options) then
Exit(CreateJSON2Error(SErrNoIDProperty,EJSONRPCInvalidRequest,Nil,transactionproperty));
OJ2:=(jdoJSONRPC2 in Options) and not (jdoJSONRPC1 in Options);
If OJ2 then
begin
if Assigned(ID) and not (ID.JSONType in [jtNull,jtString,jtNumber]) then
Exit(CreateJSON2Error(SErrINvalidIDProperty,EJSONRPCInvalidRequest,Nil,transactionproperty));
// Check presence and value of jsonrpc property
I:=O.IndexOfName('jsonrpc');
If (I=-1) then
Exit(CreateJSON2Error(SErrNoJSONRPCProperty,EJSONRPCInvalidRequest,ID,transactionproperty));
If (O.Items[i].JSONType<>jtString) or (O.Items[i].AsString<>'2.0') then
Exit(CreateJSON2Error(SErrInvalidJSONRPCProperty,EJSONRPCInvalidRequest,ID,transactionproperty));
end;
// Get method name, if it exists.
I:=O.IndexOfName(MethodProperty);
If (I<>-1) then
D:=O.Items[i]
else
Exit(CreateJSON2Error(SErrNoMethodName,[MethodProperty],EJSONRPCInvalidRequest,ID,transactionproperty));
// Check if it is a string
if Not (D is TJSONString) then
Exit(CreateJSON2Error(SErrInvalidMethodType,[MethodProperty],EJSONRPCInvalidRequest,ID,transactionproperty));
AMethodName:=D.AsString;
If (AMethodName='') then
Exit(CreateJSON2Error(SErrNoMethodName,[MethodProperty],EJSONRPCInvalidRequest,ID,transactionproperty));
// Get class name, if it exists and is required
If (ClassNameProperty<>'') then
begin
I:=O.IndexOfName(ClassNameProperty);
If (I<>-1) then
D:=O.Items[i]
else if (jdoRequireClass in options) then
Exit(CreateJSON2Error(SErrNoClassName,[ClassNameProperty],EJSONRPCInvalidRequest,ID,transactionproperty));
// Check if it is a string
if Not (D is TJSONString) then
Exit(CreateJSON2Error(SErrInvalidClassNameType,[ClassNameProperty],EJSONRPCInvalidRequest,ID,transactionproperty));
AClassName:=D.AsString;
If (AMethodName='') and (jdoRequireClass in options) then
Exit(CreateJSON2Error(SErrNoClassName,[ClassNameProperty],EJSONRPCInvalidRequest,ID,transactionproperty));
end;
// Get params, if they exist
I:=O.IndexOfName(ParamsProperty);
If (I<>-1) then
D:=O.Items[i]
else
Exit(CreateJSON2Error(SErrNoParams,[ParamsProperty],EJSONRPCInvalidParams,ID,transactionproperty));
if OJ2 then
begin
// Allow array or object
If Not (D.JSONType in [jtArray,jtObject]) then
Exit(CreateJSON2Error(SErrParamsMustBeArrayorObject,EJSONRPCInvalidParams,ID,transactionproperty));
end
else if not (jdoJSONRPC2 in Options) then
begin
// Allow only array
If Not (D.JSONType in [jtArray]) then
Exit(CreateJSON2Error(SErrParamsMustBeArray,EJSONRPCInvalidParams,ID,transactionproperty));
end;
Params:=D;
end;
function TCustomJSONRPCDispatcher.CheckRequests(Requests: TJSONData): TJSONData;
Var
A : TJSONArray;
O : TJSONObject;
ID : TJSONData;
I,J : Integer;
begin
Result:=Nil;
If (Requests is TJSONArray) then
begin
A:=Requests as TJSONArray;
If Not (jdoJSONRPC2 in Options) then
begin
Result:=TJSONArray.Create;
For I:=0 to A.Count-1 do
begin
ID:=Nil;
If (A.Items[i] is TJSONObject) then
begin
O:=A.Objects[i];
J:=O.IndexOfName('id');
if (J<>-1) then
ID:=O.Items[J].Clone;
end;
TJSONArray(Result).Add(CreateJSON2ErrorResponse(SErrJSON2NotAllowed,EJSONRPCInvalidRequest,ID,transactionproperty));
end;
end;
end
else
If not (Requests is TJSONObject) then
Result:=CreateJSON2ErrorResponse(SErrRequestMustBeObject,EJSONRPCInvalidRequest,Nil,transactionproperty);
end;
class function TCustomJSONRPCDispatcher.TransactionProperty: String;
begin
Result:='id'; // Do not localize
end;
class function TCustomJSONRPCDispatcher.MethodProperty: String;
begin
Result:='method'; // Do not localize
end;
class function TCustomJSONRPCDispatcher.ClassNameProperty: String;
begin
Result:=''; // Do not localize
end;
class function TCustomJSONRPCDispatcher.ParamsProperty: String;
begin
Result:='params'; // Do not localize
end;
constructor TCustomJSONRPCDispatcher.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FOptions:=DefaultDispatchOptions;
end;
function TCustomJSONRPCDispatcher.Execute(Requests: TJSONData;AContext : TJSONRPCCallContext = Nil): TJSONData;
begin
If Assigned(FOnStartBatch) then
FOnStartBatch(Self);
Result:=CheckRequests(Requests);
if (Result=Nil) then // Form is OK and allowed.
Result:=DoExecute(Requests,AContext);
If Assigned(FOnEndBatch) then
FOnEndBatch(Self);
end;
{ TJSONRPCHandlerDef }
procedure TJSONRPCHandlerDef.SetFPClass(const AValue: TCustomJSONRPCHandlerClass
);
begin
FPClass:=AValue;
end;
procedure TJSONRPCHandlerDef.CheckNames(const AClassName,
AMethodName: TJSONStringType);
Var
I : Integer;
begin
If Assigned(Collection) and (Collection is TJSONRPCHandlerDefs) then
begin
I:=TJSONRPCHandlerDefs(Collection).IndexOfHandler(AClassName,AMethodName);
If (I<>-1) and (Collection.Items[i]<>Self) then
if (AClassName<>'') then
JSONRPCError(SErrDuplicateJSONRPCClassHandlerName,[AClassName,AMethodName])
else
JSONRPCError(SErrDuplicateJSONRPCHandlerName,[AClassName,AMethodName]);
end;
end;
function TJSONRPCHandlerDef.GetParamDefs: TJSONParamDefs;
begin
IF (FParamDefs=Nil) then
FParamDefs:=TJSONParamDefs.Create(TJSONParamDef);
Result:=FParamDefs;
end;
procedure TJSONRPCHandlerDef.SetHandlerClassName(const AValue: TJSONStringType);
begin
if FHandlerClassName=AValue then exit;
CheckNames(AValue,HandlerMethodName);
FHandlerClassName:=AValue;
end;
procedure TJSONRPCHandlerDef.SetHandlerMethodName(const AValue: TJSONStringType
);
begin
if FHandlerMethodName=AValue then exit;
CheckNames(HandlerClassName,AValue);
FHandlerMethodName:=AValue;
end;
procedure TJSONRPCHandlerDef.SetParamDefs(AValue: TJSONParamDefs);
begin
if FParamDefs=AValue then Exit;
IF (FParamDefs=Nil) then
FParamDefs:=TJSONParamDefs.Create(TJSONParamDef);
if (AValue<>Nil) then
FParamDefs.Assign(AValue)
else
FreeAndNil(FParamDefs);
end;
function TJSONRPCHandlerDef.CreateInstance(AOwner: TComponent; out
AContainer: TComponent): TCustomJSONRPCHandler;
Var
AClass : TCustomJSONRPCHandlerClass;
DM : TDataModule;
C : TComponent;
begin
Result:=Nil;
{$ifdef wmdebug}SendDebug(Format('Creating instance for %s',[Self.HandlerMethodName]));{$endif}
If Assigned(FDataModuleClass) then
begin
{$ifdef wmdebug}SendDebug(Format('Creating datamodule from class %d ',[Ord(Assigned(FDataModuleClass))]));{$endif}
DM:=FDataModuleClass.Create(AOwner);
{$ifdef wmdebug}SendDebug(Format('Created datamodule from class %s ',[DM.ClassName]));{$endif}
C:=DM.FindComponent(FHandlerMethodName);
If (C<>Nil) and (C is TCustomJSONRPCHandler) then
Result:=TCustomJSONRPCHandler(C)
else
begin
FreeAndNil(DM);
JSONRPCError(SErrUnknownJSONRPCMethodHandler,[FHandlerMethodName]);
end;
end
else
DM:=TDataModule.CreateNew(AOwner,0);
AContainer:=DM;
If (Result=Nil) then
begin
{$ifdef wmdebug}SendDebug(Format('Creating from class pointer %d ',[Ord(Assigned(FPClass))]));{$endif}
AClass:=FPCLass;
If Assigned(FBeforeCreate) then
FBeforeCreate(Self,AClass);
Result:=AClass.Create(AContainer);
end;
If Assigned(FAfterCreate) then
FAfterCreate(Self,Result);
end;
destructor TJSONRPCHandlerDef.Destroy;
begin
FreeAndNil(FParamDefs);
inherited Destroy;
end;
function TJSONRPCHandlerDef.HaveParamDefs: Boolean;
begin
Result:=Assigned(FParamDefs);
end;
{ TJSONRPCHandlerDefs }
function TJSONRPCHandlerDefs.GetH(Index: Integer): TJSONRPCHandlerDef;
begin
Result:=TJSONRPCHandlerDef(Items[Index]);
end;
procedure TJSONRPCHandlerDefs.SetH(Index: Integer;
const AValue: TJSONRPCHandlerDef);
begin
Items[Index]:=AValue;
end;
function TJSONRPCHandlerDefs.AddHandler(const AClassName,
AMethodName: TJSONStringType): TJSONRPCHandlerDef;
begin
If (IndexOfHandler(AClassName,AMethodName)<>-1) then
If (AClassName<>'') then
JSONRPCError(SErrDuplicateJSONRPCClassHandlerName,[AClassName,AMethodName])
else
JSONRPCError(SErrDuplicateJSONRPCHandlerName,[AMethodName]);
Result:=TJSONRPCHandlerDef(Add);
Result.FHandlerClassName:=AClassName;
Result.FHandlerMethodName:=AMethodName;
end;
function TJSONRPCHandlerDefs.AddHandler(const AClassName,
AMethodName: TJSONStringType; AClass: TCustomJSONRPCHandlerClass
): TJSONRPCHandlerDef;
begin
Result:=AddHandler(AClassName,AMethodName);
Result.HandlerClass:=AClass;
end;
function TJSONRPCHandlerDefs.IndexOfHandler(const AClassName,
AMethodName: TJSONStringType): Integer;
Function IsMatch(Index : Integer) : Boolean; inline;
Var
D : TJSONRPCHandlerDef;
begin
D:=GetH(Index);
Result:=((AClassName='') or
(CompareText(D.HandlerClassName,AClassName)=0)) and
(CompareText(AMethodName,D.HandlerMethodName)=0)
end;
begin
Result:=Count-1;
While (Result>=0) and Not IsMatch(Result) do
Dec(Result)
end;
{ TCustomJSONRPCHandlerManager }
procedure TCustomJSONRPCHandlerManager.Initialize;
begin
// Do nothing
end;
procedure TCustomJSONRPCHandlerManager.UnregisterHandler(const AClassName,
AMethodName: TJSONStringType);
Var
I : Integer;
begin
I:=IndexOfHandlerDef(AClassName,AMethodName);
If (I<>-1) then
RemoveHandlerDef(I)
else
If (AClassName<>'') then
JSONRPCError(SErrUnknownJSONRPCClassMethodHandler,[AClassName,AMethodName])
else
JSONRPCError(SErrUnknownJSONRPCMethodHandler,[AMethodName]);
end;
procedure TCustomJSONRPCHandlerManager.RegisterDatamodule(
const AClass: TDatamoduleClass; const AHandlerClassName: TJSONStringType);
Var
DM : TDatamodule;
I,J : Integer;
C : TComponent;
D : TJSONRPCHandlerDef;
B : Boolean;
CN : TJSONStringType;
begin
B:=FRegistering;
try
FRegistering:=True;
DM:=AClass.Create(Self);
try
If (AHandlerClassName='') then
CN:=DM.Name
else
CN:=AHandlerClassName;
For I:=0 to DM.ComponentCount-1 do
begin
C:=DM.Components[i];
if C is TCustomJSONRPCHandler then
begin
J:=IndexOfHandlerDef(CN,C.Name);
If (J<>-1) then
JSONRPCError(SErrDuplicateRPCCLassMethodHandler,[CN,C.Name]);
D:=AddHandlerDef(CN,C.Name);
D.ArgumentCount:=TCustomJSONRPCHandler(C).ParamDefs.Count;
D.ParamDefs:=TCustomJSONRPCHandler(C).ParamDefs;
{$ifdef wmdebug}SendDebug('Registering provider '+C.Name);{$endif}
D.FDataModuleClass:=TDataModuleClass(DM.ClassType);
end;
end;
finally
DM.Free;
end;
finally
FRegistering:=B;
end;
end;
function TCustomJSONRPCHandlerManager.RegisterHandler(
const AMethodName: TJSONStringType;
AClass: TCustomJSONRPCHandlerClass;
AArgumentCount : Integer = 0): TJSONRPCHandlerDef;
begin
Result:=RegisterHandler('',AMethodName,AClass,AArgumentCount);
end;
function TCustomJSONRPCHandlerManager.RegisterHandler(
const AClassName,AMethodName: String;
AClass: TCustomJSONRPCHandlerClass;
AArgumentCount : Integer = 0): TJSONRPCHandlerDef;
Var
I : Integer;
B : Boolean;
begin
B:=FRegistering;
try
FRegistering:=True;
I:=IndexOfHandlerDef(AClassName,AMethodname);
If (I<>-1) then
If (AClassName<>'') then
JSONRPCError(SErrDuplicateRPCCLassMethodHandler,[AClassName,AMethodName])
else
JSONRPCError(SErrDuplicateRPCMethodHandler,[AMethodName]);
Result:=AddHandlerDef(AClassName,AMEthodName);
Result.HandlerClass:=AClass;
Result.ArgumentCount:=AArgumentCount;
finally
FRegistering:=B;
end;
end;
function TCustomJSONRPCHandlerManager.FindHandlerDefByName(const AClassName,
AMethodName: TJSONStringType): TJSONRPCHandlerDef;
Var
I : integer;
begin
I:=IndexOfHandlerDef(AClassName,AMethodName);
If (I=-1) then
Result:=Nil
else
Result:=GetHandlerDef(I);
end;
function TCustomJSONRPCHandlerManager.GetHandlerDefByName(const AClassName,
AMethodName: TJSONStringType): TJSONRPCHandlerDef;
begin
Result:=FindHandlerDefByName(AClassName,AMethodName);
If (Result=Nil) then
If (AClassName<>'') then
JSONRPCError(SErrUnknownJSONRPCClassMethodHandler,[AClassName,AMethodName])
else
JSONRPCError(SErrUnknownJSONRPCMethodHandler,[AMethodName]);
end;
function TCustomJSONRPCHandlerManager.GetHandler(
const ADef: TJSONRPCHandlerDef; AOwner: TComponent; out AContainer: TComponent
): TCustomJSONRPCHandler;
Var
O : TComponent;
begin
If AOwner=Nil then
O:=Self
else
O:=AOwner;
Result:=ADef.CreateInstance(O,AContainer);
end;
function TCustomJSONRPCHandlerManager.GetHandler(const AClassName,
AMethodName: TJSONStringType; AOwner: TComponent; out AContainer: TComponent
): TCustomJSONRPCHandler;
Var
D : TJSONRPCHandlerDef;
begin
D:=GetHandlerDefByname(AClassName,AMEthodName);
Result:=GetHandler(D,AOwner,AContainer);
end;
procedure TCustomJSONRPCHandlerManager.GetClassNames(List: TStrings);
begin
end;
procedure TCustomJSONRPCHandlerManager.GetMethodsOfClass(
const AClassName: TJSONStringType; List: TStrings);
begin
end;
{ TJSONRPCHandlerManager }
procedure TJSONRPCHandlerManager.RemoveHandlerDef(const Index: Integer);
begin
FHandlerDefs.Delete(Index);
end;
function TJSONRPCHandlerManager.AddHandlerDef(const AClassName,
AMethodName: TJSONStringType): TJSONRPCHandlerDef;
begin
Result:=FHandlerDefs.AddHandler(AClassName,AMethodName);
end;
function TJSONRPCHandlerManager.IndexOfHandlerDef(const AClassName,
AMethodName: TJSONStringType): Integer;
begin
Result:=FHandlerDefs.IndexOfHandler(AClassName,AMethodName);
end;
function TJSONRPCHandlerManager.GetHandlerDef(Index: Integer
): TJSONRPCHandlerDef;
begin
Result:=FHandlerDefs[Index];
end;
function TJSONRPCHandlerManager.GetHandlerDefCount: Integer;
begin
Result:=FHandlerDefs.Count;
end;
constructor TJSONRPCHandlerManager.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FHandlerDefs:=TJSONRPCHandlerDefs.Create(TJSONRPCHandlerDef);
end;
destructor TJSONRPCHandlerManager.Destroy;
begin
FreeAndNil(FHandlerDefs);
inherited Destroy;
end;
Initialization
Finalization
FreeAndNil(TheHandler);
end.
|