comparison OrthancServer/OrthancRestApi.cpp @ 697:dd1ce9a2844c

access to attachments
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 05 Feb 2014 16:46:59 +0100
parents 2d0a347e8cfc
children aae83e1e31f7
comparison
equal deleted inserted replaced
696:4c1860179cc5 697:dd1ce9a2844c
827 static void GetInstanceFile(RestApi::GetCall& call) 827 static void GetInstanceFile(RestApi::GetCall& call)
828 { 828 {
829 RETRIEVE_CONTEXT(call); 829 RETRIEVE_CONTEXT(call);
830 830
831 std::string publicId = call.GetUriComponent("id", ""); 831 std::string publicId = call.GetUriComponent("id", "");
832 context.AnswerFile(call.GetOutput(), publicId, FileContentType_Dicom); 832 context.AnswerDicomFile(call.GetOutput(), publicId, FileContentType_Dicom);
833 } 833 }
834 834
835 835
836 static void ExportInstanceFile(RestApi::PostCall& call) 836 static void ExportInstanceFile(RestApi::PostCall& call)
837 { 837 {
1652 } 1652 }
1653 1653
1654 1654
1655 // Handling of metadata ----------------------------------------------------- 1655 // Handling of metadata -----------------------------------------------------
1656 1656
1657 static void CheckValidResourceType(RestApi::Call& call)
1658 {
1659 std::string resourceType = call.GetUriComponent("resourceType", "");
1660 StringToResourceType(resourceType.c_str());
1661 }
1662
1663
1657 static void ListMetadata(RestApi::GetCall& call) 1664 static void ListMetadata(RestApi::GetCall& call)
1658 { 1665 {
1659 RETRIEVE_CONTEXT(call); 1666 RETRIEVE_CONTEXT(call);
1667 CheckValidResourceType(call);
1660 1668
1661 std::string publicId = call.GetUriComponent("id", ""); 1669 std::string publicId = call.GetUriComponent("id", "");
1662 std::list<MetadataType> metadata; 1670 std::list<MetadataType> metadata;
1663 if (context.GetIndex().ListAvailableMetadata(metadata, publicId)) 1671 if (context.GetIndex().ListAvailableMetadata(metadata, publicId))
1664 { 1672 {
1676 1684
1677 1685
1678 static void GetMetadata(RestApi::GetCall& call) 1686 static void GetMetadata(RestApi::GetCall& call)
1679 { 1687 {
1680 RETRIEVE_CONTEXT(call); 1688 RETRIEVE_CONTEXT(call);
1689 CheckValidResourceType(call);
1681 1690
1682 std::string publicId = call.GetUriComponent("id", ""); 1691 std::string publicId = call.GetUriComponent("id", "");
1683 std::string name = call.GetUriComponent("name", ""); 1692 std::string name = call.GetUriComponent("name", "");
1684 MetadataType metadata = StringToMetadata(name); 1693 MetadataType metadata = StringToMetadata(name);
1685 1694
1692 1701
1693 1702
1694 static void DeleteMetadata(RestApi::DeleteCall& call) 1703 static void DeleteMetadata(RestApi::DeleteCall& call)
1695 { 1704 {
1696 RETRIEVE_CONTEXT(call); 1705 RETRIEVE_CONTEXT(call);
1697 1706 CheckValidResourceType(call);
1707
1698 std::string publicId = call.GetUriComponent("id", ""); 1708 std::string publicId = call.GetUriComponent("id", "");
1699 std::string name = call.GetUriComponent("name", ""); 1709 std::string name = call.GetUriComponent("name", "");
1700 MetadataType metadata = StringToMetadata(name); 1710 MetadataType metadata = StringToMetadata(name);
1701 1711
1702 if (metadata >= MetadataType_StartUser && 1712 if (metadata >= MetadataType_StartUser &&
1710 1720
1711 1721
1712 static void SetMetadata(RestApi::PutCall& call) 1722 static void SetMetadata(RestApi::PutCall& call)
1713 { 1723 {
1714 RETRIEVE_CONTEXT(call); 1724 RETRIEVE_CONTEXT(call);
1725 CheckValidResourceType(call);
1715 1726
1716 std::string publicId = call.GetUriComponent("id", ""); 1727 std::string publicId = call.GetUriComponent("id", "");
1717 std::string name = call.GetUriComponent("name", ""); 1728 std::string name = call.GetUriComponent("name", "");
1718 MetadataType metadata = StringToMetadata(name); 1729 MetadataType metadata = StringToMetadata(name);
1719 std::string value = call.GetPutBody(); 1730 std::string value = call.GetPutBody();
1817 1828
1818 call.GetOutput().AnswerBuffer("{}", "application/json"); 1829 call.GetOutput().AnswerBuffer("{}", "application/json");
1819 } 1830 }
1820 1831
1821 1832
1833
1834
1835
1836
1837 // Handling of attached files -----------------------------------------------
1838
1839 static void ListAttachments(RestApi::GetCall& call)
1840 {
1841 RETRIEVE_CONTEXT(call);
1842
1843 std::string resourceType = call.GetUriComponent("resourceType", "");
1844 std::string publicId = call.GetUriComponent("id", "");
1845 std::list<FileContentType> attachments;
1846 context.GetIndex().ListAvailableAttachments(attachments, publicId, StringToResourceType(resourceType.c_str()));
1847
1848 Json::Value result = Json::arrayValue;
1849
1850 for (std::list<FileContentType>::const_iterator
1851 it = attachments.begin(); it != attachments.end(); ++it)
1852 {
1853 result.append(EnumerationToString(*it));
1854 }
1855
1856 call.GetOutput().AnswerJson(result);
1857 }
1858
1859
1860 static bool GetAttachmentInfo(FileInfo& info, RestApi::Call& call)
1861 {
1862 RETRIEVE_CONTEXT(call);
1863 CheckValidResourceType(call);
1864
1865 std::string publicId = call.GetUriComponent("id", "");
1866 std::string name = call.GetUriComponent("name", "");
1867 FileContentType contentType = StringToContentType(name);
1868
1869 return context.GetIndex().LookupAttachment(info, publicId, contentType);
1870 }
1871
1872
1873 static void GetAttachmentOperations(RestApi::GetCall& call)
1874 {
1875 FileInfo info;
1876 if (GetAttachmentInfo(info, call))
1877 {
1878 Json::Value operations = Json::arrayValue;
1879
1880 operations.append("compressed-data");
1881
1882 if (info.GetCompressedMD5() != "")
1883 {
1884 operations.append("compressed-md5");
1885 }
1886
1887 operations.append("compressed-size");
1888 operations.append("data");
1889
1890 if (info.GetUncompressedMD5() != "")
1891 {
1892 operations.append("md5");
1893 }
1894
1895 operations.append("size");
1896
1897 if (info.GetCompressedMD5() != "")
1898 {
1899 operations.append("verify-md5");
1900 }
1901
1902 call.GetOutput().AnswerJson(operations);
1903 }
1904 }
1905
1906
1907 template <int uncompress>
1908 static void GetAttachmentData(RestApi::GetCall& call)
1909 {
1910 RETRIEVE_CONTEXT(call);
1911 CheckValidResourceType(call);
1912
1913 std::string publicId = call.GetUriComponent("id", "");
1914 std::string name = call.GetUriComponent("name", "");
1915
1916 std::string content;
1917 context.ReadFile(content, publicId, StringToContentType(name),
1918 (uncompress == 1));
1919
1920 call.GetOutput().AnswerBuffer(content, "application/octet-stream");
1921 }
1922
1923
1924 static void GetAttachmentSize(RestApi::GetCall& call)
1925 {
1926 FileInfo info;
1927 if (GetAttachmentInfo(info, call))
1928 {
1929 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetUncompressedSize()), "text/plain");
1930 }
1931 }
1932
1933
1934 static void GetAttachmentCompressedSize(RestApi::GetCall& call)
1935 {
1936 FileInfo info;
1937 if (GetAttachmentInfo(info, call))
1938 {
1939 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetCompressedSize()), "text/plain");
1940 }
1941 }
1942
1943
1944 static void GetAttachmentMD5(RestApi::GetCall& call)
1945 {
1946 FileInfo info;
1947 if (GetAttachmentInfo(info, call) &&
1948 info.GetUncompressedMD5() != "")
1949 {
1950 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetUncompressedMD5()), "text/plain");
1951 }
1952 }
1953
1954
1955 static void GetAttachmentCompressedMD5(RestApi::GetCall& call)
1956 {
1957 FileInfo info;
1958 if (GetAttachmentInfo(info, call) &&
1959 info.GetCompressedMD5() != "")
1960 {
1961 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetCompressedMD5()), "text/plain");
1962 }
1963 }
1822 1964
1823 1965
1824 1966
1825 // Registration of the various REST handlers -------------------------------- 1967 // Registration of the various REST handlers --------------------------------
1826 1968
1860 Register("/instances/{id}/statistics", GetResourceStatistics); 2002 Register("/instances/{id}/statistics", GetResourceStatistics);
1861 Register("/patients/{id}/statistics", GetResourceStatistics); 2003 Register("/patients/{id}/statistics", GetResourceStatistics);
1862 Register("/studies/{id}/statistics", GetResourceStatistics); 2004 Register("/studies/{id}/statistics", GetResourceStatistics);
1863 Register("/series/{id}/statistics", GetResourceStatistics); 2005 Register("/series/{id}/statistics", GetResourceStatistics);
1864 2006
1865 Register("/instances/{id}/metadata", ListMetadata); 2007 Register("/{resourceType}/{id}/metadata", ListMetadata);
1866 Register("/instances/{id}/metadata/{name}", DeleteMetadata); 2008 Register("/{resourceType}/{id}/metadata/{name}", DeleteMetadata);
1867 Register("/instances/{id}/metadata/{name}", GetMetadata); 2009 Register("/{resourceType}/{id}/metadata/{name}", GetMetadata);
1868 Register("/instances/{id}/metadata/{name}", SetMetadata); 2010 Register("/{resourceType}/{id}/metadata/{name}", SetMetadata);
1869 Register("/patients/{id}/metadata", ListMetadata); 2011
1870 Register("/patients/{id}/metadata/{name}", DeleteMetadata); 2012 Register("/{resourceType}/{id}/attachments", ListAttachments);
1871 Register("/patients/{id}/metadata/{name}", GetMetadata); 2013 Register("/{resourceType}/{id}/attachments/{name}", GetAttachmentOperations);
1872 Register("/patients/{id}/metadata/{name}", SetMetadata); 2014 Register("/{resourceType}/{id}/attachments/{name}/compressed-data", GetAttachmentData<0>);
1873 Register("/series/{id}/metadata", ListMetadata); 2015 Register("/{resourceType}/{id}/attachments/{name}/compressed-md5", GetAttachmentCompressedMD5);
1874 Register("/series/{id}/metadata/{name}", DeleteMetadata); 2016 Register("/{resourceType}/{id}/attachments/{name}/compressed-size", GetAttachmentCompressedSize);
1875 Register("/series/{id}/metadata/{name}", GetMetadata); 2017 Register("/{resourceType}/{id}/attachments/{name}/data", GetAttachmentData<1>);
1876 Register("/series/{id}/metadata/{name}", SetMetadata); 2018 Register("/{resourceType}/{id}/attachments/{name}/md5", GetAttachmentMD5);
1877 Register("/studies/{id}/metadata", ListMetadata); 2019 Register("/{resourceType}/{id}/attachments/{name}/size", GetAttachmentSize);
1878 Register("/studies/{id}/metadata/{name}", DeleteMetadata);
1879 Register("/studies/{id}/metadata/{name}", GetMetadata);
1880 Register("/studies/{id}/metadata/{name}", SetMetadata);
1881 2020
1882 Register("/patients/{id}/protected", IsProtectedPatient); 2021 Register("/patients/{id}/protected", IsProtectedPatient);
1883 Register("/patients/{id}/protected", SetPatientProtection); 2022 Register("/patients/{id}/protected", SetPatientProtection);
1884 Register("/instances/{id}/file", GetInstanceFile); 2023 Register("/instances/{id}/file", GetInstanceFile);
1885 Register("/instances/{id}/export", ExportInstanceFile); 2024 Register("/instances/{id}/export", ExportInstanceFile);