File: StowRs.cpp

package info (click to toggle)
orthanc-dicomweb 1.21%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 1,328 kB
  • sloc: cpp: 12,558; javascript: 6,726; python: 423; sh: 140; makefile: 34
file content (326 lines) | stat: -rw-r--r-- 12,076 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
 * Orthanc - A Lightweight, RESTful DICOM Store
 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
 * Department, University Hospital of Liege, Belgium
 * Copyright (C) 2017-2023 Osimis S.A., Belgium
 * Copyright (C) 2024-2025 Orthanc Team SRL, Belgium
 * Copyright (C) 2021-2025 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 **/


#include "StowRs.h"

#include "Configuration.h"
#include "DicomWebFormatter.h"
#include "Logging.h"

namespace OrthancPlugins
{
  StowServer::StowServer(OrthancPluginContext* context,
                         const std::map<std::string, std::string>& headers,
                         const std::string& expectedStudy) :
    context_(context),
    xml_(Configuration::IsXmlExpected(headers)),
    wadoBasePublicUrl_(Configuration::GetBasePublicUrl(headers)),
    expectedStudy_(expectedStudy),
    isFirst_(true),
    result_(Json::objectValue),
    success_(Json::arrayValue),
    failed_(Json::arrayValue),
    hasBadSyntax_(false),
    hasConflict_(false),
    headers_(headers)
  { 
    std::string tmp, contentType, subType, boundary;
    if (!Orthanc::MultipartStreamReader::GetMainContentType(tmp, headers) ||
        !Orthanc::MultipartStreamReader::ParseMultipartContentType(contentType, subType, boundary, tmp))
    {
      throw Orthanc::OrthancException(Orthanc::ErrorCode_UnsupportedMediaType,
                                      "The STOW-RS server expects a multipart body in its request");
    }

    if (contentType != "multipart/related")
    {
      throw Orthanc::OrthancException(Orthanc::ErrorCode_UnsupportedMediaType,
                                      "The Content-Type of a STOW-RS request must be \"multipart/related\"");
    }

    if (subType != "application/dicom")
    {
      throw Orthanc::OrthancException(Orthanc::ErrorCode_UnsupportedMediaType,
                                      "The STOW-RS plugin currently only supports \"application/dicom\" subtype");
    }

    // Hotfix for bug #190, until the Orthanc Framework is fixed
    // https://orthanc.uclouvain.be/bugs/show_bug.cgi?id=190
    if (!boundary.empty() &&
        boundary.size() >= 2 &&
        boundary[0] == '"' &&
        boundary[boundary.size() - 1] == '"')
    {
      boundary = boundary.substr(1, boundary.size() - 2);
    }

    parser_.reset(new Orthanc::MultipartStreamReader(boundary));
    parser_->SetHandler(*this);
  }


