Mercurial > hg > orthanc
diff 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 |
line wrap: on
line diff
--- a/Core/RestApi/RestApi.cpp Fri Jul 18 16:41:10 2014 +0200 +++ b/Core/RestApi/RestApi.cpp Fri Jul 18 17:15:34 2014 +0200 @@ -47,7 +47,7 @@ { private: RestApi& api_; - HttpOutput& output_; + RestApiOutput& output_; HttpMethod method_; const HttpHandler::Arguments& headers_; const HttpHandler::Arguments& getArguments_; @@ -55,7 +55,7 @@ public: HttpHandlerVisitor(RestApi& api, - HttpOutput& output, + RestApiOutput& output, HttpMethod method, const HttpHandler::Arguments& headers, const HttpHandler::Arguments& getArguments, @@ -76,34 +76,32 @@ { if (resource.HasHandler(method_)) { - RestApiOutput output(output_); - switch (method_) { case HttpMethod_Get: { - RestApiGetCall call(output, api_, headers_, components, trailing, uri, getArguments_); + RestApiGetCall call(output_, api_, headers_, components, trailing, uri, getArguments_); resource.Handle(call); return true; } case HttpMethod_Post: { - RestApiPostCall call(output, api_, headers_, components, trailing, uri, postData_); + RestApiPostCall call(output_, api_, headers_, components, trailing, uri, postData_); resource.Handle(call); return true; } case HttpMethod_Delete: { - RestApiDeleteCall call(output, api_, headers_, components, trailing, uri); + RestApiDeleteCall call(output_, api_, headers_, components, trailing, uri); resource.Handle(call); return true; } case HttpMethod_Put: { - RestApiPutCall call(output, api_, headers_, components, trailing, uri, postData_); + RestApiPutCall call(output_, api_, headers_, components, trailing, uri, postData_); resource.Handle(call); return true; } @@ -165,7 +163,32 @@ const Arguments& getArguments, const std::string& postData) { - HttpHandlerVisitor visitor(*this, output, method, headers, getArguments, postData); + RestApiOutput wrappedOutput(output); + +#if ORTHANC_PUGIXML_ENABLED == 1 + // Look if the user wishes XML answers instead of JSON + // http://www.w3.org/Protocols/HTTP/HTRQ_Headers.html#z3 + Arguments::const_iterator it = headers.find("accept"); + if (it != headers.end()) + { + std::vector<std::string> accepted; + Toolbox::TokenizeString(accepted, it->second, ';'); + for (size_t i = 0; i < accepted.size(); i++) + { + if (accepted[i] == "application/xml") + { + wrappedOutput.SetConvertJsonToXml(true); + } + + if (accepted[i] == "application/json") + { + wrappedOutput.SetConvertJsonToXml(false); + } + } + } +#endif + + HttpHandlerVisitor visitor(*this, wrappedOutput, method, headers, getArguments, postData); if (root_.LookupResource(uri, visitor)) { @@ -175,8 +198,7 @@ Json::Value directory; if (root_.GetDirectory(directory, uri)) { - RestApiOutput tmp(output); - tmp.AnswerJson(directory); + wrappedOutput.AnswerJson(directory); return true; }