changeset 226:8a26a8e85edf

refactoring to read files
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 30 Nov 2012 09:45:29 +0100
parents 03aa59ecf6d8
children 209ca3f6db62
files OrthancServer/OrthancRestApi.h OrthancServer/OrthancRestApi2.cpp OrthancServer/ServerContext.cpp OrthancServer/ServerContext.h OrthancServer/ServerIndex.cpp OrthancServer/ServerIndex.h OrthancServer/ServerToolbox.cpp OrthancServer/ServerToolbox.h
diffstat 8 files changed, 92 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/OrthancRestApi.h	Thu Nov 29 22:28:05 2012 +0100
+++ b/OrthancServer/OrthancRestApi.h	Fri Nov 30 09:45:29 2012 +0100
@@ -35,6 +35,7 @@
 #include "../Core/HttpServer/HttpHandler.h"
 #include "ServerIndex.h"
 #include "DicomProtocol/DicomUserConnection.h"
+#include "../Core/FileStorage.h"
 
 #include <set>
 
--- a/OrthancServer/OrthancRestApi2.cpp	Thu Nov 29 22:28:05 2012 +0100
+++ b/OrthancServer/OrthancRestApi2.cpp	Fri Nov 30 09:45:29 2012 +0100
@@ -45,7 +45,8 @@
 
 
 #define RETRIEVE_CONTEXT(call)                                          \
-  OrthancRestApi2& contextApi = dynamic_cast<OrthancRestApi2&>(call.GetContext()); \
+  OrthancRestApi2& contextApi =                                         \
+    dynamic_cast<OrthancRestApi2&>(call.GetContext());                  \
   ServerContext& context = contextApi.GetContext()
 
 
@@ -150,19 +151,8 @@
   {
     RETRIEVE_CONTEXT(call);
 
-    CompressionType compressionType;
-    std::string fileUuid;
     std::string publicId = call.GetUriComponent("id", "");
-
-    if (context.GetIndex().GetFile(fileUuid, compressionType, publicId, AttachedFileType_Dicom))
-    {
-      assert(compressionType == CompressionType_None);
-
-      FilesystemHttpSender sender(context.GetFileStorage(), fileUuid);
-      sender.SetDownloadFilename(fileUuid + ".dcm");
-      sender.SetContentType("application/dicom");
-      call.GetOutput().AnswerFile(sender);
-    }
+    context.AnswerFile(call.GetOutput(), publicId, AttachedFileType_Dicom);
   }
 
 
