comparison OrthancServer/ServerContext.cpp @ 759:8cfc6119a5bd dicom-rt

integration mainline -> dicom-rt
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 16 Apr 2014 16:04:55 +0200
parents 309e686b41e7
children 537837f50fbb
comparison
equal deleted inserted replaced
605:b82292ba2083 759:8cfc6119a5bd
1 /** 1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store 2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2013 Medical Physics Department, CHU of Liege, 3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
4 * Belgium 4 * Belgium
5 * 5 *
6 * This program is free software: you can redistribute it and/or 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 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 8 * published by the Free Software Foundation, either version 3 of the
59 ServerContext::ServerContext(const boost::filesystem::path& storagePath, 59 ServerContext::ServerContext(const boost::filesystem::path& storagePath,
60 const boost::filesystem::path& indexPath) : 60 const boost::filesystem::path& indexPath) :
61 storage_(storagePath.string()), 61 storage_(storagePath.string()),
62 index_(*this, indexPath.string()), 62 index_(*this, indexPath.string()),
63 accessor_(storage_), 63 accessor_(storage_),
64 compressionEnabled_(false),
64 provider_(*this), 65 provider_(*this),
65 dicomCache_(provider_, DICOM_CACHE_SIZE) 66 dicomCache_(provider_, DICOM_CACHE_SIZE)
66 { 67 {
67 lua_.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX); 68 lua_.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX);
68 } 69 }
113 { 114 {
114 accessor_.SetCompressionForNextOperations(CompressionType_None); 115 accessor_.SetCompressionForNextOperations(CompressionType_None);
115 } 116 }
116 117
117 FileInfo dicomInfo = accessor_.Write(dicomInstance, dicomSize, FileContentType_Dicom); 118 FileInfo dicomInfo = accessor_.Write(dicomInstance, dicomSize, FileContentType_Dicom);
118 FileInfo jsonInfo = accessor_.Write(dicomJson.toStyledString(), FileContentType_Json); 119 FileInfo jsonInfo = accessor_.Write(dicomJson.toStyledString(), FileContentType_DicomAsJson);
119 120
120 ServerIndex::Attachments attachments; 121 ServerIndex::Attachments attachments;
121 attachments.push_back(dicomInfo); 122 attachments.push_back(dicomInfo);
122 attachments.push_back(jsonInfo); 123 attachments.push_back(jsonInfo);
123 124
150 151
151 return status; 152 return status;
152 } 153 }
153 154
154 155
155 void ServerContext::AnswerFile(RestApiOutput& output, 156 void ServerContext::AnswerDicomFile(RestApiOutput& output,
156 const std::string& instancePublicId, 157 const std::string& instancePublicId,
157 FileContentType content) 158 FileContentType content)
158 { 159 {
159 FileInfo attachment; 160 FileInfo attachment;
160 if (!index_.LookupAttachment(attachment, instancePublicId, content)) 161 if (!index_.LookupAttachment(attachment, instancePublicId, content))
161 { 162 {
162 throw OrthancException(ErrorCode_InternalError); 163 throw OrthancException(ErrorCode_InternalError);
173 174
174 void ServerContext::ReadJson(Json::Value& result, 175 void ServerContext::ReadJson(Json::Value& result,
175 const std::string& instancePublicId) 176 const std::string& instancePublicId)
176 { 177 {
177 std::string s; 178 std::string s;
178 ReadFile(s, instancePublicId, FileContentType_Json); 179 ReadFile(s, instancePublicId, FileContentType_DicomAsJson);
179 180
180 Json::Reader reader; 181 Json::Reader reader;
181 if (!reader.parse(s, result)) 182 if (!reader.parse(s, result))
182 { 183 {
183 throw OrthancException("Corrupted JSON file"); 184 throw OrthancException("Corrupted JSON file");
185 } 186 }
186 187
187 188
188 void ServerContext::ReadFile(std::string& result, 189 void ServerContext::ReadFile(std::string& result,
189 const std::string& instancePublicId, 190 const std::string& instancePublicId,
190 FileContentType content) 191 FileContentType content,
192 bool uncompressIfNeeded)
191 { 193 {
192 FileInfo attachment; 194 FileInfo attachment;
193 if (!index_.LookupAttachment(attachment, instancePublicId, content)) 195 if (!index_.LookupAttachment(attachment, instancePublicId, content))
194 { 196 {
195 throw OrthancException(ErrorCode_InternalError); 197 throw OrthancException(ErrorCode_InternalError);
196 } 198 }
197 199
198 accessor_.SetCompressionForNextOperations(attachment.GetCompressionType()); 200 if (uncompressIfNeeded)
201 {
202 accessor_.SetCompressionForNextOperations(attachment.GetCompressionType());
203 }
204 else
205 {
206 accessor_.SetCompressionForNextOperations(CompressionType_None);
207 }
208
199 accessor_.Read(result, attachment.GetUuid()); 209 accessor_.Read(result, attachment.GetUuid());
200 } 210 }
201 211
202 212
203 IDynamicObject* ServerContext::DicomCacheProvider::Provide(const std::string& instancePublicId) 213 IDynamicObject* ServerContext::DicomCacheProvider::Provide(const std::string& instancePublicId)
226 size_t dicomSize) 236 size_t dicomSize)
227 { 237 {
228 DicomMap dicomSummary; 238 DicomMap dicomSummary;
229 FromDcmtkBridge::Convert(dicomSummary, *dicomInstance.getDataset()); 239 FromDcmtkBridge::Convert(dicomSummary, *dicomInstance.getDataset());
230 240
231 DicomInstanceHasher hasher(dicomSummary); 241 try
232 resultPublicId = hasher.HashInstance(); 242 {
233 243 DicomInstanceHasher hasher(dicomSummary);
234 Json::Value dicomJson; 244 resultPublicId = hasher.HashInstance();
235 FromDcmtkBridge::ToJson(dicomJson, *dicomInstance.getDataset()); 245
246 Json::Value dicomJson;
247 FromDcmtkBridge::ToJson(dicomJson, *dicomInstance.getDataset());
236 248
237 StoreStatus status = StoreStatus_Failure; 249 StoreStatus status = StoreStatus_Failure;
238 if (dicomSize > 0) 250 if (dicomSize > 0)
239 { 251 {
240 status = Store(dicomBuffer, dicomSize, dicomSummary, dicomJson, ""); 252 status = Store(dicomBuffer, dicomSize, dicomSummary, dicomJson, "");
241 } 253 }
242 254
243 return status; 255 return status;
256 }
257 catch (OrthancException& e)
258 {
259 if (e.GetErrorCode() == ErrorCode_InexistentTag)
260 {
261 LogMissingRequiredTag(dicomSummary);
262 }
263
264 throw e;
265 }
244 } 266 }
245 267
246 268
247 StoreStatus ServerContext::Store(std::string& resultPublicId, 269 StoreStatus ServerContext::Store(std::string& resultPublicId,
248 DcmFileFormat& dicomInstance) 270 DcmFileFormat& dicomInstance)
266 { 288 {
267 ParsedDicomFile dicom(dicomBuffer, dicomSize); 289 ParsedDicomFile dicom(dicomBuffer, dicomSize);
268 return Store(resultPublicId, dicom.GetDicom(), dicomBuffer, dicomSize); 290 return Store(resultPublicId, dicom.GetDicom(), dicomBuffer, dicomSize);
269 } 291 }
270 292
293 void ServerContext::SetStoreMD5ForAttachments(bool storeMD5)
294 {
295 LOG(INFO) << "Storing MD5 for attachments: " << (storeMD5 ? "yes" : "no");
296 accessor_.SetStoreMD5(storeMD5);
297 }
298
299
300 bool ServerContext::AddAttachment(const std::string& resourceId,
301 FileContentType attachmentType,
302 const void* data,
303 size_t size)
304 {
305 LOG(INFO) << "Adding attachment " << EnumerationToString(attachmentType) << " to resource " << resourceId;
306
307 if (compressionEnabled_)
308 {
309 accessor_.SetCompressionForNextOperations(CompressionType_Zlib);
310 }
311 else
312 {
313 accessor_.SetCompressionForNextOperations(CompressionType_None);
314 }
315
316 FileInfo info = accessor_.Write(data, size, attachmentType);
317 StoreStatus status = index_.AddAttachment(info, resourceId);
318
319 if (status != StoreStatus_Success)
320 {
321 storage_.Remove(info.GetUuid());
322 return false;
323 }
324 else
325 {
326 return true;
327 }
328 }
271 } 329 }