comparison Plugin/Plugin.cpp @ 8:4c3437217518

fix for compatibility with simplified OrthancPluginCppWrapper
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 05 Dec 2018 09:16:51 +0100
parents 5e6de82bb10f
children 7e207ade2f1a
comparison
equal deleted inserted replaced
7:151e29acbb13 8:4c3437217518
67 { 67 {
68 OrthancPlugins::PluginContext& context = OrthancPlugins::PluginContext::GetInstance(); 68 OrthancPlugins::PluginContext& context = OrthancPlugins::PluginContext::GetInstance();
69 69
70 if (request->method != OrthancPluginHttpMethod_Get) 70 if (request->method != OrthancPluginHttpMethod_Get)
71 { 71 {
72 OrthancPluginSendMethodNotAllowed(context.GetOrthanc(), output, "GET"); 72 OrthancPluginSendMethodNotAllowed(OrthancPlugins::GetGlobalContext(), output, "GET");
73 return; 73 return;
74 } 74 }
75 75
76 assert(request->groupsCount == 1); 76 assert(request->groupsCount == 1);
77 77
157 157
158 switch (compression) 158 switch (compression)
159 { 159 {
160 case OrthancPlugins::BucketCompression_None: 160 case OrthancPlugins::BucketCompression_None:
161 { 161 {
162 OrthancPluginAnswerBuffer(context.GetOrthanc(), output, chunk.c_str(), 162 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, chunk.c_str(),
163 chunk.size(), "application/octet-stream"); 163 chunk.size(), "application/octet-stream");
164 break; 164 break;
165 } 165 }
166 166
167 case OrthancPlugins::BucketCompression_Gzip: 167 case OrthancPlugins::BucketCompression_Gzip:
168 { 168 {
169 std::string compressed; 169 std::string compressed;
170 Orthanc::GzipCompressor gzip; 170 Orthanc::GzipCompressor gzip;
171 //gzip.SetCompressionLevel(9); 171 //gzip.SetCompressionLevel(9);
172 Orthanc::IBufferCompressor::Compress(compressed, gzip, chunk); 172 Orthanc::IBufferCompressor::Compress(compressed, gzip, chunk);
173 OrthancPluginAnswerBuffer(context.GetOrthanc(), output, compressed.c_str(), 173 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, compressed.c_str(),
174 compressed.size(), "application/gzip"); 174 compressed.size(), "application/gzip");
175 break; 175 break;
176 } 176 }
177 177
178 default: 178 default:
184 184
185 static bool ParsePostBody(Json::Value& body, 185 static bool ParsePostBody(Json::Value& body,
186 OrthancPluginRestOutput* output, 186 OrthancPluginRestOutput* output,
187 const OrthancPluginHttpRequest* request) 187 const OrthancPluginHttpRequest* request)
188 { 188 {
189 OrthancPlugins::PluginContext& context = OrthancPlugins::PluginContext::GetInstance();
190
191 Json::Reader reader; 189 Json::Reader reader;
192 190
193 if (request->method != OrthancPluginHttpMethod_Post) 191 if (request->method != OrthancPluginHttpMethod_Post)
194 { 192 {
195 OrthancPluginSendMethodNotAllowed(context.GetOrthanc(), output, "POST"); 193 OrthancPluginSendMethodNotAllowed(OrthancPlugins::GetGlobalContext(), output, "POST");
196 return false; 194 return false;
197 } 195 }
198 else if (reader.parse(request->body, request->body + request->bodySize, body)) 196 else if (reader.parse(request->body, request->body + request->bodySize, body))
199 { 197 {
200 return true; 198 return true;
239 } 237 }
240 238
241 Json::FastWriter writer; 239 Json::FastWriter writer;
242 std::string s = writer.write(answer); 240 std::string s = writer.write(answer);
243 241
244 OrthancPluginAnswerBuffer(context.GetOrthanc(), output, s.c_str(), s.size(), "application/json"); 242 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, s.c_str(), s.size(), "application/json");
245 } 243 }
246 244
247 245
248 246
249 static void SubmitJob(OrthancPluginRestOutput* output, 247 static void SubmitJob(OrthancPluginRestOutput* output,
250 OrthancPlugins::OrthancJob* job, 248 OrthancPlugins::OrthancJob* job,
251 int priority) 249 int priority)
252 { 250 {
253 OrthancPlugins::PluginContext& context = OrthancPlugins::PluginContext::GetInstance(); 251 std::string id = OrthancPlugins::OrthancJob::Submit(job, priority);
254
255 std::string id = OrthancPlugins::OrthancJob::Submit(context.GetOrthanc(), job, priority);
256 252
257 Json::Value result = Json::objectValue; 253 Json::Value result = Json::objectValue;
258 result[KEY_ID] = id; 254 result[KEY_ID] = id;
259 result[KEY_PATH] = std::string(URI_JOBS) + "/" + id; 255 result[KEY_PATH] = std::string(URI_JOBS) + "/" + id;
260 256
261 std::string s = result.toStyledString(); 257 std::string s = result.toStyledString();
262 OrthancPluginAnswerBuffer(context.GetOrthanc(), output, s.c_str(), s.size(), "application/json"); 258 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, s.c_str(), s.size(), "application/json");
263 } 259 }
264 260
265 261
266 262
267 void SchedulePull(OrthancPluginRestOutput* output, 263 void SchedulePull(OrthancPluginRestOutput* output,
276 return; 272 return;
277 } 273 }
278 274
279 OrthancPlugins::TransferQuery query(body); 275 OrthancPlugins::TransferQuery query(body);
280 276
281 SubmitJob(output, new OrthancPlugins::PullJob(context.GetOrthanc(), query, 277 SubmitJob(output, new OrthancPlugins::PullJob(
282 context.GetThreadsCount(), 278 query, context.GetThreadsCount(), context.GetTargetBucketSize()),
283 context.GetTargetBucketSize()),
284 query.GetPriority()); 279 query.GetPriority());
285 } 280 }
286 281
287 282
288 283
336 Json::Value result = Json::objectValue; 331 Json::Value result = Json::objectValue;
337 result[KEY_ID] = id; 332 result[KEY_ID] = id;
338 result[KEY_PATH] = std::string(URI_PUSH) + "/" + id; 333 result[KEY_PATH] = std::string(URI_PUSH) + "/" + id;
339 334
340 std::string s = result.toStyledString(); 335 std::string s = result.toStyledString();
341 OrthancPluginAnswerBuffer(context.GetOrthanc(), output, s.c_str(), s.size(), "application/json"); 336 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, s.c_str(), s.size(), "application/json");
342 } 337 }
343 338
344 339
345 void StorePush(OrthancPluginRestOutput* output, 340 void StorePush(OrthancPluginRestOutput* output,
346 const char* url, 341 const char* url,
348 { 343 {
349 OrthancPlugins::PluginContext& context = OrthancPlugins::PluginContext::GetInstance(); 344 OrthancPlugins::PluginContext& context = OrthancPlugins::PluginContext::GetInstance();
350 345
351 if (request->method != OrthancPluginHttpMethod_Put) 346 if (request->method != OrthancPluginHttpMethod_Put)
352 { 347 {
353 OrthancPluginSendMethodNotAllowed(context.GetOrthanc(), output, "PUT"); 348 OrthancPluginSendMethodNotAllowed(OrthancPlugins::GetGlobalContext(), output, "PUT");
354 return; 349 return;
355 } 350 }
356 351
357 assert(request->groupsCount == 2); 352 assert(request->groupsCount == 2);
358 std::string transaction(request->groups[0]); 353 std::string transaction(request->groups[0]);
368 { 363 {
369 throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource); 364 throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource);
370 } 365 }
371 366
372 context.GetActivePushTransactions().Store 367 context.GetActivePushTransactions().Store
373 (context.GetOrthanc(), transaction, chunkIndex, request->body, request->bodySize); 368 (transaction, chunkIndex, request->body, request->bodySize);
374 369
375 std::string s = "{}"; 370 std::string s = "{}";
376 OrthancPluginAnswerBuffer(context.GetOrthanc(), output, s.c_str(), s.size(), "application/json"); 371 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, s.c_str(), s.size(), "application/json");
377 } 372 }
378 373
379 374
380 void CommitPush(OrthancPluginRestOutput* output, 375 void CommitPush(OrthancPluginRestOutput* output,
381 const char* url, 376 const char* url,
383 { 378 {
384 OrthancPlugins::PluginContext& context = OrthancPlugins::PluginContext::GetInstance(); 379 OrthancPlugins::PluginContext& context = OrthancPlugins::PluginContext::GetInstance();
385 380
386 if (request->method != OrthancPluginHttpMethod_Post) 381 if (request->method != OrthancPluginHttpMethod_Post)
387 { 382 {
388 OrthancPluginSendMethodNotAllowed(context.GetOrthanc(), output, "POST"); 383 OrthancPluginSendMethodNotAllowed(OrthancPlugins::GetGlobalContext(), output, "POST");
389 return; 384 return;
390 } 385 }
391 386
392 assert(request->groupsCount == 1); 387 assert(request->groupsCount == 1);
393 std::string transaction(request->groups[0]); 388 std::string transaction(request->groups[0]);
394 389
395 context. 390 context.GetActivePushTransactions().Commit(transaction);
396 GetActivePushTransactions().Commit(context.GetOrthanc(), transaction);
397 391
398 std::string s = "{}"; 392 std::string s = "{}";
399 OrthancPluginAnswerBuffer(context.GetOrthanc(), output, s.c_str(), s.size(), "application/json"); 393 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, s.c_str(), s.size(), "application/json");
400 } 394 }
401 395
402 396
403 void DiscardPush(OrthancPluginRestOutput* output, 397 void DiscardPush(OrthancPluginRestOutput* output,
404 const char* url, 398 const char* url,
406 { 400 {
407 OrthancPlugins::PluginContext& context = OrthancPlugins::PluginContext::GetInstance(); 401 OrthancPlugins::PluginContext& context = OrthancPlugins::PluginContext::GetInstance();
408 402
409 if (request->method != OrthancPluginHttpMethod_Delete) 403 if (request->method != OrthancPluginHttpMethod_Delete)
410 { 404 {
411 OrthancPluginSendMethodNotAllowed(context.GetOrthanc(), output, "DELETE"); 405 OrthancPluginSendMethodNotAllowed(OrthancPlugins::GetGlobalContext(), output, "DELETE");
412 return; 406 return;
413 } 407 }
414 408
415 assert(request->groupsCount == 1); 409 assert(request->groupsCount == 1);
416 std::string transaction(request->groups[0]); 410 std::string transaction(request->groups[0]);
417 411
418 context. 412 context.
419 GetActivePushTransactions().Discard(transaction); 413 GetActivePushTransactions().Discard(transaction);
420 414
421 std::string s = "{}"; 415 std::string s = "{}";
422 OrthancPluginAnswerBuffer(context.GetOrthanc(), output, s.c_str(), s.size(), "application/json"); 416 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, s.c_str(), s.size(), "application/json");
423 } 417 }
424 418
425 419
426 420
427 void ScheduleSend(OrthancPluginRestOutput* output, 421 void ScheduleSend(OrthancPluginRestOutput* output,
436 return; 430 return;
437 } 431 }
438 432
439 OrthancPlugins::TransferQuery query(body); 433 OrthancPlugins::TransferQuery query(body);
440 434
441 OrthancPlugins::OrthancPeers peers(context.GetOrthanc()); 435 OrthancPlugins::OrthancPeers peers;
442 436
443 std::string remoteSelf; // For pull mode 437 std::string remoteSelf; // For pull mode
444 bool pullMode = peers.LookupUserProperty(remoteSelf, query.GetPeer(), KEY_REMOTE_SELF); 438 bool pullMode = peers.LookupUserProperty(remoteSelf, query.GetPeer(), KEY_REMOTE_SELF);
445 439
446 LOG(INFO) << "Sending resources to peer \"" << query.GetPeer() << "\" using " 440 LOG(INFO) << "Sending resources to peer \"" << query.GetPeer() << "\" using "
471 result[KEY_PEER] = query.GetPeer(); 465 result[KEY_PEER] = query.GetPeer();
472 result[KEY_REMOTE_JOB] = answer[KEY_ID].asString(); 466 result[KEY_REMOTE_JOB] = answer[KEY_ID].asString();
473 result[KEY_URL] = url + answer[KEY_PATH].asString(); 467 result[KEY_URL] = url + answer[KEY_PATH].asString();
474 468
475 std::string s = result.toStyledString(); 469 std::string s = result.toStyledString();
476 OrthancPluginAnswerBuffer(context.GetOrthanc(), output, s.c_str(), s.size(), "application/json"); 470 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, s.c_str(), s.size(), "application/json");
477 } 471 }
478 else 472 else
479 { 473 {
480 LOG(ERROR) << "Cannot trigger send DICOM instances using pull mode to peer: " << query.GetPeer() 474 LOG(ERROR) << "Cannot trigger send DICOM instances using pull mode to peer: " << query.GetPeer()
481 << " (check out remote logs, and that transfer plugin is installed)"; 475 << " (check out remote logs, and that transfer plugin is installed)";
482 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol); 476 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);
483 } 477 }
484 } 478 }
485 else 479 else
486 { 480 {
487 SubmitJob(output, new OrthancPlugins::PushJob(context.GetOrthanc(), query, 481 SubmitJob(output, new OrthancPlugins::PushJob(query, context.GetCache(),
488 context.GetCache(), 482 context.GetThreadsCount(), context.GetTargetBucketSize()),
489 context.GetThreadsCount(),
490 context.GetTargetBucketSize()),
491 query.GetPriority()); 483 query.GetPriority());
492 } 484 }
493 } 485 }
494 486
495 487
524 516
525 std::auto_ptr<OrthancPlugins::OrthancJob> job; 517 std::auto_ptr<OrthancPlugins::OrthancJob> job;
526 518
527 if (type == JOB_TYPE_PULL) 519 if (type == JOB_TYPE_PULL)
528 { 520 {
529 job.reset(new OrthancPlugins::PullJob(context.GetOrthanc(), query, 521 job.reset(new OrthancPlugins::PullJob(query,
530 context.GetThreadsCount(), 522 context.GetThreadsCount(),
531 context.GetTargetBucketSize())); 523 context.GetTargetBucketSize()));
532 } 524 }
533 else if (type == JOB_TYPE_PUSH) 525 else if (type == JOB_TYPE_PUSH)
534 { 526 {
535 job.reset(new OrthancPlugins::PushJob(context.GetOrthanc(), query, 527 job.reset(new OrthancPlugins::PushJob(query,
536 context.GetCache(), 528 context.GetCache(),
537 context.GetThreadsCount(), 529 context.GetThreadsCount(),
538 context.GetTargetBucketSize())); 530 context.GetTargetBucketSize()));
539 } 531 }
540 532
542 { 534 {
543 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); 535 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
544 } 536 }
545 else 537 else
546 { 538 {
547 return OrthancPlugins::OrthancJob::Create(context.GetOrthanc(), job.release()); 539 return OrthancPlugins::OrthancJob::Create(job.release());
548 } 540 }
549 } 541 }
550 else 542 else
551 { 543 {
552 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); 544 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
573 { 565 {
574 OrthancPlugins::PluginContext& context = OrthancPlugins::PluginContext::GetInstance(); 566 OrthancPlugins::PluginContext& context = OrthancPlugins::PluginContext::GetInstance();
575 567
576 if (request->method != OrthancPluginHttpMethod_Get) 568 if (request->method != OrthancPluginHttpMethod_Get)
577 { 569 {
578 OrthancPluginSendMethodNotAllowed(context.GetOrthanc(), output, "GET"); 570 OrthancPluginSendMethodNotAllowed(OrthancPlugins::GetGlobalContext(), output, "GET");
579 return; 571 return;
580 } 572 }
581 573
582 OrthancPlugins::DetectTransferPlugin::Result detection; 574 OrthancPlugins::DetectTransferPlugin::Result detection;
583 OrthancPlugins::DetectTransferPlugin::Apply 575 OrthancPlugins::DetectTransferPlugin::Apply
584 (detection, context.GetOrthanc(), context.GetThreadsCount(), 2 /* timeout */); 576 (detection, context.GetThreadsCount(), 2 /* timeout */);
585 577
586 Json::Value result = Json::objectValue; 578 Json::Value result = Json::objectValue;
587 579
588 OrthancPlugins::OrthancPeers peers(context.GetOrthanc()); 580 OrthancPlugins::OrthancPeers peers;
589 581
590 for (OrthancPlugins::DetectTransferPlugin::Result::const_iterator 582 for (OrthancPlugins::DetectTransferPlugin::Result::const_iterator
591 it = detection.begin(); it != detection.end(); ++it) 583 it = detection.begin(); it != detection.end(); ++it)
592 { 584 {
593 if (it->second) 585 if (it->second)
608 result[it->first] = "disabled"; 600 result[it->first] = "disabled";
609 } 601 }
610 } 602 }
611 603
612 std::string s = result.toStyledString(); 604 std::string s = result.toStyledString();
613 OrthancPluginAnswerBuffer(context.GetOrthanc(), output, s.c_str(), s.size(), "application/json"); 605 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, s.c_str(), s.size(), "application/json");
614 } 606 }
615 607
616 608
617 609
618 extern "C" 610 extern "C"
620 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context) 612 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context)
621 { 613 {
622 Orthanc::Logging::Initialize(context); 614 Orthanc::Logging::Initialize(context);
623 assert(DisplayPerformanceWarning()); 615 assert(DisplayPerformanceWarning());
624 616
617 OrthancPlugins::SetGlobalContext(context);
618
625 /* Check the version of the Orthanc core */ 619 /* Check the version of the Orthanc core */
626 if (OrthancPluginCheckVersion(context) == 0) 620 if (OrthancPluginCheckVersion(context) == 0)
627 { 621 {
628 LOG(ERROR) << "Your version of Orthanc (" 622 LOG(ERROR) << "Your version of Orthanc ("
629 << context->orthancVersion << ") must be above " 623 << context->orthancVersion << ") must be above "
644 size_t maxPushTransactions = 4; 638 size_t maxPushTransactions = 4;
645 size_t memoryCacheSize = 512; // In MB 639 size_t memoryCacheSize = 512; // In MB
646 std::map<std::string, std::string> bidirectionalPeers; 640 std::map<std::string, std::string> bidirectionalPeers;
647 641
648 { 642 {
649 OrthancPlugins::OrthancConfiguration config(context); 643 OrthancPlugins::OrthancConfiguration config;
650 644
651 if (config.IsSection(KEY_PLUGIN_CONFIGURATION)) 645 if (config.IsSection(KEY_PLUGIN_CONFIGURATION))
652 { 646 {
653 OrthancPlugins::OrthancConfiguration plugin; 647 OrthancPlugins::OrthancConfiguration plugin;
654 config.GetSection(plugin, KEY_PLUGIN_CONFIGURATION); 648 config.GetSection(plugin, KEY_PLUGIN_CONFIGURATION);
660 ("MaxPushTransactions", maxPushTransactions); 654 ("MaxPushTransactions", maxPushTransactions);
661 } 655 }
662 } 656 }
663 657
664 OrthancPlugins::PluginContext::Initialize 658 OrthancPlugins::PluginContext::Initialize
665 (context, threadsCount, targetBucketSize * KB, maxPushTransactions, memoryCacheSize * MB); 659 (threadsCount, targetBucketSize * KB, maxPushTransactions, memoryCacheSize * MB);
666 660
667 OrthancPlugins::RegisterRestCallback<ServeChunks> 661 OrthancPlugins::RegisterRestCallback<ServeChunks>
668 (context, std::string(URI_CHUNKS) + "/([.0-9a-f-]+)", true); 662 (std::string(URI_CHUNKS) + "/([.0-9a-f-]+)", true);
669 663
670 OrthancPlugins::RegisterRestCallback<LookupInstances> 664 OrthancPlugins::RegisterRestCallback<LookupInstances>
671 (context, URI_LOOKUP, true); 665 (URI_LOOKUP, true);
672 666
673 OrthancPlugins::RegisterRestCallback<SchedulePull> 667 OrthancPlugins::RegisterRestCallback<SchedulePull>
674 (context, URI_PULL, true); 668 (URI_PULL, true);
675 669
676 OrthancPlugins::RegisterRestCallback<ScheduleSend> 670 OrthancPlugins::RegisterRestCallback<ScheduleSend>
677 (context, URI_SEND, true); 671 (URI_SEND, true);
678 672
679 OrthancPlugins::RegisterRestCallback<ServePeers> 673 OrthancPlugins::RegisterRestCallback<ServePeers>
680 (context, URI_PEERS, true); 674 (URI_PEERS, true);
681 675
682 if (maxPushTransactions != 0) 676 if (maxPushTransactions != 0)
683 { 677 {
684 // If no push transaction is allowed, their URIs are disabled 678 // If no push transaction is allowed, their URIs are disabled
685 OrthancPlugins::RegisterRestCallback<CreatePush> 679 OrthancPlugins::RegisterRestCallback<CreatePush>
686 (context, URI_PUSH, true); 680 (URI_PUSH, true);
687 681
688 OrthancPlugins::RegisterRestCallback<StorePush> 682 OrthancPlugins::RegisterRestCallback<StorePush>
689 (context, std::string(URI_PUSH) + "/([.0-9a-f-]+)/([0-9]+)", true); 683 (std::string(URI_PUSH) + "/([.0-9a-f-]+)/([0-9]+)", true);
690 684
691 OrthancPlugins::RegisterRestCallback<CommitPush> 685 OrthancPlugins::RegisterRestCallback<CommitPush>
692 (context, std::string(URI_PUSH) + "/([.0-9a-f-]+)/commit", true); 686 (std::string(URI_PUSH) + "/([.0-9a-f-]+)/commit", true);
693 687
694 OrthancPlugins::RegisterRestCallback<DiscardPush> 688 OrthancPlugins::RegisterRestCallback<DiscardPush>
695 (context, std::string(URI_PUSH) + "/([.0-9a-f-]+)", true); 689 (std::string(URI_PUSH) + "/([.0-9a-f-]+)", true);
696 } 690 }
697 691
698 OrthancPluginRegisterJobsUnserializer(context, Unserializer); 692 OrthancPluginRegisterJobsUnserializer(context, Unserializer);
699 693
700 /* Extend the default Orthanc Explorer with custom JavaScript */ 694 /* Extend the default Orthanc Explorer with custom JavaScript */