changeset 1137:d9c27f9f1a51

OrthancPluginSetHttpHeader
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 09 Sep 2014 17:33:46 +0200
parents 208dc67b9bab
children 4c4fdee093de
files Core/FileStorage/CompressedFileStorageAccessor.cpp Core/HttpServer/HttpOutput.h Plugins/Engine/OrthancPlugins.cpp Plugins/Engine/OrthancPlugins.h Plugins/OrthancCPlugin/OrthancCPlugin.h Plugins/Samples/Basic/Plugin.c
diffstat 6 files changed, 105 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/Core/FileStorage/CompressedFileStorageAccessor.cpp	Tue Sep 09 16:47:04 2014 +0200
+++ b/Core/FileStorage/CompressedFileStorageAccessor.cpp	Tue Sep 09 17:33:46 2014 +0200
@@ -39,6 +39,7 @@
 #include "../Uuid.h"
 
 #include <memory>
+#include <glog/logging.h>
 
 namespace Orthanc
 {
@@ -112,6 +113,7 @@
   {
     if (storage_ == NULL)
     {
+      LOG(ERROR) << "No storage area is currently available";
       throw OrthancException(ErrorCode_BadSequenceOfCalls);
     }
 
--- a/Core/HttpServer/HttpOutput.h	Tue Sep 09 16:47:04 2014 +0200
+++ b/Core/HttpServer/HttpOutput.h	Tue Sep 09 17:33:46 2014 +0200
@@ -123,6 +123,12 @@
       stateMachine_.SetCookie(cookie, value);
     }
 
+    void AddHeader(const std::string& key,
+                   const std::string& value)
+    {
+      stateMachine_.AddHeader(key, value);
+    }
+
     void SendBody(const void* buffer, size_t length);
 
     void SendBody(const std::string& str);
--- a/Plugins/Engine/OrthancPlugins.cpp	Tue Sep 09 16:47:04 2014 +0200
+++ b/Plugins/Engine/OrthancPlugins.cpp	Tue Sep 09 17:33:46 2014 +0200
@@ -406,11 +406,21 @@
 
   void OrthancPlugins::SetCookie(const void* parameters)
   {
-    const _OrthancPluginSetCookie& p = 
-      *reinterpret_cast<const _OrthancPluginSetCookie*>(parameters);
+    const _OrthancPluginSetHttpHeader& p = 
+      *reinterpret_cast<const _OrthancPluginSetHttpHeader*>(parameters);
 
     HttpOutput* translatedOutput = reinterpret_cast<HttpOutput*>(p.output);
-    translatedOutput->SetCookie(p.cookie, p.value);
+    translatedOutput->SetCookie(p.key, p.value);
+  }
+
+
+  void OrthancPlugins::SetHttpHeader(const void* parameters)
+  {
+    const _OrthancPluginSetHttpHeader& p = 
+      *reinterpret_cast<const _OrthancPluginSetHttpHeader*>(parameters);
+
+    HttpOutput* translatedOutput = reinterpret_cast<HttpOutput*>(p.output);
+    translatedOutput->AddHeader(p.key, p.value);
   }
 
 
@@ -793,6 +803,10 @@
         SetCookie(parameters);
         return true;
 
+      case _OrthancPluginService_SetHttpHeader:
+        SetHttpHeader(parameters);
+        return true;
+
       case _OrthancPluginService_LookupPatient:
       case _OrthancPluginService_LookupStudy:
       case _OrthancPluginService_LookupStudyWithAccessionNumber:
--- a/Plugins/Engine/OrthancPlugins.h	Tue Sep 09 16:47:04 2014 +0200
+++ b/Plugins/Engine/OrthancPlugins.h	Tue Sep 09 17:33:46 2014 +0200
@@ -78,6 +78,8 @@
 
     void SetCookie(const void* parameters);
 
+    void SetHttpHeader(const void* parameters);
+
   public:
     OrthancPlugins(ServerContext& context);
 
--- a/Plugins/OrthancCPlugin/OrthancCPlugin.h	Tue Sep 09 16:47:04 2014 +0200
+++ b/Plugins/OrthancCPlugin/OrthancCPlugin.h	Tue Sep 09 17:33:46 2014 +0200
@@ -251,6 +251,7 @@
     _OrthancPluginService_SendUnauthorized = 2004,
     _OrthancPluginService_SendMethodNotAllowed = 2005,
     _OrthancPluginService_SetCookie = 2006,
+    _OrthancPluginService_SetHttpHeader = 2007,
 
     /* Access to the Orthanc database and API */
     _OrthancPluginService_GetDicomForInstance = 3000,
@@ -1194,9 +1195,9 @@
   typedef struct
   {
     OrthancPluginRestOutput* output;
-    const char*              cookie;
+    const char*              key;
     const char*              value;
-  } _OrthancPluginSetCookie;
+  } _OrthancPluginSetHttpHeader;
 
   /**
    * @brief Set a cookie.
@@ -1214,14 +1215,38 @@
     const char*              cookie,
     const char*              value)
   {
-    _OrthancPluginSetCookie params;
+    _OrthancPluginSetHttpHeader params;
     params.output = output;
-    params.cookie = cookie;
+    params.key = cookie;
     params.value = value;
     context->InvokeService(context, _OrthancPluginService_SetCookie, &params);
   }
 
 
+  /**
+   * @brief Set some HTTP header.
+   *
+   * This function sets a HTTP header in the HTTP answer.
+   * 
+   * @param context The Orthanc plugin context, as received by OrthancPluginInitialize().
+   * @param output The HTTP connection to the client application.
+   * @param key The HTTP header to be set.
+   * @param value The value of the HTTP header.
+   **/
+  ORTHANC_PLUGIN_INLINE void OrthancPluginSetHttpHeader(
+    OrthancPluginContext*    context,
+    OrthancPluginRestOutput* output,
+    const char*              key,
+    const char*              value)
+  {
+    _OrthancPluginSetHttpHeader params;
+    params.output = output;
+    params.key = key;
+    params.value = value;
+    context->InvokeService(context, _OrthancPluginService_SetHttpHeader, &params);
+  }
+
+
   typedef struct
   {
     char**                      resultStringToFree;
@@ -1510,9 +1535,9 @@
     params.remove_ = remove;
 
 #ifdef  __cplusplus
-    params.free_ = free;
+    params.free_ = ::free;
 #else
-    params.free_ = ::free;
+    params.free_ = free;
 #endif
 
     context->InvokeService(context, _OrthancPluginService_RegisterStorageArea, &params);
--- a/Plugins/Samples/Basic/Plugin.c	Tue Sep 09 16:47:04 2014 +0200
+++ b/Plugins/Samples/Basic/Plugin.c	Tue Sep 09 17:33:46 2014 +0200
@@ -165,6 +165,50 @@
 }
 
 
