comparison Core/RestApi/RestApiOutput.cpp @ 216:e5d5d4a9a326

refactored upload of dicom through http
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 29 Nov 2012 11:57:35 +0100
parents c07170f3f4f7
children 78a8eaa5f30b
comparison
equal deleted inserted replaced
215:c07170f3f4f7 216:e5d5d4a9a326
37 namespace Orthanc 37 namespace Orthanc
38 { 38 {
39 RestApiOutput::RestApiOutput(HttpOutput& output) : 39 RestApiOutput::RestApiOutput(HttpOutput& output) :
40 output_(output) 40 output_(output)
41 { 41 {
42 existingResource_ = false; 42 alreadySent_ = false;
43 } 43 }
44 44
45 RestApiOutput::~RestApiOutput() 45 RestApiOutput::~RestApiOutput()
46 { 46 {
47 if (!existingResource_) 47 if (!alreadySent_)
48 { 48 {
49 output_.SendHeader(Orthanc_HttpStatus_400_BadRequest); 49 output_.SendHeader(Orthanc_HttpStatus_400_BadRequest);
50 } 50 }
51 } 51 }
52 52
53 void RestApiOutput::CheckStatus() 53 void RestApiOutput::CheckStatus()
54 { 54 {
55 if (existingResource_) 55 if (alreadySent_)
56 { 56 {
57 throw OrthancException(ErrorCode_BadSequenceOfCalls); 57 throw OrthancException(ErrorCode_BadSequenceOfCalls);
58 } 58 }
59 } 59 }
60 60
61 void RestApiOutput::AnswerFile(HttpFileSender& sender) 61 void RestApiOutput::AnswerFile(HttpFileSender& sender)
62 { 62 {
63 CheckStatus(); 63 CheckStatus();
64 sender.Send(output_); 64 sender.Send(output_);
65 existingResource_ = true; 65 alreadySent_ = true;
66 } 66 }
67 67
68 void RestApiOutput::AnswerJson(const Json::Value& value) 68 void RestApiOutput::AnswerJson(const Json::Value& value)
69 { 69 {
70 CheckStatus(); 70 CheckStatus();
71 Json::StyledWriter writer; 71 Json::StyledWriter writer;
72 std::string s = writer.write(value); 72 std::string s = writer.write(value);
73 output_.AnswerBufferWithContentType(s, "application/json"); 73 output_.AnswerBufferWithContentType(s, "application/json");
74 existingResource_ = true; 74 alreadySent_ = true;
75 } 75 }
76 76
77 void RestApiOutput::AnswerBuffer(const std::string& buffer, 77 void RestApiOutput::AnswerBuffer(const std::string& buffer,
78 const std::string& contentType) 78 const std::string& contentType)
79 { 79 {
80 CheckStatus(); 80 CheckStatus();
81 output_.AnswerBufferWithContentType(buffer, contentType); 81 output_.AnswerBufferWithContentType(buffer, contentType);
82 existingResource_ = true; 82 alreadySent_ = true;
83 } 83 }
84 84
85 void RestApiOutput::Redirect(const std::string& path) 85 void RestApiOutput::Redirect(const std::string& path)
86 { 86 {
87 CheckStatus(); 87 CheckStatus();
88 output_.Redirect(path); 88 output_.Redirect(path);
89 existingResource_ = true; 89 alreadySent_ = true;
90 }
91
92 void RestApiOutput::SignalError(Orthanc_HttpStatus status)
93 {
94 if (status != Orthanc_HttpStatus_415_UnsupportedMediaType)
95 {
96 throw OrthancException("This HTTP status is not allowed in a REST API");
97 }
98
99 CheckStatus();
100 output_.SendHeader(status);
101 alreadySent_ = true;
90 } 102 }
91 } 103 }