Mercurial > hg > orthanc
annotate Core/RestApi/RestApiOutput.cpp @ 953:f894be6e7cc1 query-retrieve
merge mainline -> query-retrieve
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 25 Jun 2014 15:34:40 +0200 |
parents | a811bdf8b8eb |
children | 8d1845feb277 |
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> |
37 | |
211 | 38 #include "../OrthancException.h" |
39 | |
209 | 40 namespace Orthanc |
41 { | |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
42 RestApiOutput::RestApiOutput(HttpOutput& output) : |
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
43 output_(output) |
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
44 { |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
45 alreadySent_ = false; |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
46 } |
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
47 |
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
48 RestApiOutput::~RestApiOutput() |
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
49 { |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
50 if (!alreadySent_) |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
51 { |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
52 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
|
53 } |
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
54 } |
211 | 55 |
56 void RestApiOutput::CheckStatus() | |
57 { | |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
58 if (alreadySent_) |
211 | 59 { |
60 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
61 } | |
62 } | |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
63 |
209 | 64 void RestApiOutput::AnswerFile(HttpFileSender& sender) |
65 { | |
211 | 66 CheckStatus(); |
209 | 67 sender.Send(output_); |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
68 alreadySent_ = true; |
209 | 69 } |
70 | |
71 void RestApiOutput::AnswerJson(const Json::Value& value) | |
72 { | |
211 | 73 CheckStatus(); |
209 | 74 Json::StyledWriter writer; |
75 std::string s = writer.write(value); | |
330 | 76 output_.AnswerBufferWithContentType(s, "application/json", cookies_); |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
77 alreadySent_ = true; |
209 | 78 } |
79 | |
80 void RestApiOutput::AnswerBuffer(const std::string& buffer, | |
81 const std::string& contentType) | |
82 { | |
211 | 83 CheckStatus(); |
330 | 84 output_.AnswerBufferWithContentType(buffer, contentType, cookies_); |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
85 alreadySent_ = true; |
209 | 86 } |
87 | |
339 | 88 void RestApiOutput::AnswerBuffer(const void* buffer, |
89 size_t length, | |
90 const std::string& contentType) | |
91 { | |
92 CheckStatus(); | |
93 output_.AnswerBufferWithContentType(buffer, length, contentType, cookies_); | |
94 alreadySent_ = true; | |
95 } | |
96 | |
215
c07170f3f4f7
refactoring of access to images in REST
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
211
diff
changeset
|
97 void RestApiOutput::Redirect(const std::string& path) |
209 | 98 { |
211 | 99 CheckStatus(); |
209 | 100 output_.Redirect(path); |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
101 alreadySent_ = true; |
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
102 } |
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
103 |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
104 void RestApiOutput::SignalError(HttpStatus status) |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
105 { |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
106 if (status != HttpStatus_403_Forbidden && |
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
107 status != HttpStatus_415_UnsupportedMediaType) |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
108 { |
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
109 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
|
110 } |
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
111 |
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
112 CheckStatus(); |
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
113 output_.SendHeader(status); |
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
114 alreadySent_ = true; |
209 | 115 } |
330 | 116 |
117 void RestApiOutput::SetCookie(const std::string& name, | |
118 const std::string& value, | |
119 unsigned int maxAge) | |
120 { | |
121 if (name.find(";") != std::string::npos || | |
122 name.find(" ") != std::string::npos || | |
123 value.find(";") != std::string::npos || | |
124 value.find(" ") != std::string::npos) | |
125 { | |
126 throw OrthancException(ErrorCode_NotImplemented); | |
127 } | |
128 | |
129 CheckStatus(); | |
130 | |
131 std::string v = value + ";path=/"; | |
132 | |
133 if (maxAge != 0) | |
134 { | |
135 v += ";max-age=" + boost::lexical_cast<std::string>(maxAge); | |
136 } | |
137 | |
138 cookies_[name] = v; | |
139 } | |
140 | |
141 void RestApiOutput::ResetCookie(const std::string& name) | |
142 { | |
143 // This marks the cookie to be deleted by the browser in 1 second, | |
144 // and before it actually gets deleted, its value is set to the | |
145 // empty string | |
146 SetCookie(name, "", 1); | |
147 } | |
209 | 148 } |