changeset 3433:caa526bb65cc

fixed C++ compatibility wrapper for OrthancPluginRegisterRestCallbackNoLock()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 18 Jun 2019 09:10:27 +0200
parents 297ad330900c
children 6503ab9489ba
files Plugins/Samples/Common/OrthancPluginCppWrapper.cpp Plugins/Samples/Common/OrthancPluginCppWrapper.h
diffstat 2 files changed, 134 insertions(+), 110 deletions(-) [+]
line wrap: on
line diff
--- a/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp	Mon Jun 17 23:37:07 2019 +0200
+++ b/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp	Tue Jun 18 09:10:27 2019 +0200
@@ -2754,125 +2754,150 @@
 
 #else
     
-    void ChunkedRestCompatibility(OrthancPluginRestOutput* output,
-                                  const char* url,
-                                  const OrthancPluginHttpRequest* request,
-                                  RestCallback         GetHandler,
-                                  ChunkedRestCallback  PostHandler,
-                                  RestCallback         DeleteHandler,
-                                  ChunkedRestCallback  PutHandler)
+    OrthancPluginErrorCode ChunkedRestCompatibility(OrthancPluginRestOutput* output,
+                                                    const char* url,
+                                                    const OrthancPluginHttpRequest* request,
+                                                    RestCallback         GetHandler,
+                                                    ChunkedRestCallback  PostHandler,
+                                                    RestCallback         DeleteHandler,
+                                                    ChunkedRestCallback  PutHandler)
     {
-      std::string allowed;
-
-      if (GetHandler != Internals::NullRestCallback)
+      try
       {
-        allowed += "GET";
-      }
-
-      if (PostHandler != Internals::NullChunkedRestCallback)
-      {
-        if (!allowed.empty())
+        std::string allowed;
+
+        if (GetHandler != Internals::NullRestCallback)
         {
-          allowed += ",";
+          allowed += "GET";
         }
-        
-        allowed += "POST";
-      }
-
-      if (DeleteHandler != Internals::NullRestCallback)
-      {
-        if (!allowed.empty())
+
+        if (PostHandler != Internals::NullChunkedRestCallback)
         {
-          allowed += ",";
-        }
+          if (!allowed.empty())
+          {
+            allowed += ",";
+          }
         
-        allowed += "DELETE";
-      }
-
-      if (PutHandler != Internals::NullChunkedRestCallback)
-      {
-        if (!allowed.empty())
-        {
-          allowed += ",";
+          allowed += "POST";
         }
+
+        if (DeleteHandler != Internals::NullRestCallback)
+        {
+          if (!allowed.empty())
+          {
+            allowed += ",";
+          }
         
-        allowed += "PUT";
-      }
-      
-      switch (request->method)
-      {
-        case OrthancPluginHttpMethod_Get:
+          allowed += "DELETE";
+        }
+
+        if (PutHandler != Internals::NullChunkedRestCallback)
         {
-          if (GetHandler == Internals::NullRestCallback)
+          if (!allowed.empty())
           {
-            OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str());
+            allowed += ",";
           }
-          else
-          {
-            GetHandler(output, url, request);
-          }
-          return;
+        
+          allowed += "PUT";
         }
-
-        case OrthancPluginHttpMethod_Post:
+      
+        switch (request->method)
         {
-          if (PostHandler == Internals::NullChunkedRestCallback)
-          {
-            OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str());
-          }
-          else
-          {
-            std::auto_ptr<IChunkedRequestReader> reader(PostHandler(url, request));
-            if (reader.get() == NULL)
+          case OrthancPluginHttpMethod_Get:
+            if (GetHandler == Internals::NullRestCallback)
+            {
+              OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str());
+            }
+            else
             {
-              ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin);
+              GetHandler(output, url, request);
+            }
+
+            break;
+
+          case OrthancPluginHttpMethod_Post:
+            if (PostHandler == Internals::NullChunkedRestCallback)
+            {
+              OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str());
             }
             else
             {
-              reader->AddChunk(request->body, request->bodySize);
-              reader->Execute(output);
+              std::auto_ptr<IChunkedRequestReader> reader(PostHandler(url, request));
+              if (reader.get() == NULL)
+              {
+                ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin);
+              }
+              else
+              {
+                reader->AddChunk(request->body, request->bodySize);
+                reader->Execute(output);
+              }
             }
