summaryrefslogtreecommitdiff
path: root/packages/fcl-web/examples/jsonrpc/demo1/wmdemo.pp
blob: 907f7a2bedfd23c55147392b9965c4eef9961a8d (plain)
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
unit wmdemo; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, HTTPDefs, websession, fpHTTP, fpWeb; 

type

  { TFPWebModule1 }

  TFPWebModule1 = class(TFPWebModule)
    procedure DataModuleCreate(Sender: TObject);
    procedure TFPWebActions0Request(Sender: TObject; ARequest: TRequest;
      AResponse: TResponse; var Handled: Boolean);
    procedure TFPWebActions1Request(Sender: TObject; ARequest: TRequest;
      AResponse: TResponse; var Handled: Boolean);
    procedure TFPWebActions2Request(Sender: TObject; ARequest: TRequest;
      AResponse: TResponse; var Handled: Boolean);
    procedure TFPWebActions3Request(Sender: TObject; ARequest: TRequest;
      AResponse: TResponse; var Handled: Boolean);
    procedure TFPWebActions4Request(Sender: TObject; ARequest: TRequest;
      AResponse: TResponse; var Handled: Boolean);
    procedure TFPWebActions5Request(Sender: TObject; ARequest: TRequest;
      AResponse: TResponse; var Handled: Boolean);
    procedure TFPWebActions6Request(Sender: TObject; ARequest: TRequest;
      AResponse: TResponse; var Handled: Boolean);
  private
    { private declarations }
  public
    { public declarations }
  end; 

var
  FPWebModule1: TFPWebModule1; 

implementation

Uses fpjson,jsonparser,fpjsonrpc,webjsonrpc, fpextdirect;

{ TFPWebModule1 }

procedure TFPWebModule1.DataModuleCreate(Sender: TObject);
begin
end;

procedure TFPWebModule1.TFPWebActions0Request(Sender: TObject;
  ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);
{
  Demo 1. Manually do everything.
  Only a single method call is supported.
}
Var
  P : TJSONParser;
  Req,Res : TJSONData;
  Env,O : TJSONObject;
  M : TJSONStringType;
  E : TJSONRPCEcho;
  I : Integer;
  ID : TJSONData;
  Err : TJSONData;

begin
  Res:=Nil;
  Err:=Nil;
  ID:=Nil;
  try
    P:=TJSONParser.Create(ARequest.Content);
    try
      Req:=P.Parse;
      try
        If Not (Req is TJSONObject) then
          JSONRPCError(SErrRequestMustBeObject);
        O:=(Req as TJSONObject);
        I:=O.IndexOfName('id');
        If (I=-1) then
          JSONRPCError(SErrNoIDProperty);
        ID:=O.Items[i].Clone;
        if O.IndexOfName('method')=-1 then
          JSONRPCError(SErrNoMethodName);
        M:=O.Strings['method'];
        If (m<>'echo') then
          JSONRPCError('Only echo method is supported');
        E:=TJSONRPCEcho.Create(Self);
        try
          I:=O.IndexOfName('params');
          Res:=E.Execute(O.Items[i],Nil);
        finally
          E.Free;
        end;
      finally
        Req.Free;
      end;
    finally
      P.Free;
    end;
  except
    On E : Exception do
      Err:=TJSONObject.Create(['message',E.Message,'name',E.ClassName,'code',-1]);
  end;
  If Assigned(ID) and (ID.JSONType<>jtNull) then
    begin
    Env:=TJSONObject.Create();
    try
      If not Assigned(Res) then
        Res:=TJSONNull.Create;
      Env.Add('result',Res);
      If (Err=Nil) then
        Err:=TJSONNull.Create;
      Env.Add('error',Err);
      Env.Add('id',ID);
      AResponse.Content:=Env.AsJSON;
    finally
      Env.Free;
    end;
    end;
  AResponse.SendContent;
  Handled:=True;
end;

procedure TFPWebModule1.TFPWebActions1Request(Sender: TObject;
  ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);

{
  Demo 2. Use a dispatcher to dispatch the requests.
  The handler is located on the owner module
  (it is created run-time, though)
}

Var
  Echohandler:TJSONRPCEcho;
  Disp : TJSONRPCDispatcher;
  P : TJSONParser;
  Req,res : TJSONData;
  O : TJSONRPCDispatchOptions;

begin
  Echohandler:=TJSONRPCEcho.Create(Self);
  try
    EchoHandler.Name:='echo';
    Disp:=TJSONRPCDispatcher.Create(Self);
    try
      O:=Disp.Options;
      Include(O,jdoRequireClass);
      Disp.Options:=O;
      P:= TJSONParser.Create(ARequest.Content);
      try
        Req:=P.Parse;
        try
          Res:=Nil;
          Res:=Disp.Execute(Req,Nil);
          try
            If Assigned(Res) then
              begin
              AResponse.Content:=Res.AsJSON;
              end;
            AResponse.SendContent;
            Handled:=True;
          finally
            FreeAndNil(Res);
          end;
        finally
          Req.Free;
        end;
      finally
        P.Free;
      end;
    finally
      Disp.Free;
    end;
  finally
    EchoHandler.Free;
  end;

end;

procedure TFPWebModule1.TFPWebActions2Request(Sender: TObject;
  ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);
{
  Demo 3. Use a dispatcher to dispatch the requests,
  The handler is registered in the JSONFPCHandlerManager.
  (it is created run-time, though)
}

