Mercurial > hg > orthanc
comparison OrthancFramework/Sources/RestApi/RestApiOutput.cpp @ 4044:d25f4c0fa160 framework
splitting code into OrthancFramework and OrthancServer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 10 Jun 2020 20:30:34 +0200 |
parents | Core/RestApi/RestApiOutput.cpp@94f4a18a79cc |
children | bf7b9edf6b81 |
comparison
equal
deleted
inserted
replaced
4043:6c6239aec462 | 4044:d25f4c0fa160 |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
34 #include "../PrecompiledHeaders.h" | |
35 #include "RestApiOutput.h" | |
36 | |
37 #include "../Logging.h" | |
38 #include "../OrthancException.h" | |
39 #include "../Toolbox.h" | |
40 | |
41 #include <boost/lexical_cast.hpp> | |
42 | |
43 | |
44 namespace Orthanc | |
45 { | |
46 RestApiOutput::RestApiOutput(HttpOutput& output, | |
47 HttpMethod method) : | |
48 output_(output), | |
49 method_(method), | |
50 convertJsonToXml_(false) | |
51 { | |
52 alreadySent_ = false; | |
53 } | |
54 | |
55 RestApiOutput::~RestApiOutput() | |
56 { | |
57 } | |
58 | |
59 void RestApiOutput::Finalize() | |
60 { | |
61 if (!alreadySent_) | |
62 { | |
63 if (method_ == HttpMethod_Post) | |
64 { | |
65 output_.SendStatus(HttpStatus_400_BadRequest); | |
66 } | |
67 else | |
68 { | |
69 output_.SendStatus(HttpStatus_404_NotFound); | |
70 } | |
71 } | |
72 } | |
73 | |
74 void RestApiOutput::CheckStatus() | |
75 { | |
76 if (alreadySent_) | |
77 { | |
78 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
79 } | |
80 } | |
81 | |
82 | |
83 void RestApiOutput::AnswerStream(IHttpStreamAnswer& stream) | |
84 { | |
85 CheckStatus(); | |
86 output_.Answer(stream); | |
87 alreadySent_ = true; | |
88 } | |
89 | |
90 void RestApiOutput::AnswerJson(const Json::Value& value) | |
91 { | |
92 CheckStatus(); | |
93 | |
94 if (convertJsonToXml_) | |
95 { | |
96 #if ORTHANC_ENABLE_PUGIXML == 1 | |
97 std::string s; | |
98 Toolbox::JsonToXml(s, value); | |
99 | |
100 output_.SetContentType(MIME_XML_UTF8); | |
101 output_.Answer(s); | |
102 #else | |
103 throw OrthancException(ErrorCode_InternalError, | |
104 "Orthanc was compiled without XML support"); | |
105 #endif | |
106 } | |
107 else | |
108 { | |
109 Json::StyledWriter writer; | |
110 std::string s = writer.write(value); | |
111 | |
112 output_.SetContentType(MIME_JSON_UTF8); | |
113 output_.Answer(s); | |
114 } | |
115 | |
116 alreadySent_ = true; | |
117 } | |
118 | |
119 void RestApiOutput::AnswerBuffer(const std::string& buffer, | |
120 MimeType contentType) | |
121 { | |
122 AnswerBuffer(buffer.size() == 0 ? NULL : buffer.c_str(), | |
123 buffer.size(), contentType); | |
124 } | |
125 | |
126 void RestApiOutput::AnswerBuffer(const void* buffer, | |
127 size_t length, | |
128 MimeType contentType) | |
129 { | |
130 CheckStatus(); | |
131 | |
132 if (convertJsonToXml_ && | |
133 contentType == MimeType_Json) | |
134 { | |
135 Json::Value json; | |
136 Json::Reader reader; | |
137 if (reader.parse(reinterpret_cast<const char*>(buffer), | |
138 reinterpret_cast<const char*>(buffer) + length, json)) | |
139 { | |
140 AnswerJson(json); | |
141 } | |
142 else | |
143 { | |
144 throw OrthancException(ErrorCode_BadFileFormat, | |
145 "The REST API tries and answers with an invalid JSON file"); | |
146 } | |
147 } | |
148 else | |
149 { | |
150 output_.SetContentType(contentType); | |
151 output_.Answer(buffer, length); | |
152 alreadySent_ = true; | |
153 } | |
154 } | |
155 | |
156 void RestApiOutput::Redirect(const std::string& path) | |
157 { | |
158 CheckStatus(); | |
159 output_.Redirect(path); | |
160 alreadySent_ = true; | |
161 } | |
162 | |
163 void RestApiOutput::SignalErrorInternal(HttpStatus status, | |
164 const char* message, | |
165 size_t messageSize) | |
166 { | |
167 if (status != HttpStatus_400_BadRequest && | |
168 status != HttpStatus_403_Forbidden && | |
169 status != HttpStatus_500_InternalServerError && | |
170 status != HttpStatus_415_UnsupportedMediaType) | |
171 { | |
172 throw OrthancException(ErrorCode_BadHttpStatusInRest); | |
173 } | |
174 | |
175 CheckStatus(); | |
176 output_.SendStatus(status, message, messageSize); | |
177 alreadySent_ = true; | |
178 } | |
179 | |
180 void RestApiOutput::SignalError(HttpStatus status) | |
181 { | |
182 SignalErrorInternal(status, NULL, 0); | |
183 } | |
184 | |
185 void RestApiOutput::SignalError(HttpStatus status, | |
186 const std::string& message) | |
187 { | |
188 SignalErrorInternal(status, message.c_str(), message.size()); | |
189 } | |
190 | |
191 void RestApiOutput::SetCookie(const std::string& name, | |
192 const std::string& value, | |
193 unsigned int maxAge) | |
194 { | |
195 if (name.find(";") != std::string::npos || | |
196 name.find(" ") != std::string::npos || | |
197 value.find(";") != std::string::npos || | |
198 value.find(" ") != std::string::npos) | |
199 { | |
200 throw OrthancException(ErrorCode_NotImplemented); | |
201 } | |
202 | |
203 CheckStatus(); | |
204 | |
205 std::string v = value + ";path=/"; | |
206 | |
207 if (maxAge != 0) | |
208 { | |
209 v += ";max-age=" + boost::lexical_cast<std::string>(maxAge); | |
210 } | |
211 | |
212 output_.SetCookie(name, v); | |
213 } | |
214 | |
215 void RestApiOutput::ResetCookie(const std::string& name) | |
216 { | |
217 // This marks the cookie to be deleted by the browser in 1 second, | |
218 // and before it actually gets deleted, its value is set to the | |
219 // empty string | |
220 SetCookie(name, "", 1); | |
221 } | |
222 } |