Mercurial > hg > orthanc
comparison Core/RestApi/RestApiOutput.cpp @ 211:b7aea293b965
list of resources
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 29 Nov 2012 09:56:46 +0100 |
parents | 96b7918a6a18 |
children | c07170f3f4f7 |
comparison
equal
deleted
inserted
replaced
210:96b7918a6a18 | 211:b7aea293b965 |
---|---|
30 **/ | 30 **/ |
31 | 31 |
32 | 32 |
33 #include "RestApiOutput.h" | 33 #include "RestApiOutput.h" |
34 | 34 |
35 #include "../OrthancException.h" | |
36 | |
35 namespace Orthanc | 37 namespace Orthanc |
36 { | 38 { |
37 RestApiOutput::RestApiOutput(HttpOutput& output) : | 39 RestApiOutput::RestApiOutput(HttpOutput& output) : |
38 output_(output) | 40 output_(output) |
39 { | 41 { |
40 existingResource_ = false; | 42 existingResource_ = false; |
41 } | 43 } |
42 | 44 |
43 | |
44 RestApiOutput::~RestApiOutput() | 45 RestApiOutput::~RestApiOutput() |
45 { | 46 { |
46 if (!existingResource_) | 47 if (!existingResource_) |
47 { | 48 { |
48 output_.SendHeader(Orthanc_HttpStatus_400_BadRequest); | 49 output_.SendHeader(Orthanc_HttpStatus_400_BadRequest); |
49 } | 50 } |
50 } | 51 } |
52 | |
53 void RestApiOutput::CheckStatus() | |
54 { | |
55 if (existingResource_) | |
56 { | |
57 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
58 } | |
59 } | |
51 | 60 |
52 void RestApiOutput::AnswerFile(HttpFileSender& sender) | 61 void RestApiOutput::AnswerFile(HttpFileSender& sender) |
53 { | 62 { |
63 CheckStatus(); | |
54 sender.Send(output_); | 64 sender.Send(output_); |
55 existingResource_ = true; | 65 existingResource_ = true; |
56 } | 66 } |
57 | 67 |
58 void RestApiOutput::AnswerJson(const Json::Value& value) | 68 void RestApiOutput::AnswerJson(const Json::Value& value) |
59 { | 69 { |
70 CheckStatus(); | |
60 Json::StyledWriter writer; | 71 Json::StyledWriter writer; |
61 std::string s = writer.write(value); | 72 std::string s = writer.write(value); |
62 output_.AnswerBufferWithContentType(s, "application/json"); | 73 output_.AnswerBufferWithContentType(s, "application/json"); |
63 existingResource_ = true; | 74 existingResource_ = true; |
64 } | 75 } |
65 | 76 |
66 void RestApiOutput::AnswerBuffer(const std::string& buffer, | 77 void RestApiOutput::AnswerBuffer(const std::string& buffer, |
67 const std::string& contentType) | 78 const std::string& contentType) |
68 { | 79 { |
80 CheckStatus(); | |
69 output_.AnswerBufferWithContentType(buffer, contentType); | 81 output_.AnswerBufferWithContentType(buffer, contentType); |
70 existingResource_ = true; | 82 existingResource_ = true; |
71 } | 83 } |
72 | 84 |
73 void RestApiOutput::Redirect(const char* path) | 85 void RestApiOutput::Redirect(const char* path) |
74 { | 86 { |
87 CheckStatus(); | |
75 output_.Redirect(path); | 88 output_.Redirect(path); |
76 existingResource_ = true; | 89 existingResource_ = true; |
77 } | 90 } |
78 } | 91 } |