changeset 897:bafc9d592632 plugins

REST callbacks are working
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 17 Jun 2014 17:43:39 +0200
parents c4053ac5db04
children 7000fc86fe62
files CMakeLists.txt OrthancServer/main.cpp Plugins/Engine/PluginsHttpHandler.cpp Plugins/Engine/PluginsHttpHandler.h Plugins/Engine/PluginsManager.cpp Plugins/Engine/PluginsManager.h Plugins/OrthancCPlugin/OrthancCPlugin.h Resources/Samples/Plugins/Basic/Plugin.c
diffstat 8 files changed, 267 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Tue Jun 17 09:57:02 2014 +0200
+++ b/CMakeLists.txt	Tue Jun 17 17:43:39 2014 +0200
@@ -117,6 +117,7 @@
 
   Plugins/Engine/SharedLibrary.cpp
   Plugins/Engine/PluginsManager.cpp
+  Plugins/Engine/PluginsHttpHandler.cpp
   )
 
 
--- a/OrthancServer/main.cpp	Tue Jun 17 09:57:02 2014 +0200
+++ b/OrthancServer/main.cpp	Tue Jun 17 17:43:39 2014 +0200
@@ -49,6 +49,7 @@
 #include "OrthancMoveRequestHandler.h"
 #include "ServerToolbox.h"
 #include "../Plugins/Engine/PluginsManager.h"
+#include "../Plugins/Engine/PluginsHttpHandler.h"
 
 using namespace Orthanc;
 
@@ -431,6 +432,8 @@
         httpServer.SetSslEnabled(false);
       }
 
