Mercurial > hg > orthanc
annotate OrthancServer/ServerContext.cpp @ 997:1b1d51e9f1a2 lua-scripting
return Json from Lua
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 03 Jul 2014 18:12:50 +0200 |
parents | cf52f3bcb2b3 |
children | 4136fab6a639 |
rev | line source |
---|---|
224 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
689 | 3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, |
224 | 4 * Belgium |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
831
84513f2ee1f3
pch for unit tests and server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
33 #include "PrecompiledHeadersServer.h" |
224 | 34 #include "ServerContext.h" |
35 | |
226
8a26a8e85edf
refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
224
diff
changeset
|
36 #include "../Core/HttpServer/FilesystemHttpSender.h" |
397
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
394
diff
changeset
|
37 #include "../Core/Lua/LuaFunctionCall.h" |
795 | 38 #include "FromDcmtkBridge.h" |
397
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
394
diff
changeset
|
39 #include "ServerToolbox.h" |
771 | 40 #include "OrthancInitialization.h" |
226
8a26a8e85edf
refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
224
diff
changeset
|
41 |
224 | 42 #include <glog/logging.h> |
394
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
376
diff
changeset
|
43 #include <EmbeddedResources.h> |
794 | 44 #include <dcmtk/dcmdata/dcfilefo.h> |
224 | 45 |
291
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
288
diff
changeset
|
46 #define ENABLE_DICOM_CACHE 1 |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
288
diff
changeset
|
47 |
397
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
394
diff
changeset
|
48 static const char* RECEIVED_INSTANCE_FILTER = "ReceivedInstanceFilter"; |
996
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
49 static const char* ON_STORED_INSTANCE = "OnStoredInstance"; |
227
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
50 |
285
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
269
diff
changeset
|
51 static const size_t DICOM_CACHE_SIZE = 2; |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
269
diff
changeset
|
52 |
227
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
53 /** |
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
54 * IMPORTANT: We make the assumption that the same instance of |
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
55 * FileStorage can be accessed from multiple threads. This seems OK |
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
56 * since the filesystem implements the required locking mechanisms, |
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
57 * but maybe a read-writer lock on the "FileStorage" could be |
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
58 * useful. Conversely, "ServerIndex" already implements mutex-based |
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
59 * locking. |
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
60 **/ |
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
61 |
224 | 62 namespace Orthanc |
63 { | |
376
2cef9c2d4148
separate path for SQLite index, manual loading of external dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
320
diff
changeset
|
64 ServerContext::ServerContext(const boost::filesystem::path& storagePath, |
2cef9c2d4148
separate path for SQLite index, manual loading of external dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
320
diff
changeset
|
65 const boost::filesystem::path& indexPath) : |
2cef9c2d4148
separate path for SQLite index, manual loading of external dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
320
diff
changeset
|
66 storage_(storagePath.string()), |
2cef9c2d4148
separate path for SQLite index, manual loading of external dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
320
diff
changeset
|
67 index_(*this, indexPath.string()), |
285
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
269
diff
changeset
|
68 accessor_(storage_), |
656 | 69 compressionEnabled_(false), |
285
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
269
diff
changeset
|
70 provider_(*this), |
995
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
942
diff
changeset
|
71 dicomCache_(provider_, DICOM_CACHE_SIZE), |
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
942
diff
changeset
|
72 scheduler_(Configuration::GetGlobalIntegerParameter("LimitJobs", 10)) |
224 | 73 { |
810
401a9633e492
configuration into a namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
795
diff
changeset
|
74 scu_.SetLocalApplicationEntityTitle(Configuration::GetGlobalStringParameter("DicomAet", "ORTHANC")); |
774 | 75 //scu_.SetMillisecondsBeforeClose(1); // The connection is always released |
76 | |
394
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
376
diff
changeset
|
77 lua_.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX); |
224 | 78 } |
79 | |
236 | 80 void ServerContext::SetCompressionEnabled(bool enabled) |
81 { | |
82 if (enabled) | |
83 LOG(WARNING) << "Disk compression is enabled"; | |
84 else | |
85 LOG(WARNING) << "Disk compression is disabled"; | |
86 | |
87 compressionEnabled_ = enabled; | |
88 } | |
89 | |
227
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
90 void ServerContext::RemoveFile(const std::string& fileUuid) |
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
91 { |
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
92 storage_.Remove(fileUuid); |
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
93 } |
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
94 |
996
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
95 |
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
96 bool ServerContext::ApplyReceivedInstanceFilter(const Json::Value& dicomJson, |
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
97 const std::string& remoteAet) |
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
98 { |
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
99 LuaContextLocker locker(*this); |
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
100 |
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
101 if (locker.GetLua().IsExistingFunction(RECEIVED_INSTANCE_FILTER)) |
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
102 { |
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
103 Json::Value simplified; |
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
104 SimplifyTags(simplified, dicomJson); |
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
105 |
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
106 LuaFunctionCall call(locker.GetLua(), RECEIVED_INSTANCE_FILTER); |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
107 call.PushJson(simplified); |
996
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
108 call.PushString(remoteAet); |
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
109 |
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
110 if (!call.ExecutePredicate()) |
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
111 { |
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
112 return false; |
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
113 } |
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
114 } |
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
115 |
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
116 return true; |
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
117 } |
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
118 |
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
119 |
304 | 120 StoreStatus ServerContext::Store(const char* dicomInstance, |
224 | 121 size_t dicomSize, |
122 const DicomMap& dicomSummary, | |
123 const Json::Value& dicomJson, | |
124 const std::string& remoteAet) | |
125 { | |
397
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
394
diff
changeset
|
126 // Test if the instance must be filtered out |
996
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
127 if (!ApplyReceivedInstanceFilter(dicomJson, remoteAet)) |
397
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
394
diff
changeset
|
128 { |
996
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
129 LOG(INFO) << "An incoming instance has been discarded by the filter"; |
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
130 return StoreStatus_FilteredOut; |
397
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
394
diff
changeset
|
131 } |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
394
diff
changeset
|
132 |
236 | 133 if (compressionEnabled_) |
134 { | |
135 accessor_.SetCompressionForNextOperations(CompressionType_Zlib); | |
136 } | |
137 else | |
138 { | |
139 accessor_.SetCompressionForNextOperations(CompressionType_None); | |
140 } | |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
227
diff
changeset
|
141 |
304 | 142 FileInfo dicomInfo = accessor_.Write(dicomInstance, dicomSize, FileContentType_Dicom); |
697
dd1ce9a2844c
access to attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
143 FileInfo jsonInfo = accessor_.Write(dicomJson.toStyledString(), FileContentType_DicomAsJson); |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
227
diff
changeset
|
144 |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
227
diff
changeset
|
145 ServerIndex::Attachments attachments; |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
227
diff
changeset
|
146 attachments.push_back(dicomInfo); |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
227
diff
changeset
|
147 attachments.push_back(jsonInfo); |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
227
diff
changeset
|
148 |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
227
diff
changeset
|
149 StoreStatus status = index_.Store(dicomSummary, attachments, remoteAet); |
224 | 150 |
151 if (status != StoreStatus_Success) | |
152 { | |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
227
diff
changeset
|
153 storage_.Remove(dicomInfo.GetUuid()); |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
227
diff
changeset
|
154 storage_.Remove(jsonInfo.GetUuid()); |
224 | 155 } |
156 | |
157 switch (status) | |
158 { | |
397
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
394
diff
changeset
|
159 case StoreStatus_Success: |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
394
diff
changeset
|
160 LOG(INFO) << "New instance stored"; |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
394
diff
changeset
|
161 break; |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
394
diff
changeset
|
162 |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
394
diff
changeset
|
163 case StoreStatus_AlreadyStored: |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
394
diff
changeset
|
164 LOG(INFO) << "Already stored"; |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
394
diff
changeset
|
165 break; |
224 | 166 |
397
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
394
diff
changeset
|
167 case StoreStatus_Failure: |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
394
diff
changeset
|
168 LOG(ERROR) << "Store failure"; |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
394
diff
changeset
|
169 break; |
224 | 170 |
397
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
394
diff
changeset
|
171 default: |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
394
diff
changeset
|
172 // This should never happen |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
394
diff
changeset
|
173 break; |
224 | 174 } |
175 | |
176 return status; | |
177 } | |
226
8a26a8e85edf
refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
224
diff
changeset
|
178 |
8a26a8e85edf
refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
224
diff
changeset
|
179 |
697
dd1ce9a2844c
access to attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
180 void ServerContext::AnswerDicomFile(RestApiOutput& output, |
dd1ce9a2844c
access to attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
181 const std::string& instancePublicId, |
dd1ce9a2844c
access to attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
182 FileContentType content) |
226
8a26a8e85edf
refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
224
diff
changeset
|
183 { |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
227
diff
changeset
|
184 FileInfo attachment; |
236 | 185 if (!index_.LookupAttachment(attachment, instancePublicId, content)) |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
227
diff
changeset
|
186 { |
236 | 187 throw OrthancException(ErrorCode_InternalError); |
188 } | |
226
8a26a8e85edf
refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
224
diff
changeset
|
189 |
236 | 190 accessor_.SetCompressionForNextOperations(attachment.GetCompressionType()); |
320 | 191 |
236 | 192 std::auto_ptr<HttpFileSender> sender(accessor_.ConstructHttpFileSender(attachment.GetUuid())); |
320 | 193 sender->SetContentType("application/dicom"); |
194 sender->SetDownloadFilename(instancePublicId + ".dcm"); | |
236 | 195 output.AnswerFile(*sender); |
226
8a26a8e85edf
refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
224
diff
changeset
|
196 } |
8a26a8e85edf
refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
224
diff
changeset
|
197 |
8a26a8e85edf
refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
224
diff
changeset
|
198 |
8a26a8e85edf
refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
224
diff
changeset
|
199 void ServerContext::ReadJson(Json::Value& result, |
8a26a8e85edf
refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
224
diff
changeset
|
200 const std::string& instancePublicId) |
8a26a8e85edf
refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
224
diff
changeset
|
201 { |
8a26a8e85edf
refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
224
diff
changeset
|
202 std::string s; |
697
dd1ce9a2844c
access to attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
203 ReadFile(s, instancePublicId, FileContentType_DicomAsJson); |
226
8a26a8e85edf
refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
224
diff
changeset
|
204 |
8a26a8e85edf
refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
224
diff
changeset
|
205 Json::Reader reader; |
8a26a8e85edf
refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
224
diff
changeset
|
206 if (!reader.parse(s, result)) |
8a26a8e85edf
refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
224
diff
changeset
|
207 { |
8a26a8e85edf
refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
224
diff
changeset
|
208 throw OrthancException("Corrupted JSON file"); |
8a26a8e85edf
refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
224
diff
changeset
|
209 } |
8a26a8e85edf
refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
224
diff
changeset
|
210 } |
227
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
211 |
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
212 |
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
213 void ServerContext::ReadFile(std::string& result, |
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
214 const std::string& instancePublicId, |
697
dd1ce9a2844c
access to attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
215 FileContentType content, |
dd1ce9a2844c
access to attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
216 bool uncompressIfNeeded) |
227
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
217 { |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
227
diff
changeset
|
218 FileInfo attachment; |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
227
diff
changeset
|
219 if (!index_.LookupAttachment(attachment, instancePublicId, content)) |
227
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
220 { |
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
221 throw OrthancException(ErrorCode_InternalError); |
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
222 } |
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
223 |
697
dd1ce9a2844c
access to attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
224 if (uncompressIfNeeded) |
dd1ce9a2844c
access to attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
225 { |
dd1ce9a2844c
access to attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
226 accessor_.SetCompressionForNextOperations(attachment.GetCompressionType()); |
dd1ce9a2844c
access to attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
227 } |
dd1ce9a2844c
access to attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
228 else |
dd1ce9a2844c
access to attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
229 { |
dd1ce9a2844c
access to attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
230 accessor_.SetCompressionForNextOperations(CompressionType_None); |
dd1ce9a2844c
access to attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
231 } |
dd1ce9a2844c
access to attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
232 |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
227
diff
changeset
|
233 accessor_.Read(result, attachment.GetUuid()); |
227
209ca3f6db62
dicom-scu from rest
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
234 } |
285
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
269
diff
changeset
|
235 |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
269
diff
changeset
|
236 |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
269
diff
changeset
|
237 IDynamicObject* ServerContext::DicomCacheProvider::Provide(const std::string& instancePublicId) |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
269
diff
changeset
|
238 { |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
269
diff
changeset
|
239 std::string content; |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
269
diff
changeset
|
240 context_.ReadFile(content, instancePublicId, FileContentType_Dicom); |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
269
diff
changeset
|
241 return new ParsedDicomFile(content); |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
269
diff
changeset
|
242 } |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
269
diff
changeset
|
243 |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
269
diff
changeset
|
244 |
778
aebf0071020e
refactoring of the mutex for the dicom cache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
774
diff
changeset
|
245 ServerContext::DicomCacheLocker::DicomCacheLocker(ServerContext& that, |
aebf0071020e
refactoring of the mutex for the dicom cache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
774
diff
changeset
|
246 const std::string& instancePublicId) : |
aebf0071020e
refactoring of the mutex for the dicom cache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
774
diff
changeset
|
247 that_(that) |
285
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
269
diff
changeset
|
248 { |
291
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
288
diff
changeset
|
249 #if ENABLE_DICOM_CACHE == 0 |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
288
diff
changeset
|
250 static std::auto_ptr<IDynamicObject> p; |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
288
diff
changeset
|
251 p.reset(provider_.Provide(instancePublicId)); |
778
aebf0071020e
refactoring of the mutex for the dicom cache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
774
diff
changeset
|
252 dicom_ = dynamic_cast<ParsedDicomFile*>(p.get()); |
291
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
288
diff
changeset
|
253 #else |
778
aebf0071020e
refactoring of the mutex for the dicom cache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
774
diff
changeset
|
254 that_.dicomCacheMutex_.lock(); |
aebf0071020e
refactoring of the mutex for the dicom cache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
774
diff
changeset
|
255 dicom_ = &dynamic_cast<ParsedDicomFile&>(that_.dicomCache_.Access(instancePublicId)); |
aebf0071020e
refactoring of the mutex for the dicom cache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
774
diff
changeset
|
256 #endif |
aebf0071020e
refactoring of the mutex for the dicom cache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
774
diff
changeset
|
257 } |
aebf0071020e
refactoring of the mutex for the dicom cache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
774
diff
changeset
|
258 |
aebf0071020e
refactoring of the mutex for the dicom cache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
774
diff
changeset
|
259 |
aebf0071020e
refactoring of the mutex for the dicom cache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
774
diff
changeset
|
260 ServerContext::DicomCacheLocker::~DicomCacheLocker() |
aebf0071020e
refactoring of the mutex for the dicom cache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
774
diff
changeset
|
261 { |
aebf0071020e
refactoring of the mutex for the dicom cache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
774
diff
changeset
|
262 #if ENABLE_DICOM_CACHE == 0 |
aebf0071020e
refactoring of the mutex for the dicom cache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
774
diff
changeset
|
263 #else |
aebf0071020e
refactoring of the mutex for the dicom cache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
774
diff
changeset
|
264 that_.dicomCacheMutex_.unlock(); |
291
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
288
diff
changeset
|
265 #endif |
285
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
269
diff
changeset
|
266 } |
304 | 267 |
268 | |
792 | 269 static DcmFileFormat& GetDicom(ParsedDicomFile& file) |
270 { | |
271 return *reinterpret_cast<DcmFileFormat*>(file.GetDcmtkObject()); | |
272 } | |
273 | |
274 | |
304 | 275 StoreStatus ServerContext::Store(std::string& resultPublicId, |
791 | 276 ParsedDicomFile& dicomInstance, |
304 | 277 const char* dicomBuffer, |
278 size_t dicomSize) | |
279 { | |
280 DicomMap dicomSummary; | |
792 | 281 FromDcmtkBridge::Convert(dicomSummary, *GetDicom(dicomInstance).getDataset()); |
304 | 282 |
730
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
283 try |
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
284 { |
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
285 DicomInstanceHasher hasher(dicomSummary); |
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
286 resultPublicId = hasher.HashInstance(); |
304 | 287 |
730
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
288 Json::Value dicomJson; |
792 | 289 FromDcmtkBridge::ToJson(dicomJson, *GetDicom(dicomInstance).getDataset()); |
304 | 290 |
730
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
291 StoreStatus status = StoreStatus_Failure; |
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
292 if (dicomSize > 0) |
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
293 { |
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
294 status = Store(dicomBuffer, dicomSize, dicomSummary, dicomJson, ""); |
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
295 } |
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
296 |
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
297 return status; |
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
298 } |
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
299 catch (OrthancException& e) |
304 | 300 { |
730
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
301 if (e.GetErrorCode() == ErrorCode_InexistentTag) |
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
302 { |
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
303 LogMissingRequiredTag(dicomSummary); |
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
304 } |
304 | 305 |
942
b3f6fb1130cd
fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
306 throw; |
730
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
307 } |
304 | 308 } |
309 | |
310 | |
311 StoreStatus ServerContext::Store(std::string& resultPublicId, | |
791 | 312 ParsedDicomFile& dicomInstance) |
304 | 313 { |
314 std::string buffer; | |
792 | 315 if (!FromDcmtkBridge::SaveToMemoryBuffer(buffer, GetDicom(dicomInstance).getDataset())) |
304 | 316 { |
317 throw OrthancException(ErrorCode_InternalError); | |
318 } | |
319 | |
320 if (buffer.size() == 0) | |
321 return Store(resultPublicId, dicomInstance, NULL, 0); | |
322 else | |
323 return Store(resultPublicId, dicomInstance, &buffer[0], buffer.size()); | |
324 } | |
325 | |
326 | |
327 StoreStatus ServerContext::Store(std::string& resultPublicId, | |
328 const char* dicomBuffer, | |
329 size_t dicomSize) | |
330 { | |
331 ParsedDicomFile dicom(dicomBuffer, dicomSize); | |
791 | 332 return Store(resultPublicId, dicom, dicomBuffer, dicomSize); |
304 | 333 } |
334 | |
791 | 335 |
336 StoreStatus ServerContext::Store(std::string& resultPublicId, | |
337 const std::string& dicomContent) | |
338 { | |
339 if (dicomContent.size() == 0) | |
340 { | |
341 return Store(resultPublicId, NULL, 0); | |
342 } | |
343 else | |
344 { | |
345 return Store(resultPublicId, &dicomContent[0], dicomContent.size()); | |
346 } | |
347 } | |
348 | |
349 | |
695
c59bc408fb10
parameter to disable the computation of the MD5
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
350 void ServerContext::SetStoreMD5ForAttachments(bool storeMD5) |
c59bc408fb10
parameter to disable the computation of the MD5
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
351 { |
c59bc408fb10
parameter to disable the computation of the MD5
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
352 LOG(INFO) << "Storing MD5 for attachments: " << (storeMD5 ? "yes" : "no"); |
c59bc408fb10
parameter to disable the computation of the MD5
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
353 accessor_.SetStoreMD5(storeMD5); |
c59bc408fb10
parameter to disable the computation of the MD5
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
354 } |
c59bc408fb10
parameter to disable the computation of the MD5
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
355 |
699
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
356 |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
357 bool ServerContext::AddAttachment(const std::string& resourceId, |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
358 FileContentType attachmentType, |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
359 const void* data, |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
360 size_t size) |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
361 { |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
362 LOG(INFO) << "Adding attachment " << EnumerationToString(attachmentType) << " to resource " << resourceId; |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
363 |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
364 if (compressionEnabled_) |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
365 { |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
366 accessor_.SetCompressionForNextOperations(CompressionType_Zlib); |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
367 } |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
368 else |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
369 { |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
370 accessor_.SetCompressionForNextOperations(CompressionType_None); |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
371 } |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
372 |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
373 FileInfo info = accessor_.Write(data, size, attachmentType); |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
374 StoreStatus status = index_.AddAttachment(info, resourceId); |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
375 |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
376 if (status != StoreStatus_Success) |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
377 { |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
378 storage_.Remove(info.GetUuid()); |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
379 return false; |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
380 } |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
381 else |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
382 { |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
383 return true; |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
384 } |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
697
diff
changeset
|
385 } |
224 | 386 } |