Mercurial > hg > orthanc
annotate Core/RestApi/RestApiOutput.cpp @ 1051:92f4bf2c5d73
HTTP GET in Lua
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 23 Jul 2014 12:59:28 +0200 |
parents | 648bf1457113 |
children | f21f7783f934 |
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 "RestApiOutput.h" |
35 | |
330 | 36 #include <boost/lexical_cast.hpp> |
1048 | 37 #include <glog/logging.h> |
330 | 38 |
211 | 39 #include "../OrthancException.h" |
40 | |
209 | 41 namespace Orthanc |
42 { | |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
43 RestApiOutput::RestApiOutput(HttpOutput& output) : |
1046
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
44 output_(output), |
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
45 convertJsonToXml_(false) |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
46 { |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
47 alreadySent_ = false; |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
48 } |
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
49 |
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
50 RestApiOutput::~RestApiOutput() |
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 if (!alreadySent_) |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
53 { |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
54 output_.SendHeader(HttpStatus_400_BadRequest); |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
55 } |
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
56 } |
211 | 57 |
58 void RestApiOutput::CheckStatus() | |
59 { | |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
60 if (alreadySent_) |
211 | 61 { |
62 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
63 } | |
64 } | |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
65 |
209 | 66 void RestApiOutput::AnswerFile(HttpFileSender& sender) |
67 { | |
211 | 68 CheckStatus(); |
209 | 69 sender.Send(output_); |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
70 alreadySent_ = true; |
209 | 71 } |
72 | |
73 void RestApiOutput::AnswerJson(const Json::Value& value) | |
74 { | |
211 | 75 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
|
76 |
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
77 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
|
78 { |
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
79 #if ORTHANC_PUGIXML_ENABLED == 1 |
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
80 std::string s; |
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
81 Toolbox::JsonToXml(s, value); |
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
82 output_.AnswerBufferWithContentType(s, "application/xml"); |
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
83 #else |
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
84 LOG(ERROR) << "Orthanc was compiled without XML support"; |
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
85 throw OrthancException(ErrorCode_InternalError); |
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
86 #endif |
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
87 } |
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
88 else |
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
89 { |
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
90 Json::StyledWriter writer; |
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
91 std::string s = writer.write(value); |
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
92 output_.AnswerBufferWithContentType(s, "application/json"); |
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 |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
95 alreadySent_ = true; |
209 | 96 } |
97 | |
98 void RestApiOutput::AnswerBuffer(const std::string& buffer, | |
99 const std::string& contentType) | |
100 { | |
211 | 101 CheckStatus(); |
1042
8d1845feb277
set cookies, not allowed methods, unauthorized in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
102 output_.AnswerBufferWithContentType(buffer, contentType); |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
103 alreadySent_ = true; |
209 | 104 } |
105 | |
339 | 106 void RestApiOutput::AnswerBuffer(const void* buffer, |
107 size_t length, | |
108 const std::string& contentType) | |
109 { | |
110 CheckStatus(); | |
1042
8d1845feb277
set cookies, not allowed methods, unauthorized in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
111 output_.AnswerBufferWithContentType(buffer, length, contentType); |
339 | 112 alreadySent_ = true; |
113 } | |
114 | |
215
c07170f3f4f7
refactoring of access to images in REST
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
211
diff
changeset
|
115 void RestApiOutput::Redirect(const std::string& path) |
209 | 116 { |
211 | 117 CheckStatus(); |
209 | 118 output_.Redirect(path); |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
119 alreadySent_ = true; |
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
120 } |
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
121 |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
122 void RestApiOutput::SignalError(HttpStatus status) |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
123 { |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
124 if (status != HttpStatus_403_Forbidden && |
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
125 status != HttpStatus_415_UnsupportedMediaType) |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
126 { |
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
127 throw OrthancException("This HTTP status is not allowed in a REST API"); |
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
128 } |
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
129 |
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
130 CheckStatus(); |
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
131 output_.SendHeader(status); |
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
132 alreadySent_ = true; |
209 | 133 } |
330 | 134 |
135 void RestApiOutput::SetCookie(const std::string& name, | |
136 const std::string& value, | |
137 unsigned int maxAge) | |
138 { | |
139 if (name.find(";") != std::string::npos || | |
140 name.find(" ") != std::string::npos || | |
141 value.find(";") != std::string::npos || | |
142 value.find(" ") != std::string::npos) | |
143 { | |
144 throw OrthancException(ErrorCode_NotImplemented); | |
145 } | |
146 | |
147 CheckStatus(); | |
148 | |
149 std::string v = value + ";path=/"; | |
150 | |
151 if (maxAge != 0) | |
152 { | |
153 v += ";max-age=" + boost::lexical_cast<std::string>(maxAge); | |
154 } | |
155 | |
1042
8d1845feb277
set cookies, not allowed methods, unauthorized in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
156 output_.SetCookie(name, v); |
330 | 157 } |
158 | |
159 void RestApiOutput::ResetCookie(const std::string& name) | |
160 { | |
161 // This marks the cookie to be deleted by the browser in 1 second, | |
162 // and before it actually gets deleted, its value is set to the | |
163 // empty string | |
164 SetCookie(name, "", 1); | |
165 } | |
209 | 166 } |