PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
kcenon::pacs::web::dicomweb::multipart_builder Class Reference

Builder for multipart MIME responses. More...

#include <dicomweb_endpoints.h>

Collaboration diagram for kcenon::pacs::web::dicomweb::multipart_builder:
Collaboration graph

Classes

struct  part
 

Public Member Functions

 multipart_builder (std::string_view content_type=media_type::dicom)
 Construct a multipart builder.
 
void add_part (std::vector< uint8_t > data, std::optional< std::string_view > content_type=std::nullopt)
 Add a part to the multipart response.
 
void add_part_with_location (std::vector< uint8_t > data, std::string_view location, std::optional< std::string_view > content_type=std::nullopt)
 Add a part with location header.
 
auto build () const -> std::string
 Build the complete multipart response body.
 
auto content_type_header () const -> std::string
 Get the Content-Type header value for this multipart response.
 
auto boundary () const -> std::string_view
 Get the boundary string.
 
auto empty () const noexcept -> bool
 Check if any parts have been added.
 
auto size () const noexcept -> size_t
 Get the number of parts.
 

Static Private Member Functions

static auto generate_boundary () -> std::string
 Generate a unique boundary string.
 

Private Attributes

std::string boundary_
 
std::string default_content_type_
 
std::vector< partparts_
 

Detailed Description

Builder for multipart MIME responses.

Generates multipart/related responses as required by WADO-RS for returning multiple DICOM objects.

Definition at line 98 of file dicomweb_endpoints.h.

Constructor & Destructor Documentation

◆ multipart_builder()

kcenon::pacs::web::dicomweb::multipart_builder::multipart_builder ( std::string_view content_type = media_type::dicom)
explicit

Construct a multipart builder.

Parameters
content_typeThe content type for parts (default: application/dicom)

Definition at line 206 of file dicomweb_endpoints.cpp.

Member Function Documentation

◆ add_part()

void kcenon::pacs::web::dicomweb::multipart_builder::add_part ( std::vector< uint8_t > data,
std::optional< std::string_view > content_type = std::nullopt )

Add a part to the multipart response.

Parameters
dataThe binary data for this part
content_typeOptional override content type for this part

Definition at line 210 of file dicomweb_endpoints.cpp.

212 {
213 part p;
214 p.data = std::move(data);
215 p.content_type = content_type.has_value()
216 ? std::string(*content_type)
218 parts_.push_back(std::move(p));
219}

References kcenon::pacs::web::dicomweb::multipart_builder::part::content_type, kcenon::pacs::web::dicomweb::multipart_builder::part::data, default_content_type_, and parts_.

◆ add_part_with_location()

void kcenon::pacs::web::dicomweb::multipart_builder::add_part_with_location ( std::vector< uint8_t > data,
std::string_view location,
std::optional< std::string_view > content_type = std::nullopt )

Add a part with location header.

Parameters
dataThe binary data for this part
locationThe Content-Location header value
content_typeOptional override content type for this part

Definition at line 221 of file dicomweb_endpoints.cpp.

224 {
225 part p;
226 p.data = std::move(data);
227 p.content_type = content_type.has_value()
228 ? std::string(*content_type)
230 p.location = std::string(location);
231 parts_.push_back(std::move(p));
232}

References kcenon::pacs::web::dicomweb::multipart_builder::part::content_type, kcenon::pacs::web::dicomweb::multipart_builder::part::data, default_content_type_, kcenon::pacs::web::dicomweb::multipart_builder::part::location, and parts_.

Referenced by kcenon::pacs::web::endpoints::register_dicomweb_endpoints_impl().

Here is the caller graph for this function:

◆ boundary()

auto kcenon::pacs::web::dicomweb::multipart_builder::boundary ( ) const -> std::string_view
nodiscard

Get the boundary string.

Returns
The boundary string

Definition at line 263 of file dicomweb_endpoints.cpp.

263 {
264 return boundary_;
265}

References boundary_.

◆ build()

auto kcenon::pacs::web::dicomweb::multipart_builder::build ( ) const -> std::string
nodiscard

