Mercurial > hg > orthanc
comparison OrthancServer/OrthancRestApi/OrthancRestResources.cpp @ 2905:ae20fccdd867
refactoring mime types
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 30 Oct 2018 11:55:23 +0100 |
parents | ae8e72009e64 |
children | 9d277f8ad698 |
comparison
equal
deleted
inserted
replaced
2904:0dd54ee073db | 2905:ae20fccdd867 |
---|---|
199 | 199 |
200 static void IsProtectedPatient(RestApiGetCall& call) | 200 static void IsProtectedPatient(RestApiGetCall& call) |
201 { | 201 { |
202 std::string publicId = call.GetUriComponent("id", ""); | 202 std::string publicId = call.GetUriComponent("id", ""); |
203 bool isProtected = OrthancRestApi::GetIndex(call).IsProtectedPatient(publicId); | 203 bool isProtected = OrthancRestApi::GetIndex(call).IsProtectedPatient(publicId); |
204 call.GetOutput().AnswerBuffer(isProtected ? "1" : "0", "text/plain"); | 204 call.GetOutput().AnswerBuffer(isProtected ? "1" : "0", MIME_PLAIN_TEXT); |
205 } | 205 } |
206 | 206 |
207 | 207 |
208 static void SetPatientProtection(RestApiPutCall& call) | 208 static void SetPatientProtection(RestApiPutCall& call) |
209 { | 209 { |
216 body = Toolbox::StripSpaces(body); | 216 body = Toolbox::StripSpaces(body); |
217 | 217 |
218 if (body == "0") | 218 if (body == "0") |
219 { | 219 { |
220 context.GetIndex().SetProtectedPatient(publicId, false); | 220 context.GetIndex().SetProtectedPatient(publicId, false); |
221 call.GetOutput().AnswerBuffer("", "text/plain"); | 221 call.GetOutput().AnswerBuffer("", MIME_PLAIN_TEXT); |
222 } | 222 } |
223 else if (body == "1") | 223 else if (body == "1") |
224 { | 224 { |
225 context.GetIndex().SetProtectedPatient(publicId, true); | 225 context.GetIndex().SetProtectedPatient(publicId, true); |
226 call.GetOutput().AnswerBuffer("", "text/plain"); | 226 call.GetOutput().AnswerBuffer("", MIME_PLAIN_TEXT); |
227 } | 227 } |
228 else | 228 else |
229 { | 229 { |
230 // Bad request | 230 // Bad request |
231 } | 231 } |
254 | 254 |
255 std::string target; | 255 std::string target; |
256 call.BodyToString(target); | 256 call.BodyToString(target); |
257 SystemToolbox::WriteFile(dicom, target); | 257 SystemToolbox::WriteFile(dicom, target); |
258 | 258 |
259 call.GetOutput().AnswerBuffer("{}", "application/json"); | 259 call.GetOutput().AnswerBuffer("{}", MIME_JSON); |
260 } | 260 } |
261 | 261 |
262 | 262 |
263 template <DicomToJsonFormat format> | 263 template <DicomToJsonFormat format> |
264 static void GetInstanceTags(RestApiGetCall& call) | 264 static void GetInstanceTags(RestApiGetCall& call) |
282 // This path allows to avoid the JSON decoding if no | 282 // This path allows to avoid the JSON decoding if no |
283 // simplification is asked, and if no "ignore-length" argument | 283 // simplification is asked, and if no "ignore-length" argument |
284 // is present | 284 // is present |
285 std::string full; | 285 std::string full; |
286 context.ReadDicomAsJson(full, publicId); | 286 context.ReadDicomAsJson(full, publicId); |
287 call.GetOutput().AnswerBuffer(full, "application/json"); | 287 call.GetOutput().AnswerBuffer(full, MIME_JSON); |
288 } | 288 } |
289 } | 289 } |
290 | 290 |
291 | 291 |
292 static void GetInstanceTagsBis(RestApiGetCall& call) | 292 static void GetInstanceTagsBis(RestApiGetCall& call) |
358 output.AnswerBuffer(answer_, format_); | 358 output.AnswerBuffer(answer_, format_); |
359 } | 359 } |
360 | 360 |
361 void EncodeUsingPng() | 361 void EncodeUsingPng() |
362 { | 362 { |
363 format_ = "image/png"; | 363 format_ = MIME_PNG; |
364 DicomImageDecoder::ExtractPngImage(answer_, image_, mode_, invert_); | 364 DicomImageDecoder::ExtractPngImage(answer_, image_, mode_, invert_); |
365 } | 365 } |
366 | 366 |
367 void EncodeUsingPam() | 367 void EncodeUsingPam() |
368 { | 368 { |
369 /** | 369 format_ = MIME_PAM; |
370 * "No Internet Media Type (aka MIME type, content type) for | |
371 * PBM has been registered with IANA, but the unofficial value | |
372 * image/x-portable-arbitrarymap is assigned by this | |
373 * specification, to be consistent with conventional values | |
374 * for the older Netpbm formats." | |
375 * http://netpbm.sourceforge.net/doc/pam.html | |
376 **/ | |
377 format_ = "image/x-portable-arbitrarymap"; | |
378 DicomImageDecoder::ExtractPamImage(answer_, image_, mode_, invert_); | 370 DicomImageDecoder::ExtractPamImage(answer_, image_, mode_, invert_); |
379 } | 371 } |
380 | 372 |
381 void EncodeUsingJpeg(uint8_t quality) | 373 void EncodeUsingJpeg(uint8_t quality) |
382 { | 374 { |
383 format_ = "image/jpeg"; | 375 format_ = MIME_JPEG; |
384 DicomImageDecoder::ExtractJpegImage(answer_, image_, mode_, invert_, quality); | 376 DicomImageDecoder::ExtractJpegImage(answer_, image_, mode_, invert_, quality); |
385 } | 377 } |
386 }; | 378 }; |
387 | 379 |
388 class EncodePng : public HttpContentNegociation::IHandler | 380 class EncodePng : public HttpContentNegociation::IHandler |
556 | 548 |
557 ImageToEncode image(decoded, mode, invert); | 549 ImageToEncode image(decoded, mode, invert); |
558 | 550 |
559 HttpContentNegociation negociation; | 551 HttpContentNegociation negociation; |
560 EncodePng png(image); | 552 EncodePng png(image); |
561 negociation.Register("image/png", png); | 553 negociation.Register(MIME_PNG, png); |
562 | 554 |
563 EncodeJpeg jpeg(image, call); | 555 EncodeJpeg jpeg(image, call); |
564 negociation.Register("image/jpeg", jpeg); | 556 negociation.Register(MIME_JPEG, jpeg); |
565 | 557 |
566 EncodePam pam(image); | 558 EncodePam pam(image); |
567 negociation.Register("image/x-portable-arbitrarymap", pam); | 559 negociation.Register(MIME_PAM, pam); |
568 | 560 |
569 if (negociation.Apply(call.GetHttpHeaders())) | 561 if (negociation.Apply(call.GetHttpHeaders())) |
570 { | 562 { |
571 image.Answer(call.GetOutput()); | 563 image.Answer(call.GetOutput()); |
572 } | 564 } |
602 std::auto_ptr<ImageAccessor> decoded(decoder.Decode(dicomContent.c_str(), dicomContent.size(), frame)); | 594 std::auto_ptr<ImageAccessor> decoded(decoder.Decode(dicomContent.c_str(), dicomContent.size(), frame)); |
603 | 595 |
604 std::string result; | 596 std::string result; |
605 decoded->ToMatlabString(result); | 597 decoded->ToMatlabString(result); |
606 | 598 |
607 call.GetOutput().AnswerBuffer(result, "text/plain"); | 599 call.GetOutput().AnswerBuffer(result, MIME_PLAIN_TEXT); |
608 } | 600 } |
609 | 601 |
610 | 602 |
611 template <bool GzipCompression> | 603 template <bool GzipCompression> |
612 static void GetRawFrame(RestApiGetCall& call) | 604 static void GetRawFrame(RestApiGetCall& call) |
715 MetadataType metadata = StringToMetadata(name); | 707 MetadataType metadata = StringToMetadata(name); |
716 | 708 |
717 std::string value; | 709 std::string value; |
718 if (OrthancRestApi::GetIndex(call).LookupMetadata(value, publicId, metadata)) | 710 if (OrthancRestApi::GetIndex(call).LookupMetadata(value, publicId, metadata)) |
719 { | 711 { |
720 call.GetOutput().AnswerBuffer(value, "text/plain"); | 712 call.GetOutput().AnswerBuffer(value, MIME_PLAIN_TEXT); |
721 } | 713 } |
722 } | 714 } |
723 | 715 |
724 | 716 |
725 static void DeleteMetadata(RestApiDeleteCall& call) | 717 static void DeleteMetadata(RestApiDeleteCall& call) |
731 MetadataType metadata = StringToMetadata(name); | 723 MetadataType metadata = StringToMetadata(name); |
732 | 724 |
733 if (IsUserMetadata(metadata)) // It is forbidden to modify internal metadata | 725 if (IsUserMetadata(metadata)) // It is forbidden to modify internal metadata |
734 { | 726 { |
735 OrthancRestApi::GetIndex(call).DeleteMetadata(publicId, metadata); | 727 OrthancRestApi::GetIndex(call).DeleteMetadata(publicId, metadata); |
736 call.GetOutput().AnswerBuffer("", "text/plain"); | 728 call.GetOutput().AnswerBuffer("", MIME_PLAIN_TEXT); |
737 } | 729 } |
738 else | 730 else |
739 { | 731 { |
740 call.GetOutput().SignalError(HttpStatus_403_Forbidden); | 732 call.GetOutput().SignalError(HttpStatus_403_Forbidden); |
741 } | 733 } |
755 | 747 |
756 if (IsUserMetadata(metadata)) // It is forbidden to modify internal metadata | 748 if (IsUserMetadata(metadata)) // It is forbidden to modify internal metadata |
757 { | 749 { |
758 // It is forbidden to modify internal metadata | 750 // It is forbidden to modify internal metadata |
759 OrthancRestApi::GetIndex(call).SetMetadata(publicId, metadata, value); | 751 OrthancRestApi::GetIndex(call).SetMetadata(publicId, metadata, value); |
760 call.GetOutput().AnswerBuffer("", "text/plain"); | 752 call.GetOutput().AnswerBuffer("", MIME_PLAIN_TEXT); |
761 } | 753 } |
762 else | 754 else |
763 { | 755 { |
764 call.GetOutput().SignalError(HttpStatus_403_Forbidden); | 756 call.GetOutput().SignalError(HttpStatus_403_Forbidden); |
765 } | 757 } |
856 else | 848 else |
857 { | 849 { |
858 // Return the raw data (possibly compressed), as stored on the filesystem | 850 // Return the raw data (possibly compressed), as stored on the filesystem |
859 std::string content; | 851 std::string content; |
860 context.ReadAttachment(content, publicId, type, false); | 852 context.ReadAttachment(content, publicId, type, false); |
861 call.GetOutput().AnswerBuffer(content, "application/octet-stream"); | 853 call.GetOutput().AnswerBuffer(content, MIME_BINARY); |
862 } | 854 } |
863 } | 855 } |
864 | 856 |
865 | 857 |
866 static void GetAttachmentSize(RestApiGetCall& call) | 858 static void GetAttachmentSize(RestApiGetCall& call) |
867 { | 859 { |
868 FileInfo info; | 860 FileInfo info; |
869 if (GetAttachmentInfo(info, call)) | 861 if (GetAttachmentInfo(info, call)) |
870 { | 862 { |
871 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetUncompressedSize()), "text/plain"); | 863 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetUncompressedSize()), MIME_PLAIN_TEXT); |
872 } | 864 } |
873 } | 865 } |
874 | 866 |
875 | 867 |
876 static void GetAttachmentCompressedSize(RestApiGetCall& call) | 868 static void GetAttachmentCompressedSize(RestApiGetCall& call) |
877 { | 869 { |
878 FileInfo info; | 870 FileInfo info; |
879 if (GetAttachmentInfo(info, call)) | 871 if (GetAttachmentInfo(info, call)) |
880 { | 872 { |
881 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetCompressedSize()), "text/plain"); | 873 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetCompressedSize()), MIME_PLAIN_TEXT); |
882 } | 874 } |
883 } | 875 } |
884 | 876 |
885 | 877 |
886 static void GetAttachmentMD5(RestApiGetCall& call) | 878 static void GetAttachmentMD5(RestApiGetCall& call) |
887 { | 879 { |
888 FileInfo info; | 880 FileInfo info; |
889 if (GetAttachmentInfo(info, call) && | 881 if (GetAttachmentInfo(info, call) && |
890 info.GetUncompressedMD5() != "") | 882 info.GetUncompressedMD5() != "") |
891 { | 883 { |
892 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetUncompressedMD5()), "text/plain"); | 884 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetUncompressedMD5()), MIME_PLAIN_TEXT); |
893 } | 885 } |
894 } | 886 } |
895 | 887 |
896 | 888 |
897 static void GetAttachmentCompressedMD5(RestApiGetCall& call) | 889 static void GetAttachmentCompressedMD5(RestApiGetCall& call) |
898 { | 890 { |
899 FileInfo info; | 891 FileInfo info; |
900 if (GetAttachmentInfo(info, call) && | 892 if (GetAttachmentInfo(info, call) && |
901 info.GetCompressedMD5() != "") | 893 info.GetCompressedMD5() != "") |
902 { | 894 { |
903 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetCompressedMD5()), "text/plain"); | 895 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetCompressedMD5()), MIME_PLAIN_TEXT); |
904 } | 896 } |
905 } | 897 } |
906 | 898 |
907 | 899 |
908 static void VerifyAttachment(RestApiPostCall& call) | 900 static void VerifyAttachment(RestApiPostCall& call) |
948 } | 940 } |
949 | 941 |
950 if (ok) | 942 if (ok) |
951 { | 943 { |
952 LOG(INFO) << "The attachment " << name << " of resource " << publicId << " has the right MD5"; | 944 LOG(INFO) << "The attachment " << name << " of resource " << publicId << " has the right MD5"; |
953 call.GetOutput().AnswerBuffer("{}", "application/json"); | 945 call.GetOutput().AnswerBuffer("{}", MIME_JSON); |
954 } | 946 } |
955 else | 947 else |
956 { | 948 { |
957 LOG(INFO) << "The attachment " << name << " of resource " << publicId << " has bad MD5!"; | 949 LOG(INFO) << "The attachment " << name << " of resource " << publicId << " has bad MD5!"; |
958 } | 950 } |
969 | 961 |
970 FileContentType contentType = StringToContentType(name); | 962 FileContentType contentType = StringToContentType(name); |
971 if (IsUserContentType(contentType) && // It is forbidden to modify internal attachments | 963 if (IsUserContentType(contentType) && // It is forbidden to modify internal attachments |
972 context.AddAttachment(publicId, StringToContentType(name), call.GetBodyData(), call.GetBodySize())) | 964 context.AddAttachment(publicId, StringToContentType(name), call.GetBodyData(), call.GetBodySize())) |
973 { | 965 { |
974 call.GetOutput().AnswerBuffer("{}", "application/json"); | 966 call.GetOutput().AnswerBuffer("{}", MIME_JSON); |
975 } | 967 } |
976 else | 968 else |
977 { | 969 { |
978 call.GetOutput().SignalError(HttpStatus_403_Forbidden); | 970 call.GetOutput().SignalError(HttpStatus_403_Forbidden); |
979 } | 971 } |
1007 } | 999 } |
1008 | 1000 |
1009 if (allowed) | 1001 if (allowed) |
1010 { | 1002 { |
1011 OrthancRestApi::GetIndex(call).DeleteAttachment(publicId, contentType); | 1003 OrthancRestApi::GetIndex(call).DeleteAttachment(publicId, contentType); |
1012 call.GetOutput().AnswerBuffer("{}", "application/json"); | 1004 call.GetOutput().AnswerBuffer("{}", MIME_JSON); |
1013 } | 1005 } |
1014 else | 1006 else |
1015 { | 1007 { |
1016 call.GetOutput().SignalError(HttpStatus_403_Forbidden); | 1008 call.GetOutput().SignalError(HttpStatus_403_Forbidden); |
1017 } | 1009 } |
1026 std::string publicId = call.GetUriComponent("id", ""); | 1018 std::string publicId = call.GetUriComponent("id", ""); |
1027 std::string name = call.GetUriComponent("name", ""); | 1019 std::string name = call.GetUriComponent("name", ""); |
1028 FileContentType contentType = StringToContentType(name); | 1020 FileContentType contentType = StringToContentType(name); |
1029 | 1021 |
1030 OrthancRestApi::GetContext(call).ChangeAttachmentCompression(publicId, contentType, compression); | 1022 OrthancRestApi::GetContext(call).ChangeAttachmentCompression(publicId, contentType, compression); |
1031 call.GetOutput().AnswerBuffer("{}", "application/json"); | 1023 call.GetOutput().AnswerBuffer("{}", MIME_JSON); |
1032 } | 1024 } |
1033 | 1025 |
1034 | 1026 |
1035 static void IsAttachmentCompressed(RestApiGetCall& call) | 1027 static void IsAttachmentCompressed(RestApiGetCall& call) |
1036 { | 1028 { |
1037 FileInfo info; | 1029 FileInfo info; |
1038 if (GetAttachmentInfo(info, call)) | 1030 if (GetAttachmentInfo(info, call)) |
1039 { | 1031 { |
1040 std::string answer = (info.GetCompressionType() == CompressionType_None) ? "0" : "1"; | 1032 std::string answer = (info.GetCompressionType() == CompressionType_None) ? "0" : "1"; |
1041 call.GetOutput().AnswerBuffer(answer, "text/plain"); | 1033 call.GetOutput().AnswerBuffer(answer, MIME_PLAIN_TEXT); |
1042 } | 1034 } |
1043 } | 1035 } |
1044 | 1036 |
1045 | 1037 |
1046 // Raw access to the DICOM tags of an instance ------------------------------ | 1038 // Raw access to the DICOM tags of an instance ------------------------------ |
1473 std::string pdf; | 1465 std::string pdf; |
1474 ServerContext::DicomCacheLocker locker(OrthancRestApi::GetContext(call), id); | 1466 ServerContext::DicomCacheLocker locker(OrthancRestApi::GetContext(call), id); |
1475 | 1467 |
1476 if (locker.GetDicom().ExtractPdf(pdf)) | 1468 if (locker.GetDicom().ExtractPdf(pdf)) |
1477 { | 1469 { |
1478 call.GetOutput().AnswerBuffer(pdf, "application/pdf"); | 1470 call.GetOutput().AnswerBuffer(pdf, MIME_PDF); |
1479 return; | 1471 return; |
1480 } | 1472 } |
1481 } | 1473 } |
1482 | 1474 |
1483 | 1475 |
1535 { | 1527 { |
1536 index.DeleteAttachment(*instance, FileContentType_DicomAsJson); | 1528 index.DeleteAttachment(*instance, FileContentType_DicomAsJson); |
1537 } | 1529 } |
1538 } | 1530 } |
1539 | 1531 |
1540 call.GetOutput().AnswerBuffer("", "text/plain"); | 1532 call.GetOutput().AnswerBuffer("", MIME_PLAIN_TEXT); |
1541 } | 1533 } |
1542 | 1534 |
1543 | 1535 |
1544 template <enum ResourceType type> | 1536 template <enum ResourceType type> |
1545 static void ReconstructResource(RestApiPostCall& call) | 1537 static void ReconstructResource(RestApiPostCall& call) |
1546 { | 1538 { |
1547 ServerContext& context = OrthancRestApi::GetContext(call); | 1539 ServerContext& context = OrthancRestApi::GetContext(call); |
1548 ServerToolbox::ReconstructResource(context, call.GetUriComponent("id", "")); | 1540 ServerToolbox::ReconstructResource(context, call.GetUriComponent("id", "")); |
1549 call.GetOutput().AnswerBuffer("", "text/plain"); | 1541 call.GetOutput().AnswerBuffer("", MIME_PLAIN_TEXT); |
1550 } | 1542 } |
1551 | 1543 |
1552 | 1544 |
1553 static void ReconstructAllResources(RestApiPostCall& call) | 1545 static void ReconstructAllResources(RestApiPostCall& call) |
1554 { | 1546 { |
1561 study = studies.begin(); study != studies.end(); ++study) | 1553 study = studies.begin(); study != studies.end(); ++study) |
1562 { | 1554 { |
1563 ServerToolbox::ReconstructResource(context, *study); | 1555 ServerToolbox::ReconstructResource(context, *study); |
1564 } | 1556 } |
1565 | 1557 |
1566 call.GetOutput().AnswerBuffer("", "text/plain"); | 1558 call.GetOutput().AnswerBuffer("", MIME_PLAIN_TEXT); |
1567 } | 1559 } |
1568 | 1560 |
1569 | 1561 |
1570 void OrthancRestApi::RegisterResources() | 1562 void OrthancRestApi::RegisterResources() |
1571 { | 1563 { |