wmdemo.pp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. unit wmdemo;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, HTTPDefs, websession, fpHTTP, fpWeb;
  6. type
  7. { TFPWebModule1 }
  8. TFPWebModule1 = class(TFPWebModule)
  9. procedure TFPWebActions0Request(Sender: TObject; ARequest: TRequest;
  10. AResponse: TResponse; var Handled: Boolean);
  11. procedure TFPWebActions1Request(Sender: TObject; ARequest: TRequest;
  12. AResponse: TResponse; var Handled: Boolean);
  13. procedure TFPWebActions2Request(Sender: TObject; ARequest: TRequest;
  14. AResponse: TResponse; var Handled: Boolean);
  15. procedure TFPWebActions3Request(Sender: TObject; ARequest: TRequest;
  16. AResponse: TResponse; var Handled: Boolean);
  17. procedure TFPWebActions4Request(Sender: TObject; ARequest: TRequest;
  18. AResponse: TResponse; var Handled: Boolean);
  19. procedure TFPWebActions5Request(Sender: TObject; ARequest: TRequest;
  20. AResponse: TResponse; var Handled: Boolean);
  21. procedure TFPWebActions6Request(Sender: TObject; ARequest: TRequest;
  22. AResponse: TResponse; var Handled: Boolean);
  23. private
  24. { private declarations }
  25. public
  26. { public declarations }
  27. end;
  28. var
  29. FPWebModule1: TFPWebModule1;
  30. implementation
  31. {$R *.lfm}
  32. Uses fpjson,jsonparser,fpjsonrpc,webjsonrpc, fpextdirect;
  33. { TFPWebModule1 }
  34. procedure TFPWebModule1.TFPWebActions0Request(Sender: TObject;
  35. ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);
  36. {
  37. Demo 1. Manually do everything.
  38. Only a single method call is supported.
  39. }
  40. Var
  41. P : TJSONParser;
  42. Req,Res : TJSONData;
  43. Env,O : TJSONObject;
  44. M : TJSONStringType;
  45. E : TJSONRPCEcho;
  46. I : Integer;
  47. ID : TJSONData;
  48. Err : TJSONData;
  49. begin
  50. Res:=Nil;
  51. Err:=Nil;
  52. ID:=Nil;
  53. try
  54. P:=TJSONParser.Create(ARequest.Content);
  55. try
  56. Req:=P.Parse;
  57. try
  58. If Not (Req is TJSONObject) then
  59. JSONRPCError(SErrRequestMustBeObject);
  60. O:=(Req as TJSONObject);
  61. I:=O.IndexOfName('id');
  62. If (I=-1) then
  63. JSONRPCError(SErrNoIDProperty);
  64. ID:=O.Items[i].Clone;
  65. if O.IndexOfName('method')=-1 then
  66. JSONRPCError(SErrNoMethodName);
  67. M:=O.Strings['method'];
  68. If (m<>'echo') then
  69. JSONRPCError('Only echo method is supported');
  70. E:=TJSONRPCEcho.Create(Self);
  71. try
  72. I:=O.IndexOfName('params');
  73. Res:=E.Execute(O.Items[i],Nil);
  74. finally
  75. E.Free;
  76. end;
  77. finally
  78. Req.Free;
  79. end;
  80. finally
  81. P.Free;
  82. end;
  83. except
  84. On E : Exception do
  85. Err:=TJSONObject.Create(['message',E.Message,'name',E.ClassName,'code',-1]);
  86. end;
  87. If Assigned(ID) and (ID.JSONType<>jtNull) then
  88. begin
  89. Env:=TJSONObject.Create();
  90. try
  91. If not Assigned(Res) then
  92. Res:=TJSONNull.Create;
  93. Env.Add('result',Res);
  94. If (Err=Nil) then
  95. Err:=TJSONNull.Create;
  96. Env.Add('error',Err);
  97. Env.Add('id',ID);
  98. AResponse.Content:=Env.AsJSON;
  99. finally
  100. Env.Free;
  101. end;
  102. end;
  103. AResponse.SendContent;
  104. Handled:=True;
  105. end;
  106. procedure TFPWebModule1.TFPWebActions1Request(Sender: TObject;
  107. ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);
  108. {
  109. Demo 2. Use a dispatcher to dispatch the requests.
  110. The handler is located on the owner module
  111. (it is created run-time, though).
  112. }
  113. Var
  114. Echohandler:TJSONRPCEcho;
  115. Disp : TJSONRPCDispatcher;
  116. P : TJSONParser;
  117. Req,res : TJSONData;
  118. O : TJSONRPCDispatchOptions;
  119. begin
  120. Echohandler:=TJSONRPCEcho.Create(Self);
  121. try
  122. EchoHandler.Name:='echo';
  123. Disp:=TJSONRPCDispatcher.Create(Self);
  124. try
  125. O:=Disp.Options;
  126. Include(O,jdoRequireClass);
  127. Disp.Options:=O;
  128. P:= TJSONParser.Create(ARequest.Content);
  129. try
  130. Req:=P.Parse;
  131. try
  132. Res:=Nil;
  133. Res:=Disp.Execute(Req,Nil);
  134. try
  135. If Assigned(Res) then
  136. begin
  137. AResponse.Content:=Res.AsJSON;
  138. end;
  139. AResponse.SendContent;
  140. Handled:=True;
  141. finally
  142. FreeAndNil(Res);
  143. end;
  144. finally
  145. Req.Free;
  146. end;
  147. finally
  148. P.Free;
  149. end;
  150. finally
  151. Disp.Free;
  152. end;
  153. finally
  154. EchoHandler.Free;
  155. end;
  156. end;
  157. procedure TFPWebModule1.TFPWebActions2Request(Sender: TObject;
  158. ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);
  159. {
  160. Demo 3. Use a dispatcher to dispatch the requests.
  161. The handler is registered in the JSONFPCHandlerManager
  162. (it is created run-time, though).
  163. }
  164. Var
  165. Disp : TJSONRPCDispatcher;
  166. P : TJSONParser;
  167. Req,res : TJSONData;
  168. O : TJSONRPCDispatchOptions;
  169. begin
  170. JSONRpcHandlerManager.RegisterHandler('','echo',TJSONRPCEcho);
  171. try
  172. Disp:=TJSONRPCDispatcher.Create(Self);
  173. try
  174. O:=Disp.Options;
  175. Include(O,jdoSearchRegistry);
  176. Disp.Options:=O;
  177. P:= TJSONParser.Create(ARequest.Content);
  178. try
  179. Req:=P.Parse;
  180. try
  181. Res:=Nil;
  182. Res:=Disp.Execute(Req,Nil);
  183. try
  184. If Assigned(Res) then
  185. begin
  186. AResponse.Content:=Res.AsJSON;
  187. end;
  188. AResponse.SendContent;
  189. Handled:=True;
  190. finally
  191. FreeAndNil(Res);
  192. end;
  193. finally
  194. Req.Free;
  195. end;
  196. finally
  197. P.Free;
  198. end;
  199. finally
  200. Disp.Free;
  201. end;
  202. finally
  203. JSONRpcHandlerManager.UnRegisterHandler('','echo');
  204. end;
  205. end;
  206. procedure TFPWebModule1.TFPWebActions3Request(Sender: TObject;
  207. ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);
  208. {
  209. Demo 4. Ext.Direct dispatcher
  210. The handler is registered in the JSONFPCHandlerManager
  211. (it is created run-time, though).
  212. }
  213. Var
  214. Disp : TExtDirectDispatcher;
  215. P : TJSONParser;
  216. Req,res : TJSONData;
  217. O : TJSONRPCDispatchOptions;
  218. begin
  219. JSONRpcHandlerManager.RegisterHandler('test','echo',TJSONRPCEcho);
  220. try
  221. Disp:=TExtDirectDispatcher.Create(Self);
  222. try
  223. O:=Disp.Options;
  224. Include(O,jdoSearchRegistry);
  225. Disp.Options:=O;
  226. P:= TJSONParser.Create(ARequest.Content);
  227. try
  228. Req:=P.Parse;
  229. try
  230. Res:=Nil;
  231. Res:=Disp.Execute(Req,Nil);
  232. try
  233. If Assigned(Res) then
  234. begin
  235. AResponse.Content:=Res.AsJSON;
  236. end;
  237. AResponse.ContentLength:=Length(AResponse.Content);
  238. AResponse.SendContent;
  239. Handled:=True;
  240. finally
  241. FreeAndNil(Res);
  242. end;
  243. finally
  244. Req.Free;
  245. end;
  246. finally
  247. P.Free;
  248. end;
  249. finally
  250. Disp.Free;
  251. end;
  252. finally
  253. JSONRpcHandlerManager.UnRegisterHandler('','echo');
  254. end;
  255. end;
  256. procedure TFPWebModule1.TFPWebActions4Request(Sender: TObject;
  257. ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);
  258. {
  259. Demo 5. Using a TJSONRPCContentProducer.
  260. The handler is registered in the JSONFPCHandlerManager
  261. (it is created run-time, though).
  262. }
  263. Var
  264. Cont : TJSONRPCContentProducer;
  265. disp : TJSONRPCDispatcher;
  266. begin
  267. JSONRpcHandlerManager.RegisterHandler('test','echo',TJSONRPCEcho);
  268. try
  269. Cont:=TJSONRPCContentProducer.Create(Self);
  270. try
  271. disp:=TJSONRPCDispatcher.Create(Cont);
  272. Disp.Options:=Disp.OPtions+[jdoSearchRegistry];
  273. Cont.Dispatcher:=Disp;
  274. AResponse.ContentStream:=TMemoryStream.Create;
  275. try
  276. Cont.GetContent(ARequest,AResponse.ContentStream,Handled);
  277. AResponse.ContentLength:=AResponse.ContentStream.Size;
  278. If Handled then
  279. AResponse.SendContent;
  280. finally
  281. AResponse.ContentStream.Free;
  282. end;
  283. finally
  284. Cont.Free;
  285. end;
  286. finally
  287. JSONRpcHandlerManager.UnRegisterHandler('','echo');
  288. end;
  289. end;
  290. procedure TFPWebModule1.TFPWebActions5Request(Sender: TObject;
  291. ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);
  292. {
  293. Demo 6. Creating an API response for Ext.Direct
  294. The handler is registered in the JSONFPCHandlerManager
  295. (it is created run-time, though).
  296. }
  297. Var
  298. D : TExtDirectDispatcher;
  299. I : Integer;
  300. begin
  301. JSONRpcHandlerManager.RegisterHandler('test','echo',TJSONRPCEcho);
  302. try
  303. D:=TExtDirectDispatcher.Create(Self);
  304. try
  305. D.URL:=BaseURL+'ExtDirect';
  306. D.Options:=D.Options+[jdoSearchRegistry];
  307. AResponse.Content:=D.APIAsString;
  308. AResponse.ContentLength:=Length(AResponse.Content);
  309. finally
  310. D.Free;
  311. end;
  312. finally
  313. JSONRpcHandlerManager.UnRegisterHandler('','echo');
  314. end;
  315. end;
  316. procedure TFPWebModule1.TFPWebActions6Request(Sender: TObject;
  317. ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);
  318. {
  319. Demo 6. Using a TJSONRPCModule instance to handle the request.
  320. The handler is registered in the JSONFPCHandlerManager.
  321. (it is created run-time, though).
  322. }
  323. Var
  324. M : TJSONRPCModule;
  325. begin
  326. JSONRpcHandlerManager.RegisterHandler('test','echo',TJSONRPCEcho);
  327. try
  328. M:=TJSONRPCModule.CreateNew(Self,0);
  329. try
  330. M.HandleRequest(ARequest,AResponse);
  331. Handled:=True;
  332. finally
  333. M.Free;
  334. end;
  335. finally
  336. JSONRpcHandlerManager.UnRegisterHandler('','echo');
  337. end;
  338. end;
  339. initialization
  340. RegisterHTTPModule('echo', TFPWebModule1);
  341. end.