Mercurial > hg > orthanc
comparison Core/RestApi/RestApi.cpp @ 1046:00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 18 Jul 2014 17:15:34 +0200 |
parents | 8d1845feb277 |
children | 0332e6e8c679 |
comparison
equal
deleted
inserted
replaced
1045:0bfeeb6d340f | 1046:00f9f36bcd94 |
---|---|
45 // Anonymous namespace to avoid clashes between compilation modules | 45 // Anonymous namespace to avoid clashes between compilation modules |
46 class HttpHandlerVisitor : public RestApiHierarchy::IVisitor | 46 class HttpHandlerVisitor : public RestApiHierarchy::IVisitor |
47 { | 47 { |
48 private: | 48 private: |
49 RestApi& api_; | 49 RestApi& api_; |
50 HttpOutput& output_; | 50 RestApiOutput& output_; |
51 HttpMethod method_; | 51 HttpMethod method_; |
52 const HttpHandler::Arguments& headers_; | 52 const HttpHandler::Arguments& headers_; |
53 const HttpHandler::Arguments& getArguments_; | 53 const HttpHandler::Arguments& getArguments_; |
54 const std::string& postData_; | 54 const std::string& postData_; |
55 | 55 |
56 public: | 56 public: |
57 HttpHandlerVisitor(RestApi& api, | 57 HttpHandlerVisitor(RestApi& api, |
58 HttpOutput& output, | 58 RestApiOutput& output, |
59 HttpMethod method, | 59 HttpMethod method, |
60 const HttpHandler::Arguments& headers, | 60 const HttpHandler::Arguments& headers, |
61 const HttpHandler::Arguments& getArguments, | 61 const HttpHandler::Arguments& getArguments, |
62 const std::string& postData) : | 62 const std::string& postData) : |
63 api_(api), | 63 api_(api), |
74 const HttpHandler::Arguments& components, | 74 const HttpHandler::Arguments& components, |
75 const UriComponents& trailing) | 75 const UriComponents& trailing) |
76 { | 76 { |
77 if (resource.HasHandler(method_)) | 77 if (resource.HasHandler(method_)) |
78 { | 78 { |
79 RestApiOutput output(output_); | |
80 | |
81 switch (method_) | 79 switch (method_) |
82 { | 80 { |
83 case HttpMethod_Get: | 81 case HttpMethod_Get: |
84 { | 82 { |
85 RestApiGetCall call(output, api_, headers_, components, trailing, uri, getArguments_); | 83 RestApiGetCall call(output_, api_, headers_, components, trailing, uri, getArguments_); |
86 resource.Handle(call); | 84 resource.Handle(call); |
87 return true; | 85 return true; |
88 } | 86 } |
89 | 87 |
90 case HttpMethod_Post: | 88 case HttpMethod_Post: |
91 { | 89 { |
92 RestApiPostCall call(output, api_, headers_, components, trailing, uri, postData_); | 90 RestApiPostCall call(output_, api_, headers_, components, trailing, uri, postData_); |
93 resource.Handle(call); | 91 resource.Handle(call); |
94 return true; | 92 return true; |
95 } | 93 } |
96 | 94 |
97 case HttpMethod_Delete: | 95 case HttpMethod_Delete: |
98 { | 96 { |
99 RestApiDeleteCall call(output, api_, headers_, components, trailing, uri); | 97 RestApiDeleteCall call(output_, api_, headers_, components, trailing, uri); |
100 resource.Handle(call); | 98 resource.Handle(call); |
101 return true; | 99 return true; |
102 } | 100 } |
103 | 101 |
104 case HttpMethod_Put: | 102 case HttpMethod_Put: |
105 { | 103 { |
106 RestApiPutCall call(output, api_, headers_, components, trailing, uri, postData_); | 104 RestApiPutCall call(output_, api_, headers_, components, trailing, uri, postData_); |
107 resource.Handle(call); | 105 resource.Handle(call); |
108 return true; | 106 return true; |
109 } | 107 } |
110 | 108 |
111 default: | 109 default: |
163 const UriComponents& uri, | 161 const UriComponents& uri, |
164 const Arguments& headers, | 162 const Arguments& headers, |
165 const Arguments& getArguments, | 163 const Arguments& getArguments, |
166 const std::string& postData) | 164 const std::string& postData) |
167 { | 165 { |
168 HttpHandlerVisitor visitor(*this, output, method, headers, getArguments, postData); | 166 RestApiOutput wrappedOutput(output); |
167 | |
168 #if ORTHANC_PUGIXML_ENABLED == 1 | |
169 // Look if the user wishes XML answers instead of JSON | |
170 // http://www.w3.org/Protocols/HTTP/HTRQ_Headers.html#z3 | |
171 Arguments::const_iterator it = headers.find("accept"); | |
172 if (it != headers.end()) | |
173 { | |
174 std::vector<std::string> accepted; | |
175 Toolbox::TokenizeString(accepted, it->second, ';'); | |
176 for (size_t i = 0; i < accepted.size(); i++) | |
177 { | |
178 if (accepted[i] == "application/xml") | |
179 { | |
180 wrappedOutput.SetConvertJsonToXml(true); | |
181 } | |
182 | |
183 if (accepted[i] == "application/json") | |
184 { | |
185 wrappedOutput.SetConvertJsonToXml(false); | |
186 } | |
187 } | |
188 } | |
189 #endif | |
190 | |
191 HttpHandlerVisitor visitor(*this, wrappedOutput, method, headers, getArguments, postData); | |
169 | 192 |
170 if (root_.LookupResource(uri, visitor)) | 193 if (root_.LookupResource(uri, visitor)) |
171 { | 194 { |
172 return true; | 195 return true; |
173 } | 196 } |
174 | 197 |
175 Json::Value directory; | 198 Json::Value directory; |
176 if (root_.GetDirectory(directory, uri)) | 199 if (root_.GetDirectory(directory, uri)) |
177 { | 200 { |
178 RestApiOutput tmp(output); | 201 wrappedOutput.AnswerJson(directory); |
179 tmp.AnswerJson(directory); | |
180 return true; | 202 return true; |
181 } | 203 } |
182 | 204 |
183 std::set<HttpMethod> methods; | 205 std::set<HttpMethod> methods; |
184 root_.GetAcceptedMethods(methods, uri); | 206 root_.GetAcceptedMethods(methods, uri); |