Qore RestHandler Module Reference  1.1
 All Classes Namespaces Functions Variables Groups Pages
RestHandler.qm.dox.h
1 // -*- mode: c++; indent-tabs-mode: nil -*-
3 
4 /* RestHandler.qm Copyright 2013 Qore Technologies, sro
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23 */
24 
25 // this module requires Qore 0.8.8 or better
26 
27 // require type definitions everywhere
28 
29 // enable all warnings
30 
31 
32 
33 
34 
35 
36 /* Version History
37  * 2014-01-20 v1.1: David Nichols <david@qore.org>
38  + added support for the OPTIONS method
39  + return an error if an unsupported HTTP method is used in a REST Call
40 
41  * 2013-10-04 v1.0: David Nichols <david@qore.org>
42  + the initial version of the RestHandler module
43 */
44 
308 namespace RestHandler {
312 
313 public:
314 private:
315 
316 public:
317 
318  constructor();
319 
320 
322  abstract string name();
323 
325 
341  *AbstractRestClass subClass(string name, hash cx, *hash args);
342 
343 
345  hash handleRequest(RestHandler rh, *list cl, string mn, hash cx, *hash args);
346 
347 
349  private hash dispatch(RestHandler rh, *string oname, string mn, *hash ah, hash cx);
350 
351 
353  private hash unknownSubClassError(string name);
354 
355  };
356 
358 
369 
370 public:
371  public :
373  const Err501 = (
374  "code": 501,
375  "body": "not implemented",
376  );
377 
379  const Methods = (
380  "GET": "get",
381  "POST": "post",
382  "PUT": "put",
383  "DELETE": "del",
384  "OPTIONS": "options",
385  );
386 
388  const MimeDataTypes = (
389  MimeTypeJsonRpc: (
390  "serialize": \makeJSONString(),
391  "deserialize": \parseJSON(),
392  ),
393  MimeTypeYaml: (
394  "serialize": \makeYAML(),
395  "deserialize": \parseYAML(),
396  ),
397 
398  MimeTypeYamlRpc: (
399  "serialize": \makeYAML(),
400  "deserialize": \parseYAML(),
401  ),
402  MimeTypeXml: (
403  "serialize": \makeXMLRPCValueString(),
404  "deserialize": \parseXMLRPCValue(),
405  ),
406  MimeTypeHtml: (
407  "serialize": string (any $body) { return sprintf("<pre>%N</pre>", $body); },
408  ),
409  );
410 
411 public:
412 
413  private :
416 
418  Mutex mx();
419 
420 public:
421 
423 
425  constructor(*AbstractAuthenticator auth);
426 
427 
429  string name();
430 
431 
433 
455  hash handleRequest(hash cx, hash hdr, *data b);
456 
457 
459  removeRootPath(reference path);
460 
461 
463  requestDeserializationError(hash hdr, hash cx, string body);
464 
465 
468 
469 
471  logInfo(string fmt);
472 
473 
475  logError(string fmt);
476 
477 
479  logDebug(string fmt);
480 
481 
484 
485 
487  static hash makeResponse(int code, any body, *hash hdr);
488  };
489 };
logError(string fmt)
default implementation is empty; this method is called with error log messages; reimplement in subcla...
private hash dispatch(RestHandler rh, *string oname, string mn, *hash ah, hash cx)
this method is called to dispatch requests on the given object
string sprintf(string fmt,...)
const Err501
a hash for a 501 Unimplemented error message
Definition: RestHandler.qm.dox.h:373
hash ch
class hash: name -&gt; AbstractRestClass
Definition: RestHandler.qm.dox.h:415
string name()
returns the name of the root REST class
const MimeTypeYaml
const MimeTypeXml
static hash makeResponse(int code, any body, *hash hdr)
creates a hash for an HTTP response with the response code and a literal response message body ...
logDebug(string fmt)
default implementation is empty; this method is called with debug log messages; reimplement in subcla...
const MimeTypeJsonRpc
const MimeDataTypes
supported mime types for de/serializing responses
Definition: RestHandler.qm.dox.h:388
removeRootPath(reference path)
default implementation is empty
list list(...)
requestDeserializationError(hash hdr, hash cx, string body)
default implementation is empty
responseSerializationError(hash cx, *hash aih, hash rv)
default implementation is empty
addClass(AbstractRestClass cls)
adds a REST class to the handler
logInfo(string fmt)
default implementation is empty; this method is called with informational log messages; reimplement i...
const MimeTypeYamlRpc
*AbstractRestClass subClass(string name, hash cx, *hash args)
this method will be called to find a sub-class (ie with GET /invoices/1 - if this class represents &quot;...
hash handleRequest(RestHandler rh, *list cl, string mn, hash cx, *hash args)
this method is called by the RestHandler class to match the right object with incoming requests ...
the base abstract class for REST handler classes
Definition: RestHandler.qm.dox.h:311
const Methods
supported HTTP methods
Definition: RestHandler.qm.dox.h:379
constructor(*AbstractAuthenticator auth)
create the object optionally with the given AbstractAuthenticator
hash handleRequest(hash cx, hash hdr, *data b)
called by the HTTP server to handle incoming HTTP requests
abstract string name()
this provides the name of the REST class
private hash unknownSubClassError(string name)
throws a &quot;REST-CLASS-ERROR&quot; exception when a request tries to access an unknown subclass ...
Mutex mx()
mutex lock
const MimeTypeHtml
hash hash(object obj)
this is the main handler class to be registered with the HttpServer
Definition: RestHandler.qm.dox.h:368