Mercurial > hg > orthanc
comparison OrthancServer/Sources/OrthancMoveRequestHandler.cpp @ 4540:9c0cff7a6ca2 db-changes
integration mainline->db-changes
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 02 Mar 2021 16:51:19 +0100 |
parents | c1f36fd13730 |
children | f0038043fb97 753f87dac208 |
comparison
equal
deleted
inserted
replaced
3141:f2b300e46755 | 4540:9c0cff7a6ca2 |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
34 #include "PrecompiledHeadersServer.h" | |
35 #include "OrthancMoveRequestHandler.h" | |
36 | |
37 #include "../../OrthancFramework/Sources/DicomFormat/DicomArray.h" | |
38 #include "../../OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.h" | |
39 #include "../../OrthancFramework/Sources/Logging.h" | |
40 #include "../../OrthancFramework/Sources/MetricsRegistry.h" | |
41 | |
42 #include "OrthancConfiguration.h" | |
43 #include "ServerContext.h" | |
44 #include "ServerJobs/DicomModalityStoreJob.h" | |
45 | |
46 | |
47 namespace Orthanc | |
48 { | |
49 namespace | |
50 { | |
51 // Anonymous namespace to avoid clashes between compilation modules | |
52 | |
53 class SynchronousMove : public IMoveRequestIterator | |
54 { | |
55 private: | |
56 ServerContext& context_; | |
57 const std::string& localAet_; | |
58 std::vector<std::string> instances_; | |
59 size_t position_; | |
60 RemoteModalityParameters remote_; | |
61 std::string originatorAet_; | |
62 uint16_t originatorId_; | |
63 std::unique_ptr<DicomStoreUserConnection> connection_; | |
64 | |
65 public: | |
66 SynchronousMove(ServerContext& context, | |
67 const std::string& targetAet, | |
68 const std::vector<std::string>& publicIds, | |
69 const std::string& originatorAet, | |
70 uint16_t originatorId) : | |
71 context_(context), | |
72 localAet_(context.GetDefaultLocalApplicationEntityTitle()), | |
73 position_(0), | |
74 originatorAet_(originatorAet), | |
75 originatorId_(originatorId) | |
76 { | |
77 { | |
78 OrthancConfiguration::ReaderLock lock; | |
79 remote_ = lock.GetConfiguration().GetModalityUsingAet(targetAet); | |
80 } | |
81 | |
82 for (size_t i = 0; i < publicIds.size(); i++) | |
83 { | |
84 CLOG(INFO, DICOM) << "Sending resource " << publicIds[i] << " to modality \"" | |
85 << targetAet << "\" in synchronous mode"; | |
86 | |
87 std::list<std::string> tmp; | |
88 context_.GetIndex().GetChildInstances(tmp, publicIds[i]); | |
89 | |
90 instances_.reserve(tmp.size()); | |
91 for (std::list<std::string>::iterator it = tmp.begin(); it != tmp.end(); ++it) | |
92 { | |
93 instances_.push_back(*it); | |
94 } | |
95 } | |
96 } | |
97 | |
98 virtual unsigned int GetSubOperationCount() const | |
99 { | |
100 return instances_.size(); | |
101 } | |
102 | |
103 virtual Status DoNext() | |
104 { | |
105 if (position_ >= instances_.size()) | |
106 { | |
107 return Status_Failure; | |
108 } | |
109 | |
110 const std::string& id = instances_[position_++]; | |
111 | |
112 std::string dicom; | |
113 context_.ReadDicom(dicom, id); | |
114 | |
115 if (connection_.get() == NULL) | |
116 { | |
117 DicomAssociationParameters params(localAet_, remote_); | |
118 connection_.reset(new DicomStoreUserConnection(params)); | |
119 } | |
120 | |
121 std::string sopClassUid, sopInstanceUid; // Unused | |
122 context_.StoreWithTranscoding(sopClassUid, sopInstanceUid, *connection_, dicom, | |
123 true, originatorAet_, originatorId_); | |
124 | |
125 return Status_Success; | |
126 } | |
127 }; | |
128 | |
129 | |
130 class AsynchronousMove : public IMoveRequestIterator | |
131 { | |
132 private: | |
133 ServerContext& context_; | |
134 std::unique_ptr<DicomModalityStoreJob> job_; | |
135 size_t position_; | |
136 size_t countInstances_; | |
137 | |
138 public: | |
139 AsynchronousMove(ServerContext& context, | |
140 const std::string& targetAet, | |
141 const std::vector<std::string>& publicIds, | |
142 const std::string& originatorAet, | |
143 uint16_t originatorId) : | |
144 context_(context), | |
145 job_(new DicomModalityStoreJob(context)), | |
146 position_(0) | |
147 { | |
148 job_->SetDescription("C-MOVE"); | |
149 //job_->SetPermissive(true); // This was the behavior of Orthanc < 1.6.0 | |
150 job_->SetPermissive(false); | |
151 job_->SetLocalAet(context.GetDefaultLocalApplicationEntityTitle()); | |
152 | |
153 { | |
154 OrthancConfiguration::ReaderLock lock; | |
155 job_->SetRemoteModality(lock.GetConfiguration().GetModalityUsingAet(targetAet)); | |
156 } | |
157 | |
158 if (originatorId != 0) | |
159 { | |
160 job_->SetMoveOriginator(originatorAet, originatorId); | |
161 } | |
162 | |
163 for (size_t i = 0; i < publicIds.size(); i++) | |
164 { | |
165 CLOG(INFO, DICOM) << "Sending resource " << publicIds[i] << " to modality \"" | |
166 << targetAet << "\" in asynchronous mode"; | |
167 | |
168 std::list<std::string> tmp; | |
169 context_.GetIndex().GetChildInstances(tmp, publicIds[i]); | |
170 | |
171 countInstances_ = tmp.size(); | |
172 | |
173 job_->Reserve(job_->GetCommandsCount() + tmp.size()); | |
174 | |
175 for (std::list<std::string>::iterator it = tmp.begin(); it != tmp.end(); ++it) | |
176 { | |
177 job_->AddInstance(*it); | |
178 } | |
179 } | |
180 } | |
181 | |
182 virtual unsigned int GetSubOperationCount() const | |
183 { | |
184 return countInstances_; | |
185 } | |
186 | |
187 virtual Status DoNext() | |
188 { | |
189 if (position_ >= countInstances_) | |
190 { | |
191 return Status_Failure; | |
192 } | |
193 | |
194 if (position_ == 0) | |
195 { | |
196 context_.GetJobsEngine().GetRegistry().Submit(job_.release(), 0 /* priority */); | |
197 } | |
198 | |
199 position_ ++; | |
200 return Status_Success; | |
201 } | |
202 }; | |
203 } | |
204 | |
205 | |
206 static bool IsNonEmptyTag(const DicomMap& dicom, | |
207 const DicomTag& tag) | |
208 { | |
209 const DicomValue* value = dicom.TestAndGetValue(tag); | |
210 if (value == NULL || | |
211 value->IsNull() || | |
212 value->IsBinary()) | |
213 { | |
214 return false; | |
215 } | |
216 else | |
217 { | |
218 return !value->GetContent().empty(); | |
219 } | |
220 } | |
221 | |
222 | |
223 bool OrthancMoveRequestHandler::LookupIdentifiers(std::vector<std::string>& publicIds, | |
224 ResourceType level, | |
225 const DicomMap& input) | |
226 { | |
227 DicomTag tag(0, 0); // Dummy initialization | |
228 | |
229 switch (level) | |
230 { | |
231 case ResourceType_Patient: | |
232 tag = DICOM_TAG_PATIENT_ID; | |
233 break; | |
234 | |
235 case ResourceType_Study: | |
236 // The test below using "IsNonEmptyTag()" fixes compatibility | |
237 // with Ambra C-FIND SCU: | |
238 // https://groups.google.com/g/orthanc-users/c/yIUnZ9v9-Zs/m/GQPXiAOiCQAJ | |
239 if (IsNonEmptyTag(input, DICOM_TAG_ACCESSION_NUMBER)) | |
240 { | |
241 tag = DICOM_TAG_ACCESSION_NUMBER; | |
242 } | |
243 else | |
244 { | |
245 tag = DICOM_TAG_STUDY_INSTANCE_UID; | |
246 } | |
247 break; | |
248 | |
249 case ResourceType_Series: | |
250 tag = DICOM_TAG_SERIES_INSTANCE_UID; | |
251 break; | |
252 | |
253 case ResourceType_Instance: | |
254 tag = DICOM_TAG_SOP_INSTANCE_UID; | |
255 break; | |
256 | |
257 default: | |
258 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
259 } | |
260 | |
261 if (!input.HasTag(tag)) | |
262 { | |
263 return false; | |
264 } | |
265 | |
266 const DicomValue& value = input.GetValue(tag); | |
267 if (value.IsNull() || | |
268 value.IsBinary()) | |
269 { | |
270 return false; | |
271 } | |
272 else | |
273 { | |
274 const std::string& content = value.GetContent(); | |
275 | |
276 /** | |
277 * This tokenization fixes issue 154 ("Matching against list of | |
278 * UID-s by C-MOVE"). | |
279 * https://bugs.orthanc-server.com/show_bug.cgi?id=154 | |
280 **/ | |
281 | |
282 std::vector<std::string> tokens; | |
283 Toolbox::TokenizeString(tokens, content, '\\'); | |
284 for (size_t i = 0; i < tokens.size(); i++) | |
285 { | |
286 std::vector<std::string> matches; | |
287 context_.GetIndex().LookupIdentifierExact(matches, level, tag, tokens[i]); | |
288 | |
289 // Concatenate "publicIds" with "matches" | |
290 publicIds.insert(publicIds.end(), matches.begin(), matches.end()); | |
291 } | |
292 | |
293 return true; | |
294 } | |
295 } | |
296 | |
297 | |
298 static IMoveRequestIterator* CreateIterator(ServerContext& context, | |
299 const std::string& targetAet, | |
300 const std::vector<std::string>& publicIds, | |
301 const std::string& originatorAet, | |
302 uint16_t originatorId) | |
303 { | |
304 if (publicIds.empty()) | |
305 { | |
306 throw OrthancException(ErrorCode_BadRequest, | |
307 "C-MOVE request matching no resource stored in Orthanc"); | |
308 } | |
309 | |
310 bool synchronous; | |
311 | |
312 { | |
313 OrthancConfiguration::ReaderLock lock; | |
314 synchronous = lock.GetConfiguration().GetBooleanParameter("SynchronousCMove", true); | |
315 } | |
316 | |
317 if (synchronous) | |
318 { | |
319 return new SynchronousMove(context, targetAet, publicIds, originatorAet, originatorId); | |
320 } | |
321 else | |
322 { | |
323 return new AsynchronousMove(context, targetAet, publicIds, originatorAet, originatorId); | |
324 } | |
325 } | |
326 | |
327 | |
328 IMoveRequestIterator* OrthancMoveRequestHandler::Handle(const std::string& targetAet, | |
329 const DicomMap& input, | |
330 const std::string& originatorIp, | |
331 const std::string& originatorAet, | |
332 const std::string& calledAet, | |
333 uint16_t originatorId) | |
334 { | |
335 MetricsRegistry::Timer timer(context_.GetMetricsRegistry(), "orthanc_move_scp_duration_ms"); | |
336 | |
337 CLOG(WARNING, DICOM) << "Move-SCU request received for AET \"" << targetAet << "\""; | |
338 | |
339 { | |
340 DicomArray query(input); | |
341 for (size_t i = 0; i < query.GetSize(); i++) | |
342 { | |
343 if (!query.GetElement(i).GetValue().IsNull()) | |
344 { | |
345 CLOG(INFO, DICOM) << " (" << query.GetElement(i).GetTag().Format() | |
346 << ") " << FromDcmtkBridge::GetTagName(query.GetElement(i)) | |
347 << " = " << context_.GetDeidentifiedContent(query.GetElement(i)); | |
348 } | |
349 } | |
350 } | |
351 | |
352 /** | |
353 * Retrieve the query level. | |
354 **/ | |
355 | |
356 const DicomValue* levelTmp = input.TestAndGetValue(DICOM_TAG_QUERY_RETRIEVE_LEVEL); | |
357 | |
358 if (levelTmp == NULL || | |
359 levelTmp->IsNull() || | |
360 levelTmp->IsBinary()) | |
361 { | |
362 // The query level is not present in the C-Move request, which | |
363 // does not follow the DICOM standard. This is for instance the | |
364 // behavior of Tudor DICOM. Try and automatically deduce the | |
365 // query level: Start from the instance level, going up to the | |
366 // patient level until a valid DICOM identifier is found. | |
367 | |
368 std::vector<std::string> publicIds; | |
369 | |
370 if (LookupIdentifiers(publicIds, ResourceType_Instance, input) || | |
371 LookupIdentifiers(publicIds, ResourceType_Series, input) || | |
372 LookupIdentifiers(publicIds, ResourceType_Study, input) || | |
373 LookupIdentifiers(publicIds, ResourceType_Patient, input)) | |
374 { | |
375 return CreateIterator(context_, targetAet, publicIds, originatorAet, originatorId); | |
376 } | |
377 else | |
378 { | |
379 // No identifier is present in the request. | |
380 throw OrthancException(ErrorCode_BadRequest, "Invalid fields in a C-MOVE request"); | |
381 } | |
382 } | |
383 | |
384 assert(levelTmp != NULL); | |
385 ResourceType level = StringToResourceType(levelTmp->GetContent().c_str()); | |
386 | |
387 | |
388 /** | |
389 * Lookup for the resource to be sent. | |
390 **/ | |
391 | |
392 std::vector<std::string> publicIds; | |
393 | |
394 if (LookupIdentifiers(publicIds, level, input)) | |
395 { | |
396 return CreateIterator(context_, targetAet, publicIds, originatorAet, originatorId); | |
397 } | |
398 else | |
399 { | |
400 throw OrthancException(ErrorCode_BadRequest, "No DICOM identifier provided in the C-MOVE request for this query retrieve level"); | |
401 } | |
402 } | |
403 } |