annotate Plugins/DicomWeb/DicomWeb.py @ 654:3117e3f73b9a

qido-rs sequence
author Alain Mazy <am@orthanc.team>
date Wed, 05 Jun 2024 11:21:22 +0200
parents 5d7b6e43ab7d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
33
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 #!/usr/bin/python
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 # Orthanc - A Lightweight, RESTful DICOM Store
73
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 33
diff changeset
4 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
33
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 # Department, University Hospital of Liege, Belgium
649
5d7b6e43ab7d updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 640
diff changeset
6 # Copyright (C) 2017-2023 Osimis S.A., Belgium
5d7b6e43ab7d updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 640
diff changeset
7 # Copyright (C) 2024-2024 Orthanc Team SRL, Belgium
640
9f8276ac1cdd update year to 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 511
diff changeset
8 # Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
33
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 #
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 # This program is free software: you can redistribute it and/or
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 # modify it under the terms of the GNU General Public License as
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 # published by the Free Software Foundation, either version 3 of the
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 # License, or (at your option) any later version.
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 #
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 # This program is distributed in the hope that it will be useful, but
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 # General Public License for more details.
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 #
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 # You should have received a copy of the GNU General Public License
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 import os
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 import sys
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 import email
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
27 import uuid
33
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 from email.mime.multipart import MIMEMultipart
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 from email.mime.application import MIMEApplication
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'Tests'))
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 from Toolbox import *
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
36 def _AttachPart(body, path, contentType, boundary):
33
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 with open(path, 'rb') as f:
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
38 body += bytearray('--%s\r\n' % boundary, 'ascii')
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
39 body += bytearray('Content-Type: %s\r\n\r\n' % contentType, 'ascii')
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
40 body += f.read()
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
41 body += bytearray('\r\n', 'ascii')
33
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43
398
9528e2a03d3c adapt DICOMweb tests following fix of issue 196 (STOW-RS: Should return 200 only when successfully stored all instances)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
44 def SendStowRaw(orthanc, uri, dicom):
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
45 # We do not use Python's "email" package, as it uses LF (\n) for line
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
46 # endings instead of CRLF (\r\n) for binary messages, as required by
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
47 # RFC 1341
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
48 # http://stackoverflow.com/questions/3086860/how-do-i-generate-a-multipart-mime-message-with-correct-crlf-in-python
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
49 # https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
50
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
51 # Create a multipart message whose body contains all the input DICOM files
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
52 boundary = str(uuid.uuid4()) # The boundary is a random UUID
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
53 body = bytearray()
33
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 if isinstance(dicom, list):
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 for i in range(dicom):
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
57 _AttachPart(body, dicom[i], 'application/dicom', boundary)
33
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 else:
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
59 _AttachPart(body, dicom, 'application/dicom', boundary)
33
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
61 # Closing boundary
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
62 body += bytearray('--%s--' % boundary, 'ascii')
33
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
64 # Do the HTTP POST request to the STOW-RS server
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
65 headers = {
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
66 'Content-Type' : 'multipart/related; type=application/dicom; boundary=%s' % boundary,
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
67 'Accept' : 'application/json',
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
68 }
33
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69
398
9528e2a03d3c adapt DICOMweb tests following fix of issue 196 (STOW-RS: Should return 200 only when successfully stored all instances)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
70 (response, content) = DoPostRaw(orthanc, uri, body, headers = headers)
9528e2a03d3c adapt DICOMweb tests following fix of issue 196 (STOW-RS: Should return 200 only when successfully stored all instances)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
71
9528e2a03d3c adapt DICOMweb tests following fix of issue 196 (STOW-RS: Should return 200 only when successfully stored all instances)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
72 return (response.status, DecodeJson(content))
9528e2a03d3c adapt DICOMweb tests following fix of issue 196 (STOW-RS: Should return 200 only when successfully stored all instances)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
73
9528e2a03d3c adapt DICOMweb tests following fix of issue 196 (STOW-RS: Should return 200 only when successfully stored all instances)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
74
9528e2a03d3c adapt DICOMweb tests following fix of issue 196 (STOW-RS: Should return 200 only when successfully stored all instances)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
75 def SendStow(orthanc, uri, dicom):
9528e2a03d3c adapt DICOMweb tests following fix of issue 196 (STOW-RS: Should return 200 only when successfully stored all instances)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
76 (status, content) = SendStowRaw(orthanc, uri, dicom)
9528e2a03d3c adapt DICOMweb tests following fix of issue 196 (STOW-RS: Should return 200 only when successfully stored all instances)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
77 if not (status in [ 200 ]):
9528e2a03d3c adapt DICOMweb tests following fix of issue 196 (STOW-RS: Should return 200 only when successfully stored all instances)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
78 raise Exception('Bad status: %d' % status)
9528e2a03d3c adapt DICOMweb tests following fix of issue 196 (STOW-RS: Should return 200 only when successfully stored all instances)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
79 else:
9528e2a03d3c adapt DICOMweb tests following fix of issue 196 (STOW-RS: Should return 200 only when successfully stored all instances)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
80 return content
222
0f03ee6ffa80 DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
81
0f03ee6ffa80 DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
82
297
f95cd3af1c7a DICOMweb: test_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 260
diff changeset
83 def DoGetMultipart(orthanc, uri, headers = {}, returnHeaders = False):
222
0f03ee6ffa80 DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
84 answer = DoGetRaw(orthanc, uri, headers = headers)
0f03ee6ffa80 DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
85
0f03ee6ffa80 DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
86 header = ''
0f03ee6ffa80 DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
87 for i in answer[0]:
0f03ee6ffa80 DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
88 header += '%s: %s\r\n' % (i, answer[0][i])
0f03ee6ffa80 DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
89
226
230aede7f8d5 test_bitbucket_issue_96
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 222
diff changeset
90 b = bytearray()
230aede7f8d5 test_bitbucket_issue_96
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 222
diff changeset
91 b.extend(header.encode('ascii'))
230aede7f8d5 test_bitbucket_issue_96
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 222
diff changeset
92 b.extend(b'\r\n')
230aede7f8d5 test_bitbucket_issue_96
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 222
diff changeset
93 b.extend(answer[1])
230aede7f8d5 test_bitbucket_issue_96
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 222
diff changeset
94
230aede7f8d5 test_bitbucket_issue_96
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 222
diff changeset
95 if (sys.version_info >= (3, 0)):
230aede7f8d5 test_bitbucket_issue_96
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 222
diff changeset
96 msg = email.message_from_bytes(b)
230aede7f8d5 test_bitbucket_issue_96
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 222
diff changeset
97 else:
230aede7f8d5 test_bitbucket_issue_96
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 222
diff changeset
98 msg = email.message_from_string(b)
230aede7f8d5 test_bitbucket_issue_96
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 222
diff changeset
99
222
0f03ee6ffa80 DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
100 if not msg.is_multipart():
0f03ee6ffa80 DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
101 raise Exception('Not a multipart message')
0f03ee6ffa80 DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
102
0f03ee6ffa80 DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
103 result = []
0f03ee6ffa80 DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
104
0f03ee6ffa80 DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
105 for part in msg.walk():
226
230aede7f8d5 test_bitbucket_issue_96
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 222
diff changeset
106 payload = part.get_payload(decode = True)
222
0f03ee6ffa80 DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
107 if payload != None:
297
f95cd3af1c7a DICOMweb: test_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 260
diff changeset
108 if returnHeaders:
f95cd3af1c7a DICOMweb: test_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 260
diff changeset
109 h = {}
f95cd3af1c7a DICOMweb: test_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 260
diff changeset
110 for (key, value) in part.items():
f95cd3af1c7a DICOMweb: test_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 260
diff changeset
111 h[key] = value
f95cd3af1c7a DICOMweb: test_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 260
diff changeset
112 result.append((payload, h))
f95cd3af1c7a DICOMweb: test_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 260
diff changeset
113 else:
f95cd3af1c7a DICOMweb: test_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 260
diff changeset
114 result.append(payload)
222
0f03ee6ffa80 DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
115
0f03ee6ffa80 DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
116 return result