All Classes Namespaces Functions Variables Groups Pages
RestHandler.qm.dox.h
// -*- mode: c++; indent-tabs-mode: nil -*-
/* RestHandler.qm Copyright 2013 Qore Technologies, sro
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
// this module requires Qore 0.8.8 or better
// require type definitions everywhere
// enable all warnings
/* Version History
* 2014-01-20 v1.1: David Nichols <david@qore.org>
+ added support for the OPTIONS method
+ return an error if an unsupported HTTP method is used in a REST Call
* 2013-10-04 v1.0: David Nichols <david@qore.org>
+ the initial version of the RestHandler module
*/
namespace RestHandler {
class AbstractRestClass {
public:
private:
public:
constructor();
abstract string name();
*AbstractRestClass subClass(string name, hash cx, *hash args);
hash handleRequest(RestHandler rh, *list cl, string mn, hash cx, *hash args);
private hash dispatch(RestHandler rh, *string oname, string mn, *hash ah, hash cx);
private hash unknownSubClassError(string name);
};
class RestHandler : public HttpServer::AbstractHttpRequestHandler,public AbstractRestClass {
public:
public :
const Err501 = (
"code": 501,
"body": "not implemented",
);
const Methods = (
"GET": "get",
"POST": "post",
"PUT": "put",
"DELETE": "del",
"OPTIONS": "options",
);
const MimeDataTypes = (
MimeTypeJsonRpc: (
"serialize": \makeJSONString(),
"deserialize": \parseJSON(),
),
MimeTypeYaml: (
"serialize": \makeYAML(),
"deserialize": \parseYAML(),
),
MimeTypeYamlRpc: (
"serialize": \makeYAML(),
"deserialize": \parseYAML(),
),
MimeTypeXml: (
"serialize": \makeXMLRPCValueString(),
"deserialize": \parseXMLRPCValue(),
),
MimeTypeHtml: (
"serialize": string (any $body) { return sprintf("<pre>%N</pre>", $body); },
),
);
public:
private :
hash ch;
Mutex mx();
public:
constructor(*AbstractAuthenticator auth);
string name();
hash handleRequest(hash cx, hash hdr, *data b);
removeRootPath(reference path);
requestDeserializationError(hash hdr, hash cx, string body);
responseSerializationError(hash cx, *hash aih, hash rv);
logInfo(string fmt);
logError(string fmt);
logDebug(string fmt);
addClass(AbstractRestClass cls);
static hash makeResponse(int code, any body, *hash hdr);
};
};
RestHandler::RestHandler::logError
logError(string fmt)
default implementation is empty; this method is called with error log messages; reimplement in subcla...
RestHandler::AbstractRestClass::dispatch
private hash dispatch(RestHandler rh, *string oname, string mn, *hash ah, hash cx)
this method is called to dispatch requests on the given object
sprintf
string sprintf(string fmt,...)
RestHandler::RestHandler::Err501
const Err501
a hash for a 501 Unimplemented error message
Definition: RestHandler.qm.dox.h:373
RestHandler::RestHandler::ch
hash ch
class hash: name -&gt; AbstractRestClass
Definition: RestHandler.qm.dox.h:415
HttpServer::AbstractHttpRequestHandler
RestHandler::RestHandler::name
string name()
returns the name of the root REST class
HttpServer::AbstractHttpRequestHandler::auth
*AbstractAuthenticator auth
MimeTypeYaml
const MimeTypeYaml
MimeTypeXml
const MimeTypeXml
RestHandler::RestHandler::makeResponse
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 ...
RestHandler::RestHandler::logDebug
logDebug(string fmt)
default implementation is empty; this method is called with debug log messages; reimplement in subcla...
MimeTypeJsonRpc
const MimeTypeJsonRpc
RestHandler::RestHandler::MimeDataTypes
const MimeDataTypes
supported mime types for de/serializing responses
Definition: RestHandler.qm.dox.h:388
RestHandler::RestHandler::removeRootPath
removeRootPath(reference path)
default implementation is empty
list
list list(...)
RestHandler::RestHandler::requestDeserializationError
requestDeserializationError(hash hdr, hash cx, string body)
default implementation is empty
RestHandler::RestHandler::responseSerializationError
responseSerializationError(hash cx, *hash aih, hash rv)
default implementation is empty
RestHandler::RestHandler::addClass
addClass(AbstractRestClass cls)
adds a REST class to the handler
RestHandler::RestHandler::logInfo
logInfo(string fmt)
default implementation is empty; this method is called with informational log messages; reimplement i...
MimeTypeYamlRpc
const MimeTypeYamlRpc
RestHandler::AbstractRestClass::subClass
*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;...
RestHandler::AbstractRestClass::handleRequest
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 ...
RestHandler::AbstractRestClass
the base abstract class for REST handler classes
Definition: RestHandler.qm.dox.h:311
RestHandler::RestHandler::Methods
const Methods
supported HTTP methods
Definition: RestHandler.qm.dox.h:379
RestHandler::RestHandler::constructor
constructor(*AbstractAuthenticator auth)
create the object optionally with the given AbstractAuthenticator
RestHandler::RestHandler::handleRequest
hash handleRequest(hash cx, hash hdr, *data b)
called by the HTTP server to handle incoming HTTP requests
RestHandler::AbstractRestClass::name
abstract string name()
this provides the name of the REST class
RestHandler::AbstractRestClass::unknownSubClassError
private hash unknownSubClassError(string name)
throws a &quot;REST-CLASS-ERROR&quot; exception when a request tries to access an unknown subclass ...
RestHandler::RestHandler::mx
Mutex mx()
mutex lock
MimeTypeHtml
const MimeTypeHtml
hash
hash hash(object obj)
RestHandler::RestHandler
this is the main handler class to be registered with the HttpServer
Definition: RestHandler.qm.dox.h:368