comparison OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp @ 5058:d4e5ca0c9307

Fix the "Never" option of the "StorageAccessOnFind" that was sill accessing files (bug introduced in 1.11.0)
author Alain Mazy <am@osimis.io>
date Wed, 03 Aug 2022 10:49:50 +0200
parents 255b02c68908
children e69a3ff39bc5
comparison
equal deleted inserted replaced
5057:e6f26be401fa 5058:d4e5ca0c9307
129 static void AnswerListOfResources(RestApiOutput& output, 129 static void AnswerListOfResources(RestApiOutput& output,
130 ServerContext& context, 130 ServerContext& context,
131 const std::list<std::string>& resources, 131 const std::list<std::string>& resources,
132 const std::map<std::string, std::string>& instancesIds, // optional: the id of an instance for each found resource. 132 const std::map<std::string, std::string>& instancesIds, // optional: the id of an instance for each found resource.
133 const std::map<std::string, boost::shared_ptr<DicomMap> >& resourcesMainDicomTags, // optional: all tags read from DB for a resource (current level and upper levels) 133 const std::map<std::string, boost::shared_ptr<DicomMap> >& resourcesMainDicomTags, // optional: all tags read from DB for a resource (current level and upper levels)
134 const std::map<std::string, Json::Value>& resourcesDicomAsJson, // optional: the dicom-as-json for each resource 134 const std::map<std::string, const Json::Value*>& resourcesDicomAsJson, // optional: the dicom-as-json for each resource
135 ResourceType level, 135 ResourceType level,
136 bool expand, 136 bool expand,
137 DicomToJsonFormat format, 137 DicomToJsonFormat format,
138 const std::set<DicomTag>& requestedTags) 138 const std::set<DicomTag>& requestedTags,
139 bool allowStorageAccess)
139 { 140 {
140 Json::Value answer = Json::arrayValue; 141 Json::Value answer = Json::arrayValue;
141 142
142 for (std::list<std::string>::const_iterator 143 for (std::list<std::string>::const_iterator
143 resource = resources.begin(); resource != resources.end(); ++resource) 144 resource = resources.begin(); resource != resources.end(); ++resource)
144 { 145 {
145 if (expand) 146 if (expand)
146 { 147 {
147 Json::Value expanded; 148 Json::Value expanded;
148 if (context.ExpandResource(expanded, *resource, level, format, requestedTags)) 149
150 std::map<std::string, std::string>::const_iterator instanceId = instancesIds.find(*resource);
151 if (instanceId != instancesIds.end()) // if it is found in instancesIds, it is also in resourcesDicomAsJson and mainDicomTags
152 {
153 // reuse data already collected before (e.g during lookup)
154 std::map<std::string, boost::shared_ptr<DicomMap> >::const_iterator mainDicomTags = resourcesMainDicomTags.find(*resource);
155 std::map<std::string, const Json::Value*>::const_iterator dicomAsJson = resourcesDicomAsJson.find(*resource);
156
157 context.ExpandResource(expanded, *resource,
158 *(mainDicomTags->second.get()),
159 instanceId->second,
160 dicomAsJson->second,
161 level, format, requestedTags, allowStorageAccess);
162 }
163 else
164 {
165 context.ExpandResource(expanded, *resource, level, format, requestedTags, allowStorageAccess);
166 }
167
168 if (expanded.type() == Json::objectValue)
149 { 169 {
150 answer.append(expanded); 170 answer.append(expanded);
151 } 171 }
152 } 172 }
153 else 173 else
164 ServerContext& context, 184 ServerContext& context,
165 const std::list<std::string>& resources, 185 const std::list<std::string>& resources,
166 ResourceType level, 186 ResourceType level,
167 bool expand, 187 bool expand,
168 DicomToJsonFormat format, 188 DicomToJsonFormat format,
169 const std::set<DicomTag>& requestedTags) 189 const std::set<DicomTag>& requestedTags,
190 bool allowStorageAccess)
170 { 191 {
171 std::map<std::string, std::string> unusedInstancesIds; 192 std::map<std::string, std::string> unusedInstancesIds;
172 std::map<std::string, boost::shared_ptr<DicomMap> > unusedResourcesMainDicomTags; 193 std::map<std::string, boost::shared_ptr<DicomMap> > unusedResourcesMainDicomTags;
173 std::map<std::string, Json::Value> unusedResourcesDicomAsJson; 194 std::map<std::string, const Json::Value* > unusedResourcesDicomAsJson;
174 195
175 AnswerListOfResources(output, context, resources, unusedInstancesIds, unusedResourcesMainDicomTags, unusedResourcesDicomAsJson, level, expand, format, requestedTags); 196 AnswerListOfResources(output, context, resources, unusedInstancesIds, unusedResourcesMainDicomTags, unusedResourcesDicomAsJson, level, expand, format, requestedTags, allowStorageAccess);
176 } 197 }
177 198
178 199
179 template <enum ResourceType resourceType> 200 template <enum ResourceType resourceType>
180 static void ListResources(RestApiGetCall& call) 201 static void ListResources(RestApiGetCall& call)
233 index.GetAllUuids(result, resourceType); 254 index.GetAllUuids(result, resourceType);
234 } 255 }
235 256
236 AnswerListOfResources(call.GetOutput(), context, result, resourceType, call.HasArgument("expand"), 257 AnswerListOfResources(call.GetOutput(), context, result, resourceType, call.HasArgument("expand"),
237 OrthancRestApi::GetDicomFormat(call, DicomToJsonFormat_Human), 258 OrthancRestApi::GetDicomFormat(call, DicomToJsonFormat_Human),
238 requestedTags); 259 requestedTags,
260 true /* allowStorageAccess */);
239 } 261 }
240 262
241 263
242 264
243 template <enum ResourceType resourceType> 265 template <enum ResourceType resourceType>
264 std::set<DicomTag> requestedTags; 286 std::set<DicomTag> requestedTags;
265 OrthancRestApi::GetRequestedTags(requestedTags, call); 287 OrthancRestApi::GetRequestedTags(requestedTags, call);
266 288
267 Json::Value json; 289 Json::Value json;
268 if (OrthancRestApi::GetContext(call).ExpandResource( 290 if (OrthancRestApi::GetContext(call).ExpandResource(
269 json, call.GetUriComponent("id", ""), resourceType, format, requestedTags)) 291 json, call.GetUriComponent("id", ""), resourceType, format, requestedTags, true /* allowStorageAccess */))
270 { 292 {
271 call.GetOutput().AnswerJson(json); 293 call.GetOutput().AnswerJson(json);
272 } 294 }
273 } 295 }
274 296
2846 class FindVisitor : public ServerContext::ILookupVisitor 2868 class FindVisitor : public ServerContext::ILookupVisitor
2847 { 2869 {
2848 private: 2870 private:
2849 bool isComplete_; 2871 bool isComplete_;
2850 std::list<std::string> resources_; 2872 std::list<std::string> resources_;
2873 FindStorageAccessMode findStorageAccessMode_;
2851 2874
2852 // cache the data we used during lookup and that we could reuse when building the answers 2875 // cache the data we used during lookup and that we could reuse when building the answers
2853 std::map<std::string, std::string> instancesIds_; // the id of an instance for each found resource. 2876 std::map<std::string, std::string> instancesIds_; // the id of an instance for each found resource.
2854 std::map<std::string, boost::shared_ptr<DicomMap> > resourcesMainDicomTags_; // all tags read from DB for a resource (current level and upper levels) 2877 std::map<std::string, boost::shared_ptr<DicomMap> > resourcesMainDicomTags_; // all tags read from DB for a resource (current level and upper levels)
2855 std::map<std::string, Json::Value> resourcesDicomAsJson_; // the dicom-as-json for a resource 2878 std::map<std::string, const Json::Value* > resourcesDicomAsJson_; // the dicom-as-json for a resource
2856 2879
2857 DicomToJsonFormat format_; 2880 DicomToJsonFormat format_;
2858 2881
2859 public: 2882 public:
2860 explicit FindVisitor(DicomToJsonFormat format) : 2883 explicit FindVisitor(DicomToJsonFormat format, FindStorageAccessMode findStorageAccessMode) :
2861 isComplete_(false), 2884 isComplete_(false),
2885 findStorageAccessMode_(findStorageAccessMode),
2862 format_(format) 2886 format_(format)
2863 { 2887 {
2864 } 2888 }
2865 2889
2866 virtual bool IsDicomAsJsonNeeded() const ORTHANC_OVERRIDE 2890 virtual bool IsDicomAsJsonNeeded() const ORTHANC_OVERRIDE
2888 ServerContext& context, 2912 ServerContext& context,
2889 ResourceType level, 2913 ResourceType level,
2890 bool expand, 2914 bool expand,
2891 const std::set<DicomTag>& requestedTags) const 2915 const std::set<DicomTag>& requestedTags) const
2892 { 2916 {
2893 AnswerListOfResources(output, context, resources_, instancesIds_, resourcesMainDicomTags_, resourcesDicomAsJson_, level, expand, format_, requestedTags); 2917 AnswerListOfResources(output, context, resources_, instancesIds_, resourcesMainDicomTags_, resourcesDicomAsJson_, level, expand, format_, requestedTags, IsStorageAccessAllowedForAnswers(findStorageAccessMode_));
2894 } 2918 }
2895 }; 2919 };
2896 } 2920 }
2897 2921
2898 2922
3054 query.AddRestConstraint(FromDcmtkBridge::ParseTag(members[i]), 3078 query.AddRestConstraint(FromDcmtkBridge::ParseTag(members[i]),
3055 value, caseSensitive, true); 3079 value, caseSensitive, true);
3056 } 3080 }
3057 } 3081 }
3058 3082
3059 FindVisitor visitor(OrthancRestApi::GetDicomFormat(request, DicomToJsonFormat_Human)); 3083 FindVisitor visitor(OrthancRestApi::GetDicomFormat(request, DicomToJsonFormat_Human), context.GetFindStorageAccessMode());
3060 context.Apply(visitor, query, level, since, limit); 3084 context.Apply(visitor, query, level, since, limit);
3061 visitor.Answer(call.GetOutput(), context, level, expand, requestedTags); 3085 visitor.Answer(call.GetOutput(), context, level, expand, requestedTags);
3062 } 3086 }
3063 } 3087 }
3064 3088
3117 3141
3118 for (std::list<std::string>::const_iterator 3142 for (std::list<std::string>::const_iterator
3119 it = a.begin(); it != a.end(); ++it) 3143 it = a.begin(); it != a.end(); ++it)
3120 { 3144 {
3121 Json::Value resource; 3145 Json::Value resource;
3122 if (OrthancRestApi::GetContext(call).ExpandResource(resource, *it, end, format, requestedTags)) 3146 if (OrthancRestApi::GetContext(call).ExpandResource(resource, *it, end, format, requestedTags, true /* allowStorageAccess */))
3123 { 3147 {
3124 result.append(resource); 3148 result.append(resource);
3125 } 3149 }
3126 } 3150 }
3127 3151
3236 assert(currentType == end); 3260 assert(currentType == end);
3237 3261
3238 const DicomToJsonFormat format = OrthancRestApi::GetDicomFormat(call, DicomToJsonFormat_Human); 3262 const DicomToJsonFormat format = OrthancRestApi::GetDicomFormat(call, DicomToJsonFormat_Human);
3239 3263
3240 Json::Value resource; 3264 Json::Value resource;
3241 if (OrthancRestApi::GetContext(call).ExpandResource(resource, current, end, format, requestedTags)) 3265 if (OrthancRestApi::GetContext(call).ExpandResource(resource, current, end, format, requestedTags, true /* allowStorageAccess */))
3242 { 3266 {
3243 call.GetOutput().AnswerJson(resource); 3267 call.GetOutput().AnswerJson(resource);
3244 } 3268 }
3245 } 3269 }
3246 3270
3643 it = interest.begin(); it != interest.end(); ++it) 3667 it = interest.begin(); it != interest.end(); ++it)
3644 { 3668 {
3645 Json::Value item; 3669 Json::Value item;
3646 std::set<DicomTag> emptyRequestedTags; // not supported for bulk content 3670 std::set<DicomTag> emptyRequestedTags; // not supported for bulk content
3647 3671
3648 if (OrthancRestApi::GetContext(call).ExpandResource(item, *it, level, format, emptyRequestedTags)) 3672 if (OrthancRestApi::GetContext(call).ExpandResource(item, *it, level, format, emptyRequestedTags, true /* allowStorageAccess */))
3649 { 3673 {
3650 if (metadata) 3674 if (metadata)
3651 { 3675 {
3652 AddMetadata(item[METADATA], index, *it, level); 3676 AddMetadata(item[METADATA], index, *it, level);
3653 } 3677 }
3668 ResourceType level; 3692 ResourceType level;
3669 Json::Value item; 3693 Json::Value item;
3670 std::set<DicomTag> emptyRequestedTags; // not supported for bulk content 3694 std::set<DicomTag> emptyRequestedTags; // not supported for bulk content
3671 3695
3672 if (index.LookupResourceType(level, *it) && 3696 if (index.LookupResourceType(level, *it) &&
3673 OrthancRestApi::GetContext(call).ExpandResource(item, *it, level, format, emptyRequestedTags)) 3697 OrthancRestApi::GetContext(call).ExpandResource(item, *it, level, format, emptyRequestedTags, true /* allowStorageAccess */))
3674 { 3698 {
3675 if (metadata) 3699 if (metadata)
3676 { 3700 {
3677 AddMetadata(item[METADATA], index, *it, level); 3701 AddMetadata(item[METADATA], index, *it, level);
3678 } 3702 }