Mercurial > hg > orthanc
comparison OrthancServer/OrthancRestApi/OrthancRestResources.cpp @ 3001:7695a9c81099
refactoring /tools/find using LookupResource::IVisitor
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 11 Dec 2018 18:36:38 +0100 |
parents | 0a52af0c66e7 |
children | 5ae3ff2398e9 |
comparison
equal
deleted
inserted
replaced
2998:0a52af0c66e7 | 3001:7695a9c81099 |
---|---|
1267 | 1267 |
1268 call.GetOutput().AnswerJson(result); | 1268 call.GetOutput().AnswerJson(result); |
1269 } | 1269 } |
1270 | 1270 |
1271 | 1271 |
1272 namespace | |
1273 { | |
1274 class FindVisitor : public LookupResource::IVisitor | |
1275 { | |
1276 private: | |
1277 bool isComplete_; | |
1278 std::list<std::string> resources_; | |
1279 | |
1280 public: | |
1281 FindVisitor() : | |
1282 isComplete_(false) | |
1283 { | |
1284 } | |
1285 | |
1286 virtual void MarkAsComplete() | |
1287 { | |
1288 isComplete_ = true; // Unused information as of Orthanc 1.5.0 | |
1289 } | |
1290 | |
1291 virtual void Visit(const std::string& publicId, | |
1292 const Json::Value& dicom) | |
1293 { | |
1294 resources_.push_back(publicId); | |
1295 } | |
1296 | |
1297 void Answer(RestApiOutput& output, | |
1298 ServerIndex& index, | |
1299 ResourceType level, | |
1300 bool expand) const | |
1301 { | |
1302 AnswerListOfResources(output, index, resources_, level, expand); | |
1303 } | |
1304 }; | |
1305 } | |
1306 | |
1307 | |
1272 static void Find(RestApiPostCall& call) | 1308 static void Find(RestApiPostCall& call) |
1273 { | 1309 { |
1274 static const char* const KEY_CASE_SENSITIVE = "CaseSensitive"; | 1310 static const char* const KEY_CASE_SENSITIVE = "CaseSensitive"; |
1275 static const char* const KEY_EXPAND = "Expand"; | 1311 static const char* const KEY_EXPAND = "Expand"; |
1276 static const char* const KEY_LEVEL = "Level"; | 1312 static const char* const KEY_LEVEL = "Level"; |
1279 static const char* const KEY_SINCE = "Since"; | 1315 static const char* const KEY_SINCE = "Since"; |
1280 | 1316 |
1281 ServerContext& context = OrthancRestApi::GetContext(call); | 1317 ServerContext& context = OrthancRestApi::GetContext(call); |
1282 | 1318 |
1283 Json::Value request; | 1319 Json::Value request; |
1284 if (call.ParseJsonRequest(request) && | 1320 if (!call.ParseJsonRequest(request) || |
1285 request.type() == Json::objectValue && | 1321 request.type() != Json::objectValue) |
1286 request.isMember(KEY_LEVEL) && | 1322 { |
1287 request.isMember(KEY_QUERY) && | 1323 throw OrthancException(ErrorCode_BadRequest, |
1288 request[KEY_LEVEL].type() == Json::stringValue && | 1324 "The body must contain a JSON object"); |
1289 request[KEY_QUERY].type() == Json::objectValue && | 1325 } |
1290 (!request.isMember(KEY_CASE_SENSITIVE) || request[KEY_CASE_SENSITIVE].type() == Json::booleanValue) && | 1326 else if (!request.isMember(KEY_LEVEL) || |
1291 (!request.isMember(KEY_LIMIT) || request[KEY_LIMIT].type() == Json::intValue) && | 1327 request[KEY_LEVEL].type() != Json::stringValue) |
1292 (!request.isMember(KEY_SINCE) || request[KEY_SINCE].type() == Json::intValue)) | 1328 { |
1329 throw OrthancException(ErrorCode_BadRequest, | |
1330 "Field \"" + std::string(KEY_LEVEL) + "\" is missing, or should be a string"); | |
1331 } | |
1332 else if (!request.isMember(KEY_QUERY) && | |
1333 request[KEY_QUERY].type() != Json::objectValue) | |
1334 { | |
1335 throw OrthancException(ErrorCode_BadRequest, | |
1336 "Field \"" + std::string(KEY_QUERY) + "\" is missing, or should be a JSON object"); | |
1337 } | |
1338 else if (request.isMember(KEY_CASE_SENSITIVE) && | |
1339 request[KEY_CASE_SENSITIVE].type() != Json::booleanValue) | |
1340 { | |
1341 throw OrthancException(ErrorCode_BadRequest, | |
1342 "Field \"" + std::string(KEY_CASE_SENSITIVE) + "\" should be a Boolean"); | |
1343 } | |
1344 else if (request.isMember(KEY_LIMIT) && | |
1345 request[KEY_LIMIT].type() != Json::intValue) | |
1346 { | |
1347 throw OrthancException(ErrorCode_BadRequest, | |
1348 "Field \"" + std::string(KEY_LIMIT) + "\" should be an integer"); | |
1349 } | |
1350 else if (request.isMember(KEY_SINCE) && | |
1351 request[KEY_SINCE].type() != Json::intValue) | |
1352 { | |
1353 throw OrthancException(ErrorCode_BadRequest, | |
1354 "Field \"" + std::string(KEY_SINCE) + "\" should be an integer"); | |
1355 } | |
1356 else | |
1293 { | 1357 { |
1294 bool expand = false; | 1358 bool expand = false; |
1295 if (request.isMember(KEY_EXPAND)) | 1359 if (request.isMember(KEY_EXPAND)) |
1296 { | 1360 { |
1297 expand = request[KEY_EXPAND].asBool(); | 1361 expand = request[KEY_EXPAND].asBool(); |
1307 if (request.isMember(KEY_LIMIT)) | 1371 if (request.isMember(KEY_LIMIT)) |
1308 { | 1372 { |
1309 int tmp = request[KEY_LIMIT].asInt(); | 1373 int tmp = request[KEY_LIMIT].asInt(); |
1310 if (tmp < 0) | 1374 if (tmp < 0) |
1311 { | 1375 { |
1312 throw OrthancException(ErrorCode_ParameterOutOfRange); | 1376 throw OrthancException(ErrorCode_ParameterOutOfRange, |
1377 "Field \"" + std::string(KEY_LIMIT) + "\" should be a positive integer"); | |
1313 } | 1378 } |
1314 | 1379 |
1315 limit = static_cast<size_t>(tmp); | 1380 limit = static_cast<size_t>(tmp); |
1316 } | 1381 } |
1317 | 1382 |
1319 if (request.isMember(KEY_SINCE)) | 1384 if (request.isMember(KEY_SINCE)) |
1320 { | 1385 { |
1321 int tmp = request[KEY_SINCE].asInt(); | 1386 int tmp = request[KEY_SINCE].asInt(); |
1322 if (tmp < 0) | 1387 if (tmp < 0) |
1323 { | 1388 { |
1324 throw OrthancException(ErrorCode_ParameterOutOfRange); | 1389 throw OrthancException(ErrorCode_ParameterOutOfRange, |
1390 "Field \"" + std::string(KEY_SINCE) + "\" should be a positive integer"); | |
1325 } | 1391 } |
1326 | 1392 |
1327 since = static_cast<size_t>(tmp); | 1393 since = static_cast<size_t>(tmp); |
1328 } | 1394 } |
1329 | 1395 |
1334 Json::Value::Members members = request[KEY_QUERY].getMemberNames(); | 1400 Json::Value::Members members = request[KEY_QUERY].getMemberNames(); |
1335 for (size_t i = 0; i < members.size(); i++) | 1401 for (size_t i = 0; i < members.size(); i++) |
1336 { | 1402 { |
1337 if (request[KEY_QUERY][members[i]].type() != Json::stringValue) | 1403 if (request[KEY_QUERY][members[i]].type() != Json::stringValue) |
1338 { | 1404 { |
1339 throw OrthancException(ErrorCode_BadRequest); | 1405 throw OrthancException(ErrorCode_BadRequest, |
1406 "Tag \"" + members[i] + "\" should be associated with a string"); | |
1340 } | 1407 } |
1341 | 1408 |
1342 query.AddDicomConstraint(FromDcmtkBridge::ParseTag(members[i]), | 1409 query.AddDicomConstraint(FromDcmtkBridge::ParseTag(members[i]), |
1343 request[KEY_QUERY][members[i]].asString(), | 1410 request[KEY_QUERY][members[i]].asString(), |
1344 caseSensitive); | 1411 caseSensitive); |
1345 } | 1412 } |
1346 | 1413 |
1347 bool isComplete; | 1414 FindVisitor visitor; |
1348 std::list<std::string> resources; | 1415 context.Apply(visitor, query, since, limit); |
1349 context.Apply(isComplete, resources, query, since, limit); | 1416 visitor.Answer(call.GetOutput(), context.GetIndex(), query.GetLevel(), expand); |
1350 AnswerListOfResources(call.GetOutput(), context.GetIndex(), | |
1351 resources, query.GetLevel(), expand); | |
1352 } | |
1353 else | |
1354 { | |
1355 throw OrthancException(ErrorCode_BadRequest); | |
1356 } | 1417 } |
1357 } | 1418 } |
1358 | 1419 |
1359 | 1420 |
1360 template <enum ResourceType start, | 1421 template <enum ResourceType start, |