Mercurial > hg > orthanc
annotate Core/RestApi/RestApi.h @ 339:639272ef7615
answer raw buffers
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 16 Jan 2013 15:14:54 +0100 |
parents | c51c9ed603e6 |
children | 753e69f9326e |
rev | line source |
---|---|
209 | 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 "../HttpServer/HttpHandler.h" | |
36 #include "RestApiPath.h" | |
37 #include "RestApiOutput.h" | |
38 | |
217 | 39 #include <list> |
40 | |
209 | 41 namespace Orthanc |
42 { | |
43 class RestApi : public HttpHandler | |
44 { | |
331
5a96dac27959
base class for rest calls made public
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
45 public: |
5a96dac27959
base class for rest calls made public
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
46 class Call |
209 | 47 { |
48 friend class RestApi; | |
49 | |
50 private: | |
51 RestApiOutput* output_; | |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
52 RestApi* context_; |
209 | 53 const HttpHandler::Arguments* httpHeaders_; |
54 const RestApiPath::Components* uriComponents_; | |
55 const UriComponents* trailing_; | |
215
c07170f3f4f7
refactoring of access to images in REST
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
56 const UriComponents* fullUri_; |
209 | 57 |
304 | 58 protected: |
59 static bool ParseJsonRequestInternal(Json::Value& result, | |
60 const char* request); | |
61 | |
209 | 62 public: |
63 RestApiOutput& GetOutput() | |
64 { | |
65 return *output_; | |
66 } | |
67 | |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
68 RestApi& GetContext() |
209 | 69 { |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
70 return *context_; |
209 | 71 } |
72 | |
215
c07170f3f4f7
refactoring of access to images in REST
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
73 const UriComponents& GetFullUri() const |
209 | 74 { |
215
c07170f3f4f7
refactoring of access to images in REST
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
75 return *fullUri_; |
209 | 76 } |
285
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
77 |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
78 const UriComponents& GetTrailingUri() const |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
79 { |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
80 return *trailing_; |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
81 } |
209 | 82 |
83 std::string GetUriComponent(const std::string& name, | |
231 | 84 const std::string& defaultValue) const |
209 | 85 { |
86 return HttpHandler::GetArgument(*uriComponents_, name, defaultValue); | |
87 } | |
212 | 88 |
89 std::string GetHttpHeader(const std::string& name, | |
231 | 90 const std::string& defaultValue) const |
212 | 91 { |
92 return HttpHandler::GetArgument(*httpHeaders_, name, defaultValue); | |
93 } | |
304 | 94 |
332 | 95 const HttpHandler::Arguments& GetHttpHeaders() const |
96 { | |
97 return *httpHeaders_; | |
98 } | |
99 | |
330 | 100 void ParseCookies(HttpHandler::Arguments& result) const |
101 { | |
102 HttpHandler::ParseCookies(result, *httpHeaders_); | |
103 } | |
104 | |
304 | 105 virtual bool ParseJsonRequest(Json::Value& result) const = 0; |
209 | 106 }; |
107 | |
108 | |
331
5a96dac27959
base class for rest calls made public
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
109 class GetCall : public Call |
209 | 110 { |
111 friend class RestApi; | |
112 | |
113 private: | |
114 const HttpHandler::Arguments* getArguments_; | |
115 | |
116 public: | |
117 std::string GetArgument(const std::string& name, | |
231 | 118 const std::string& defaultValue) const |
209 | 119 { |
120 return HttpHandler::GetArgument(*getArguments_, name, defaultValue); | |
121 } | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
122 |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
123 bool HasArgument(const std::string& name) const |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
124 { |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
125 return getArguments_->find(name) != getArguments_->end(); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
126 } |
304 | 127 |
128 virtual bool ParseJsonRequest(Json::Value& result) const; | |
209 | 129 }; |
130 | |
331
5a96dac27959
base class for rest calls made public
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
131 class PutCall : public Call |
209 | 132 { |
133 friend class RestApi; | |
134 | |
135 private: | |
136 const std::string* data_; | |
137 | |
138 public: | |
304 | 139 const std::string& GetPutBody() const |
209 | 140 { |
141 return *data_; | |
142 } | |
304 | 143 |
144 virtual bool ParseJsonRequest(Json::Value& result) const | |
145 { | |
146 return ParseJsonRequestInternal(result, GetPutBody().c_str()); | |
147 } | |
209 | 148 }; |
149 | |
331
5a96dac27959
base class for rest calls made public
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
150 class PostCall : public Call |
209 | 151 { |
152 friend class RestApi; | |
153 | |
154 private: | |
155 const std::string* data_; | |
156 | |
157 public: | |
304 | 158 const std::string& GetPostBody() const |
209 | 159 { |
160 return *data_; | |
161 } | |
304 | 162 |
163 virtual bool ParseJsonRequest(Json::Value& result) const | |
164 { | |
165 return ParseJsonRequestInternal(result, GetPostBody().c_str()); | |
166 } | |
209 | 167 }; |
168 | |
331
5a96dac27959
base class for rest calls made public
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
169 class DeleteCall : public Call |
209 | 170 { |
304 | 171 public: |
172 virtual bool ParseJsonRequest(Json::Value& result) const | |
173 { | |
174 result.clear(); | |
175 return true; | |
176 } | |
209 | 177 }; |
178 | |
179 typedef void (*GetHandler) (GetCall& call); | |
180 | |
181 typedef void (*DeleteHandler) (DeleteCall& call); | |
182 | |
183 typedef void (*PutHandler) (PutCall& call); | |
184 | |
185 typedef void (*PostHandler) (PostCall& call); | |
186 | |
187 private: | |
188 typedef std::list< std::pair<RestApiPath*, GetHandler> > GetHandlers; | |
189 typedef std::list< std::pair<RestApiPath*, PutHandler> > PutHandlers; | |
190 typedef std::list< std::pair<RestApiPath*, PostHandler> > PostHandlers; | |
191 typedef std::list< std::pair<RestApiPath*, DeleteHandler> > DeleteHandlers; | |
192 | |
193 GetHandlers getHandlers_; | |
194 PutHandlers putHandlers_; | |
195 PostHandlers postHandlers_; | |
196 DeleteHandlers deleteHandlers_; | |
197 | |
198 bool IsGetAccepted(const UriComponents& uri); | |
199 bool IsPutAccepted(const UriComponents& uri); | |
200 bool IsPostAccepted(const UriComponents& uri); | |
201 bool IsDeleteAccepted(const UriComponents& uri); | |
202 | |
203 std::string GetAcceptedMethods(const UriComponents& uri); | |
204 | |
205 public: | |
206 RestApi() | |
207 { | |
208 } | |
209 | |
210 ~RestApi(); | |
211 | |
212 virtual bool IsServedUri(const UriComponents& uri); | |
213 | |
214 virtual void Handle(HttpOutput& output, | |
215 const std::string& method, | |
216 const UriComponents& uri, | |
217 const Arguments& headers, | |
218 const Arguments& getArguments, | |
219 const std::string& postData); | |
220 | |
221 void Register(const std::string& path, | |
222 GetHandler handler); | |
223 | |
224 void Register(const std::string& path, | |
225 PutHandler handler); | |
226 | |
227 void Register(const std::string& path, | |
228 PostHandler handler); | |
229 | |
230 void Register(const std::string& path, | |
231 DeleteHandler handler); | |
232 }; | |
233 } |