comparison Plugins/Engine/OrthancPlugins.cpp @ 1447:5ba7471780ae

refactoring: HttpToolbox, DumpJson in Lua
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 01 Jul 2015 17:42:06 +0200
parents 8dc80ba768aa
children b737acb13da5
comparison
equal deleted inserted replaced
1446:8dc80ba768aa 1447:5ba7471780ae
640 bool afterPlugins) 640 bool afterPlugins)
641 { 641 {
642 const _OrthancPluginRestApiPostPut& p = 642 const _OrthancPluginRestApiPostPut& p =
643 *reinterpret_cast<const _OrthancPluginRestApiPostPut*>(parameters); 643 *reinterpret_cast<const _OrthancPluginRestApiPostPut*>(parameters);
644 644
645 // TODO : Use "HttpToolbox::SimplePost()" 645 LOG(INFO) << "Plugin making REST " << EnumerationToString(isPost ? HttpMethod_Post : HttpMethod_Put)
646 646 << " call on URI " << p.uri << (afterPlugins ? " (after plugins)" : " (built-in API)");
647 IHttpHandler::Arguments headers; // No HTTP header 647
648 IHttpHandler::GetArguments getArguments; // No GET argument for POST/PUT 648 CheckContextAvailable();
649 649 IHttpHandler& handler = pimpl_->context_->GetHttpHandler().RestrictToOrthancRestApi(!afterPlugins);
650 UriComponents uri; 650
651 Toolbox::SplitUriComponents(uri, p.uri);
652
653 StringHttpOutput stream;
654 HttpOutput http(stream, false /* no keep alive */);
655
656 HttpMethod method = (isPost ? HttpMethod_Post : HttpMethod_Put);
657 LOG(INFO) << "Plugin making REST " << EnumerationToString(method) << " call on URI " << p.uri
658 << (afterPlugins ? " (after plugins)" : " (built-in API)");
659
660 bool ok = false;
661 std::string result; 651 std::string result;
662 652 if (isPost ?
663 if (afterPlugins) 653 HttpToolbox::SimplePost(result, handler, p.uri, p.body, p.bodySize) :
664 { 654 HttpToolbox::SimplePut (result, handler, p.uri, p.body, p.bodySize))
665 ok = Handle(http, method, uri, headers, getArguments, p.body, p.bodySize); 655 {
666 }
667
668 if (!ok)
669 {
670 ok = (pimpl_->restApi_ != NULL &&
671 pimpl_->restApi_->Handle(http, method, uri, headers, getArguments, p.body, p.bodySize));
672 }
673
674 if (ok)
675 {
676 stream.GetOutput(result);
677 CopyToMemoryBuffer(*p.target, result); 656 CopyToMemoryBuffer(*p.target, result);
678 } 657 }
679 else 658 else
680 { 659 {
681 throw OrthancException(ErrorCode_BadRequest); 660 throw OrthancException(ErrorCode_BadRequest);
684 663
685 664
686 void OrthancPlugins::RestApiDelete(const void* parameters, 665 void OrthancPlugins::RestApiDelete(const void* parameters,
687 bool afterPlugins) 666 bool afterPlugins)
688 { 667 {
689 // The "parameters" point to the URI 668 const char* uri = reinterpret_cast<const char*>(parameters);
690 UriComponents uri; 669 LOG(INFO) << "Plugin making REST DELETE call on URI " << uri
691 Toolbox::SplitUriComponents(uri, reinterpret_cast<const char*>(parameters));
692
693 // TODO : Use "HttpToolbox::SimpleDelete()"
694
695 IHttpHandler::Arguments headers; // No HTTP header
696 IHttpHandler::GetArguments getArguments; // No GET argument for POST/PUT
697
698 StringHttpOutput stream;
699 HttpOutput http(stream, false /* no keep alive */);
700
701 LOG(INFO) << "Plugin making REST DELETE call on URI "
702 << reinterpret_cast<const char*>(parameters)
703 << (afterPlugins ? " (after plugins)" : " (built-in API)"); 670 << (afterPlugins ? " (after plugins)" : " (built-in API)");
704 671
705 bool ok = false; 672 CheckContextAvailable();
706 673 IHttpHandler& handler = pimpl_->context_->GetHttpHandler().RestrictToOrthancRestApi(!afterPlugins);
707 if (afterPlugins) 674
708 { 675 if (!HttpToolbox::SimpleDelete(handler, uri))
709 ok = Handle(http, HttpMethod_Delete, uri, headers, getArguments,
710 NULL /* no body for DELETE */, 0);
711 }
712
713 if (!ok)
714 {
715 ok = (pimpl_->restApi_ != NULL &&
716 pimpl_->restApi_->Handle(http, HttpMethod_Delete, uri, headers, getArguments,
717 NULL /* no body for DELETE */, 0));
718 }
719
720 if (!ok)
721 { 676 {
722 throw OrthancException(ErrorCode_BadRequest); 677 throw OrthancException(ErrorCode_BadRequest);
723 } 678 }
724 } 679 }
725 680