comparison Plugins/Engine/OrthancPlugins.cpp @ 1282:7bccdd221e2b

Plugins can do REST calls to other plugins
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 03 Feb 2015 11:52:21 +0100
parents d6a65dc6d0ac
children 6e7e5ed91c2d
comparison
equal deleted inserted replaced
1281:8dac11c78d71 1282:7bccdd221e2b
185 OrthancRestApi* restApi_; 185 OrthancRestApi* restApi_;
186 OnStoredCallbacks onStoredCallbacks_; 186 OnStoredCallbacks onStoredCallbacks_;
187 OnChangeCallbacks onChangeCallbacks_; 187 OnChangeCallbacks onChangeCallbacks_;
188 bool hasStorageArea_; 188 bool hasStorageArea_;
189 _OrthancPluginRegisterStorageArea storageArea_; 189 _OrthancPluginRegisterStorageArea storageArea_;
190 boost::mutex callbackMutex_; 190 boost::recursive_mutex callbackMutex_;
191 SharedMessageQueue pendingChanges_; 191 SharedMessageQueue pendingChanges_;
192 boost::thread changeThread_; 192 boost::thread changeThread_;
193 bool done_; 193 bool done_;
194 Properties properties_; 194 Properties properties_;
195 int argc_; 195 int argc_;
213 { 213 {
214 std::auto_ptr<IDynamicObject> obj(that->pendingChanges_.Dequeue(500)); 214 std::auto_ptr<IDynamicObject> obj(that->pendingChanges_.Dequeue(500));
215 215
216 if (obj.get() != NULL) 216 if (obj.get() != NULL)
217 { 217 {
218 boost::mutex::scoped_lock lock(that->callbackMutex_); 218 boost::recursive_mutex::scoped_lock lock(that->callbackMutex_);
219 PendingChange& change = *dynamic_cast<PendingChange*>(obj.get()); 219 PendingChange& change = *dynamic_cast<PendingChange*>(obj.get());
220 change.Submit(that->onChangeCallbacks_); 220 change.Submit(that->onChangeCallbacks_);
221 } 221 }
222 } 222 }
223 } 223 }
395 395
396 assert(callback != NULL); 396 assert(callback != NULL);
397 int32_t error; 397 int32_t error;
398 398
399 { 399 {
400 boost::mutex::scoped_lock lock(pimpl_->callbackMutex_); 400 boost::recursive_mutex::scoped_lock lock(pimpl_->callbackMutex_);
401 error = callback(reinterpret_cast<OrthancPluginRestOutput*>(&output), 401 error = callback(reinterpret_cast<OrthancPluginRestOutput*>(&output),
402 flatUri.c_str(), 402 flatUri.c_str(),
403 &request); 403 &request);
404 } 404 }
405 405
421 421
422 422
423 void OrthancPlugins::SignalStoredInstance(DicomInstanceToStore& instance, 423 void OrthancPlugins::SignalStoredInstance(DicomInstanceToStore& instance,
424 const std::string& instanceId) 424 const std::string& instanceId)
425 { 425 {
426 boost::mutex::scoped_lock lock(pimpl_->callbackMutex_); 426 boost::recursive_mutex::scoped_lock lock(pimpl_->callbackMutex_);
427 427
428 for (PImpl::OnStoredCallbacks::const_iterator 428 for (PImpl::OnStoredCallbacks::const_iterator
429 callback = pimpl_->onStoredCallbacks_.begin(); 429 callback = pimpl_->onStoredCallbacks_.begin();
430 callback != pimpl_->onStoredCallbacks_.end(); ++callback) 430 callback != pimpl_->onStoredCallbacks_.end(); ++callback)
431 { 431 {
649 pimpl_->context_.ReadFile(dicom, p.instanceId, FileContentType_Dicom); 649 pimpl_->context_.ReadFile(dicom, p.instanceId, FileContentType_Dicom);
650 CopyToMemoryBuffer(*p.target, dicom); 650 CopyToMemoryBuffer(*p.target, dicom);
651 } 651 }
652 652
653 653
654 void OrthancPlugins::RestApiGet(const void* parameters) 654 void OrthancPlugins::RestApiGet(const void* parameters,
655 bool afterPlugins)
655 { 656 {
656 const _OrthancPluginRestApiGet& p = 657 const _OrthancPluginRestApiGet& p =
657 *reinterpret_cast<const _OrthancPluginRestApiGet*>(parameters); 658 *reinterpret_cast<const _OrthancPluginRestApiGet*>(parameters);
658 659
659 HttpHandler::Arguments headers; // No HTTP header 660 HttpHandler::Arguments headers; // No HTTP header
664 HttpHandler::ParseGetQuery(uri, getArguments, p.uri); 665 HttpHandler::ParseGetQuery(uri, getArguments, p.uri);
665 666
666 StringHttpOutput stream; 667 StringHttpOutput stream;
667 HttpOutput http(stream, false /* no keep alive */); 668 HttpOutput http(stream, false /* no keep alive */);
668 669
669 LOG(INFO) << "Plugin making REST GET call on URI " << p.uri; 670 LOG(INFO) << "Plugin making REST GET call on URI " << p.uri
670 671 << (afterPlugins ? " (after plugins)" : " (built-in API)");
671 if (pimpl_->restApi_ != NULL && 672
672 pimpl_->restApi_->Handle(http, HttpMethod_Get, uri, headers, getArguments, body)) 673 bool ok = false;
673 { 674 std::string result;
674 std::string result; 675
676 if (afterPlugins)
677 {
678 ok = Handle(http, HttpMethod_Get, uri, headers, getArguments, body);
679 }
680
681 if (!ok)
682 {
683 ok = (pimpl_->restApi_ != NULL &&
684 pimpl_->restApi_->Handle(http, HttpMethod_Get, uri, headers, getArguments, body));
685 }
686
687 if (ok)
688 {
675 stream.GetOutput(result); 689 stream.GetOutput(result);
676 CopyToMemoryBuffer(*p.target, result); 690 CopyToMemoryBuffer(*p.target, result);
677 } 691 }
678 else 692 else
679 { 693 {
680 throw OrthancException(ErrorCode_BadRequest); 694 throw OrthancException(ErrorCode_BadRequest);
681 } 695 }
682 } 696 }
683 697
684 698
685 void OrthancPlugins::RestApiPostPut(bool isPost, const void* parameters) 699 void OrthancPlugins::RestApiPostPut(bool isPost,
700 const void* parameters,
701 bool afterPlugins)
686 { 702 {
687 const _OrthancPluginRestApiPostPut& p = 703 const _OrthancPluginRestApiPostPut& p =
688 *reinterpret_cast<const _OrthancPluginRestApiPostPut*>(parameters); 704 *reinterpret_cast<const _OrthancPluginRestApiPostPut*>(parameters);
689 705
690 HttpHandler::Arguments headers; // No HTTP header 706 HttpHandler::Arguments headers; // No HTTP header
698 714
699 StringHttpOutput stream; 715 StringHttpOutput stream;
700 HttpOutput http(stream, false /* no keep alive */); 716 HttpOutput http(stream, false /* no keep alive */);
701 717
702 HttpMethod method = (isPost ? HttpMethod_Post : HttpMethod_Put); 718 HttpMethod method = (isPost ? HttpMethod_Post : HttpMethod_Put);
703 LOG(INFO) << "Plugin making REST " << EnumerationToString(method) << " call on URI " << p.uri; 719 LOG(INFO) << "Plugin making REST " << EnumerationToString(method) << " call on URI " << p.uri
704 720 << (afterPlugins ? " (after plugins)" : " (built-in API)");
705 if (pimpl_->restApi_ != NULL && 721
706 pimpl_->restApi_->Handle(http, method, uri, headers, getArguments, body)) 722 bool ok = false;
707 { 723 std::string result;
708 std::string result; 724
725 if (afterPlugins)
726 {
727 ok = Handle(http, method, uri, headers, getArguments, body);
728 }
729
730 if (!ok)
731 {
732 ok = (pimpl_->restApi_ != NULL &&
733 pimpl_->restApi_->Handle(http, method, uri, headers, getArguments, body));
734 }
735
736 if (ok)
737 {
709 stream.GetOutput(result); 738 stream.GetOutput(result);
710 CopyToMemoryBuffer(*p.target, result); 739 CopyToMemoryBuffer(*p.target, result);
711 } 740 }
712 else 741 else
713 { 742 {
714 throw OrthancException(ErrorCode_BadRequest); 743 throw OrthancException(ErrorCode_BadRequest);
715 } 744 }
716 } 745 }
717 746
718 747
719 void OrthancPlugins::RestApiDelete(const void* parameters) 748 void OrthancPlugins::RestApiDelete(const void* parameters,
749 bool afterPlugins)
720 { 750 {
721 // The "parameters" point to the URI 751 // The "parameters" point to the URI
722 UriComponents uri; 752 UriComponents uri;
723 Toolbox::SplitUriComponents(uri, reinterpret_cast<const char*>(parameters)); 753 Toolbox::SplitUriComponents(uri, reinterpret_cast<const char*>(parameters));
724 754
728 758
729 StringHttpOutput stream; 759 StringHttpOutput stream;
730 HttpOutput http(stream, false /* no keep alive */); 760 HttpOutput http(stream, false /* no keep alive */);
731 761
732 LOG(INFO) << "Plugin making REST DELETE call on URI " 762 LOG(INFO) << "Plugin making REST DELETE call on URI "
733 << reinterpret_cast<const char*>(parameters); 763 << reinterpret_cast<const char*>(parameters)
734 764 << (afterPlugins ? " (after plugins)" : " (built-in API)");
735 if (pimpl_->restApi_ == NULL || 765
736 !pimpl_->restApi_->Handle(http, HttpMethod_Delete, uri, headers, getArguments, body)) 766 bool ok = false;
767
768 if (afterPlugins)
769 {
770 ok = Handle(http, HttpMethod_Delete, uri, headers, getArguments, body);
771 }
772
773 if (!ok)
774 {
775 ok = (pimpl_->restApi_ != NULL &&
776 pimpl_->restApi_->Handle(http, HttpMethod_Delete, uri, headers, getArguments, body));
777 }
778
779 if (!ok)
737 { 780 {
738 throw OrthancException(ErrorCode_BadRequest); 781 throw OrthancException(ErrorCode_BadRequest);
739 } 782 }
740 } 783 }
741 784
961 case _OrthancPluginService_GetDicomForInstance: 1004 case _OrthancPluginService_GetDicomForInstance:
962 GetDicomForInstance(parameters); 1005 GetDicomForInstance(parameters);
963 return true; 1006 return true;
964 1007
965 case _OrthancPluginService_RestApiGet: 1008 case _OrthancPluginService_RestApiGet:
966 RestApiGet(parameters); 1009 RestApiGet(parameters, false);
1010 return true;
1011
1012 case _OrthancPluginService_RestApiGetAfterPlugins:
1013 RestApiGet(parameters, true);
967 return true; 1014 return true;
968 1015
969 case _OrthancPluginService_RestApiPost: 1016 case _OrthancPluginService_RestApiPost:
970 RestApiPostPut(true, parameters); 1017 RestApiPostPut(true, parameters, false);
1018 return true;
1019
1020 case _OrthancPluginService_RestApiPostAfterPlugins:
1021 RestApiPostPut(true, parameters, true);
971 return true; 1022 return true;
972 1023
973 case _OrthancPluginService_RestApiDelete: 1024 case _OrthancPluginService_RestApiDelete:
974 RestApiDelete(parameters); 1025 RestApiDelete(parameters, false);
1026 return true;
1027
1028 case _OrthancPluginService_RestApiDeleteAfterPlugins:
1029 RestApiDelete(parameters, true);
975 return true; 1030 return true;
976 1031
977 case _OrthancPluginService_RestApiPut: 1032 case _OrthancPluginService_RestApiPut:
978 RestApiPostPut(false, parameters); 1033 RestApiPostPut(false, parameters, false);
1034 return true;
1035
1036 case _OrthancPluginService_RestApiPutAfterPlugins:
1037 RestApiPostPut(false, parameters, true);
979 return true; 1038 return true;
980 1039
981 case _OrthancPluginService_Redirect: 1040 case _OrthancPluginService_Redirect:
982 Redirect(parameters); 1041 Redirect(parameters);
983 return true; 1042 return true;