+      httpServer.RegisterHandler(new PluginsHttpHandler(pluginsManager));
+
 #if ORTHANC_STANDALONE == 1
       httpServer.RegisterHandler(new EmbeddedResourceHttpHandler("/app", EmbeddedResources::ORTHANC_EXPLORER));
 #else
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Plugins/Engine/PluginsHttpHandler.cpp	Tue Jun 17 17:43:39 2014 +0200
@@ -0,0 +1,169 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
+ * Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * In addition, as a special exception, the copyright holders of this
+ * program give permission to link the code of its release with the
+ * OpenSSL project's "OpenSSL" library (or with modified versions of it
+ * that use the same license as the "OpenSSL" library), and distribute
+ * the linked executables. You must obey the GNU General Public License
+ * in all respects for all of the code used other than "OpenSSL". If you
+ * modify file(s) with this exception, you may extend this exception to
+ * your version of the file(s), but you are not obligated to do so. If
+ * you do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source files
+ * in the program, then also delete it here.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#include "PluginsHttpHandler.h"
+
+#include "../../Core/OrthancException.h"
+#include "../../Core/Toolbox.h"
+
+#include <boost/regex.hpp> 
+#include <glog/logging.h>
+
+namespace Orthanc
+{
+  struct PluginsHttpHandler::PImpl
+  {
+    typedef std::pair<boost::regex*, OrthancPluginRestCallback> Callback;
+    typedef std::list<Callback>  Callbacks;
+
+    Callbacks callbacks_;
+    OrthancPluginRestCallback currentCallback_;
+
+    PImpl() : currentCallback_(NULL)
+    {
+    }
+  };
+
+
+  PluginsHttpHandler::PluginsHttpHandler(const PluginsManager& manager)
+  {
+    pimpl_.reset(new PImpl);
+
+    for (PluginsManager::RestCallbacks::const_iterator
+           it = manager.GetRestCallbacks().begin(); it != manager.GetRestCallbacks().end(); ++it)
+    {
+      pimpl_->callbacks_.push_back(std::make_pair(new boost::regex(it->first), it->second));
+    }
+  }
+
+  
+  PluginsHttpHandler::~PluginsHttpHandler()
+  {
+    for (PImpl::Callbacks::iterator it = pimpl_->callbacks_.begin(); 
+         it != pimpl_->callbacks_.end(); it++)
+    {
+      delete it->first;
+    }
+  }
+
+
+  bool PluginsHttpHandler::IsServedUri(const UriComponents& uri)
+  {
+    pimpl_->currentCallback_ = NULL;    
+    std::string tmp = Toolbox::FlattenUri(uri);
+
+    for (PImpl::Callbacks::const_iterator it = pimpl_->callbacks_.begin(); 
+         it != pimpl_->callbacks_.end(); it++)
+    {
+      if (boost::regex_match(tmp, *(it->first)))
+      {
+        pimpl_->currentCallback_ = it->second;
+        return true;
+      }
+    }
+
+    return false;
+  }
+
+  bool PluginsHttpHandler::Handle(HttpOutput& output,
+                                  HttpMethod method,
+                                  const UriComponents& uri,
+                                  const Arguments& headers,
+                                  const Arguments& getArguments,
+                                  const std::string& postData)
+  {
+    std::string flatUri = Toolbox::FlattenUri(uri);
+
+    LOG(INFO) << "Delegating HTTP request to plugin for URI: " << flatUri;
+
+    std::vector<const char*> getKeys(getArguments.size());
+    std::vector<const char*> getValues(getArguments.size());
+
+    OrthancPluginHttpMethod methodPlugin;
+    switch (method)
+    {
+      case HttpMethod_Get:
+      {
+        methodPlugin = OrthancPluginHttpMethod_Get;
+
+        size_t i = 0;
+        for (Arguments::const_iterator it = getArguments.begin(); 
+             it != getArguments.end(); it++, i++)
+        {
+          getKeys[i] = it->first.c_str();
+          getValues[i] = it->second.c_str();
+        }
+
+        break;
+      }
+
+      case HttpMethod_Post:
+        methodPlugin = OrthancPluginHttpMethod_Post;
+        break;
+
+      case HttpMethod_Delete:
+        methodPlugin = OrthancPluginHttpMethod_Delete;
+        break;
+
+      case HttpMethod_Put:
+        methodPlugin = OrthancPluginHttpMethod_Put;
+        break;
+
+      default:
+        throw OrthancException(ErrorCode_InternalError);
+    }
+
+    assert(pimpl_->currentCallback_ != NULL);
+    assert(getKeys.size() == getValues.size());
+    int32_t error = (*pimpl_->currentCallback_) (reinterpret_cast<OrthancPluginRestOutput*>(&output), 
+                                                 methodPlugin, flatUri.c_str(), 
+                                                 getKeys.size() ? &getKeys[0] : NULL,
+                                                 getKeys.size() ? &getValues[0] : NULL,
+                                                 getKeys.size(), 
+                                                 postData.size() ? &postData[0] : NULL, postData.size());
+
+    if (error < 0)
+    {
+      LOG(ERROR) << "Plugin failed with error code " << error;
+      return false;
+    }
+    else
+    {
+      if (error > 0)
+      {
+        LOG(WARNING) << "Plugin finished with warning code " << error;
+      }
+
+      return true;
+    }
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Plugins/Engine/PluginsHttpHandler.h	Tue Jun 17 17:43:39 2014 +0200
@@ -0,0 +1,64 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
+ * Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * In addition, as a special exception, the copyright holders of this
+ * program give permission to link the code of its release with the
+ * OpenSSL project's "OpenSSL" library (or with modified versions of it
+ * that use the same license as the "OpenSSL" library), and distribute
+ * the linked executables. You must obey the GNU General Public License
+ * in all respects for all of the code used other than "OpenSSL". If you
+ * modify file(s) with this exception, you may extend this exception to
+ * your version of the file(s), but you are not obligated to do so. If
+ * you do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source files
+ * in the program, then also delete it here.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#pragma once
+
+#include "PluginsManager.h"
+#include "../../Core/HttpServer/HttpHandler.h"
+
+#include <list>
+#include <boost/shared_ptr.hpp>
+
+namespace Orthanc
+{
+  class PluginsHttpHandler : public HttpHandler
+  {
+  private:
+    struct PImpl;
+
+    boost::shared_ptr<PImpl> pimpl_;
+
+  public:
+    PluginsHttpHandler(const PluginsManager& manager);
+
+    virtual ~PluginsHttpHandler();
+
+    virtual bool IsServedUri(const UriComponents& uri);
+
+    virtual bool Handle(HttpOutput& output,
+                        HttpMethod method,
+                        const UriComponents& uri,
+                        const Arguments& headers,
+                        const Arguments& getArguments,
+                        const std::string& postData);
+  };
+}
--- a/Plugins/Engine/PluginsManager.cpp	Tue Jun 17 09:57:02 2014 +0200
+++ b/Plugins/Engine/PluginsManager.cpp	Tue Jun 17 17:43:39 2014 +0200
@@ -33,6 +33,7 @@
 #include "PluginsManager.h"
 
 #include "../../Core/Toolbox.h"
+#include "../../Core/HttpServer/HttpOutput.h"
 
 #include <glog/logging.h>
 #include <cassert>
@@ -150,13 +151,9 @@
                                             const char* pathRegularExpression, 
                                             OrthancPluginRestCallback callback)
   {
-    // TODO
     LOG(INFO) << "Plugin has registered a REST callback on: " << pathRegularExpression;
-
     PluginsManager* manager = reinterpret_cast<PluginsManager*>(context->pimpl);
-    manager->restCallbacks_.push_back(callback);
-
-    callback(NULL, OrthancPluginHttpMethod_Get, "/hello/world", NULL, 0);
+    manager->restCallbacks_.push_back(std::make_pair(pathRegularExpression, callback));
   }
 
 