@@ -171,27 +161,20 @@
   {
     RETRIEVE_CONTEXT(call);
 
-    CompressionType compressionType;
-    std::string fileUuid;
     std::string publicId = call.GetUriComponent("id", "");
-
-    if (context.GetIndex().GetFile(fileUuid, compressionType, publicId, AttachedFileType_Json))
-    {
-      assert(compressionType == CompressionType_None);
-
-      Json::Value full;
-      ReadJson(full, context.GetFileStorage(), fileUuid);
+    
+    Json::Value full;
+    context.ReadJson(full, publicId);
 
-      if (simplify)
-      {
-        Json::Value simplified;
-        SimplifyTags(simplified, full);
-        call.GetOutput().AnswerJson(simplified);
-      }
-      else
-      {
-        call.GetOutput().AnswerJson(full);
-      }
+    if (simplify)
+    {
+      Json::Value simplified;
+      SimplifyTags(simplified, full);
+      call.GetOutput().AnswerJson(simplified);
+    }
+    else
+    {
+      call.GetOutput().AnswerJson(full);
     }
   }
 
@@ -259,7 +242,12 @@
       }
       catch (OrthancException& e)
       {
-        //if (e.GetErrorCode() == ErrorCode_NotImplemented)
+        if (e.GetErrorCode() == ErrorCode_ParameterOutOfRange)
+        {
+          // The frame number is out of the range for this DICOM
+          // instance, the resource is not existent
+        }
+        else
         {
           std::string root = "";
           for (size_t i = 1; i < call.GetFullUri().size(); i++)
--- a/OrthancServer/ServerContext.cpp	Thu Nov 29 22:28:05 2012 +0100
+++ b/OrthancServer/ServerContext.cpp	Fri Nov 30 09:45:29 2012 +0100
@@ -32,13 +32,15 @@
 
 #include "ServerContext.h"
 
+#include "../Core/HttpServer/FilesystemHttpSender.h"
+
 #include <glog/logging.h>
 
 namespace Orthanc
 {
   ServerContext::ServerContext(const boost::filesystem::path& path) :
     storage_(path.string()),
-    index_(storage_, path.string())
+    index_(*this, path.string())
   {
   }
 
@@ -75,4 +77,47 @@
 
     return status;
   }
+
+  
+  void ServerContext::AnswerFile(RestApiOutput& output,
+                                 const std::string& instancePublicId,
+                                 AttachedFileType content)
+  {
+    CompressionType compressionType;
+    std::string fileUuid;
+
+    if (index_.GetFile(fileUuid, compressionType, 
+                       instancePublicId, AttachedFileType_Dicom))
+    {
+      assert(compressionType == CompressionType_None);
+
+      FilesystemHttpSender sender(storage_, fileUuid);
+      sender.SetDownloadFilename(fileUuid + ".dcm");
+      sender.SetContentType("application/dicom");
+      output.AnswerFile(sender);
+    }
+  }
+
+
+  void ServerContext::ReadJson(Json::Value& result,
+                               const std::string& instancePublicId)
+  {
+    CompressionType compressionType;
+    std::string fileUuid;
+    if (!index_.GetFile(fileUuid, compressionType, instancePublicId, AttachedFileType_Json))
+    {
+      throw OrthancException(ErrorCode_InternalError);
+    }
+
+    assert(compressionType == CompressionType_None);
+
+    std::string s;
+    storage_.ReadFile(s, fileUuid);
+
+    Json::Reader reader;
+    if (!reader.parse(s, result))
+    {
+      throw OrthancException("Corrupted JSON file");
+    }
+  }
 }
--- a/OrthancServer/ServerContext.h	Thu Nov 29 22:28:05 2012 +0100
+++ b/OrthancServer/ServerContext.h	Fri Nov 30 09:45:29 2012 +0100
@@ -34,6 +34,7 @@
 
 #include "ServerIndex.h"
 #include "../Core/FileStorage.h"
+#include "../Core/RestApi/RestApiOutput.h"
 
 namespace Orthanc
 {
@@ -51,15 +52,28 @@
       return index_;
     }
 
+    // TODO REMOVE THIS, SINCE IT IS NOT PROTECTED BY MUTEXES
     FileStorage& GetFileStorage()
     {
       return storage_;
     }
 
+    void RemoveFile(const std::string& fileUuid)
+    {
+      storage_.Remove(fileUuid);
+    }
+
     StoreStatus Store(const char* dicomFile,
                       size_t dicomSize,
                       const DicomMap& dicomSummary,
                       const Json::Value& dicomJson,
                       const std::string& remoteAet);
+
+    void AnswerFile(RestApiOutput& output,
+                    const std::string& instancePublicId,
+                    AttachedFileType content);
+
+    void ReadJson(Json::Value& result,
+                  const std::string& instancePublicId);
   };
 }
--- a/OrthancServer/ServerIndex.cpp	Thu Nov 29 22:28:05 2012 +0100
+++ b/OrthancServer/ServerIndex.cpp	Fri Nov 30 09:45:29 2012 +0100
@@ -32,8 +32,6 @@
 
 #include "ServerIndex.h"
 
-using namespace Orthanc;
-
 #ifndef NOMINMAX
 #define NOMINMAX
 #endif
