comparison OrthancServer/Sources/OrthancMoveRequestHandler.cpp @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents OrthancServer/OrthancMoveRequestHandler.cpp@8f7ad4989fec
children 05b8fd21089c
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
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-2020 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 "../../Core/DicomParsing/FromDcmtkBridge.h"
38 #include "../Core/DicomFormat/DicomArray.h"
39 #include "../Core/Logging.h"
40 #include "../Core/MetricsRegistry.h"
41 #include "OrthancConfiguration.h"
42 #include "ServerContext.h"
43 #include "ServerJobs/DicomModalityStoreJob.h"
44
45
46 namespace Orthanc
47 {
48 namespace
49 {
50 // Anonymous namespace to avoid clashes between compilation modules
51
52 class SynchronousMove : public IMoveRequestIterator
53 {
54 private:
55 ServerContext& context_;
56 const std::string& localAet_;
57 std::vector<std::string> instances_;
58 size_t position_;
59 RemoteModalityParameters remote_;
60 std::string originatorAet_;
61 uint16_t originatorId_;
62 std::unique_ptr<DicomStoreUserConnection> connection_;
63
64 public:
65 SynchronousMove(ServerContext& context,
66 const std::string& targetAet,
67 const std::vector<std::string>& publicIds,
68 const std::string& originatorAet,
69 uint16_t originatorId) :
70 context_(context),
71 localAet_(context.GetDefaultLocalApplicationEntityTitle()),
72 position_(0),
73 originatorAet_(originatorAet),
74 originatorId_(originatorId)
75 {
76 {
77 OrthancConfiguration::ReaderLock lock;
78 remote_ = lock.GetConfiguration().GetModalityUsingAet(targetAet);
79 }
80
81 for (size_t i = 0; i < publicIds.size(); i++)
82 {
83 LOG(INFO) << "Sending resource " << publicIds[i] << " to modality \""
84 << targetAet << "\" in synchronous mode";
85
86 std::list<std::string> tmp;
87 context_.GetIndex().GetChildInstances(tmp, publicIds[i]);
88
89 instances_.reserve(tmp.size());
90 for (std::list<std::string>::iterator it = tmp.begin(); it != tmp.end(); ++it)
91 {
92 instances_.push_back(*it);
93 }
94 }
95 }
96
97 virtual unsigned int GetSubOperationCount() const
98 {
99 return instances_.size();
100 }
101
102 virtual Status DoNext()
103 {
104 if (position_ >= instances_.size())
105 {
106 return Status_Failure;
107 }
108
109 const std::string& id = instances_[position_++];
110
111 std::string dicom;
112 context_.ReadDicom(dicom, id);
113
114 if (connection_.get() == NULL)
115 {
116 DicomAssociationParameters params(localAet_, remote_);
117 connection_.reset(new DicomStoreUserConnection(params));
118 }
119
120 std::string sopClassUid, sopInstanceUid; // Unused
121
122 const void* data = dicom.empty() ? NULL : dicom.c_str();
123 connection_->Store(sopClassUid, sopInstanceUid, data, dicom.size(),
124 true, originatorAet_, originatorId_);
125
126 return Status_Success;
127 }
128 };
129
130
131 class AsynchronousMove : public IMoveRequestIterator
132 {
133 private:
134 ServerContext& context_;
135 std::unique_ptr<DicomModalityStoreJob> job_;
136 size_t position_;
137 size_t countInstances_;
138
139 public:
140 AsynchronousMove(ServerContext& context,
141 const std::string& targetAet,
142 const std::vector<std::string>& publicIds,
143 const std::string& originatorAet,
144 uint16_t originatorId) :
145 context_(context),
146 job_(new DicomModalityStoreJob(context)),
147 position_(0)
148 {
149 job_->SetDescription("C-MOVE");
150 //job_->SetPermissive(true); // This was the behavior of Orthanc < 1.6.0
151 job_->SetPermissive(false);
152 job_->SetLocalAet(context.GetDefaultLocalApplicationEntityTitle());
153
154 {
155 OrthancConfiguration::ReaderLock lock;
156 job_->SetRemoteModality(lock.GetConfiguration().GetModalityUsingAet(targetAet));
157 }
158
159 if (originatorId != 0)
160 {
161 job_->SetMoveOriginator(originatorAet, originatorId);
162 }
163
164 for (size_t i = 0; i < publicIds.size(); i++)
165 {
166 LOG(INFO) << "Sending resource " << publicIds[i] << " to modality \""
167 << targetAet << "\" in asynchronous mode";
168
169 std::list<std::string> tmp;
170 context_.GetIndex().GetChildInstances(tmp, publicIds[i]);
171
172 countInstances_ = tmp.size();
173
174 job_->Reserve(job_->GetCommandsCount() + tmp.size());
175
176 for (std::list<std::string>::iterator it = tmp.begin(); it != tmp.end(); ++it)
177 {
178 job_->AddInstance(*it);
179 }
180 }
181 }
182
183 virtual unsigned int GetSubOperationCount() const
184 {
185 return countInstances_;
186 }
187
188 virtual Status DoNext()
189 {
190 if (position_ >= countInstances_)
191 {
192 return Status_Failure;
193 }
194
195 if (position_ == 0)
196 {
197 context_.GetJobsEngine().GetRegistry().Submit(job_.release(), 0 /* priority */);
198 }
199
200 position_ ++;
201 return Status_Success;
202 }
203 };
204 }
205
206
207 bool OrthancMoveRequestHandler::LookupIdentifiers(std::vector<std::string>& publicIds,
208 ResourceType level,
209 const DicomMap& input)
210 {
211 DicomTag tag(0, 0); // Dummy initialization
212
213 switch (level)
214 {
215 case ResourceType_Patient:
216 tag = DICOM_TAG_PATIENT_ID;
217 break;
218
219 case ResourceType_Study:
220 tag = (input.HasTag(DICOM_TAG_ACCESSION_NUMBER) ?
221 DICOM_TAG_ACCESSION_NUMBER : DICOM_TAG_STUDY_INSTANCE_UID);
222 break;
223
224 case ResourceType_Series:
225 tag = DICOM_TAG_SERIES_INSTANCE_UID;
226 break;
227
228 case ResourceType_Instance:
229 tag = DICOM_TAG_SOP_INSTANCE_UID;
230 break;
231
232 default:
233 throw OrthancException(ErrorCode_ParameterOutOfRange);
234 }
235
236 if (!input.HasTag(tag))
237 {
238 return false;
239 }
240
241 const DicomValue& value = input.GetValue(tag);
242 if (value.IsNull() ||
243 value.IsBinary())
244 {
245 return false;
246 }
247 else
248 {
249 const std::string& content = value.GetContent();
250
251 /**
252 * This tokenization fixes issue 154 ("Matching against list of
253 * UID-s by C-MOVE").
254 * https://bitbucket.org/sjodogne/orthanc/issues/154/
255 **/
256
257 std::vector<std::string> tokens;
258 Toolbox::TokenizeString(tokens, content, '\\');
259 for (size_t i = 0; i < tokens.size(); i++)
260 {
261 std::vector<std::string> matches;
262 context_.GetIndex().LookupIdentifierExact(matches, level, tag, tokens[i]);
263
264 // Concatenate "publicIds" with "matches"
265 publicIds.insert(publicIds.end(), matches.begin(), matches.end());
266 }
267
268 return true;
269 }
270 }
271
272
273 static IMoveRequestIterator* CreateIterator(ServerContext& context,
274 const std::string& targetAet,
275 const std::vector<std::string>& publicIds,
276 const std::string& originatorAet,
277 uint16_t originatorId)
278 {
279 if (publicIds.empty())
280 {
281 throw OrthancException(ErrorCode_BadRequest,
282 "C-MOVE request matching no resource stored in Orthanc");
283 }
284
285 bool synchronous;
286
287 {
288 OrthancConfiguration::ReaderLock lock;
289 synchronous = lock.GetConfiguration().GetBooleanParameter("SynchronousCMove", true);
290 }
291
292 if (synchronous)
293 {
294 return new SynchronousMove(context, targetAet, publicIds, originatorAet, originatorId);
295 }
296 else
297 {
298 return new AsynchronousMove(context, targetAet, publicIds, originatorAet, originatorId);
299 }
300 }
301
302
303 IMoveRequestIterator* OrthancMoveRequestHandler::Handle(const std::string& targetAet,
304 const DicomMap& input,
305 const std::string& originatorIp,
306 const std::string& originatorAet,
307 const std::string& calledAet,
308 uint16_t originatorId)
309 {
310 MetricsRegistry::Timer timer(context_.GetMetricsRegistry(), "orthanc_move_scp_duration_ms");
311
312 LOG(WARNING) << "Move-SCU request received for AET \"" << targetAet << "\"";
313
314 {
315 DicomArray query(input);
316 for (size_t i = 0; i < query.GetSize(); i++)
317 {
318 if (!query.GetElement(i).GetValue().IsNull())
319 {
320 LOG(INFO) << " " << query.GetElement(i).GetTag()
321 << " " << FromDcmtkBridge::GetTagName(query.GetElement(i))
322 << " = " << query.GetElement(i).GetValue().GetContent();
323 }
324 }
325 }
326
327 /**
328 * Retrieve the query level.
329 **/
330
331 const DicomValue* levelTmp = input.TestAndGetValue(DICOM_TAG_QUERY_RETRIEVE_LEVEL);
332
333 if (levelTmp == NULL ||
334 levelTmp->IsNull() ||
335 levelTmp->IsBinary())
336 {
337 // The query level is not present in the C-Move request, which
338 // does not follow the DICOM standard. This is for instance the
339 // behavior of Tudor DICOM. Try and automatically deduce the
340 // query level: Start from the instance level, going up to the
341 // patient level until a valid DICOM identifier is found.
342
343 std::vector<std::string> publicIds;
344
345 if (LookupIdentifiers(publicIds, ResourceType_Instance, input) ||
346 LookupIdentifiers(publicIds, ResourceType_Series, input) ||
347 LookupIdentifiers(publicIds, ResourceType_Study, input) ||
348 LookupIdentifiers(publicIds, ResourceType_Patient, input))
349 {
350 return CreateIterator(context_, targetAet, publicIds, originatorAet, originatorId);
351 }
352 else
353 {
354 // No identifier is present in the request.
355 throw OrthancException(ErrorCode_BadRequest, "Invalid fields in a C-MOVE request");
356 }
357 }
358
359 assert(levelTmp != NULL);
360 ResourceType level = StringToResourceType(levelTmp->GetContent().c_str());
361
362
363 /**
364 * Lookup for the resource to be sent.
365 **/
366
367 std::vector<std::string> publicIds;
368
369 if (LookupIdentifiers(publicIds, level, input))
370 {
371 return CreateIterator(context_, targetAet, publicIds, originatorAet, originatorId);
372 }
373 else
374 {
375 throw OrthancException(ErrorCode_BadRequest, "Invalid fields in a C-MOVE request");
376 }
377 }
378 }