Mercurial > hg > orthanc
annotate Core/RestApi/RestApi.cpp @ 1003:1d35281d967c lua-scripting
DicomInstanceToStore
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 04 Jul 2014 16:51:33 +0200 |
parents | c550e99c452b |
children | 6968356679c0 |
rev | line source |
---|---|
209 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
689 | 3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, |
209 | 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 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
33 #include "../PrecompiledHeaders.h" |
209 | 34 #include "RestApi.h" |
35 | |
249 | 36 #include <stdlib.h> // To define "_exit()" under Windows |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
37 #include <glog/logging.h> |
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
38 |
209 | 39 namespace Orthanc |
40 { | |
41 bool RestApi::IsGetAccepted(const UriComponents& uri) | |
42 { | |
43 for (GetHandlers::const_iterator it = getHandlers_.begin(); | |
656 | 44 it != getHandlers_.end(); ++it) |
209 | 45 { |
46 if (it->first->Match(uri)) | |
47 { | |
48 return true; | |
49 } | |
50 } | |
51 | |
52 return false; | |
53 } | |
54 | |
55 bool RestApi::IsPutAccepted(const UriComponents& uri) | |
56 { | |
57 for (PutHandlers::const_iterator it = putHandlers_.begin(); | |
656 | 58 it != putHandlers_.end(); ++it) |
209 | 59 { |
60 if (it->first->Match(uri)) | |
61 { | |
62 return true; | |
63 } | |
64 } | |
65 | |
66 return false; | |
67 } | |
68 | |
69 bool RestApi::IsPostAccepted(const UriComponents& uri) | |
70 { | |
71 for (PostHandlers::const_iterator it = postHandlers_.begin(); | |
656 | 72 it != postHandlers_.end(); ++it) |
209 | 73 { |
74 if (it->first->Match(uri)) | |
75 { | |
76 return true; | |
77 } | |
78 } | |
79 | |
80 return false; | |
81 } | |
82 | |
83 bool RestApi::IsDeleteAccepted(const UriComponents& uri) | |
84 { | |
85 for (DeleteHandlers::const_iterator it = deleteHandlers_.begin(); | |
656 | 86 it != deleteHandlers_.end(); ++it) |
209 | 87 { |
88 if (it->first->Match(uri)) | |
89 { | |
90 return true; | |
91 } | |
92 } | |
93 | |
94 return false; | |
95 } | |
96 | |
97 static void AddMethod(std::string& target, | |
98 const std::string& method) | |
99 { | |
100 if (target.size() > 0) | |
101 target += "," + method; | |
102 else | |
103 target = method; | |
104 } | |
105 | |
106 std::string RestApi::GetAcceptedMethods(const UriComponents& uri) | |
107 { | |
108 std::string s; | |
109 | |
110 if (IsGetAccepted(uri)) | |
111 AddMethod(s, "GET"); | |
112 | |
113 if (IsPutAccepted(uri)) | |
114 AddMethod(s, "PUT"); | |
115 | |
116 if (IsPostAccepted(uri)) | |
117 AddMethod(s, "POST"); | |
118 | |
119 if (IsDeleteAccepted(uri)) | |
120 AddMethod(s, "DELETE"); | |
121 | |
122 return s; | |
123 } | |
124 | |
125 RestApi::~RestApi() | |
126 { | |
127 for (GetHandlers::iterator it = getHandlers_.begin(); | |
656 | 128 it != getHandlers_.end(); ++it) |
209 | 129 { |
130 delete it->first; | |
131 } | |
132 | |
133 for (PutHandlers::iterator it = putHandlers_.begin(); | |
656 | 134 it != putHandlers_.end(); ++it) |
209 | 135 { |
136 delete it->first; | |
137 } | |
138 | |
139 for (PostHandlers::iterator it = postHandlers_.begin(); | |
656 | 140 it != postHandlers_.end(); ++it) |
209 | 141 { |
142 delete it->first; | |
143 } | |
144 | |
145 for (DeleteHandlers::iterator it = deleteHandlers_.begin(); | |
656 | 146 it != deleteHandlers_.end(); ++it) |
209 | 147 { |
148 delete it->first; | |
149 } | |
150 } | |
151 | |
152 bool RestApi::IsServedUri(const UriComponents& uri) | |
153 { | |
154 return (IsGetAccepted(uri) || | |
155 IsPutAccepted(uri) || | |
156 IsPostAccepted(uri) || | |
157 IsDeleteAccepted(uri)); | |
158 } | |
159 | |
160 void RestApi::Handle(HttpOutput& output, | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
416
diff
changeset
|
161 HttpMethod method, |
209 | 162 const UriComponents& uri, |
163 const Arguments& headers, | |
164 const Arguments& getArguments, | |
165 const std::string& postData) | |
166 { | |
167 bool ok = false; | |
168 RestApiOutput restOutput(output); | |
975 | 169 HttpHandler::Arguments components; |
209 | 170 UriComponents trailing; |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
171 |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
416
diff
changeset
|
172 if (method == HttpMethod_Get) |
209 | 173 { |
174 for (GetHandlers::const_iterator it = getHandlers_.begin(); | |
656 | 175 it != getHandlers_.end(); ++it) |
209 | 176 { |
177 if (it->first->Match(components, trailing, uri)) | |
178 { | |
416 | 179 //LOG(INFO) << "REST GET call on: " << Toolbox::FlattenUri(uri); |
209 | 180 ok = true; |
974 | 181 RestApiGetCall call(restOutput, *this, headers, components, trailing, uri, getArguments); |
209 | 182 it->second(call); |
183 } | |
184 } | |
185 } | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
416
diff
changeset
|
186 else if (method == HttpMethod_Put) |
209 | 187 { |
188 for (PutHandlers::const_iterator it = putHandlers_.begin(); | |
656 | 189 it != putHandlers_.end(); ++it) |
209 | 190 { |
191 if (it->first->Match(components, trailing, uri)) | |
192 { | |
416 | 193 //LOG(INFO) << "REST PUT call on: " << Toolbox::FlattenUri(uri); |
209 | 194 ok = true; |
974 | 195 RestApiPutCall call(restOutput, *this, headers, components, trailing, uri, postData); |
209 | 196 it->second(call); |
197 } | |
198 } | |
199 } | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
416
diff
changeset
|
200 else if (method == HttpMethod_Post) |
209 | 201 { |
202 for (PostHandlers::const_iterator it = postHandlers_.begin(); | |
656 | 203 it != postHandlers_.end(); ++it) |
209 | 204 { |
205 if (it->first->Match(components, trailing, uri)) | |
206 { | |
416 | 207 //LOG(INFO) << "REST POST call on: " << Toolbox::FlattenUri(uri); |
209 | 208 ok = true; |
974 | 209 RestApiPostCall call(restOutput, *this, headers, components, trailing, uri, postData); |
209 | 210 it->second(call); |
211 } | |
212 } | |
213 } | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
416
diff
changeset
|
214 else if (method == HttpMethod_Delete) |
209 | 215 { |
216 for (DeleteHandlers::const_iterator it = deleteHandlers_.begin(); | |
656 | 217 it != deleteHandlers_.end(); ++it) |
209 | 218 { |
219 if (it->first->Match(components, trailing, uri)) | |
220 { | |
416 | 221 //LOG(INFO) << "REST DELETE call on: " << Toolbox::FlattenUri(uri); |
209 | 222 ok = true; |
974 | 223 RestApiDeleteCall call(restOutput, *this, headers, components, trailing, uri); |
209 | 224 it->second(call); |
225 } | |
226 } | |
227 } | |
228 | |
229 if (!ok) | |
230 { | |
477 | 231 LOG(INFO) << "REST method " << EnumerationToString(method) |
416 | 232 << " not allowed on: " << Toolbox::FlattenUri(uri); |
209 | 233 output.SendMethodNotAllowedError(GetAcceptedMethods(uri)); |
234 } | |
235 } | |
236 | |
237 void RestApi::Register(const std::string& path, | |
974 | 238 RestApiGetCall::Handler handler) |
209 | 239 { |
240 getHandlers_.push_back(std::make_pair(new RestApiPath(path), handler)); | |
241 } | |
242 | |
243 void RestApi::Register(const std::string& path, | |
974 | 244 RestApiPutCall::Handler handler) |
209 | 245 { |
246 putHandlers_.push_back(std::make_pair(new RestApiPath(path), handler)); | |
247 } | |
248 | |
249 void RestApi::Register(const std::string& path, | |
974 | 250 RestApiPostCall::Handler handler) |
209 | 251 { |
252 postHandlers_.push_back(std::make_pair(new RestApiPath(path), handler)); | |
253 } | |
254 | |
255 void RestApi::Register(const std::string& path, | |
974 | 256 RestApiDeleteCall::Handler handler) |
209 | 257 { |
258 deleteHandlers_.push_back(std::make_pair(new RestApiPath(path), handler)); | |
259 } | |
260 } |