+ORTHANC_PLUGINS_API int32_t CallbackCreateDicom(OrthancPluginRestOutput* output,
+                                                const char* url,
+                                                const OrthancPluginHttpRequest* request)
+{
+  const char* pathLocator = "\"Path\" : \"";
+  char info[1024];
+  char *id, *eos;
+  OrthancPluginMemoryBuffer tmp;
+
+  if (request->method != OrthancPluginHttpMethod_Post)
+  {
+    OrthancPluginSendMethodNotAllowed(context, output, "POST");
+  }
+  else
+  {
+    /* Make POST request to create a new DICOM instance */
+    sprintf(info, "{\"PatientName\":\"Test\"}");
+    OrthancPluginRestApiPost(context, &tmp, "/tools/create-dicom", info, strlen(info));
+
+    /**
+     * Recover the ID of the created instance is constructed by a
+     * quick-and-dirty parsing of a JSON string.
+     **/
+    id = strstr((char*) tmp.data, pathLocator) + strlen(pathLocator);
+    eos = strchr(id, '\"');
+    eos[0] = '\0';
+
+    /* Delete the newly created DICOM instance. */
+    OrthancPluginRestApiDelete(context, id);
+    OrthancPluginFreeMemoryBuffer(context, &tmp);
+
+    /* Set some cookie */
+    OrthancPluginSetCookie(context, output, "hello", "world");
+
+    /* Set some HTTP header */
+    OrthancPluginSetHttpHeader(context, output, "Cache-Control", "max-age=0, no-cache");
+    
+    OrthancPluginAnswerBuffer(context, output, "OK\n", 3, "text/plain");
+  }
+
+  return 0;
+}
+
+
 ORTHANC_PLUGINS_API int32_t OnStoredCallback(OrthancPluginDicomInstance* instance,
                                              const char* instanceId)
 {
@@ -203,10 +247,8 @@
 
 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* c)
 {
-  const char* pathLocator = "\"Path\" : \"";
   OrthancPluginMemoryBuffer tmp;
   char info[1024];
-  char *id, *eos;
 
   context = c;
   OrthancPluginLogWarning(context, "Sample plugin is initializing");
@@ -230,6 +272,7 @@
   OrthancPluginRegisterRestCallback(context, "/plu.*/image", Callback2);
   OrthancPluginRegisterRestCallback(context, "/plugin/instances/([^/]+)/info", Callback3);
   OrthancPluginRegisterRestCallback(context, "/instances/([^/]+)/preview", Callback4);
+  OrthancPluginRegisterRestCallback(context, "/plugin/create", CallbackCreateDicom);
 
   OrthancPluginRegisterOnStoredInstanceCallback(context, OnStoredCallback);
 
@@ -238,27 +281,11 @@
   OrthancPluginFreeMemoryBuffer(context, &tmp);
   OrthancPluginRestApiGet(context, &tmp, "/changes?limit=1");
   OrthancPluginFreeMemoryBuffer(context, &tmp);
- 
-  /* Make POST request to create a new DICOM instance */
-  sprintf(info, "{\"PatientName\":\"Test\"}");
-  OrthancPluginRestApiPost(context, &tmp, "/tools/create-dicom", info, strlen(info));
-
-  /**
-   * Recover he ID of the created instance is constructed by a
-   * quick-and-dirty parsing of a JSON string.
-   **/
-  id = strstr((char*) tmp.data, pathLocator) + strlen(pathLocator);
-  eos = strchr(id, '\"');
-  eos[0] = '\0';
-
-  /* Delete the newly created DICOM instance. */
-  OrthancPluginRestApiDelete(context, id);
-  OrthancPluginFreeMemoryBuffer(context, &tmp);
-
+  
   /* Play with PUT by defining a new target modality. */
   sprintf(info, "[ \"STORESCP\", \"localhost\", 2000 ]");
   OrthancPluginRestApiPut(context, &tmp, "/modalities/demo", info, strlen(info));
-
+ 
   return 0;
 }