Build the complete multipart response body.

Returns
The multipart response body as string

Definition at line 234 of file dicomweb_endpoints.cpp.

234 {
235 std::ostringstream oss;
236
237 for (const auto& p : parts_) {
238 oss << "--" << boundary_ << "\r\n";
239 oss << "Content-Type: " << p.content_type << "\r\n";
240 if (!p.location.empty()) {
241 oss << "Content-Location: " << p.location << "\r\n";
242 }
243 oss << "\r\n";
244 oss.write(reinterpret_cast<const char*>(p.data.data()),
245 static_cast<std::streamsize>(p.data.size()));
246 oss << "\r\n";
247 }
248
249 if (!parts_.empty()) {
250 oss << "--" << boundary_ << "--\r\n";
251 }
252
253 return oss.str();
254}

References boundary_, and parts_.

Referenced by kcenon::pacs::web::endpoints::register_dicomweb_endpoints_impl().

Here is the caller graph for this function:

◆ content_type_header()

auto kcenon::pacs::web::dicomweb::multipart_builder::content_type_header ( ) const -> std::string
nodiscard

Get the Content-Type header value for this multipart response.

Returns
Complete Content-Type header including boundary

Definition at line 256 of file dicomweb_endpoints.cpp.

256 {
257 std::ostringstream oss;
258 oss << "multipart/related; type=\"" << default_content_type_
259 << "\"; boundary=" << boundary_;
260 return oss.str();
261}

References boundary_, and default_content_type_.

Referenced by kcenon::pacs::web::endpoints::register_dicomweb_endpoints_impl().

Here is the caller graph for this function:

◆ empty()

auto kcenon::pacs::web::dicomweb::multipart_builder::empty ( ) const -> bool
nodiscardnoexcept

Check if any parts have been added.

Returns
true if at least one part exists

Definition at line 267 of file dicomweb_endpoints.cpp.

267 {
268 return parts_.empty();
269}

References parts_.

Referenced by kcenon::pacs::web::endpoints::register_dicomweb_endpoints_impl().

Here is the caller graph for this function:

◆ generate_boundary()

auto kcenon::pacs::web::dicomweb::multipart_builder::generate_boundary ( ) -> std::string
staticnodiscardprivate

Generate a unique boundary string.

Returns
Boundary string

Definition at line 275 of file dicomweb_endpoints.cpp.

275 {
276 // Generate a unique boundary using timestamp and random number
277 auto now = std::chrono::system_clock::now();
278 auto timestamp = std::chrono::duration_cast<std::chrono::microseconds>(
279 now.time_since_epoch())
280 .count();
281
282 std::random_device rd;
283 std::mt19937 gen(rd());
284 std::uniform_int_distribution<> dis(0, 999999);
285
286 std::ostringstream oss;
287 oss << "----=_Part_" << timestamp << "_" << dis(gen);
288 return oss.str();
289}

◆ size()

auto kcenon::pacs::web::dicomweb::multipart_builder::size ( ) const -> size_t
nodiscardnoexcept

Get the number of parts.

Returns
Number of parts added

Definition at line 271 of file dicomweb_endpoints.cpp.

271 {
272 return parts_.size();
273}

References parts_.

Referenced by kcenon::pacs::web::endpoints::register_dicomweb_endpoints_impl().

Here is the caller graph for this function:

Member Data Documentation

◆ boundary_

std::string kcenon::pacs::web::dicomweb::multipart_builder::boundary_
private

Definition at line 163 of file dicomweb_endpoints.h.

Referenced by boundary(), build(), and content_type_header().

◆ default_content_type_

std::string kcenon::pacs::web::dicomweb::multipart_builder::default_content_type_
private

Definition at line 164 of file dicomweb_endpoints.h.

Referenced by add_part(), add_part_with_location(), and content_type_header().

◆ parts_

std::vector<part> kcenon::pacs::web::dicomweb::multipart_builder::parts_
private

Definition at line 165 of file dicomweb_endpoints.h.

Referenced by add_part(), add_part_with_location(), build(), empty(), and size().


The documentation for this class was generated from the following files: