changeset 1780:94990da8710e

OrthancPluginRestApiGet2
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 13 Nov 2015 15:06:45 +0100
parents c24dac8c1d4e
children 5ad4e4d92ecb
files Core/HttpServer/HttpToolbox.cpp Core/HttpServer/HttpToolbox.h NEWS Plugins/Engine/OrthancPlugins.cpp Plugins/Engine/OrthancPlugins.h Plugins/Include/orthanc/OrthancCPlugin.h Plugins/Samples/Basic/Plugin.c
diffstat 7 files changed, 108 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/Core/HttpServer/HttpToolbox.cpp	Fri Nov 13 13:20:28 2015 +0100
+++ b/Core/HttpServer/HttpToolbox.cpp	Fri Nov 13 15:06:45 2015 +0100
@@ -201,10 +201,9 @@
   bool HttpToolbox::SimpleGet(std::string& result,
                               IHttpHandler& handler,
                               RequestOrigin origin,
-                              const std::string& uri)
+                              const std::string& uri,
+                              const IHttpHandler::Arguments& httpHeaders)
   {
-    IHttpHandler::Arguments headers;  // No HTTP header
-
     UriComponents curi;
     IHttpHandler::GetArguments getArguments;
     ParseGetQuery(curi, getArguments, uri.c_str());
@@ -213,7 +212,7 @@
     HttpOutput http(stream, false /* no keep alive */);
 
     if (handler.Handle(http, origin, LOCALHOST, "", HttpMethod_Get, curi, 
-                       headers, getArguments, NULL /* no body for GET */, 0))
+                       httpHeaders, getArguments, NULL /* no body for GET */, 0))
     {
       stream.GetOutput(result);
       return true;
@@ -225,6 +224,16 @@
   }
 
 
+  bool HttpToolbox::SimpleGet(std::string& result,
+                              IHttpHandler& handler,
+                              RequestOrigin origin,
+                              const std::string& uri)
+  {
+    IHttpHandler::Arguments headers;  // No HTTP header
+    return SimpleGet(result, handler, origin, uri, headers);
+  }
+
+
   static bool SimplePostOrPut(std::string& result,
                               IHttpHandler& handler,
                               RequestOrigin origin,
--- a/Core/HttpServer/HttpToolbox.h	Fri Nov 13 13:20:28 2015 +0100
+++ b/Core/HttpServer/HttpToolbox.h	Fri Nov 13 15:06:45 2015 +0100
@@ -65,6 +65,12 @@
                           RequestOrigin origin,
                           const std::string& uri);
 
+    static bool SimpleGet(std::string& result,
+                          IHttpHandler& handler,
+                          RequestOrigin origin,
+                          const std::string& uri,
+                          const IHttpHandler::Arguments& httpHeaders);
+
     static bool SimplePost(std::string& result,
                            IHttpHandler& handler,
                            RequestOrigin origin,
--- a/NEWS	Fri Nov 13 13:20:28 2015 +0100
+++ b/NEWS	Fri Nov 13 15:06:45 2015 +0100
@@ -19,8 +19,11 @@
 
 * New function "OrthancPluginRegisterErrorCode()" to declare custom error codes
 * New function "OrthancPluginRegisterDictionaryTag()" to declare custom DICOM tags
+* New function "OrthancPluginRestApiGet2()" to provide HTTP headers when calling Orthanc API
 * New "OrthancStarted", "OrthancStopped", "UpdatedAttachment" 
   and "UpdatedMetadata" events in change callbacks
+* "/system" URI gives information about the plugins used for storage area and DB back-end
+* Plugin callbacks should now return explicit "OrthancPluginErrorCode" instead of integers
 
 Lua
 ---
@@ -33,8 +36,6 @@
 * Full refactoring of the searching features
 * C-Move SCP for studies using AccessionNumber tag
 * Fix issue 4 (C-Store Association not renegotiated on Specific-to-specific transfer syntax change)
-* "/system" URI gives information about the plugins used for storage area and DB back-end
-* Plugin callbacks should now return explicit "OrthancPluginErrorCode" instead of integers
 * "/tools/create-dicom" can create tags with unknown VR
 * "--logdir" flag creates a single log file instead of 3 separate files for errors/warnings/infos
 * "--errors" flag lists the error codes that could be returned by Orthanc
--- a/Plugins/Engine/OrthancPlugins.cpp	Fri Nov 13 13:20:28 2015 +0100
+++ b/Plugins/Engine/OrthancPlugins.cpp	Fri Nov 13 15:06:45 2015 +0100
@@ -819,6 +819,36 @@
   }
 
 
+  void OrthancPlugins::RestApiGet2(const void* parameters)
+  {
+    const _OrthancPluginRestApiGet2& p = 
+      *reinterpret_cast<const _OrthancPluginRestApiGet2*>(parameters);
+        
+    LOG(INFO) << "Plugin making REST GET call on URI " << p.uri
+              << (p.afterPlugins ? " (after plugins)" : " (built-in API)");
+
+    IHttpHandler::Arguments headers;
+
+    for (uint32_t i = 0; i < p.headersCount; i++)
+    {
+      headers[p.headersKeys[i]] = p.headersValues[i];
+    }
+
+    CheckContextAvailable();
+    IHttpHandler& handler = pimpl_->context_->GetHttpHandler().RestrictToOrthancRestApi(!p.afterPlugins);
+
+    std::string result;
+    if (HttpToolbox::SimpleGet(result, handler, RequestOrigin_Plugins, p.uri, headers))
+    {
+      CopyToMemoryBuffer(*p.target, result);
+    }
+    else
+    {
+      throw OrthancException(ErrorCode_BadRequest);
+    }
+  }
+
+
   void OrthancPlugins::RestApiPostPut(bool isPost, 
                                       const void* parameters,
                                       bool afterPlugins)
@@ -1395,6 +1425,10 @@
         RestApiGet(parameters, true);
         return true;
 
+      case _OrthancPluginService_RestApiGet2:
+        RestApiGet2(parameters);
+        return true;
+
       case _OrthancPluginService_RestApiPost:
         RestApiPostPut(true, parameters, false);
         return true;
--- a/Plugins/Engine/OrthancPlugins.h	Fri Nov 13 13:20:28 2015 +0100
+++ b/Plugins/Engine/OrthancPlugins.h	Fri Nov 13 15:06:45 2015 +0100
@@ -91,6 +91,8 @@
     void RestApiGet(const void* parameters,
                     bool afterPlugins);
 
+    void RestApiGet2(const void* parameters);
+
     void RestApiPostPut(bool isPost, 
                         const void* parameters,
                         bool afterPlugins);
--- a/Plugins/Include/orthanc/OrthancCPlugin.h	Fri Nov 13 13:20:28 2015 +0100
+++ b/Plugins/Include/orthanc/OrthancCPlugin.h	Fri Nov 13 15:06:45 2015 +0100
@@ -428,6 +428,7 @@
     _OrthancPluginService_RestApiDeleteAfterPlugins = 3012,
     _OrthancPluginService_RestApiPutAfterPlugins = 3013,
     _OrthancPluginService_ReconstructMainDicomTags = 3014,
+    _OrthancPluginService_RestApiGet2 = 3015,
 
     /* Access to DICOM instances */
     _OrthancPluginService_GetInstanceRemoteAet = 4000,
@@ -3974,6 +3975,55 @@
   }
 
 
+  typedef struct
+  {
+    OrthancPluginMemoryBuffer*  target;
+    const char*                 uri;
+    uint32_t                    headersCount;
+    const char* const*          headersKeys;
+    const char* const*          headersValues;
+    int32_t                     afterPlugins;
+  } _OrthancPluginRestApiGet2;
+
+  /**
+   * @brief Make a GET call to the Orthanc REST API, with custom HTTP headers.
+   * 
+   * Make a GET call to the Orthanc REST API with extended
+   * parameters. The result to the query is stored into a newly
+   * allocated memory buffer.
+   * 
+   * @param context The Orthanc plugin context, as received by OrthancPluginInitialize().
+   * @param target The target memory buffer.
+   * @param uri The URI in the built-in Orthanc API.
+   * @param headersCount The number of HTTP headers.
+   * @param headersKeys Array containing the keys of the HTTP headers.
+   * @param headersValues Array containing the values of the HTTP headers.
+   * @param afterPlugins If 0, the built-in API of Orthanc is used.
+   * If 1, the API is tainted by the plugins.
+   * @return 0 if success, or the error code if failure.
+   * @see OrthancPluginRestApiGet, OrthancPluginRestApiGetAfterPlugins
+   * @ingroup Orthanc
+   **/
+  ORTHANC_PLUGIN_INLINE OrthancPluginErrorCode  OrthancPluginRestApiGet2(
+    OrthancPluginContext*       context,
+    OrthancPluginMemoryBuffer*  target,
+    const char*                 uri,
+    uint32_t                    headersCount,
+    const char* const*          headersKeys,
+    const char* const*          headersValues,
+    int32_t                     afterPlugins)
+  {
+    _OrthancPluginRestApiGet2 params;
+    params.target = target;
+    params.uri = uri;
+    params.headersCount = headersCount;
+    params.headersKeys = headersKeys;
+    params.headersValues = headersValues;
+    params.afterPlugins = afterPlugins;
+
+    return context->InvokeService(context, _OrthancPluginService_RestApiGet2, &params);
+  }
+
 #ifdef  __cplusplus
 }
 #endif
--- a/Plugins/Samples/Basic/Plugin.c	Fri Nov 13 13:20:28 2015 +0100
+++ b/Plugins/Samples/Basic/Plugin.c	Fri Nov 13 15:06:45 2015 +0100
@@ -194,9 +194,7 @@
   }
   else
   {
-    printf("ICI1\n");
     error = OrthancPluginRestApiGetAfterPlugins(context, &tmp, request->groups[1]);
-    printf("ICI2\n");
   }
 
   if (error)