Nuria Framework - Network
Network module of the NuriaProject Framework
 All Classes Functions Typedefs Enumerations Enumerator
restfulhttpnode.hpp
1 /* Copyright (c) 2014, The Nuria Project
2  * This software is provided 'as-is', without any express or implied
3  * warranty. In no event will the authors be held liable for any damages
4  * arising from the use of this software.
5  * Permission is granted to anyone to use this software for any purpose,
6  * including commercial applications, and to alter it and redistribute it
7  * freely, subject to the following restrictions:
8  * 1. The origin of this software must not be misrepresented; you must not
9  * claim that you wrote the original software. If you use this software
10  * in a product, an acknowledgment in the product documentation would be
11  * appreciated but is not required.
12  * 2. Altered source versions must be plainly marked as such, and must not be
13  * misrepresented as being the original software.
14  * 3. This notice may not be removed or altered from any source
15  * distribution.
16  */
17 
18 #ifndef NURIA_RESTFULHTTPNODE_HPP
19 #define NURIA_RESTFULHTTPNODE_HPP
20 
21 #include "httpclient.hpp"
22 #include "httpnode.hpp"
23 
27 #define NURIA_RESTFUL(Path) NURIA_ANNOTATE(org.nuriaproject.network.restful, Path)
28 
34 #define NURIA_RESTFUL_VERBS(Verbs) NURIA_ANNOTATE(org.nuriaproject.network.restful.verbs, int(Verbs))
35 
36 namespace Nuria {
37 
38 namespace Internal { struct RestfulHttpNodeSlotData; }
39 
40 class RestfulHttpNodePrivate;
41 class MetaMethod;
42 class MetaObject;
43 
110 class NURIA_NETWORK_EXPORT RestfulHttpNode : public HttpNode {
111  Q_OBJECT
112  Q_ENUMS(ReplyFormat)
113 public:
114 
123  explicit RestfulHttpNode (void *object, MetaObject *metaObject, const QString &resourceName,
124  HttpNode *parent = 0);
125 
127  explicit RestfulHttpNode (const QString &resourceName = QString (), HttpNode *parent = 0);
128 
130  ~RestfulHttpNode ();
131 
142  void setRestfulHandler (HttpClient::HttpVerbs verbs, const QString &path,
143  const QStringList &argumentNames, const Callback &callback);
144 
150  void setRestfulHandler (const QString &path, const QStringList &argumentNames,
151  const Callback &callback);
152 
153 protected:
154 
163  virtual QByteArray convertVariantToData (const QVariant &variant);
164 
170  virtual QVariant convertArgumentToVariant (const QString &argumentData, int targetType);
171 
178  virtual void conversionFailure (const QVariant &variant, HttpClient *client);
179 
184  QByteArray generateResultData (QVariant result, HttpClient *client);
185 
187  bool invokePath (const QString &path, const QStringList &parts,
188  int index, HttpClient *client) override;
189 
190 private:
191  friend class RestfulHttpNodePrivate;
192 
193  void registerAnnotatedHandlers ();
194  void registerMetaMethod (MetaMethod &method);
195  void registerRestfulHandlerFromMethod (const QString &path, MetaMethod &method);
196  void delayedRegisterMetaObject ();
197  QStringList argumentNamesWithoutClient (MetaMethod &method);
198  QString compilePathRegEx (QString path);
199  bool invokeMatch (Internal::RestfulHttpNodeSlotData &slotData,
200  QRegularExpressionMatch &match, HttpClient *client);
201  QVariantList argumentValues (const QStringList &names, const QList<int> &types,
202  QRegularExpressionMatch &match, HttpClient *client);
203  bool writeResponse (const QVariant &response, HttpClient *client);
204 
205  RestfulHttpNodePrivate *d_ptr;
206 
207 };
208 
209 }
210 
211 #endif // NURIA_RESTFULHTTPNODE_HPP
The RestfulHttpNode class makes it easy to write RESTful APIs.
Definition: restfulhttpnode.hpp:110
The HttpClient class represents a connection to a client.
Definition: httpclient.hpp:92
The HttpNode class represents a virtual directory (or a virtual file) HttpServer uses these nodes to ...
Definition: httpnode.hpp:140