@@ -44,6 +42,7 @@
 #include "../Core/DicomFormat/DicomArray.h"
 #include "../Core/SQLite/Transaction.h"
 #include "FromDcmtkBridge.h"
+#include "ServerContext.h"
 
 #include <boost/lexical_cast.hpp>
 #include <stdio.h>
@@ -56,14 +55,14 @@
     class ServerIndexListener : public IServerIndexListener
     {
     private:
-      FileStorage& fileStorage_;
+      ServerContext& context_;
       bool hasRemainingLevel_;
       ResourceType remainingType_;
       std::string remainingPublicId_;
 
     public:
-      ServerIndexListener(FileStorage& fileStorage) : 
-        fileStorage_(fileStorage),
+      ServerIndexListener(ServerContext& context) : 
+        context_(context),
         hasRemainingLevel_(false)
       {
         assert(ResourceType_Patient < ResourceType_Study &&
@@ -100,7 +99,7 @@
       virtual void SignalFileDeleted(const std::string& fileUuid)
       {
         assert(Toolbox::IsUuid(fileUuid));
-        fileStorage_.Remove(fileUuid);
+        context_.RemoveFile(fileUuid);
       }
 
       bool HasRemainingLevel() const
@@ -180,10 +179,10 @@
   }
 
 
-  ServerIndex::ServerIndex(FileStorage& fileStorage,
+  ServerIndex::ServerIndex(ServerContext& context,
                            const std::string& dbPath) : mutex_()
   {
-    listener_.reset(new Internals::ServerIndexListener(fileStorage));
+    listener_.reset(new Internals::ServerIndexListener(context));
 
     if (dbPath == ":memory:")
     {
--- a/OrthancServer/ServerIndex.h	Thu Nov 29 22:28:05 2012 +0100
+++ b/OrthancServer/ServerIndex.h	Fri Nov 30 09:45:29 2012 +0100
@@ -36,7 +36,6 @@
 #include <boost/noncopyable.hpp>
 #include "../Core/SQLite/Connection.h"
 #include "../Core/DicomFormat/DicomMap.h"
-#include "../Core/FileStorage.h"
 #include "../Core/DicomFormat/DicomInstanceHasher.h"
 #include "ServerEnumerations.h"
 
@@ -45,6 +44,8 @@
 
 namespace Orthanc
 {
+  class ServerContext;
+
   namespace Internals
   {
     class ServerIndexListener;
@@ -66,7 +67,7 @@
     SeriesStatus GetSeriesStatus(int id);
 
   public:
-    ServerIndex(FileStorage& fileStorage,
+    ServerIndex(ServerContext& context,
                 const std::string& dbPath);
 
     ~ServerIndex();
--- a/OrthancServer/ServerToolbox.cpp	Thu Nov 29 22:28:05 2012 +0100
+++ b/OrthancServer/ServerToolbox.cpp	Fri Nov 30 09:45:29 2012 +0100
@@ -36,20 +36,6 @@
 
 namespace Orthanc
 {
-  void ReadJson(Json::Value& target,
-                const FileStorage& storage,
-                const std::string& fileUuid)
-  {
-    std::string s;
-    storage.ReadFile(s, fileUuid);
-
-    Json::Reader reader;
-    if (!reader.parse(s, target))
-    {
-      throw OrthancException("Corrupted JSON file");
-    }
-  }
-
   void SimplifyTags(Json::Value& target,
                     const Json::Value& source)
   {
--- a/OrthancServer/ServerToolbox.h	Thu Nov 29 22:28:05 2012 +0100
+++ b/OrthancServer/ServerToolbox.h	Fri Nov 30 09:45:29 2012 +0100
@@ -38,10 +38,6 @@
 
 namespace Orthanc
 {
-  void ReadJson(Json::Value& target,
-                const FileStorage& storage,
-                const std::string& fileUuid);
-
   void SimplifyTags(Json::Value& target,
                     const Json::Value& source);
 }