Mercurial > hg > orthanc
comparison Core/RestApi/RestApi.cpp @ 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 #include "RestApi.h" | |
34 | |
35 namespace Orthanc | |
36 { | |
37 bool RestApi::IsGetAccepted(const UriComponents& uri) | |
38 { | |
39 for (GetHandlers::const_iterator it = getHandlers_.begin(); | |
40 it != getHandlers_.end(); it++) | |
41 { | |
42 if (it->first->Match(uri)) | |
43 { | |
44 return true; | |
45 } | |
46 } | |
47 | |
48 return false; | |
49 } | |
50 | |
51 bool RestApi::IsPutAccepted(const UriComponents& uri) | |
52 { | |
53 for (PutHandlers::const_iterator it = putHandlers_.begin(); | |
54 it != putHandlers_.end(); it++) | |
55 { | |
56 if (it->first->Match(uri)) | |
57 { | |
58 return true; | |
59 } | |
60 } | |
61 | |
62 return false; | |
63 } | |
64 | |
65 bool RestApi::IsPostAccepted(const UriComponents& uri) | |
66 { | |
67 for (PostHandlers::const_iterator it = postHandlers_.begin(); | |
68 it != postHandlers_.end(); it++) | |
69 { | |
70 if (it->first->Match(uri)) | |
71 { | |
72 return true; | |
73 } | |
74 } | |
75 | |
76 return false; | |
77 } | |
78 | |
79 bool RestApi::IsDeleteAccepted(const UriComponents& uri) | |
80 { | |
81 for (DeleteHandlers::const_iterator it = deleteHandlers_.begin(); | |
82 it != deleteHandlers_.end(); it++) | |
83 { | |
84 if (it->first->Match(uri)) | |
85 { | |
86 return true; | |
87 } | |
88 } | |
89 | |
90 return false; | |
91 } | |
92 | |
93 static void AddMethod(std::string& target, | |
94 const std::string& method) | |
95 { | |
96 if (target.size() > 0) | |
97 target += "," + method; | |
98 else | |
99 target = method; | |
100 } | |
101 | |
102 std::string RestApi::GetAcceptedMethods(const UriComponents& uri) | |
103 { | |
104 std::string s; | |
105 | |
106 if (IsGetAccepted(uri)) | |
107 AddMethod(s, "GET"); | |
108 | |
109 if (IsPutAccepted(uri)) | |
110 AddMethod(s, "PUT"); | |
111 | |
112 if (IsPostAccepted(uri)) | |
113 AddMethod(s, "POST"); | |
114 | |
115 if (IsDeleteAccepted(uri)) | |
116 AddMethod(s, "DELETE"); | |
117 | |
118 return s; | |
119 } | |
120 | |
121 RestApi::~RestApi() | |
122 { | |
123 for (GetHandlers::iterator it = getHandlers_.begin(); | |
124 it != getHandlers_.end(); it++) | |
125 { | |
126 delete it->first; | |
127 } | |
128 | |
129 for (PutHandlers::iterator it = putHandlers_.begin(); | |
130 it != putHandlers_.end(); it++) | |
131 { | |
132 delete it->first; | |
133 } | |
134 | |
135 for (PostHandlers::iterator it = postHandlers_.begin(); | |
136 it != postHandlers_.end(); it++) | |
137 { | |
138 delete it->first; | |
139 } | |
140 | |
141 for (DeleteHandlers::iterator it = deleteHandlers_.begin(); | |
142 it != deleteHandlers_.end(); it++) | |
143 { | |
144 delete it->first; | |
145 } | |
146 } | |
147 | |
148 bool RestApi::IsServedUri(const UriComponents& uri) | |
149 { | |
150 return (IsGetAccepted(uri) || | |
151 IsPutAccepted(uri) || | |
152 IsPostAccepted(uri) || | |
153 IsDeleteAccepted(uri)); | |
154 } | |
155 | |
156 void RestApi::Handle(HttpOutput& output, | |
157 const std::string& method, | |
158 const UriComponents& uri, | |
159 const Arguments& headers, | |
160 const Arguments& getArguments, | |
161 const std::string& postData) | |
162 { | |
163 bool ok = false; | |
164 RestApiOutput restOutput(output); | |
165 RestApiPath::Components components; | |
166 UriComponents trailing; | |
167 | |
168 if (method == "GET") | |
169 { | |
170 for (GetHandlers::const_iterator it = getHandlers_.begin(); | |
171 it != getHandlers_.end(); it++) | |
172 { | |
173 if (it->first->Match(components, trailing, uri)) | |
174 { | |
175 ok = true; | |
176 GetCall call; | |
177 call.output_ = &restOutput; | |
178 call.context_ = context_.get(); | |
179 call.httpHeaders_ = &headers; | |
180 call.uriComponents_ = &components; | |
181 call.trailing_ = &trailing; | |
182 | |
183 call.getArguments_ = &getArguments; | |
184 it->second(call); | |
185 } | |
186 } | |
187 } | |
188 else if (method == "PUT") | |
189 { | |
190 for (PutHandlers::const_iterator it = putHandlers_.begin(); | |
191 it != putHandlers_.end(); it++) | |
192 { | |
193 if (it->first->Match(components, trailing, uri)) | |
194 { | |
195 ok = true; | |
196 PutCall call; | |
197 call.output_ = &restOutput; | |
198 call.context_ = context_.get(); | |
199 call.httpHeaders_ = &headers; | |
200 call.uriComponents_ = &components; | |
201 call.trailing_ = &trailing; | |
202 | |
203 call.data_ = &postData; | |
204 it->second(call); | |
205 } | |
206 } | |
207 } | |
208 else if (method == "POST") | |
209 { | |
210 for (PostHandlers::const_iterator it = postHandlers_.begin(); | |
211 it != postHandlers_.end(); it++) | |
212 { | |
213 if (it->first->Match(components, trailing, uri)) | |
214 { | |
215 ok = true; | |
216 PostCall call; | |
217 call.output_ = &restOutput; | |
218 call.context_ = context_.get(); | |
219 call.httpHeaders_ = &headers; | |
220 call.uriComponents_ = &components; | |
221 call.trailing_ = &trailing; | |
222 | |
223 call.data_ = &postData; | |
224 it->second(call); | |
225 } | |
226 } | |
227 } | |
228 else if (method == "DELETE") | |
229 { | |
230 for (DeleteHandlers::const_iterator it = deleteHandlers_.begin(); | |
231 it != deleteHandlers_.end(); it++) | |
232 { | |
233 if (it->first->Match(components, trailing, uri)) | |
234 { | |
235 ok = true; | |
236 DeleteCall call; | |
237 call.output_ = &restOutput; | |
238 call.context_ = context_.get(); | |
239 call.httpHeaders_ = &headers; | |
240 call.uriComponents_ = &components; | |
241 call.trailing_ = &trailing; | |
242 it->second(call); | |
243 } | |
244 } | |
245 } | |
246 | |
247 if (!ok) | |
248 { | |
249 output.SendMethodNotAllowedError(GetAcceptedMethods(uri)); | |
250 } | |
251 } | |
252 | |
253 void RestApi::Register(const std::string& path, | |
254 GetHandler handler) | |
255 { | |
256 getHandlers_.push_back(std::make_pair(new RestApiPath(path), handler)); | |
257 } | |
258 | |
259 void RestApi::Register(const std::string& path, | |
260 PutHandler handler) | |
261 { | |
262 putHandlers_.push_back(std::make_pair(new RestApiPath(path), handler)); | |
263 } | |
264 | |
265 void RestApi::Register(const std::string& path, | |
266 PostHandler handler) | |
267 { | |
268 postHandlers_.push_back(std::make_pair(new RestApiPath(path), handler)); | |
269 } | |
270 | |
271 void RestApi::Register(const std::string& path, | |
272 DeleteHandler handler) | |
273 { | |
274 deleteHandlers_.push_back(std::make_pair(new RestApiPath(path), handler)); | |
275 } | |
276 } |