Mercurial > hg > orthanc
annotate Core/RestApi/RestApi.h @ 322:7233461e2f61
new sample
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 07 Jan 2013 15:33:53 +0100 |
parents | 4eea080e6e7a |
children | 78a8eaa5f30b |
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 { | |
45 private: | |
46 class SharedCall | |
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 |
95 virtual bool ParseJsonRequest(Json::Value& result) const = 0; | |
209 | 96 }; |
97 | |
98 | |
99 public: | |
100 class GetCall : public SharedCall | |
101 { | |
102 friend class RestApi; | |
103 | |
104 private: | |
105 const HttpHandler::Arguments* getArguments_; | |
106 | |
107 public: | |
108 std::string GetArgument(const std::string& name, | |
231 | 109 const std::string& defaultValue) const |
209 | 110 { |
111 return HttpHandler::GetArgument(*getArguments_, name, defaultValue); | |
112 } | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
113 |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
114 bool HasArgument(const std::string& name) const |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
115 { |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
116 return getArguments_->find(name) != getArguments_->end(); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
117 } |
304 | 118 |
119 virtual bool ParseJsonRequest(Json::Value& result) const; | |
209 | 120 }; |
121 | |
122 class PutCall : public SharedCall | |
123 { | |
124 friend class RestApi; | |
125 | |
126 private: | |
127 const std::string* data_; | |
128 | |
129 public: | |
304 | 130 const std::string& GetPutBody() const |
209 | 131 { |
132 return *data_; | |
133 } | |
304 | 134 |
135 virtual bool ParseJsonRequest(Json::Value& result) const | |
136 { | |
137 return ParseJsonRequestInternal(result, GetPutBody().c_str()); | |
138 } | |
209 | 139 }; |
140 | |
141 class PostCall : public SharedCall | |
142 { | |
143 friend class RestApi; | |
144 | |
145 private: | |
146 const std::string* data_; | |
147 | |
148 public: | |
304 | 149 const std::string& GetPostBody() const |
209 | 150 { |
151 return *data_; | |
152 } | |
304 | 153 |
154 virtual bool ParseJsonRequest(Json::Value& result) const | |
155 { | |
156 return ParseJsonRequestInternal(result, GetPostBody().c_str()); | |
157 } | |
209 | 158 }; |
159 | |
160 class DeleteCall : public SharedCall | |
161 { | |
304 | 162 public: |
163 virtual bool ParseJsonRequest(Json::Value& result) const | |
164 { | |
165 result.clear(); | |
166 return true; | |
167 } | |
209 | 168 }; |
169 | |
170 typedef void (*GetHandler) (GetCall& call); | |
171 | |
172 typedef void (*DeleteHandler) (DeleteCall& call); | |
173 | |
174 typedef void (*PutHandler) (PutCall& call); | |
175 | |
176 typedef void (*PostHandler) (PostCall& call); | |
177 | |
178 private: | |
179 typedef std::list< std::pair<RestApiPath*, GetHandler> > GetHandlers; | |
180 typedef std::list< std::pair<RestApiPath*, PutHandler> > PutHandlers; | |
181 typedef std::list< std::pair<RestApiPath*, PostHandler> > PostHandlers; | |
182 typedef std::list< std::pair<RestApiPath*, DeleteHandler> > DeleteHandlers; | |
183 | |
184 GetHandlers getHandlers_; | |
185 PutHandlers putHandlers_; | |
186 PostHandlers postHandlers_; | |
187 DeleteHandlers deleteHandlers_; | |
188 | |
189 bool IsGetAccepted(const UriComponents& uri); | |
190 bool IsPutAccepted(const UriComponents& uri); | |
191 bool IsPostAccepted(const UriComponents& uri); | |
192 bool IsDeleteAccepted(const UriComponents& uri); | |
193 | |
194 std::string GetAcceptedMethods(const UriComponents& uri); | |
195 | |
196 public: | |
197 RestApi() | |
198 { | |
199 } | |
200 | |
201 ~RestApi(); | |
202 | |
203 virtual bool IsServedUri(const UriComponents& uri); | |
204 | |
205 virtual void Handle(HttpOutput& output, | |
206 const std::string& method, | |
207 const UriComponents& uri, | |
208 const Arguments& headers, | |
209 const Arguments& getArguments, | |
210 const std::string& postData); | |
211 | |
212 void Register(const std::string& path, | |
213 GetHandler handler); | |
214 | |
215 void Register(const std::string& path, | |
216 PutHandler handler); | |
217 | |
218 void Register(const std::string& path, | |
219 PostHandler handler); | |
220 | |
221 void Register(const std::string& path, | |
222 DeleteHandler handler); | |
223 }; | |
224 } |