comparison Core/RestApi/RestApi.h @ 209:9960642f0f45

abstraction of RestApi
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 28 Nov 2012 17:22:07 +0100
parents
children 96b7918a6a18
comparison
equal deleted inserted replaced
208:de640de989b8 209:9960642f0f45
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege,
4 * Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * In addition, as a special exception, the copyright holders of this
12 * program give permission to link the code of its release with the
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
14 * that use the same license as the "OpenSSL" library), and distribute
15 * the linked executables. You must obey the GNU General Public License
16 * in all respects for all of the code used other than "OpenSSL". If you
17 * modify file(s) with this exception, you may extend this exception to
18 * your version of the file(s), but you are not obligated to do so. If
19 * you do not wish to do so, delete this exception statement from your
20 * version. If you delete this exception statement from all source files
21 * in the program, then also delete it here.
22 *
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 **/
31
32
33 #pragma once
34
35 #include "../IDynamicObject.h"
36 #include "../HttpServer/HttpHandler.h"
37 #include "RestApiPath.h"
38 #include "RestApiOutput.h"
39
40 namespace Orthanc
41 {
42 class RestApi : public HttpHandler
43 {
44 private:
45 class SharedCall
46 {
47 friend class RestApi;
48
49 private:
50 RestApiOutput* output_;
51 IDynamicObject* context_;
52 const HttpHandler::Arguments* httpHeaders_;
53 const RestApiPath::Components* uriComponents_;
54 const UriComponents* trailing_;
55
56 public:
57 RestApiOutput& GetOutput()
58 {
59 return *output_;
60 }
61
62 IDynamicObject* GetContext()
63 {
64 return context_;
65 }
66
67 const HttpHandler::Arguments& GetHttpHeaders() const
68 {
69 return *httpHeaders_;
70 }
71
72 const RestApiPath::Components& GetUriComponents() const
73 {
74 return *uriComponents_;
75 }
76
77 const UriComponents& GetTrailing() const
78 {
79 return *trailing_;
80 }
81
82 std::string GetUriComponent(const std::string& name,
83 const std::string& defaultValue)
84 {
85 return HttpHandler::GetArgument(*uriComponents_, name, defaultValue);
86 }
87 };
88
89
90 public:
91 class GetCall : public SharedCall
92 {
93 friend class RestApi;
94
95 private:
96 const HttpHandler::Arguments* getArguments_;
97
98 public:
99 std::string GetArgument(const std::string& name,
100 const std::string& defaultValue)
101 {
102 return HttpHandler::GetArgument(*getArguments_, name, defaultValue);
103 }
104 };
105
106 class PutCall : public SharedCall
107 {
108 friend class RestApi;
109
110 private:
111 const std::string* data_;
112
113 public:
114 const std::string& GetData()
115 {
116 return *data_;
117 }
118 };
119
120 class PostCall : public SharedCall
121 {
122 friend class RestApi;
123
124 private:
125 const std::string* data_;
126
127 public:
128 const std::string& GetData()
129 {
130 return *data_;
131 }
132 };
133
134 class DeleteCall : public SharedCall
135 {
136 };
137
138 typedef void (*GetHandler) (GetCall& call);
139
140 typedef void (*DeleteHandler) (DeleteCall& call);
141
142 typedef void (*PutHandler) (PutCall& call);
143
144 typedef void (*PostHandler) (PostCall& call);
145
146 private:
147 typedef std::list< std::pair<RestApiPath*, GetHandler> > GetHandlers;
148 typedef std::list< std::pair<RestApiPath*, PutHandler> > PutHandlers;
149 typedef std::list< std::pair<RestApiPath*, PostHandler> > PostHandlers;
150 typedef std::list< std::pair<RestApiPath*, DeleteHandler> > DeleteHandlers;
151
152 // CAUTION: PLEASE INTRODUCE MUTEX BETWEEN CONTEXTS !!!
153 std::auto_ptr<IDynamicObject> context_;
154
155 GetHandlers getHandlers_;
156 PutHandlers putHandlers_;
157 PostHandlers postHandlers_;
158 DeleteHandlers deleteHandlers_;
159
160 bool IsGetAccepted(const UriComponents& uri);
161 bool IsPutAccepted(const UriComponents& uri);
162 bool IsPostAccepted(const UriComponents& uri);
163 bool IsDeleteAccepted(const UriComponents& uri);
164
165 std::string GetAcceptedMethods(const UriComponents& uri);
166
167 public:
168 RestApi()
169 {
170 }
171
172 void SetContext(IDynamicObject* context) // This takes the ownership
173 {
174 context_.reset(context);
175 }
176
177 ~RestApi();
178
179 virtual bool IsServedUri(const UriComponents& uri);
180
181 virtual void Handle(HttpOutput& output,
182 const std::string& method,
183 const UriComponents& uri,
184 const Arguments& headers,
185 const Arguments& getArguments,
186 const std::string& postData);
187
188 void Register(const std::string& path,
189 GetHandler handler);
190
191 void Register(const std::string& path,
192 PutHandler handler);
193
194 void Register(const std::string& path,
195 PostHandler handler);
196
197 void Register(const std::string& path,
198 DeleteHandler handler);
199 };
200 }