annotate Core/RestApi/RestApiOutput.cpp @ 3640:94f4a18a79cc

upgrade to year 2020
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 31 Jan 2020 17:14:59 +0100
parents 7eb5405b7ed8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1288
6e7e5ed91c2d upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1194
diff changeset
4 * Department, University Hospital of Liege, Belgium
3640
94f4a18a79cc upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3314
diff changeset
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU General Public License as
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * published by the Free Software Foundation, either version 3 of the
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * License, or (at your option) any later version.
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * In addition, as a special exception, the copyright holders of this
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * program give permission to link the code of its release with the
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * that use the same license as the "OpenSSL" library), and distribute
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * the linked executables. You must obey the GNU General Public License
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * in all respects for all of the code used other than "OpenSSL". If you
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * modify file(s) with this exception, you may extend this exception to
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * your version of the file(s), but you are not obligated to do so. If
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * you do not wish to do so, delete this exception statement from your
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * version. If you delete this exception statement from all source files
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 * in the program, then also delete it here.
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 *
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * This program is distributed in the hope that it will be useful, but
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 * General Public License for more details.
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 *
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * You should have received a copy of the GNU General Public License
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 **/
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
824
a811bdf8b8eb precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
34 #include "../PrecompiledHeaders.h"
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 #include "RestApiOutput.h"
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
1486
f967bdf8534e refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1368
diff changeset
37 #include "../Logging.h"
f967bdf8534e refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1368
diff changeset
38 #include "../OrthancException.h"
2142
5a8840920121 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2136
diff changeset
39 #include "../Toolbox.h"
1486
f967bdf8534e refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1368
diff changeset
40
330
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
41 #include <boost/lexical_cast.hpp>
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
42
211
b7aea293b965 list of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 210
diff changeset
43
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 namespace Orthanc
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 {
1368
b22ba8c5edbe query retrieve
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
46 RestApiOutput::RestApiOutput(HttpOutput& output,
b22ba8c5edbe query retrieve
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
47 HttpMethod method) :
1046
00f9f36bcd94 on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
48 output_(output),
1368
b22ba8c5edbe query retrieve
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
49 method_(method),
1046
00f9f36bcd94 on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
50 convertJsonToXml_(false)
210
96b7918a6a18 start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 209
diff changeset
51 {
216
e5d5d4a9a326 refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 215
diff changeset
52 alreadySent_ = false;
210
96b7918a6a18 start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 209
diff changeset
53 }
96b7918a6a18 start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 209
diff changeset
54
96b7918a6a18 start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 209
diff changeset
55 RestApiOutput::~RestApiOutput()
96b7918a6a18 start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 209
diff changeset
56 {
96b7918a6a18 start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 209
diff changeset
57 }
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1100
diff changeset
58
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1100
diff changeset
59 void RestApiOutput::Finalize()
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1100
diff changeset
60 {
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1100
diff changeset
61 if (!alreadySent_)
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1100
diff changeset
62 {
1368
b22ba8c5edbe query retrieve
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
63 if (method_ == HttpMethod_Post)
b22ba8c5edbe query retrieve
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
64 {
b22ba8c5edbe query retrieve
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
65 output_.SendStatus(HttpStatus_400_BadRequest);
b22ba8c5edbe query retrieve
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
66 }
b22ba8c5edbe query retrieve
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
67 else
b22ba8c5edbe query retrieve
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
68 {
b22ba8c5edbe query retrieve
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
69 output_.SendStatus(HttpStatus_404_NotFound);
b22ba8c5edbe query retrieve
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
70 }
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1100
diff changeset
71 }
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1100
diff changeset
72 }
211
b7aea293b965 list of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 210
diff changeset
73
b7aea293b965 list of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 210
diff changeset
74 void RestApiOutput::CheckStatus()
b7aea293b965 list of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 210
diff changeset
75 {
216
e5d5d4a9a326 refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 215
diff changeset
76 if (alreadySent_)
211
b7aea293b965 list of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 210
diff changeset
77 {
b7aea293b965 list of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 210
diff changeset
78 throw OrthancException(ErrorCode_BadSequenceOfCalls);
b7aea293b965 list of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 210
diff changeset
79 }
b7aea293b965 list of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 210
diff changeset
80 }
210
96b7918a6a18 start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 209
diff changeset
81
1514
d73a2178b319 support of deflate and gzip content-types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1511
diff changeset
82
1519
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1518
diff changeset
83 void RestApiOutput::AnswerStream(IHttpStreamAnswer& stream)
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 {
211
b7aea293b965 list of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 210
diff changeset
85 CheckStatus();
1519
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1518
diff changeset
86 output_.Answer(stream);
216
e5d5d4a9a326 refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 215
diff changeset
87 alreadySent_ = true;
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 }
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 void RestApiOutput::AnswerJson(const Json::Value& value)
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 {
211
b7aea293b965 list of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 210
diff changeset
92 CheckStatus();
1046
00f9f36bcd94 on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
93
00f9f36bcd94 on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
94 if (convertJsonToXml_)
00f9f36bcd94 on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
95 {
2136
dd609a99d39a uniformization of the macro naming
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2118
diff changeset
96 #if ORTHANC_ENABLE_PUGIXML == 1
1517
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1515
diff changeset
97 std::string s;
1046
00f9f36bcd94 on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
98 Toolbox::JsonToXml(s, value);
2905
ae20fccdd867 refactoring mime types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
99
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2905
diff changeset
100 output_.SetContentType(MIME_XML_UTF8);
1521
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
101 output_.Answer(s);
1046
00f9f36bcd94 on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
102 #else
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
103 throw OrthancException(ErrorCode_InternalError,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
104 "Orthanc was compiled without XML support");
1046
00f9f36bcd94 on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
105 #endif
00f9f36bcd94 on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
106 }
00f9f36bcd94 on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
107 else
00f9f36bcd94 on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
108 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2905
diff changeset
109 Json::StyledWriter writer;
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2905
diff changeset
110 std::string s = writer.write(value);
2905
ae20fccdd867 refactoring mime types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
111
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2905
diff changeset
112 output_.SetContentType(MIME_JSON_UTF8);
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2905
diff changeset
113 output_.Answer(s);
1046
00f9f36bcd94 on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
114 }
00f9f36bcd94 on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
115
216
e5d5d4a9a326 refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 215
diff changeset
116 alreadySent_ = true;
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 }
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 void RestApiOutput::AnswerBuffer(const std::string& buffer,
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2905
diff changeset
120 MimeType contentType)
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 {
1514
d73a2178b319 support of deflate and gzip content-types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1511
diff changeset
122 AnswerBuffer(buffer.size() == 0 ? NULL : buffer.c_str(),
d73a2178b319 support of deflate and gzip content-types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1511
diff changeset
123 buffer.size(), contentType);
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 }
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125
339
639272ef7615 answer raw buffers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 330
diff changeset
126 void RestApiOutput::AnswerBuffer(const void* buffer,
639272ef7615 answer raw buffers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 330
diff changeset
127 size_t length,
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2905
diff changeset
128 MimeType contentType)
339
639272ef7615 answer raw buffers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 330
diff changeset
129 {
639272ef7615 answer raw buffers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 330
diff changeset
130 CheckStatus();
3314
7eb5405b7ed8 Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
131
7eb5405b7ed8 Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
132 if (convertJsonToXml_ &&
7eb5405b7ed8 Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
133 contentType == MimeType_Json)
7eb5405b7ed8 Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
134 {
7eb5405b7ed8 Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
135 Json::Value json;
7eb5405b7ed8 Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
136 Json::Reader reader;
7eb5405b7ed8 Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
137 if (reader.parse(reinterpret_cast<const char*>(buffer),
7eb5405b7ed8 Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
138 reinterpret_cast<const char*>(buffer) + length, json))
7eb5405b7ed8 Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
139 {
7eb5405b7ed8 Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
140 AnswerJson(json);
7eb5405b7ed8 Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
141 }
7eb5405b7ed8 Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
142 else
7eb5405b7ed8 Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
143 {
7eb5405b7ed8 Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
144 throw OrthancException(ErrorCode_BadFileFormat,
7eb5405b7ed8 Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
145 "The REST API tries and answers with an invalid JSON file");
7eb5405b7ed8 Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
146 }
7eb5405b7ed8 Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
147 }
7eb5405b7ed8 Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
148 else
7eb5405b7ed8 Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
149 {
7eb5405b7ed8 Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
150 output_.SetContentType(contentType);
7eb5405b7ed8 Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
151 output_.Answer(buffer, length);
7eb5405b7ed8 Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
152 alreadySent_ = true;
7eb5405b7ed8 Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
153 }
339
639272ef7615 answer raw buffers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 330
diff changeset
154 }
639272ef7615 answer raw buffers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 330
diff changeset
155
215
c07170f3f4f7 refactoring of access to images in REST
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 211
diff changeset
156 void RestApiOutput::Redirect(const std::string& path)
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 {
211
b7aea293b965 list of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 210
diff changeset
158 CheckStatus();
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 output_.Redirect(path);
216
e5d5d4a9a326 refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 215
diff changeset
160 alreadySent_ = true;
e5d5d4a9a326 refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 215
diff changeset
161 }
e5d5d4a9a326 refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 215
diff changeset
162
1567
9c5d93510414 If error while calling the REST API, the answer body contains an error description
jodogne
parents: 1521
diff changeset
163 void RestApiOutput::SignalErrorInternal(HttpStatus status,
9c5d93510414 If error while calling the REST API, the answer body contains an error description
jodogne
parents: 1521
diff changeset
164 const char* message,
9c5d93510414 If error while calling the REST API, the answer body contains an error description
jodogne
parents: 1521
diff changeset
165 size_t messageSize)
216
e5d5d4a9a326 refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 215
diff changeset
166 {
1194
7d2c8d28ad87 generalization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1165
diff changeset
167 if (status != HttpStatus_400_BadRequest &&
7d2c8d28ad87 generalization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1165
diff changeset
168 status != HttpStatus_403_Forbidden &&
1165
0561f2087cc9 Fix reporting of errors in Orthanc Explorer when sending images to peers/modalities
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1113
diff changeset
169 status != HttpStatus_500_InternalServerError &&
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 398
diff changeset
170 status != HttpStatus_415_UnsupportedMediaType)
216
e5d5d4a9a326 refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 215
diff changeset
171 {
1582
bd1889029cbb encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1567
diff changeset
172 throw OrthancException(ErrorCode_BadHttpStatusInRest);
216
e5d5d4a9a326 refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 215
diff changeset
173 }
e5d5d4a9a326 refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 215
diff changeset
174
e5d5d4a9a326 refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 215
diff changeset
175 CheckStatus();
1567
9c5d93510414 If error while calling the REST API, the answer body contains an error description
jodogne
parents: 1521
diff changeset
176 output_.SendStatus(status, message, messageSize);
216
e5d5d4a9a326 refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 215
diff changeset
177 alreadySent_ = true;
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 }
330
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
179
1567
9c5d93510414 If error while calling the REST API, the answer body contains an error description
jodogne
parents: 1521
diff changeset
180 void RestApiOutput::SignalError(HttpStatus status)
9c5d93510414 If error while calling the REST API, the answer body contains an error description
jodogne
parents: 1521
diff changeset
181 {
9c5d93510414 If error while calling the REST API, the answer body contains an error description
jodogne
parents: 1521
diff changeset
182 SignalErrorInternal(status, NULL, 0);
9c5d93510414 If error while calling the REST API, the answer body contains an error description
jodogne
parents: 1521
diff changeset
183 }
9c5d93510414 If error while calling the REST API, the answer body contains an error description
jodogne
parents: 1521
diff changeset
184
9c5d93510414 If error while calling the REST API, the answer body contains an error description
jodogne
parents: 1521
diff changeset
185 void RestApiOutput::SignalError(HttpStatus status,
9c5d93510414 If error while calling the REST API, the answer body contains an error description
jodogne
parents: 1521
diff changeset
186 const std::string& message)
9c5d93510414 If error while calling the REST API, the answer body contains an error description
jodogne
parents: 1521
diff changeset
187 {
9c5d93510414 If error while calling the REST API, the answer body contains an error description
jodogne
parents: 1521
diff changeset
188 SignalErrorInternal(status, message.c_str(), message.size());
9c5d93510414 If error while calling the REST API, the answer body contains an error description
jodogne
parents: 1521
diff changeset
189 }
9c5d93510414 If error while calling the REST API, the answer body contains an error description
jodogne
parents: 1521
diff changeset
190
330
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
191 void RestApiOutput::SetCookie(const std::string& name,
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
192 const std::string& value,
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
193 unsigned int maxAge)
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
194 {
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
195 if (name.find(";") != std::string::npos ||
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
196 name.find(" ") != std::string::npos ||
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
197 value.find(";") != std::string::npos ||
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
198 value.find(" ") != std::string::npos)
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
199 {
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
200 throw OrthancException(ErrorCode_NotImplemented);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
201 }
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
202
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
203 CheckStatus();
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
204
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
205 std::string v = value + ";path=/";
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
206
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
207 if (maxAge != 0)
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
208 {
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
209 v += ";max-age=" + boost::lexical_cast<std::string>(maxAge);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
210 }
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
211
1042
8d1845feb277 set cookies, not allowed methods, unauthorized in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
212 output_.SetCookie(name, v);
330
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
213 }
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
214
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
215 void RestApiOutput::ResetCookie(const std::string& name)
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
216 {
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
217 // This marks the cookie to be deleted by the browser in 1 second,
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
218 // and before it actually gets deleted, its value is set to the
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
219 // empty string
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
220 SetCookie(name, "", 1);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
221 }
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 }