@@ -165,7 +162,8 @@
                            uint32_t answerSize,
                            const char* mimeType)
   {
-    std::cout << "MIME " << mimeType << ": " << answer << std::endl;
+    HttpOutput* translatedOutput = reinterpret_cast<HttpOutput*>(output);
+    translatedOutput->AnswerBufferWithContentType(answer, answerSize, mimeType);
   }
 
 
--- a/Plugins/Engine/PluginsManager.h	Tue Jun 17 09:57:02 2014 +0200
+++ b/Plugins/Engine/PluginsManager.h	Tue Jun 17 17:43:39 2014 +0200
@@ -42,10 +42,11 @@
 {
   class PluginsManager : boost::noncopyable
   {
+  public:
+    typedef std::list< std::pair<std::string, OrthancPluginRestCallback> >  RestCallbacks;
+
   private:
     typedef std::map<std::string, SharedLibrary*>  Plugins;
-    typedef std::list<OrthancPluginRestCallback>  RestCallbacks;
-
     OrthancPluginContext  context_;
     Plugins  plugins_;
     RestCallbacks  restCallbacks_;
@@ -63,5 +64,10 @@
 
     void ScanFolderForPlugins(const std::string& path,
                               bool isRecursive);
+
+    const RestCallbacks& GetRestCallbacks() const
+    {
+      return restCallbacks_;
+    }
   };
 }
--- a/Plugins/OrthancCPlugin/OrthancCPlugin.h	Tue Jun 17 09:57:02 2014 +0200
+++ b/Plugins/OrthancCPlugin/OrthancCPlugin.h	Tue Jun 17 17:43:39 2014 +0200
@@ -71,8 +71,10 @@
   typedef int32_t (*OrthancPluginRestCallback) (OrthancPluginRestOutput* output,
                                                 OrthancPluginHttpMethod method,
                                                 const char* url,
-                                                const char* body,
-                                                uint32_t bodySize);
+                                                const char* const* getKeys,
+                                                const char* const* getValues,
+                                                uint32_t getSize,
+                                                const char* body, uint32_t bodySize);
 
   typedef struct OrthancPluginContext_t
   {
--- a/Resources/Samples/Plugins/Basic/Plugin.c	Tue Jun 17 09:57:02 2014 +0200
+++ b/Resources/Samples/Plugins/Basic/Plugin.c	Tue Jun 17 17:43:39 2014 +0200
@@ -36,13 +36,26 @@
 ORTHANC_PLUGINS_API int32_t Callback(OrthancPluginRestOutput* output,
                                      OrthancPluginHttpMethod method,
                                      const char* url,
+                                     const char* const* getKeys,
+                                     const char* const* getValues,
+                                     uint32_t getSize,
                                      const char* body,
                                      uint32_t bodySize)
 {
   char buffer[1024];
-  sprintf(buffer, "Callback on URL [%s]\n", url);
+  uint32_t i;
+
+  sprintf(buffer, "Callback on URL [%s] with body [%s]", url, body);
   context->LogInfo(buffer);
+
   context->AnswerBuffer(output, buffer, strlen(buffer), "text/plain");
+
+  for (i = 0; i < getSize; i++)
+  {
+    sprintf(buffer, "  [%s] = [%s]", getKeys[i], getValues[i]);
+    context->LogInfo(buffer);    
+  }
+
   return 1;
 }