  void StowServer::HandlePart(const Orthanc::MultipartStreamReader::HttpHeaders& headers,
                              const void* part,
                              size_t size)
  {
    std::string contentType;

    if (!Orthanc::MultipartStreamReader::GetMainContentType(contentType, headers) ||
        contentType.find("application/dicom") == std::string::npos)
    {
      throw Orthanc::OrthancException(
        Orthanc::ErrorCode_UnsupportedMediaType,
        "The STOW-RS request contains a part that is not "
        "\"application/dicom\" (it is: \"" + contentType + "\")");
    }

    Json::Value dicom;
    bool ok = false;

    try
    {
      OrthancString s;
      s.Assign(OrthancPluginDicomBufferToJson(context_, part, size,
                                              OrthancPluginDicomToJsonFormat_Short,
                                              OrthancPluginDicomToJsonFlags_None, 256));

      if (s.GetContent() != NULL)
      {
        ok = true;
        s.ToJson(dicom);
      }
    }
    catch (Orthanc::OrthancException&)
    {
    }           

    if (!ok)
    {
      // Bad DICOM file
      LOG(WARNING) << "STOW-RS cannot parse an incoming DICOM file";
      hasBadSyntax_ = true;
      return;
    }

    if (dicom.type() != Json::objectValue ||
        !dicom.isMember(Orthanc::DICOM_TAG_SERIES_INSTANCE_UID.Format()) ||
        !dicom.isMember(Orthanc::DICOM_TAG_SOP_CLASS_UID.Format()) ||
        !dicom.isMember(Orthanc::DICOM_TAG_SOP_INSTANCE_UID.Format()) ||
        !dicom.isMember(Orthanc::DICOM_TAG_STUDY_INSTANCE_UID.Format()) ||
        dicom[Orthanc::DICOM_TAG_SERIES_INSTANCE_UID.Format()].type() != Json::stringValue ||
        dicom[Orthanc::DICOM_TAG_SOP_CLASS_UID.Format()].type() != Json::stringValue ||
        dicom[Orthanc::DICOM_TAG_SOP_INSTANCE_UID.Format()].type() != Json::stringValue ||
        dicom[Orthanc::DICOM_TAG_STUDY_INSTANCE_UID.Format()].type() != Json::stringValue)
    {
      LOG(WARNING) << "STOW-RS: Missing a mandatory tag in incoming DICOM file";
      hasBadSyntax_ = true;      

      if (dicom.isMember(Orthanc::DICOM_TAG_SOP_CLASS_UID.Format()) &&
          dicom.isMember(Orthanc::DICOM_TAG_SOP_INSTANCE_UID.Format()) &&
          dicom[Orthanc::DICOM_TAG_SOP_CLASS_UID.Format()].type() == Json::stringValue &&
          dicom[Orthanc::DICOM_TAG_SOP_INSTANCE_UID.Format()].type() == Json::stringValue)
      {
        Json::Value item = Json::objectValue;
        item[DICOM_TAG_REFERENCED_SOP_CLASS_UID.Format()] = dicom[Orthanc::DICOM_TAG_SOP_CLASS_UID.Format()].asString();
        item[DICOM_TAG_REFERENCED_SOP_INSTANCE_UID.Format()] = dicom[Orthanc::DICOM_TAG_SOP_INSTANCE_UID.Format()].asString();
        item[DICOM_TAG_FAILURE_REASON.Format()] =
          boost::lexical_cast<std::string>(0xC000);  // Error: Cannot understand
        failed_.append(item);
      }

      return;
    }

    const std::string seriesInstanceUid = dicom[Orthanc::DICOM_TAG_SERIES_INSTANCE_UID.Format()].asString();
    const std::string sopClassUid = dicom[Orthanc::DICOM_TAG_SOP_CLASS_UID.Format()].asString();
    const std::string sopInstanceUid = dicom[Orthanc::DICOM_TAG_SOP_INSTANCE_UID.Format()].asString();
    const std::string studyInstanceUid = dicom[Orthanc::DICOM_TAG_STUDY_INSTANCE_UID.Format()].asString();

    Json::Value item = Json::objectValue;
    item[DICOM_TAG_REFERENCED_SOP_CLASS_UID.Format()] = sopClassUid;
    item[DICOM_TAG_REFERENCED_SOP_INSTANCE_UID.Format()] = sopInstanceUid;
      
    if (!expectedStudy_.empty() &&
        studyInstanceUid != expectedStudy_)
    {
      LOG(WARNING) << "STOW-RS request restricted to study [" << expectedStudy_ << 
                      "], but received instance from study [" << studyInstanceUid << "]";

      hasConflict_ = true;

      item[DICOM_TAG_FAILURE_REASON.Format()] =
        boost::lexical_cast<std::string>(0x0110);  // Processing failure
      failed_.append(item);
    }
    else
    {
      if (isFirst_)
      {
        std::string url = wadoBasePublicUrl_ + "studies/" + studyInstanceUid;
        result_[DICOM_TAG_RETRIEVE_URL.Format()] = url;
        isFirst_ = false;
      }

      uint16_t failureReason = 0;
      try
      {
        MemoryBuffer tmp;

        // make sure to forward the auth headers in the request that is sent to Orthanc (to allow usage of the auth plugin)
        // since we do not know which header is being used, we include all the headers from the STOW-RS request (headers_), replace
        // the "content-disposition" + "content-type" that are rebuilt from the multi-part message and remove the headers that might
        // be mi-interpreted by Orthanc core (like "content-length" that is actually the "content-length" from the whole STOW-RS request, not the length of this file)
        Orthanc::MultipartStreamReader::HttpHeaders mergedHeaders = headers_;
        Orthanc::MultipartStreamReader::HttpHeaders::iterator foundContentLength = mergedHeaders.find("content-length");
        if (foundContentLength != mergedHeaders.end())
        {
          mergedHeaders.erase(foundContentLength);
        }
        
        ok = tmp.RestApiPost("/instances", part, size, mergedHeaders, true);
        tmp.Clear();
      }
      catch (Orthanc::OrthancException& ex)
      {
        ok = false;
        if (ex.GetErrorCode() == Orthanc::ErrorCode_FullStorage)
        {
          failureReason = 0xA700;  // out-of-resources
        }
        else
        {
          failureReason = 0x0110;  // processing error
        }
      }

      if (ok)
      {
        std::string url = (wadoBasePublicUrl_ + 
                           "studies/" + studyInstanceUid +
                           "/series/" + seriesInstanceUid +
                           "/instances/" + sopInstanceUid);

        item[DICOM_TAG_RETRIEVE_URL.Format()] = url;
        success_.append(item);      
      }
      else
      {
        LOG(ERROR) << "Orthanc was unable to store one instance in a STOW-RS request";
        item[DICOM_TAG_FAILURE_REASON.Format()] =
          boost::lexical_cast<std::string>(failureReason);
        failed_.append(item);
      }
    }
  }


