comparison OrthancServer/OrthancRestApi/OrthancRestResources.cpp @ 974:83622b0f544c

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 30 Jun 2014 14:44:05 +0200
parents cc7a4ae474c1
children 1701dcb6f554
comparison
equal deleted inserted replaced
972:2047e6f033bd 974:83622b0f544c
41 namespace Orthanc 41 namespace Orthanc
42 { 42 {
43 // List all the patients, studies, series or instances ---------------------- 43 // List all the patients, studies, series or instances ----------------------
44 44
45 template <enum ResourceType resourceType> 45 template <enum ResourceType resourceType>
46 static void ListResources(RestApi::GetCall& call) 46 static void ListResources(RestApiGetCall& call)
47 { 47 {
48 Json::Value result; 48 Json::Value result;
49 OrthancRestApi::GetIndex(call).GetAllUuids(result, resourceType); 49 OrthancRestApi::GetIndex(call).GetAllUuids(result, resourceType);
50 call.GetOutput().AnswerJson(result); 50 call.GetOutput().AnswerJson(result);
51 } 51 }
52 52
53 template <enum ResourceType resourceType> 53 template <enum ResourceType resourceType>
54 static void GetSingleResource(RestApi::GetCall& call) 54 static void GetSingleResource(RestApiGetCall& call)
55 { 55 {
56 Json::Value result; 56 Json::Value result;
57 if (OrthancRestApi::GetIndex(call).LookupResource(result, call.GetUriComponent("id", ""), resourceType)) 57 if (OrthancRestApi::GetIndex(call).LookupResource(result, call.GetUriComponent("id", ""), resourceType))
58 { 58 {
59 call.GetOutput().AnswerJson(result); 59 call.GetOutput().AnswerJson(result);
60 } 60 }
61 } 61 }
62 62
63 template <enum ResourceType resourceType> 63 template <enum ResourceType resourceType>
64 static void DeleteSingleResource(RestApi::DeleteCall& call) 64 static void DeleteSingleResource(RestApiDeleteCall& call)
65 { 65 {
66 Json::Value result; 66 Json::Value result;
67 if (OrthancRestApi::GetIndex(call).DeleteResource(result, call.GetUriComponent("id", ""), resourceType)) 67 if (OrthancRestApi::GetIndex(call).DeleteResource(result, call.GetUriComponent("id", ""), resourceType))
68 { 68 {
69 call.GetOutput().AnswerJson(result); 69 call.GetOutput().AnswerJson(result);
71 } 71 }
72 72
73 73
74 // Get information about a single patient ----------------------------------- 74 // Get information about a single patient -----------------------------------
75 75
76 static void IsProtectedPatient(RestApi::GetCall& call) 76 static void IsProtectedPatient(RestApiGetCall& call)
77 { 77 {
78 std::string publicId = call.GetUriComponent("id", ""); 78 std::string publicId = call.GetUriComponent("id", "");
79 bool isProtected = OrthancRestApi::GetIndex(call).IsProtectedPatient(publicId); 79 bool isProtected = OrthancRestApi::GetIndex(call).IsProtectedPatient(publicId);
80 call.GetOutput().AnswerBuffer(isProtected ? "1" : "0", "text/plain"); 80 call.GetOutput().AnswerBuffer(isProtected ? "1" : "0", "text/plain");
81 } 81 }
82 82
83 83
84 static void SetPatientProtection(RestApi::PutCall& call) 84 static void SetPatientProtection(RestApiPutCall& call)
85 { 85 {
86 ServerContext& context = OrthancRestApi::GetContext(call); 86 ServerContext& context = OrthancRestApi::GetContext(call);
87 87
88 std::string publicId = call.GetUriComponent("id", ""); 88 std::string publicId = call.GetUriComponent("id", "");
89 std::string s = Toolbox::StripSpaces(call.GetPutBody()); 89 std::string s = Toolbox::StripSpaces(call.GetPutBody());
105 } 105 }
106 106
107 107
108 // Get information about a single instance ---------------------------------- 108 // Get information about a single instance ----------------------------------
109 109
110 static void GetInstanceFile(RestApi::GetCall& call) 110 static void GetInstanceFile(RestApiGetCall& call)
111 { 111 {
112 ServerContext& context = OrthancRestApi::GetContext(call); 112 ServerContext& context = OrthancRestApi::GetContext(call);
113 113
114 std::string publicId = call.GetUriComponent("id", ""); 114 std::string publicId = call.GetUriComponent("id", "");
115 context.AnswerDicomFile(call.GetOutput(), publicId, FileContentType_Dicom); 115 context.AnswerDicomFile(call.GetOutput(), publicId, FileContentType_Dicom);
116 } 116 }
117 117
118 118
119 static void ExportInstanceFile(RestApi::PostCall& call) 119 static void ExportInstanceFile(RestApiPostCall& call)
120 { 120 {
121 ServerContext& context = OrthancRestApi::GetContext(call); 121 ServerContext& context = OrthancRestApi::GetContext(call);
122 122
123 std::string publicId = call.GetUriComponent("id", ""); 123 std::string publicId = call.GetUriComponent("id", "");
124 124
130 call.GetOutput().AnswerBuffer("{}", "application/json"); 130 call.GetOutput().AnswerBuffer("{}", "application/json");
131 } 131 }
132 132
133 133
134 template <bool simplify> 134 template <bool simplify>
135 static void GetInstanceTags(RestApi::GetCall& call) 135 static void GetInstanceTags(RestApiGetCall& call)
136 { 136 {
137 ServerContext& context = OrthancRestApi::GetContext(call); 137 ServerContext& context = OrthancRestApi::GetContext(call);
138 138
139 std::string publicId = call.GetUriComponent("id", ""); 139 std::string publicId = call.GetUriComponent("id", "");
140 140
152 call.GetOutput().AnswerJson(full); 152 call.GetOutput().AnswerJson(full);
153 } 153 }
154 } 154 }
155 155
156 156
157 static void GetInstanceTagsBis(RestApi::GetCall& call) 157 static void GetInstanceTagsBis(RestApiGetCall& call)
158 { 158 {
159 bool simplify = call.HasArgument("simplify"); 159 bool simplify = call.HasArgument("simplify");
160 160
161 if (simplify) 161 if (simplify)
162 { 162 {
167 GetInstanceTags<false>(call); 167 GetInstanceTags<false>(call);
168 } 168 }
169 } 169 }
170 170
171 171
172 static void ListFrames(RestApi::GetCall& call) 172 static void ListFrames(RestApiGetCall& call)
173 { 173 {
174 Json::Value instance; 174 Json::Value instance;
175 if (OrthancRestApi::GetIndex(call).LookupResource(instance, call.GetUriComponent("id", ""), ResourceType_Instance)) 175 if (OrthancRestApi::GetIndex(call).LookupResource(instance, call.GetUriComponent("id", ""), ResourceType_Instance))
176 { 176 {
177 unsigned int numberOfFrames = 1; 177 unsigned int numberOfFrames = 1;
195 } 195 }
196 } 196 }
197 197
198 198
199 template <enum ImageExtractionMode mode> 199 template <enum ImageExtractionMode mode>
200 static void GetImage(RestApi::GetCall& call) 200 static void GetImage(RestApiGetCall& call)
201 { 201 {
202 ServerContext& context = OrthancRestApi::GetContext(call); 202 ServerContext& context = OrthancRestApi::GetContext(call);
203 203
204 std::string frameId = call.GetUriComponent("frame", "0"); 204 std::string frameId = call.GetUriComponent("frame", "0");
205 205
243 } 243 }
244 } 244 }
245 } 245 }
246 246
247 247
248 static void GetMatlabImage(RestApi::GetCall& call) 248 static void GetMatlabImage(RestApiGetCall& call)
249 { 249 {
250 ServerContext& context = OrthancRestApi::GetContext(call); 250 ServerContext& context = OrthancRestApi::GetContext(call);
251 251
252 std::string frameId = call.GetUriComponent("frame", "0"); 252 std::string frameId = call.GetUriComponent("frame", "0");
253 253
277 call.GetOutput().AnswerBuffer(result, "text/plain"); 277 call.GetOutput().AnswerBuffer(result, "text/plain");
278 } 278 }
279 279
280 280
281 281
282 static void GetResourceStatistics(RestApi::GetCall& call) 282 static void GetResourceStatistics(RestApiGetCall& call)
283 { 283 {
284 std::string publicId = call.GetUriComponent("id", ""); 284 std::string publicId = call.GetUriComponent("id", "");
285 Json::Value result; 285 Json::Value result;
286 OrthancRestApi::GetIndex(call).GetStatistics(result, publicId); 286 OrthancRestApi::GetIndex(call).GetStatistics(result, publicId);
287 call.GetOutput().AnswerJson(result); 287 call.GetOutput().AnswerJson(result);
289 289
290 290
291 291
292 // Handling of metadata ----------------------------------------------------- 292 // Handling of metadata -----------------------------------------------------
293 293
294 static void CheckValidResourceType(RestApi::Call& call) 294 static void CheckValidResourceType(RestApiCall& call)
295 { 295 {
296 std::string resourceType = call.GetUriComponent("resourceType", ""); 296 std::string resourceType = call.GetUriComponent("resourceType", "");
297 StringToResourceType(resourceType.c_str()); 297 StringToResourceType(resourceType.c_str());
298 } 298 }
299 299
300 300
301 static void ListMetadata(RestApi::GetCall& call) 301 static void ListMetadata(RestApiGetCall& call)
302 { 302 {
303 CheckValidResourceType(call); 303 CheckValidResourceType(call);
304 304
305 std::string publicId = call.GetUriComponent("id", ""); 305 std::string publicId = call.GetUriComponent("id", "");
306 std::list<MetadataType> metadata; 306 std::list<MetadataType> metadata;
316 316
317 call.GetOutput().AnswerJson(result); 317 call.GetOutput().AnswerJson(result);
318 } 318 }
319 319
320 320
321 static void GetMetadata(RestApi::GetCall& call) 321 static void GetMetadata(RestApiGetCall& call)
322 { 322 {
323 CheckValidResourceType(call); 323 CheckValidResourceType(call);
324 324
325 std::string publicId = call.GetUriComponent("id", ""); 325 std::string publicId = call.GetUriComponent("id", "");
326 std::string name = call.GetUriComponent("name", ""); 326 std::string name = call.GetUriComponent("name", "");
332 call.GetOutput().AnswerBuffer(value, "text/plain"); 332 call.GetOutput().AnswerBuffer(value, "text/plain");
333 } 333 }
334 } 334 }
335 335
336 336
337 static void DeleteMetadata(RestApi::DeleteCall& call) 337 static void DeleteMetadata(RestApiDeleteCall& call)
338 { 338 {
339 CheckValidResourceType(call); 339 CheckValidResourceType(call);
340 340
341 std::string publicId = call.GetUriComponent("id", ""); 341 std::string publicId = call.GetUriComponent("id", "");
342 std::string name = call.GetUriComponent("name", ""); 342 std::string name = call.GetUriComponent("name", "");
350 call.GetOutput().AnswerBuffer("", "text/plain"); 350 call.GetOutput().AnswerBuffer("", "text/plain");
351 } 351 }
352 } 352 }
353 353
354 354
355 static void SetMetadata(RestApi::PutCall& call) 355 static void SetMetadata(RestApiPutCall& call)
356 { 356 {
357 CheckValidResourceType(call); 357 CheckValidResourceType(call);
358 358
359 std::string publicId = call.GetUriComponent("id", ""); 359 std::string publicId = call.GetUriComponent("id", "");
360 std::string name = call.GetUriComponent("name", ""); 360 std::string name = call.GetUriComponent("name", "");
373 373
374 374
375 375
376 // Handling of attached files ----------------------------------------------- 376 // Handling of attached files -----------------------------------------------
377 377
378 static void ListAttachments(RestApi::GetCall& call) 378 static void ListAttachments(RestApiGetCall& call)
379 { 379 {
380 std::string resourceType = call.GetUriComponent("resourceType", ""); 380 std::string resourceType = call.GetUriComponent("resourceType", "");
381 std::string publicId = call.GetUriComponent("id", ""); 381 std::string publicId = call.GetUriComponent("id", "");
382 std::list<FileContentType> attachments; 382 std::list<FileContentType> attachments;
383 OrthancRestApi::GetIndex(call).ListAvailableAttachments(attachments, publicId, StringToResourceType(resourceType.c_str())); 383 OrthancRestApi::GetIndex(call).ListAvailableAttachments(attachments, publicId, StringToResourceType(resourceType.c_str()));
392 392
393 call.GetOutput().AnswerJson(result); 393 call.GetOutput().AnswerJson(result);
394 } 394 }
395 395
396 396
397 static bool GetAttachmentInfo(FileInfo& info, RestApi::Call& call) 397 static bool GetAttachmentInfo(FileInfo& info, RestApiCall& call)
398 { 398 {
399 CheckValidResourceType(call); 399 CheckValidResourceType(call);
400 400
401 std::string publicId = call.GetUriComponent("id", ""); 401 std::string publicId = call.GetUriComponent("id", "");
402 std::string name = call.GetUriComponent("name", ""); 402 std::string name = call.GetUriComponent("name", "");
404 404
405 return OrthancRestApi::GetIndex(call).LookupAttachment(info, publicId, contentType); 405 return OrthancRestApi::GetIndex(call).LookupAttachment(info, publicId, contentType);
406 } 406 }
407 407
408 408
409 static void GetAttachmentOperations(RestApi::GetCall& call) 409 static void GetAttachmentOperations(RestApiGetCall& call)
410 { 410 {
411 FileInfo info; 411 FileInfo info;
412 if (GetAttachmentInfo(info, call)) 412 if (GetAttachmentInfo(info, call))
413 { 413 {
414 Json::Value operations = Json::arrayValue; 414 Json::Value operations = Json::arrayValue;
440 } 440 }
441 } 441 }
442 442
443 443
444 template <int uncompress> 444 template <int uncompress>
445 static void GetAttachmentData(RestApi::GetCall& call) 445 static void GetAttachmentData(RestApiGetCall& call)
446 { 446 {
447 ServerContext& context = OrthancRestApi::GetContext(call); 447 ServerContext& context = OrthancRestApi::GetContext(call);
448 448
449 CheckValidResourceType(call); 449 CheckValidResourceType(call);
450 450
457 457
458 call.GetOutput().AnswerBuffer(content, "application/octet-stream"); 458 call.GetOutput().AnswerBuffer(content, "application/octet-stream");
459 } 459 }
460 460
461 461
462 static void GetAttachmentSize(RestApi::GetCall& call) 462 static void GetAttachmentSize(RestApiGetCall& call)
463 { 463 {
464 FileInfo info; 464 FileInfo info;
465 if (GetAttachmentInfo(info, call)) 465 if (GetAttachmentInfo(info, call))
466 { 466 {
467 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetUncompressedSize()), "text/plain"); 467 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetUncompressedSize()), "text/plain");
468 } 468 }
469 } 469 }
470 470
471 471
472 static void GetAttachmentCompressedSize(RestApi::GetCall& call) 472 static void GetAttachmentCompressedSize(RestApiGetCall& call)
473 { 473 {
474 FileInfo info; 474 FileInfo info;
475 if (GetAttachmentInfo(info, call)) 475 if (GetAttachmentInfo(info, call))
476 { 476 {
477 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetCompressedSize()), "text/plain"); 477 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetCompressedSize()), "text/plain");
478 } 478 }
479 } 479 }
480 480
481 481
482 static void GetAttachmentMD5(RestApi::GetCall& call) 482 static void GetAttachmentMD5(RestApiGetCall& call)
483 { 483 {
484 FileInfo info; 484 FileInfo info;
485 if (GetAttachmentInfo(info, call) && 485 if (GetAttachmentInfo(info, call) &&
486 info.GetUncompressedMD5() != "") 486 info.GetUncompressedMD5() != "")
487 { 487 {
488 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetUncompressedMD5()), "text/plain"); 488 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetUncompressedMD5()), "text/plain");
489 } 489 }
490 } 490 }
491 491
492 492
493 static void GetAttachmentCompressedMD5(RestApi::GetCall& call) 493 static void GetAttachmentCompressedMD5(RestApiGetCall& call)
494 { 494 {
495 FileInfo info; 495 FileInfo info;
496 if (GetAttachmentInfo(info, call) && 496 if (GetAttachmentInfo(info, call) &&
497 info.GetCompressedMD5() != "") 497 info.GetCompressedMD5() != "")
498 { 498 {
499 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetCompressedMD5()), "text/plain"); 499 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetCompressedMD5()), "text/plain");
500 } 500 }
501 } 501 }
502 502
503 503
504 static void VerifyAttachment(RestApi::PostCall& call) 504 static void VerifyAttachment(RestApiPostCall& call)
505 { 505 {
506 ServerContext& context = OrthancRestApi::GetContext(call); 506 ServerContext& context = OrthancRestApi::GetContext(call);
507 CheckValidResourceType(call); 507 CheckValidResourceType(call);
508 508
509 std::string publicId = call.GetUriComponent("id", ""); 509 std::string publicId = call.GetUriComponent("id", "");
553 LOG(INFO) << "The attachment " << name << " of resource " << publicId << " has bad MD5!"; 553 LOG(INFO) << "The attachment " << name << " of resource " << publicId << " has bad MD5!";
554 } 554 }
555 } 555 }
556 556
557 557
558 static void UploadAttachment(RestApi::PutCall& call) 558 static void UploadAttachment(RestApiPutCall& call)
559 { 559 {
560 ServerContext& context = OrthancRestApi::GetContext(call); 560 ServerContext& context = OrthancRestApi::GetContext(call);
561 CheckValidResourceType(call); 561 CheckValidResourceType(call);
562 562
563 std::string publicId = call.GetUriComponent("id", ""); 563 std::string publicId = call.GetUriComponent("id", "");
573 call.GetOutput().AnswerBuffer("{}", "application/json"); 573 call.GetOutput().AnswerBuffer("{}", "application/json");
574 } 574 }
575 } 575 }
576 576
577 577
578 static void DeleteAttachment(RestApi::DeleteCall& call) 578 static void DeleteAttachment(RestApiDeleteCall& call)
579 { 579 {
580 CheckValidResourceType(call); 580 CheckValidResourceType(call);
581 581
582 std::string publicId = call.GetUriComponent("id", ""); 582 std::string publicId = call.GetUriComponent("id", "");
583 std::string name = call.GetUriComponent("name", ""); 583 std::string name = call.GetUriComponent("name", "");
593 } 593 }
594 594
595 595
596 // Raw access to the DICOM tags of an instance ------------------------------ 596 // Raw access to the DICOM tags of an instance ------------------------------
597 597
598 static void GetRawContent(RestApi::GetCall& call) 598 static void GetRawContent(RestApiGetCall& call)
599 { 599 {
600 std::string id = call.GetUriComponent("id", ""); 600 std::string id = call.GetUriComponent("id", "");
601 601
602 ServerContext::DicomCacheLocker locker(OrthancRestApi::GetContext(call), id); 602 ServerContext::DicomCacheLocker locker(OrthancRestApi::GetContext(call), id);
603 603
679 679
680 return true; 680 return true;
681 } 681 }
682 682
683 683
684 static void GetSharedTags(RestApi::GetCall& call) 684 static void GetSharedTags(RestApiGetCall& call)
685 { 685 {
686 ServerContext& context = OrthancRestApi::GetContext(call); 686 ServerContext& context = OrthancRestApi::GetContext(call);
687 std::string publicId = call.GetUriComponent("id", ""); 687 std::string publicId = call.GetUriComponent("id", "");
688 bool simplify = call.HasArgument("simplify"); 688 bool simplify = call.HasArgument("simplify");
689 689
704 } 704 }
705 } 705 }
706 706
707 707
708 template <enum ResourceType resourceType> 708 template <enum ResourceType resourceType>
709 static void GetModule(RestApi::GetCall& call) 709 static void GetModule(RestApiGetCall& call)
710 { 710 {
711 ServerContext& context = OrthancRestApi::GetContext(call); 711 ServerContext& context = OrthancRestApi::GetContext(call);
712 std::string publicId = call.GetUriComponent("id", ""); 712 std::string publicId = call.GetUriComponent("id", "");
713 bool simplify = call.HasArgument("simplify"); 713 bool simplify = call.HasArgument("simplify");
714 714