comparison OrthancServer/ServerEnumerations.cpp @ 1772:53e045b5a8ec

MIME content type can be associated to custom attachments (cf. "UserContentType")
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 12 Nov 2015 14:36:27 +0100
parents 7e0b5e413c7c
children 613df4362575
comparison
equal deleted inserted replaced
1771:8790488ae98b 1772:53e045b5a8ec
33 #include "PrecompiledHeadersServer.h" 33 #include "PrecompiledHeadersServer.h"
34 #include "ServerEnumerations.h" 34 #include "ServerEnumerations.h"
35 35
36 #include "../Core/OrthancException.h" 36 #include "../Core/OrthancException.h"
37 #include "../Core/EnumerationDictionary.h" 37 #include "../Core/EnumerationDictionary.h"
38 #include "../Core/Logging.h"
38 #include "../Core/Toolbox.h" 39 #include "../Core/Toolbox.h"
39 40
40 #include <boost/thread.hpp> 41 #include <boost/thread.hpp>
41 42
42 namespace Orthanc 43 namespace Orthanc
43 { 44 {
45 typedef std::map<FileContentType, std::string> MimeTypes;
46
44 static boost::mutex enumerationsMutex_; 47 static boost::mutex enumerationsMutex_;
45 static Toolbox::EnumerationDictionary<MetadataType> dictMetadataType_; 48 static Toolbox::EnumerationDictionary<MetadataType> dictMetadataType_;
46 static Toolbox::EnumerationDictionary<FileContentType> dictContentType_; 49 static Toolbox::EnumerationDictionary<FileContentType> dictContentType_;
50 static MimeTypes mimeTypes_;
47 51
48 void InitializeServerEnumerations() 52 void InitializeServerEnumerations()
49 { 53 {
50 boost::mutex::scoped_lock lock(enumerationsMutex_); 54 boost::mutex::scoped_lock lock(enumerationsMutex_);
51 55
70 boost::mutex::scoped_lock lock(enumerationsMutex_); 74 boost::mutex::scoped_lock lock(enumerationsMutex_);
71 75
72 if (metadata < static_cast<int>(MetadataType_StartUser) || 76 if (metadata < static_cast<int>(MetadataType_StartUser) ||
73 metadata > static_cast<int>(MetadataType_EndUser)) 77 metadata > static_cast<int>(MetadataType_EndUser))
74 { 78 {
79 LOG(ERROR) << "A user content type must have index between "
80 << static_cast<int>(MetadataType_StartUser) << " and "
81 << static_cast<int>(MetadataType_EndUser) << ", but \""
82 << name << "\" has index " << metadata;
83
75 throw OrthancException(ErrorCode_ParameterOutOfRange); 84 throw OrthancException(ErrorCode_ParameterOutOfRange);
76 } 85 }
77 86
78 dictMetadataType_.Add(static_cast<MetadataType>(metadata), name); 87 MetadataType type = static_cast<MetadataType>(metadata);
88
89 if (dictMetadataType_.Contains(type))
90 {
91 LOG(ERROR) << "Cannot associate user content type \""
92 << name << "\" with index " << metadata
93 << ", as this index is already used";
94
95 throw OrthancException(ErrorCode_ParameterOutOfRange);
96 }
97
98 dictMetadataType_.Add(type, name);
79 } 99 }
80 100
81 std::string EnumerationToString(MetadataType type) 101 std::string EnumerationToString(MetadataType type)
82 { 102 {
83 // This function MUST return a "std::string" and not "const 103 // This function MUST return a "std::string" and not "const
91 boost::mutex::scoped_lock lock(enumerationsMutex_); 111 boost::mutex::scoped_lock lock(enumerationsMutex_);
92 return dictMetadataType_.Translate(str); 112 return dictMetadataType_.Translate(str);
93 } 113 }
94 114
95 void RegisterUserContentType(int contentType, 115 void RegisterUserContentType(int contentType,
96 const std::string& name) 116 const std::string& name,
117 const std::string& mime)
97 { 118 {
98 boost::mutex::scoped_lock lock(enumerationsMutex_); 119 boost::mutex::scoped_lock lock(enumerationsMutex_);
99 120
100 if (contentType < static_cast<int>(FileContentType_StartUser) || 121 if (contentType < static_cast<int>(FileContentType_StartUser) ||
101 contentType > static_cast<int>(FileContentType_EndUser)) 122 contentType > static_cast<int>(FileContentType_EndUser))
102 { 123 {
124 LOG(ERROR) << "A user content type must have index between "
125 << static_cast<int>(FileContentType_StartUser) << " and "
126 << static_cast<int>(FileContentType_EndUser) << ", but \""
127 << name << "\" has index " << contentType;
128
103 throw OrthancException(ErrorCode_ParameterOutOfRange); 129 throw OrthancException(ErrorCode_ParameterOutOfRange);
104 } 130 }
105 131
106 dictContentType_.Add(static_cast<FileContentType>(contentType), name); 132 FileContentType type = static_cast<FileContentType>(contentType);
133 if (dictContentType_.Contains(type))
134 {
135 LOG(ERROR) << "Cannot associate user content type \""
136 << name << "\" with index " << contentType
137 << ", as this index is already used";
138
139 throw OrthancException(ErrorCode_ParameterOutOfRange);
140 }
141
142 dictContentType_.Add(type, name);
143 mimeTypes_[type] = mime;
107 } 144 }
108 145
109 std::string EnumerationToString(FileContentType type) 146 std::string EnumerationToString(FileContentType type)
110 { 147 {
111 // This function MUST return a "std::string" and not "const 148 // This function MUST return a "std::string" and not "const
112 // char*", as the result is not a static string 149 // char*", as the result is not a static string
113 boost::mutex::scoped_lock lock(enumerationsMutex_); 150 boost::mutex::scoped_lock lock(enumerationsMutex_);
114 return dictContentType_.Translate(type); 151 return dictContentType_.Translate(type);
152 }
153
154 std::string GetFileContentMime(FileContentType type)
155 {
156 if (type >= FileContentType_StartUser &&
157 type <= FileContentType_EndUser)
158 {
159 boost::mutex::scoped_lock lock(enumerationsMutex_);
160
161 MimeTypes::const_iterator it = mimeTypes_.find(type);
162 if (it != mimeTypes_.end())
163 {
164 return it->second;
165 }
166 }
167
168 switch (type)
169 {
170 case FileContentType_Dicom:
171 return "application/dicom";
172
173 case FileContentType_DicomAsJson:
174 return "application/json";
175
176 default:
177 return "application/octet-stream";
178 }
115 } 179 }
116 180
117 FileContentType StringToContentType(const std::string& str) 181 FileContentType StringToContentType(const std::string& str)
118 { 182 {
119 boost::mutex::scoped_lock lock(enumerationsMutex_); 183 boost::mutex::scoped_lock lock(enumerationsMutex_);