Browse Source

* Some fixes & improvements

Michaël Van Canneyt 3 years ago
parent
commit
2be3978bb4
1 changed files with 22 additions and 5 deletions
  1. 22 5
      packages/fcl-web/src/jsonrpc/fpjsonrpc.pp

+ 22 - 5
packages/fcl-web/src/jsonrpc/fpjsonrpc.pp

@@ -112,6 +112,9 @@ Type
     Destructor Destroy; override;
     Destructor Destroy; override;
     Procedure CheckParams(Const Params : TJSONData);
     Procedure CheckParams(Const Params : TJSONData);
     Function ParamByName(Const AName : String) : TJSONData;
     Function ParamByName(Const AName : String) : TJSONData;
+    // Called before execute is called.
+    Procedure SetRequestClassAndMethod(const aClassName,aMethodName : String); virtual;
+    // Actually call method.
     Function Execute(Const Params : TJSONData; AContext : TJSONRPCCallContext = Nil) : TJSONData;
     Function Execute(Const Params : TJSONData; AContext : TJSONRPCCallContext = Nil) : TJSONData;
     // Checked on incoming request
     // Checked on incoming request
     Property ParamDefs : TJSONParamDefs Read FParamDefs Write SetParamDefs;
     Property ParamDefs : TJSONParamDefs Read FParamDefs Write SetParamDefs;
@@ -746,6 +749,12 @@ begin
     end;
     end;
 end;
 end;
 
 
+procedure TCustomJSONRPCHandler.SetRequestClassAndMethod(const aClassName, aMethodName: String);
+begin
+  // Do nothing
+  if aClassName=aMethodName then;
+end;
+
 procedure TCustomJSONRPCHandler.SetParamDefs(const AValue: TJSONParamDefs);
 procedure TCustomJSONRPCHandler.SetParamDefs(const AValue: TJSONParamDefs);
 begin
 begin
   if FParamDefs=AValue then exit;
   if FParamDefs=AValue then exit;
@@ -1127,14 +1136,22 @@ function TCustomJSONRPCDispatcher.ExecuteMethod(Const AClassName,AMethodName: TJ
 Var
 Var
   H : TCustomJSONRPCHandler;
   H : TCustomJSONRPCHandler;
   FreeObject : TComponent;
   FreeObject : TComponent;
+  aClonedID : TJSONData;
 
 
 begin
 begin
   H:=FindHandler(AClassName,AMethodName,AContext,FreeObject);
   H:=FindHandler(AClassName,AMethodName,AContext,FreeObject);
   If (H=Nil) then
   If (H=Nil) then
+    begin
+    if Assigned(ID) then
+      aClonedID:=ID.Clone
+    else
+      aClonedID:=TJSONNull.Create;
     if (AClassName='') then
     if (AClassName='') then
-      Exit(CreateJSON2Error(SErrInvalidMethodName,[AMethodName],EJSONRPCMethodNotFound,ID.Clone,transactionProperty))
+      Exit(CreateJSON2Error(SErrInvalidMethodName,[AMethodName],EJSONRPCMethodNotFound,aClonedID,transactionProperty))
     else
     else
-      Exit(CreateJSON2Error(SErrInvalidClassMethodName,[AClassName,AMethodName],EJSONRPCMethodNotFound,ID.Clone,transactionProperty));
+      Exit(CreateJSON2Error(SErrInvalidClassMethodName,[AClassName,AMethodName],EJSONRPCMethodNotFound,aClonedID,transactionProperty));
+    end;
+  H.SetRequestClassAndMethod(aClassName,aMethodName);
   try
   try
     If Assigned(FOndispatchRequest) then
     If Assigned(FOndispatchRequest) then
       FOndispatchRequest(Self,AClassName,AMethodName,Params);
       FOndispatchRequest(Self,AClassName,AMethodName,Params);
@@ -1194,7 +1211,7 @@ begin
       If (Result=Nil) then
       If (Result=Nil) then
         begin
         begin
         // No response, and a response was expected.
         // No response, and a response was expected.
-        if (ID<>Nil) or not (jdoNotifications in Options) then
+        if (ID<>Nil) and (jdoStrictNotifications in Options) then
           Result:=CreateJSON2Error(SErrNoResponse,[M],EJSONRPCInternalError,ID,transactionProperty);
           Result:=CreateJSON2Error(SErrNoResponse,[M],EJSONRPCInternalError,ID,transactionProperty);
         end
         end
       else
       else
@@ -1308,7 +1325,7 @@ begin
     if Not (D is TJSONString) then
     if Not (D is TJSONString) then
       Exit(CreateJSON2Error(SErrInvalidClassNameType,[ClassNameProperty],EJSONRPCInvalidRequest,ID,transactionproperty));
       Exit(CreateJSON2Error(SErrInvalidClassNameType,[ClassNameProperty],EJSONRPCInvalidRequest,ID,transactionproperty));
     AClassName:=D.AsString;
     AClassName:=D.AsString;
-    If (AMethodName='') and (jdoRequireClass in options)  then
+    If (AClassName='') and (jdoRequireClass in options)  then
       Exit(CreateJSON2Error(SErrNoClassName,[ClassNameProperty],EJSONRPCInvalidRequest,ID,transactionproperty));
       Exit(CreateJSON2Error(SErrNoClassName,[ClassNameProperty],EJSONRPCInvalidRequest,ID,transactionproperty));
     end;
     end;
   // Get params, if they exist
   // Get params, if they exist
@@ -1379,7 +1396,7 @@ end;
 
 
 class function TCustomJSONRPCDispatcher.ClassNameProperty: String;
 class function TCustomJSONRPCDispatcher.ClassNameProperty: String;
 begin
 begin
-  Result:=''; // Do not localize
+  Result:='classname'; // Do not localize
 end;
 end;
 
 
 class function TCustomJSONRPCDispatcher.ParamsProperty: String;
 class function TCustomJSONRPCDispatcher.ParamsProperty: String;