ctkXnatAPI.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*=============================================================================
  2. Library: XNAT/Core
  3. Copyright (c) University College London,
  4. Centre for Medical Image Computing
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. // ctkXnatAPI includes
  16. #include "ctkXnatAPI_p.h"
  17. #include "ctkXnatResourceCatalogXmlParser.h"
  18. #include "qRestResult.h"
  19. #include <QNetworkReply>
  20. #include <QRegExp>
  21. #include <QUrl>
  22. #if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
  23. #include <QUrlQuery>
  24. #endif
  25. // --------------------------------------------------------------------------
  26. // ctkXnatAPI methods
  27. // --------------------------------------------------------------------------
  28. ctkXnatAPI::ctkXnatAPI(QObject* _parent)
  29. : Superclass(_parent)
  30. {
  31. }
  32. // --------------------------------------------------------------------------
  33. ctkXnatAPI::~ctkXnatAPI()
  34. {
  35. }
  36. // --------------------------------------------------------------------------
  37. QUuid ctkXnatAPI::get(const QString& resource, const Parameters& parameters, const qRestAPI::RawHeaders& rawHeaders)
  38. {
  39. QUrl url = this->createUrl(resource, parameters);
  40. #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
  41. url.addQueryItem("format", "json");
  42. #else
  43. QUrlQuery urlQuery(url);
  44. urlQuery.addQueryItem("format", "json");
  45. url.setQuery(urlQuery);
  46. #endif
  47. QNetworkReply* queryReply = this->sendRequest(QNetworkAccessManager::GetOperation, url, rawHeaders);
  48. QUuid queryId = queryReply->property("uuid").toString();
  49. return queryId;
  50. }
  51. // --------------------------------------------------------------------------
  52. void ctkXnatAPI::parseResponse(qRestResult* restResult, const QByteArray& response)
  53. {
  54. static QRegExp identifierPattern("[a-zA-Z][a-zA-Z0-9_]*");
  55. QList<QVariantMap> result;
  56. if (response.isEmpty())
  57. {
  58. // Some operations do not return result. E.g. creating a project.
  59. }
  60. else if (response.startsWith("<html>"))
  61. {
  62. // Some operations return an XML description of an object.
  63. // E.g. GET query for a specific subject.
  64. restResult->setError(QString("Bad data: ") + response, qRestAPI::ResponseParseError);
  65. }
  66. else if (response.startsWith("<?xml "))
  67. {
  68. // Some operations return an XML description of an object.
  69. // E.g. GET query for a specific subject.
  70. result = this->parseXmlResponse(restResult, response);
  71. }
  72. else if (response[0] == '{')
  73. {
  74. // Other operations return a json description of an object.
  75. // E.g. GET query of the list of subjects
  76. result = this->parseJsonResponse(restResult, response);
  77. }
  78. else if (identifierPattern.exactMatch(response))
  79. {
  80. // Some operations return the identifier of the newly created object.
  81. // E.g. creating a subject.
  82. QVariantMap map;
  83. map["ID"] = response;
  84. map["content"] = response;
  85. result.push_back(map);
  86. }
  87. else
  88. {
  89. QVariantMap map;
  90. map["content"] = response;
  91. result.push_back(map);
  92. }
  93. restResult->setResult(result);
  94. }
  95. // --------------------------------------------------------------------------
  96. QList<QVariantMap> ctkXnatAPI::parseXmlResponse(qRestResult* /*restResult*/, const QByteArray& response)
  97. {
  98. QList<QVariantMap> result;
  99. // In this case a resource catalog xml was requested
  100. if (response.contains("<cat:Catalog"))
  101. {
  102. ctkXnatResourceCatalogXmlParser parser;
  103. parser.setData(response);
  104. parser.parseXml(result);
  105. }
  106. return result;
  107. }
  108. // --------------------------------------------------------------------------
  109. QList<QVariantMap> ctkXnatAPI::parseJsonResponse(qRestResult* restResult, const QByteArray& response)
  110. {
  111. QScriptValue scriptValue = this->ScriptEngine.evaluate("(" + QString(response) + ")");
  112. QList<QVariantMap> result;
  113. // e.g. {"ResultSet":{"Result": [{"p1":"v1","p2":"v2",...}], "totalRecords":"13"}}
  114. QScriptValue resultSet = scriptValue.property("ResultSet");
  115. QScriptValue data = resultSet.property("Result");
  116. if (!data.isObject())
  117. {
  118. if (!data.toString().isEmpty())
  119. {
  120. restResult->setError(QString("Bad data: ") + data.toString(), qRestAPI::ResponseParseError);
  121. }
  122. }
  123. if (data.isArray())
  124. {
  125. quint32 length = data.property("length").toUInt32();
  126. for(quint32 i = 0; i < length; ++i)
  127. {
  128. qRestAPI::appendScriptValueToVariantMapList(result, data.property(i));
  129. }
  130. }
  131. else
  132. {
  133. qRestAPI::appendScriptValueToVariantMapList(result, data);
  134. }
  135. return result;
  136. }