comparison PalanthirServer/PalanthirRestApi.cpp @ 50:a15e90e5d6fc

rename in code
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 05 Sep 2012 15:50:12 +0200
parents 491a97842e31
children 293038baf8f1
comparison
equal deleted inserted replaced
49:e1a3ae0dadf3 50:a15e90e5d6fc
1 /** 1 /**
2 * Palantir - A Lightweight, RESTful DICOM Store 2 * Palanthir - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege, 3 * Copyright (C) 2012 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
16 * You should have received a copy of the GNU General Public License 16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 **/ 18 **/
19 19
20 20
21 #include "PalantirRestApi.h" 21 #include "PalanthirRestApi.h"
22 22
23 #include "PalantirInitialization.h" 23 #include "PalanthirInitialization.h"
24 #include "FromDcmtkBridge.h" 24 #include "FromDcmtkBridge.h"
25 #include "../Core/Uuid.h" 25 #include "../Core/Uuid.h"
26 26
27 #include <dcmtk/dcmdata/dcistrmb.h> 27 #include <dcmtk/dcmdata/dcistrmb.h>
28 #include <dcmtk/dcmdata/dcfilefo.h> 28 #include <dcmtk/dcmdata/dcfilefo.h>
29 #include <boost/lexical_cast.hpp> 29 #include <boost/lexical_cast.hpp>
30 30
31 namespace Palantir 31 namespace Palanthir
32 { 32 {
33 static void SendJson(HttpOutput& output, 33 static void SendJson(HttpOutput& output,
34 const Json::Value& value) 34 const Json::Value& value)
35 { 35 {
36 Json::StyledWriter writer; 36 Json::StyledWriter writer;
94 94
95 Json::Value source; 95 Json::Value source;
96 Json::Reader reader; 96 Json::Reader reader;
97 if (!reader.parse(s, source)) 97 if (!reader.parse(s, source))
98 { 98 {
99 throw PalantirException("Corrupted JSON file"); 99 throw PalanthirException("Corrupted JSON file");
100 } 100 }
101 101
102 SimplifyTagsRecursion(target, source); 102 SimplifyTagsRecursion(target, source);
103 } 103 }
104 104
105 105
106 bool PalantirRestApi::Store(Json::Value& result, 106 bool PalanthirRestApi::Store(Json::Value& result,
107 const std::string& postData) 107 const std::string& postData)
108 { 108 {
109 // Prepare an input stream for the memory buffer 109 // Prepare an input stream for the memory buffer
110 DcmInputBufferStream is; 110 DcmInputBufferStream is;
111 if (postData.size() > 0) 111 if (postData.size() > 0)
112 { 112 {
154 } 154 }
155 155
156 return false; 156 return false;
157 } 157 }
158 158
159 void PalantirRestApi::ConnectToModality(DicomUserConnection& c, 159 void PalanthirRestApi::ConnectToModality(DicomUserConnection& c,
160 const std::string& name) 160 const std::string& name)
161 { 161 {
162 std::string aet, address; 162 std::string aet, address;
163 int port; 163 int port;
164 GetDicomModality(name, aet, address, port); 164 GetDicomModality(name, aet, address, port);
165 c.SetLocalApplicationEntityTitle(GetGlobalStringParameter("DicomAet", "PALANTIR")); 165 c.SetLocalApplicationEntityTitle(GetGlobalStringParameter("DicomAet", "PALANTHIR"));
166 c.SetDistantApplicationEntityTitle(aet); 166 c.SetDistantApplicationEntityTitle(aet);
167 c.SetDistantHost(address); 167 c.SetDistantHost(address);
168 c.SetDistantPort(port); 168 c.SetDistantPort(port);
169 c.Open(); 169 c.Open();
170 } 170 }
171 171
172 bool PalantirRestApi::MergeQueryAndTemplate(DicomMap& result, 172 bool PalanthirRestApi::MergeQueryAndTemplate(DicomMap& result,
173 const std::string& postData) 173 const std::string& postData)
174 { 174 {
175 Json::Value query; 175 Json::Value query;
176 Json::Reader reader; 176 Json::Reader reader;
177 177
178 if (!reader.parse(postData, query) || 178 if (!reader.parse(postData, query) ||
189 } 189 }
190 190
191 return true; 191 return true;
192 } 192 }
193 193
194 bool PalantirRestApi::DicomFindPatient(Json::Value& result, 194 bool PalanthirRestApi::DicomFindPatient(Json::Value& result,
195 DicomUserConnection& c, 195 DicomUserConnection& c,
196 const std::string& postData) 196 const std::string& postData)
197 { 197 {
198 DicomMap m; 198 DicomMap m;
199 DicomMap::SetupFindPatientTemplate(m); 199 DicomMap::SetupFindPatientTemplate(m);
200 if (!MergeQueryAndTemplate(m, postData)) 200 if (!MergeQueryAndTemplate(m, postData))
201 { 201 {
206 c.FindPatient(answers, m); 206 c.FindPatient(answers, m);
207 answers.ToJson(result); 207 answers.ToJson(result);
208 return true; 208 return true;
209 } 209 }
210 210
211 bool PalantirRestApi::DicomFindStudy(Json::Value& result, 211 bool PalanthirRestApi::DicomFindStudy(Json::Value& result,
212 DicomUserConnection& c, 212 DicomUserConnection& c,
213 const std::string& postData) 213 const std::string& postData)
214 { 214 {
215 DicomMap m; 215 DicomMap m;
216 DicomMap::SetupFindStudyTemplate(m); 216 DicomMap::SetupFindStudyTemplate(m);
217 if (!MergeQueryAndTemplate(m, postData)) 217 if (!MergeQueryAndTemplate(m, postData))
218 { 218 {
229 c.FindStudy(answers, m); 229 c.FindStudy(answers, m);
230 answers.ToJson(result); 230 answers.ToJson(result);
231 return true; 231 return true;
232 } 232 }
233 233
234 bool PalantirRestApi::DicomFindSeries(Json::Value& result, 234 bool PalanthirRestApi::DicomFindSeries(Json::Value& result,
235 DicomUserConnection& c, 235 DicomUserConnection& c,
236 const std::string& postData) 236 const std::string& postData)
237 { 237 {
238 DicomMap m; 238 DicomMap m;
239 DicomMap::SetupFindSeriesTemplate(m); 239 DicomMap::SetupFindSeriesTemplate(m);
240 if (!MergeQueryAndTemplate(m, postData)) 240 if (!MergeQueryAndTemplate(m, postData))
241 { 241 {
253 c.FindSeries(answers, m); 253 c.FindSeries(answers, m);
254 answers.ToJson(result); 254 answers.ToJson(result);
255 return true; 255 return true;
256 } 256 }
257 257
258 bool PalantirRestApi::DicomFind(Json::Value& result, 258 bool PalanthirRestApi::DicomFind(Json::Value& result,
259 DicomUserConnection& c, 259 DicomUserConnection& c,
260 const std::string& postData) 260 const std::string& postData)
261 { 261 {
262 DicomMap m; 262 DicomMap m;
263 DicomMap::SetupFindPatientTemplate(m); 263 DicomMap::SetupFindPatientTemplate(m);
264 if (!MergeQueryAndTemplate(m, postData)) 264 if (!MergeQueryAndTemplate(m, postData))
265 { 265 {
323 return true; 323 return true;
324 } 324 }
325 325
326 326
327 327
328 bool PalantirRestApi::DicomStore(Json::Value& result, 328 bool PalanthirRestApi::DicomStore(Json::Value& result,
329 DicomUserConnection& c, 329 DicomUserConnection& c,
330 const std::string& postData) 330 const std::string& postData)
331 { 331 {
332 Json::Value found(Json::objectValue); 332 Json::Value found(Json::objectValue);
333 333
334 if (!Toolbox::IsUuid(postData)) 334 if (!Toolbox::IsUuid(postData))
335 { 335 {
369 369
370 return true; 370 return true;
371 } 371 }
372 372
373 373
374 PalantirRestApi::PalantirRestApi(ServerIndex& index, 374 PalanthirRestApi::PalanthirRestApi(ServerIndex& index,
375 const std::string& path) : 375 const std::string& path) :
376 index_(index), 376 index_(index),
377 storage_(path) 377 storage_(path)
378 { 378 {
379 GetListOfDicomModalities(modalities_); 379 GetListOfDicomModalities(modalities_);
380 } 380 }
381 381
382 382
383 void PalantirRestApi::Handle( 383 void PalanthirRestApi::Handle(
384 HttpOutput& output, 384 HttpOutput& output,
385 const std::string& method, 385 const std::string& method,
386 const UriComponents& uri, 386 const UriComponents& uri,
387 const Arguments& headers, 387 const Arguments& headers,
388 const Arguments& arguments, 388 const Arguments& arguments,
424 SendJson(output, result); 424 SendJson(output, result);
425 return; 425 return;
426 } 426 }
427 else 427 else
428 { 428 {
429 output.SendHeader(Palantir_HttpStatus_415_UnsupportedMediaType); 429 output.SendHeader(Palanthir_HttpStatus_415_UnsupportedMediaType);
430 return; 430 return;
431 } 431 }
432 } 432 }
433 else 433 else
434 { 434 {
593 { 593 {
594 FromDcmtkBridge::ExtractPngImage(png, dicomContent, ImageExtractionMode_UInt16); 594 FromDcmtkBridge::ExtractPngImage(png, dicomContent, ImageExtractionMode_UInt16);
595 } 595 }
596 else 596 else
597 { 597 {
598 throw PalantirException(ErrorCode_InternalError); 598 throw PalanthirException(ErrorCode_InternalError);
599 } 599 }
600 600
601 output.AnswerBufferWithContentType(png, "image/png"); 601 output.AnswerBufferWithContentType(png, "image/png");
602 return; 602 return;
603 } 603 }
604 catch (PalantirException&) 604 catch (PalanthirException&)
605 { 605 {
606 output.Redirect("/app/images/Unsupported.png"); 606 output.Redirect("/app/images/Unsupported.png");
607 return; 607 return;
608 } 608 }
609 } 609 }
627 since = boost::lexical_cast<int64_t>(GetArgument(arguments, "since", "0")); 627 since = boost::lexical_cast<int64_t>(GetArgument(arguments, "since", "0"));
628 limit = boost::lexical_cast<unsigned int>(GetArgument(arguments, "limit", "0")); 628 limit = boost::lexical_cast<unsigned int>(GetArgument(arguments, "limit", "0"));
629 } 629 }
630 catch (boost::bad_lexical_cast) 630 catch (boost::bad_lexical_cast)
631 { 631 {
632 output.SendHeader(Palantir_HttpStatus_400_BadRequest); 632 output.SendHeader(Palanthir_HttpStatus_400_BadRequest);
633 return; 633 return;
634 } 634 }
635 635
636 if (limit == 0 || limit > MAX_RESULTS) 636 if (limit == 0 || limit > MAX_RESULTS)
637 { 637 {
638 limit = MAX_RESULTS; 638 limit = MAX_RESULTS;
639 } 639 }
640 640
641 if (!index_.GetChanges(result, since, filter, limit)) 641 if (!index_.GetChanges(result, since, filter, limit))
642 { 642 {
643 output.SendHeader(Palantir_HttpStatus_400_BadRequest); 643 output.SendHeader(Palanthir_HttpStatus_400_BadRequest);
644 return; 644 return;
645 } 645 }
646 646
647 existingResource = true; 647 existingResource = true;
648 } 648 }
728 (uri[2] == "find-study" && !DicomFindStudy(result, connection, postData)) || 728 (uri[2] == "find-study" && !DicomFindStudy(result, connection, postData)) ||
729 (uri[2] == "find-series" && !DicomFindSeries(result, connection, postData)) || 729 (uri[2] == "find-series" && !DicomFindSeries(result, connection, postData)) ||
730 (uri[2] == "find" && !DicomFind(result, connection, postData)) || 730 (uri[2] == "find" && !DicomFind(result, connection, postData)) ||
731 (uri[2] == "store" && !DicomStore(result, connection, postData))) 731 (uri[2] == "store" && !DicomStore(result, connection, postData)))
732 { 732 {
733 output.SendHeader(Palantir_HttpStatus_400_BadRequest); 733 output.SendHeader(Palanthir_HttpStatus_400_BadRequest);
734 return; 734 return;
735 } 735 }
736 } 736 }
737 } 737 }
738 } 738 }
742 { 742 {
743 SendJson(output, result); 743 SendJson(output, result);
744 } 744 }
745 else 745 else
746 { 746 {
747 output.SendHeader(Palantir_HttpStatus_404_NotFound); 747 output.SendHeader(Palanthir_HttpStatus_404_NotFound);
748 } 748 }
749 } 749 }
750 } 750 }