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