PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
document_consumer.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
11
12#include "common/http_client.h"
15#include "common/wss_signer.h"
18
20
21#include <memory>
22#include <string>
23#include <utility>
24
25namespace kcenon::pacs::ihe::xds {
26
27namespace {
28
29// ITI-43 Retrieve Document Set-b SOAP action URI. The default
30// http_options::soap_action is the ITI-41 action, which would make the
31// registry reject the POST on the first line of WS-Addressing routing. The
32// Consumer overwrites it on a copy of the caller's options so the caller's
33// struct is preserved.
34constexpr const char* kIti43SoapAction = "urn:ihe:iti:2007:RetrieveDocumentSet";
35
36// Content-Type of a plain SOAP 1.2 request with no MTOM attachments. ITI-43
37// requests carry only the envelope; MTOM only appears on the response.
38constexpr const char* kSoapContentType =
39 "application/soap+xml; charset=UTF-8; "
40 "action=\"urn:ihe:iti:2007:RetrieveDocumentSet\"";
41
42} // namespace
43
45public:
47 : opts_(std::move(opts)), signing_(std::move(signing)) {}
48
50 std::shared_ptr<kcenon::pacs::security::xds_audit_sink> sink) {
51 audit_sink_ = std::move(sink);
52 }
53
55 const std::string& document_unique_id,
56 const std::string& repository_unique_id) {
57 auto do_audit =
59 audit(document_unique_id, repository_unique_id, r);
60 return r;
61 };
62
63 if (document_unique_id.empty()) {
64 return do_audit(kcenon::common::make_error<document_response>(
65 static_cast<int>(
67 "retrieve: document_unique_id is empty",
68 std::string(error_source)));
69 }
70 if (repository_unique_id.empty()) {
71 return do_audit(kcenon::common::make_error<document_response>(
72 static_cast<int>(
74 "retrieve: repository_unique_id is empty",
75 std::string(error_source)));
76 }
77
79 req.document_unique_id = document_unique_id;
80 req.repository_unique_id = repository_unique_id;
81
82 auto env_result = detail::build_iti43_envelope(req);
83 if (env_result.is_err()) {
84 return do_audit(kcenon::common::make_error<document_response>(
85 env_result.error()));
86 }
87 auto env = env_result.value();
88
89 auto sign_result = detail::sign_envelope(
92 if (sign_result.is_err()) {
93 return do_audit(kcenon::common::make_error<document_response>(
94 sign_result.error()));
95 }
96
97 // Per-call copy: the ITI-43 SOAP action is specific to this
98 // transaction and must not mutate the shared opts_ member, so
99 // other transaction types - if this Consumer later grows more
100 // retrieve variants - cannot pick up a stale action from a
101 // previous call. This also preserves whatever the caller set.
102 http_options call_opts = opts_;
103 call_opts.soap_action = kIti43SoapAction;
104
105 auto http_result =
106 detail::http_post(call_opts, kSoapContentType, env.xml);
107 if (http_result.is_err()) {
108 return do_audit(kcenon::common::make_error<document_response>(
109 http_result.error()));
110 }
111 auto response = http_result.value();
112
113 auto parsed =
114 detail::parse_mtom_response(response.content_type, response.body);
115 if (parsed.is_err()) {
116 return do_audit(kcenon::common::make_error<document_response>(
117 parsed.error()));
118 }
119 auto mtom = parsed.value();
120
121 auto verify = detail::verify_envelope_integrity(mtom.root_xml);
122 if (verify.is_err()) {
123 return do_audit(kcenon::common::make_error<document_response>(
124 verify.error()));
125 }
126
127 return do_audit(detail::parse_retrieve_response(
128 mtom.root_xml, mtom.parts, req));
129 }
130
131private:
132 void audit(const std::string& document_unique_id,
133 const std::string& repository_unique_id,
135 if (!audit_sink_) return;
136 kcenon::pacs::security::xds_iti43_event e;
137 e.source_endpoint = opts_.endpoint;
138 e.document_unique_id = document_unique_id;
139 e.repository_unique_id = repository_unique_id;
140 if (r.is_ok()) {
142 } else {
143 e.outcome =
145 e.failure_description = r.error().message;
146 }
147 audit_sink_->on_iti43_retrieve(e);
148 }
149
152 std::shared_ptr<kcenon::pacs::security::xds_audit_sink> audit_sink_;
153};
154
156 signing_material signing)
157 : impl_(std::make_unique<impl>(std::move(opts), std::move(signing))) {}
158
161 document_consumer&&) noexcept = default;
162
164
166 const std::string& document_unique_id,
167 const std::string& repository_unique_id) {
168 return impl_->retrieve(document_unique_id, repository_unique_id);
169}
170
172 std::shared_ptr<kcenon::pacs::security::xds_audit_sink> sink) {
173 impl_->set_audit_sink(std::move(sink));
174}
175
176} // namespace kcenon::pacs::ihe::xds
void set_audit_sink(std::shared_ptr< kcenon::pacs::security::xds_audit_sink > sink)
std::shared_ptr< kcenon::pacs::security::xds_audit_sink > audit_sink_
impl(http_options opts, signing_material signing)
void audit(const std::string &document_unique_id, const std::string &repository_unique_id, const kcenon::common::Result< document_response > &r)
kcenon::common::Result< document_response > retrieve(const std::string &document_unique_id, const std::string &repository_unique_id)
ITI-43 Document Consumer actor.
kcenon::common::Result< document_response > retrieve(const std::string &document_unique_id, const std::string &repository_unique_id)
Retrieve one document by its (documentUniqueId, repositoryUniqueId) pair.
document_consumer(http_options opts, signing_material signing)
Construct an ITI-43 Document Consumer bound to a repository endpoint and a signing credential.
void set_audit_sink(std::shared_ptr< kcenon::pacs::security::xds_audit_sink > sink)
Install an ATNA audit sink.
IHE XDS.b Document Consumer Actor (ITI-43 Retrieve Document Set).
libcurl-backed HTTPS POST client for ITI-41 transport.
MTOM / XOP multipart/related packager for ITI-41.
kcenon::common::Result< document_response > parse_retrieve_response(const std::string &root_xml, const std::vector< mtom_part > &parts, const retrieve_request &req)
Parse a RetrieveDocumentSetResponse envelope and resolve the attachment that matches req.
kcenon::common::Result< built_envelope > build_iti43_envelope(const retrieve_request &req)
Build a SOAP envelope for ITI-43 from a retrieve_request.
kcenon::common::Result< http_response > http_post(const http_options &opts, const std::string &content_type, const std::string &body)
POST body to opts.endpoint with the given Content-Type.
kcenon::common::Result< bool > sign_envelope(built_envelope &env, std::string_view cert_pem, std::string_view key_pem, std::string_view key_password)
Sign the body + timestamp of env in place.
kcenon::common::Result< parsed_mtom > parse_mtom_response(const std::string &content_type_header, const std::string &body)
Parse a multipart/related MTOM response body.
kcenon::common::Result< bool > verify_envelope_integrity(std::string_view signed_xml)
Verify the structural integrity of a previously signed envelope against the certificate embedded in i...
constexpr std::string_view error_source
Short identifier used as the "source" field of make_error.
Definition errors.h:95
SOAP 1.2 envelope builder for IHE XDS.b ITI-43 RetrieveDocumentSet.
Parser for IHE XDS.b ITI-43 RetrieveDocumentSetResponse.
SOAP 1.2 envelope builder for IHE XDS.b ITI-41 requests.
ITI-43 RetrieveDocumentSet response payload.
HTTPS transport options for the Document Source actor.
ITI-43 RetrieveDocumentSet request target.
Signing material for WS-Security XML-DSig.
WS-Security XML-DSig signer backed by OpenSSL EVP_DigestSign.
ATNA audit event helpers for the IHE XDS.b actors (ITI-18/41/43).