Var
  Disp : TJSONRPCDispatcher;
  P : TJSONParser;
  Req,res : TJSONData;
  O : TJSONRPCDispatchOptions;

begin
  JSONRpcHandlerManager.RegisterHandler('','echo',TJSONRPCEcho);
  try
    Disp:=TJSONRPCDispatcher.Create(Self);
    try
      O:=Disp.Options;
      Include(O,jdoSearchRegistry);
      Disp.Options:=O;
      P:= TJSONParser.Create(ARequest.Content);
      try
        Req:=P.Parse;
        try
          Res:=Nil;
          Res:=Disp.Execute(Req,Nil);
          try
            If Assigned(Res) then
              begin
              AResponse.Content:=Res.AsJSON;
              end;
            AResponse.SendContent;
            Handled:=True;
          finally
            FreeAndNil(Res);
          end;
        finally
          Req.Free;
        end;
      finally
        P.Free;
      end;
    finally
      Disp.Free;
    end;
  finally
    JSONRpcHandlerManager.UnRegisterHandler('','echo');
  end;
end;

procedure TFPWebModule1.TFPWebActions3Request(Sender: TObject;
  ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);

{
  Demo 4. Ext.Direct dispatcher
  The handler is registered in the JSONFPCHandlerManager.
  (it is created run-time, though)
}

Var
  Disp : TExtDirectDispatcher;
  P : TJSONParser;
  Req,res : TJSONData;
  O : TJSONRPCDispatchOptions;

begin
  JSONRpcHandlerManager.RegisterHandler('test','echo',TJSONRPCEcho);
  try
    Disp:=TExtDirectDispatcher.Create(Self);
    try
      O:=Disp.Options;
      Include(O,jdoSearchRegistry);
      Disp.Options:=O;
      P:= TJSONParser.Create(ARequest.Content);
      try
        Req:=P.Parse;
        try
          Res:=Nil;
          Res:=Disp.Execute(Req,Nil);
          try
            If Assigned(Res) then
              begin
              AResponse.Content:=Res.AsJSON;
              end;
            AResponse.ContentLength:=Length(AResponse.Content);
            AResponse.SendContent;
            Handled:=True;
          finally
            FreeAndNil(Res);
          end;
        finally
          Req.Free;
        end;
      finally
        P.Free;
      end;
    finally
      Disp.Free;
    end;
  finally
    JSONRpcHandlerManager.UnRegisterHandler('','echo');
  end;
end;

procedure TFPWebModule1.TFPWebActions4Request(Sender: TObject;
  ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);

{
  Demo 5. Using a TJSONRPCContentProducer.
  The handler is registered in the JSONFPCHandlerManager.
  (it is created run-time, though)
}

Var
  Cont : TJSONRPCContentProducer;
  disp : TJSONRPCDispatcher;

begin
  JSONRpcHandlerManager.RegisterHandler('test','echo',TJSONRPCEcho);
  try
    Cont:=TJSONRPCContentProducer.Create(Self);
    try
      disp:=TJSONRPCDispatcher.Create(Cont);
      Disp.Options:=Disp.OPtions+[jdoSearchRegistry];
      Cont.Dispatcher:=Disp;
      AResponse.ContentStream:=TMemoryStream.Create;
      try
        Cont.GetContent(ARequest,AResponse.ContentStream,Handled);
        AResponse.ContentLength:=AResponse.ContentStream.Size;
        If Handled then
          AResponse.SendContent;
      finally
        AResponse.ContentStream.Free;
      end;
    finally
      Cont.Free;
    end;
  finally
    JSONRpcHandlerManager.UnRegisterHandler('','echo');
  end;
end;

procedure TFPWebModule1.TFPWebActions5Request(Sender: TObject;
  ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);
{
  Demo 6. creating an API response for Ext.Direct
  The handler is registered in the JSONFPCHandlerManager.
  (it is created run-time, though)
}

Var
  D : TExtDirectDispatcher;
  I : Integer;

begin
  JSONRpcHandlerManager.RegisterHandler('test','echo',TJSONRPCEcho);
  try
    D:=TExtDirectDispatcher.Create(Self);
    try
      D.URL:=BaseURL+'ExtDirect';
      D.Options:=D.Options+[jdoSearchRegistry];
      AResponse.Content:=D.APIAsString;
      AResponse.ContentLength:=Length(AResponse.Content);
    finally
      D.Free;
    end;
  finally
    JSONRpcHandlerManager.UnRegisterHandler('','echo');
  end;
end;

procedure TFPWebModule1.TFPWebActions6Request(Sender: TObject;
  ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);
{
  Demo 6. Using a TJSONRPCModule instance to handle the request.
  The handler is registered in the JSONFPCHandlerManager.
  (it is created run-time, though)
}

Var
  M : TJSONRPCModule;

begin
  JSONRpcHandlerManager.RegisterHandler('test','echo',TJSONRPCEcho);
  try
    M:=TJSONRPCModule.CreateNew(Self,0);
    try
      M.HandleRequest(ARequest,AResponse);
      Handled:=True;
    finally
      M.Free;
    end;
  finally
    JSONRpcHandlerManager.UnRegisterHandler('','echo');
  end;
end;

initialization
  {$I wmdemo.lrs}

  RegisterHTTPModule('echo', TFPWebModule1);
end.