  void StowServer::AddChunk(const void* data,
                            size_t size)
  {
    assert(parser_.get() != NULL);
    parser_->AddChunk(data, size);
  }


  void StowServer::Execute(OrthancPluginRestOutput* output)
  {
    assert(parser_.get() != NULL);
    parser_->CloseStream();

    if (failed_.size() > 0)
    {
      // new in 1.19: don't include the failed sequence if there are no failures (https://discourse.orthanc-server.org/t/orthanc-dicomweb-stowrs-server-request-response-compatibility/5763)
      result_[DICOM_TAG_FAILED_SOP_SEQUENCE.Format()] = failed_;
    }

    result_[DICOM_TAG_REFERENCED_SOP_SEQUENCE.Format()] = success_;
    
    std::string answer;
    
    DicomWebFormatter::Apply(answer, context_, result_, xml_,
                             OrthancPluginDicomWebBinaryMode_Ignore, "");

    // http://dicom.nema.org/medical/dicom/current/output/html/part18.html#table_10.5.3-1
    uint16_t statusCode = 200;
    if (hasBadSyntax_)
    {
      statusCode = 400;
    }
    else if (hasConflict_)
    {
      statusCode = 409;
    }
    else if (failed_.size() > 0 && success_.size() == 0)  // only failed instances but not a conflict or bad syntax -> 400
    {
      statusCode = 400;
    }
    else if (failed_.size() > 0 && success_.size() > 0) // 202 = Accepted but some instances have failures
    {
      statusCode = 202;
    }

    if (statusCode == 200)
    {
      OrthancPluginAnswerBuffer(context_, output, answer.c_str(), answer.size(),
                                xml_ ? "application/dicom+xml" : "application/dicom+json");
    }
    else
    {
      // TODO: if statusCode is 202, the content will only be sent if HttpDescribeErrors is set to true -> would need OrthancPluginAnswerBuffer with an HttpStatusCode arg
      OrthancPluginSetHttpHeader(context_, output, "Content-Type", xml_ ? "application/dicom+xml" : "application/dicom+json");
      OrthancPluginSendHttpStatus(context_, output, statusCode, answer.c_str(), answer.size());  
    }
  };

  
  IChunkedRequestReader* StowServer::PostCallback(const char* url,
                                                  const OrthancPluginHttpRequest* request)
  {
    OrthancPluginContext* context = GetGlobalContext();
  
    if (request->method != OrthancPluginHttpMethod_Post)
    {
      throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
    }

    std::map<std::string, std::string> headers;
    OrthancPlugins::GetHttpHeaders(headers, request);

    std::string expectedStudy;
    if (request->groupsCount == 1)
    {
      expectedStudy = request->groups[0];
    }

    if (expectedStudy.empty())
    {
      LOG(INFO) << "STOW-RS request without study";
    }
    else
    {
      LOG(INFO) << "STOW-RS request restricted to study UID " << expectedStudy;
    }

    return new StowServer(context, headers, expectedStudy);
  }
}