changeset 2062:40ffd0e8676a

generation of etag in ServeFolders
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 02 Jul 2016 15:09:03 +0200
parents 8856f15b3e02
children ed383e7a6753
files CMakeLists.txt Plugins/Samples/Common/OrthancPluginCppWrapper.cpp Plugins/Samples/Common/OrthancPluginCppWrapper.h Plugins/Samples/ServeFolders/CMakeLists.txt Plugins/Samples/ServeFolders/Plugin.cpp
diffstat 5 files changed, 49 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Sat Jul 02 11:16:25 2016 +0200
+++ b/CMakeLists.txt	Sat Jul 02 15:09:03 2016 +0200
@@ -546,6 +546,7 @@
     ${BOOST_SOURCES}
     ${JSONCPP_SOURCES}
     Plugins/Samples/ServeFolders/Plugin.cpp
+    Plugins/Samples/Common/OrthancPluginCppWrapper.cpp
     ${SERVE_FOLDERS_RESOURCES}
     )
 
--- a/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp	Sat Jul 02 11:16:25 2016 +0200
+++ b/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp	Sat Jul 02 15:09:03 2016 +0200
@@ -61,6 +61,18 @@
   }
 
 
+  void MemoryBuffer::Check(OrthancPluginErrorCode code)
+  {
+    if (code != OrthancPluginErrorCode_Success)
+    {
+      // Prevent using garbage information
+      buffer_.data = NULL;
+      buffer_.size = 0;
+      throw PluginException(code);
+    }
+  }
+
+
   MemoryBuffer::MemoryBuffer(OrthancPluginContext* context) : 
     context_(context)
   {
@@ -250,7 +262,14 @@
     Json::FastWriter writer;
     std::string s = writer.write(tags);
     
-    PluginException::Check(OrthancPluginCreateDicom(context_, &buffer_, s.c_str(), NULL, flags));
+    Check(OrthancPluginCreateDicom(context_, &buffer_, s.c_str(), NULL, flags));
+  }
+
+
+  void MemoryBuffer::ReadFile(const std::string& path)
+  {
+    Clear();
+    Check(OrthancPluginReadFile(context_, &buffer_, path.c_str()));
   }
 
 
--- a/Plugins/Samples/Common/OrthancPluginCppWrapper.h	Sat Jul 02 11:16:25 2016 +0200
+++ b/Plugins/Samples/Common/OrthancPluginCppWrapper.h	Sat Jul 02 15:09:03 2016 +0200
@@ -76,6 +76,8 @@
     OrthancPluginContext*      context_;
     OrthancPluginMemoryBuffer  buffer_;
 
+    void Check(OrthancPluginErrorCode code);
+
   public:
     MemoryBuffer(OrthancPluginContext* context);
 
@@ -152,6 +154,8 @@
 
     void CreateDicom(const Json::Value& tags,
                      OrthancPluginCreateDicomFlags flags);
+
+    void ReadFile(const std::string& path);
   };
 
 
--- a/Plugins/Samples/ServeFolders/CMakeLists.txt	Sat Jul 02 11:16:25 2016 +0200
+++ b/Plugins/Samples/ServeFolders/CMakeLists.txt	Sat Jul 02 15:09:03 2016 +0200
@@ -16,6 +16,7 @@
 
 add_library(ServeFolders SHARED 
   Plugin.cpp
+  ${CMAKE_SOURCE_DIR}/../Common/OrthancPluginCppWrapper.cpp
   ${JSONCPP_SOURCES}
   ${BOOST_SOURCES}
   )
--- a/Plugins/Samples/ServeFolders/Plugin.cpp	Sat Jul 02 11:16:25 2016 +0200
+++ b/Plugins/Samples/ServeFolders/Plugin.cpp	Sat Jul 02 15:09:03 2016 +0200
@@ -18,17 +18,19 @@
  **/
 
 
-#include <orthanc/OrthancCPlugin.h>
+#include "../Common/OrthancPluginCppWrapper.h"
 
 #include <json/reader.h>
 #include <json/value.h>
 #include <boost/filesystem.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
 
 
 static OrthancPluginContext* context_ = NULL;
 static std::map<std::string, std::string> folders_;
 static const char* INDEX_URI = "/app/plugin-serve-folders.html";
 static bool allowCache_ = true;
+static bool generateETag_ = true;  // TODO parameter
 
 
 static void SetHttpHeaders(OrthancPluginRestOutput* output)
@@ -112,17 +114,17 @@
 static bool ReadFile(std::string& target,
                      const std::string& path)
 {
-  OrthancPluginMemoryBuffer buffer;
-  if (OrthancPluginReadFile(context_, &buffer, path.c_str()))
+  try
+  {
+    OrthancPlugins::MemoryBuffer buffer(context_);
+    buffer.ReadFile(path);
+    buffer.ToString(target);
+    return true;
+  }
+  catch (OrthancPlugins::PluginException)
   {
     return false;
   }
-  else
-  {
-    target.assign(reinterpret_cast<const char*>(buffer.data), buffer.size);
-    OrthancPluginFreeMemoryBuffer(context_, &buffer);
-    return true;
-  }
 }
 
 
@@ -247,6 +249,18 @@
       if (ReadFile(s, path))
       {
         const char* resource = s.size() ? s.c_str() : NULL;
+
+        if (generateETag_)
+        {
+          OrthancPlugins::OrthancString md5(context_, OrthancPluginComputeMd5(context_, resource, s.size()));
+          std::string etag = "\"" + std::string(md5.GetContent()) + "\"";
+          OrthancPluginSetHttpHeader(context_, output, "ETag", etag.c_str());
+        }
+
+        boost::posix_time::ptime lastModification = boost::posix_time::from_time_t(fs::last_write_time(path));
+        std::string t = boost::posix_time::to_iso_string(lastModification);
+        OrthancPluginSetHttpHeader(context_, output, "Last-Modified", t.c_str());
+
         SetHttpHeaders(output);
         OrthancPluginAnswerBuffer(context_, output, resource, s.size(), mime);
       }