-          }
-          return;
-        }
-
-        case OrthancPluginHttpMethod_Delete:
-        {
-          if (DeleteHandler == Internals::NullRestCallback)
-          {
-            OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str());
-          }
-          else
-          {
-            DeleteHandler(output, url, request);
-          }
-          return;
-        }
-
-        case OrthancPluginHttpMethod_Put:
-        {
-          if (PutHandler == Internals::NullChunkedRestCallback)
-          {
-            OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str());
-          }
-          else
-          {
-            std::auto_ptr<IChunkedRequestReader> reader(PutHandler(url, request));
-            if (reader.get() == NULL)
+
+            break;
+
+          case OrthancPluginHttpMethod_Delete:
+            if (DeleteHandler == Internals::NullRestCallback)
+            {
+              OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str());
+            }
+            else
             {
-              ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin);
+              DeleteHandler(output, url, request);
+            }
+
+            break;
+
+          case OrthancPluginHttpMethod_Put:
+            if (PutHandler == Internals::NullChunkedRestCallback)
+            {
+              OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str());
             }
             else
             {
-              reader->AddChunk(request->body, request->bodySize);
-              reader->Execute(output);
+              std::auto_ptr<IChunkedRequestReader> reader(PutHandler(url, request));
+              if (reader.get() == NULL)
+              {
+                ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin);
+              }
+              else
+              {
+                reader->AddChunk(request->body, request->bodySize);
+                reader->Execute(output);
+              }
             }
-          }
-          return;
+
+            break;
+
+          default:
+            ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
         }
 
-        default:
-          ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
+        return OrthancPluginErrorCode_Success;
+      }
+      catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e)
+      {
+#if HAS_ORTHANC_EXCEPTION == 1 && HAS_ORTHANC_PLUGIN_EXCEPTION_DETAILS == 1
+        if (HasGlobalContext() &&
+            e.HasDetails())
+        {
+          // The "false" instructs Orthanc not to log the detailed
+          // error message. This is to avoid duplicating the details,
+          // because "OrthancException" already does it on construction.
+          OrthancPluginSetHttpErrorDetails
+            (GetGlobalContext(), output, e.GetDetails(), false);
+        }
+#endif
+
+        return static_cast<OrthancPluginErrorCode>(e.GetErrorCode());
+      }
+      catch (boost::bad_lexical_cast&)
+      {
+        return OrthancPluginErrorCode_BadFileFormat;
+      }
+      catch (...)
+      {
+        return OrthancPluginErrorCode_Plugin;
       }
     }
 #endif
--- a/Plugins/Samples/Common/OrthancPluginCppWrapper.h	Mon Jun 17 23:37:07 2019 +0200
+++ b/Plugins/Samples/Common/OrthancPluginCppWrapper.h	Tue Jun 18 09:10:27 2019 +0200
@@ -1014,14 +1014,14 @@
       OrthancPluginServerChunkedRequestReader* reader);
 
 #else  
-    
-    void ChunkedRestCompatibility(OrthancPluginRestOutput* output,
-                                  const char* url,
-                                  const OrthancPluginHttpRequest* request,
-                                  RestCallback GetHandler,
-                                  ChunkedRestCallback PostHandler,
-                                  RestCallback DeleteHandler,
-                                  ChunkedRestCallback PutHandler);
+
+    OrthancPluginErrorCode ChunkedRestCompatibility(OrthancPluginRestOutput* output,
+                                                    const char* url,
+                                                    const OrthancPluginHttpRequest* request,
+                                                    RestCallback GetHandler,
+                                                    ChunkedRestCallback PostHandler,
+                                                    RestCallback DeleteHandler,
+                                                    ChunkedRestCallback PutHandler);
 
     template<
       RestCallback         GetHandler,
@@ -1029,12 +1029,12 @@
       RestCallback         DeleteHandler,
       ChunkedRestCallback  PutHandler
       >
-    static void ChunkedRestCompatibility(OrthancPluginRestOutput* output,
-                                         const char* url,
-                                         const OrthancPluginHttpRequest* request)
+    inline OrthancPluginErrorCode ChunkedRestCompatibility(OrthancPluginRestOutput* output,
+                                                           const char* url,
+                                                           const OrthancPluginHttpRequest* request)
     {
-      ChunkedRestCompatibility(output, url, request, GetHandler,
-                               PostHandler, DeleteHandler, PutHandler);
+      return ChunkedRestCompatibility(output, url, request, GetHandler,
+                                      PostHandler, DeleteHandler, PutHandler);
     }
 #endif
   }
@@ -1068,10 +1068,9 @@
       LogWarning("Performance warning: The plugin was compiled against a pre-1.5.7 version "
                  "of the Orthanc SDK. Multipart transfers will be entirely stored in RAM.");
       
-      OrthancPluginRegisterRestCallback(
+      OrthancPluginRegisterRestCallbackNoLock(
         GetGlobalContext(), uri.c_str(), 
-        Internals::Protect< Internals::ChunkedRestCompatibility<
-        GetHandler, PostHandler, DeleteHandler, PutHandler> >);
+        Internals::ChunkedRestCompatibility<GetHandler, PostHandler, DeleteHandler, PutHandler>);
 #endif
     }
   };