KD SOAP API Documentation: KDSoapMessageWriter.cpp Source File

| KD SOAP API Documentation 2.2

Loading...
Searching...
No Matches
KDSoapMessageWriter.cpp
Go to the documentation of this file.
1/****************************************************************************
2**
3** This file is part of the KD Soap project.
4**
5** SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6**
7** SPDX-License-Identifier: MIT
8**
9****************************************************************************/
10#include "KDSoapClientInterface_p.h"
11#include "KDSoapMessageWriter_p.h"
12#include "KDSoapNamespaceManager.h"
13#include "KDSoapNamespacePrefixes_p.h"
14#include "KDSoapValue.h"
15#include <QDebug>
16#include <QVariant>
17
18KDSoapMessageWriter::KDSoapMessageWriter()
19 : m_version(KDSoap::SOAP1_1)
20{
21}
22
23 void KDSoapMessageWriter::setVersion(KDSoap::SoapVersion version)
24{
25 m_version = version;
26}
27
28 void KDSoapMessageWriter::setMessageNamespace(const QString &ns)
29{
30 m_messageNamespace = ns;
31}
32
33 QByteArray KDSoapMessageWriter::messageToXml(const KDSoapMessage &message, const QString &method, const KDSoapHeaders &headers,
34 const QMap<QString, KDSoapMessage> &persistentHeaders, const KDSoapAuthentication &authentication) const
35{
36 QByteArray data;
37 QXmlStreamWriter writer(&data);
38 writer.writeStartDocument();
39
40 KDSoapNamespacePrefixes namespacePrefixes;
41 namespacePrefixes.writeStandardNamespaces(writer, m_version, message.hasMessageAddressingProperties(),
42 message.messageAddressingProperties().addressingNamespace());
43
44 QString soapEnvelope;
45 QString soapEncoding;
46 if (m_version == KDSoap::SOAP1_1) {
47 soapEnvelope = KDSoapNamespaceManager::soapEnvelope();
48 soapEncoding = KDSoapNamespaceManager::soapEncoding();
49 } else if (m_version == KDSoap::SOAP1_2) {
50 soapEnvelope = KDSoapNamespaceManager::soapEnvelope200305();
51 soapEncoding = KDSoapNamespaceManager::soapEncoding200305();
52 }
53
54 writer.writeStartElement(soapEnvelope, QLatin1String("Envelope"));
55
56 // This has been removed, see https://msdn.microsoft.com/en-us/library/ms995710.aspx for details
57 // writer.writeAttribute(soapEnvelope, QLatin1String("encodingStyle"), soapEncoding);
58
59 QString messageNamespace = m_messageNamespace;
60 if (!message.namespaceUri().isEmpty() && messageNamespace != message.namespaceUri()) {
61 messageNamespace = message.namespaceUri();
62 }
63
64 if (!headers.isEmpty() || !persistentHeaders.isEmpty() || message.hasMessageAddressingProperties() || authentication.hasWSUsernameTokenHeader()) {
65 // This writeNamespace line adds the xmlns:n1 to <Envelope>, which looks ugly and unusual (and breaks all unittests)
66 // However it's the best solution in case of headers, otherwise we get n1 in the header and n2 in the body,
67 // and xsi:type attributes that refer to n1, which isn't defined in the body...
68 namespacePrefixes.writeNamespace(writer, messageNamespace, QLatin1String("n1") /*make configurable?*/);
69 writer.writeStartElement(soapEnvelope, QLatin1String("Header"));
70 for (const KDSoapMessage &header : qAsConst(persistentHeaders)) {
71 header.writeChildren(namespacePrefixes, writer, header.use(), messageNamespace, true);
72 }
73 for (const KDSoapMessage &header : qAsConst(headers)) {
74 header.writeChildren(namespacePrefixes, writer, header.use(), messageNamespace, true);
75 }
76 if (message.hasMessageAddressingProperties()) {
77 message.messageAddressingProperties().writeMessageAddressingProperties(namespacePrefixes, writer, messageNamespace, true);
78 }
79 if (authentication.hasWSUsernameTokenHeader()) {
80 authentication.writeWSUsernameTokenHeader(writer);
81 }
82 writer.writeEndElement(); // Header
83 } else {
84 // So in the standard case (no headers) we just rely on Qt calling it n1 and insert it into the map.
85 // Calling this after the writeStartElement(ns, elementName) below leads to a double-definition of n1.
86 namespacePrefixes.insert(messageNamespace, QString::fromLatin1("n1"));
87 }
88
89 writer.writeStartElement(soapEnvelope, QLatin1String("Body"));
90
91 const QString elementName = !method.isEmpty() ? method : message.name();
92 if (elementName.isEmpty()) {
93 if (message.isNil()) {
94 // null message, ok (e.g. no arguments, in document/literal mode)
95 } else {
96 qWarning("ERROR: Non-empty message with an empty name!");
97 qDebug() << message;
98 }
99 } else {
100 // Note that the message itself is always qualified.
101 // isQualified() is only for child elements.
102 if (!message.isFault()) {
103 writer.writeStartElement(messageNamespace, elementName);
104 } else {
105 // Fault element should be inside soap namespace
106 writer.writeStartElement(soapEnvelope, elementName);
107 }
108 message.writeElementContents(namespacePrefixes, writer, message.use(), messageNamespace);
109 writer.writeEndElement();
110 }
111 writer.writeEndElement(); // Body
112 writer.writeEndElement(); // Envelope
113 writer.writeEndDocument();
114
115 return data;
116}
KDSoapClientInterface_p.h
KDSoapMessageWriter_p.h
KDSoapNamespaceManager.h
KDSoapNamespacePrefixes_p.h
KDSoapValue.h
KDSoapAuthentication
Definition KDSoapAuthentication.h:31
KDSoapHeaders
Definition KDSoapMessage.h:185
KDSoapMessageAddressingProperties::addressingNamespace
KDSoapAddressingNamespace addressingNamespace() const
Definition KDSoapMessageAddressingProperties.cpp:193
KDSoapMessageWriter::KDSoapMessageWriter
KDSoapMessageWriter()
Definition KDSoapMessageWriter.cpp:18
KDSoapMessageWriter::messageToXml
QByteArray messageToXml(const KDSoapMessage &message, const QString &method, const KDSoapHeaders &headers, const QMap< QString, KDSoapMessage > &persistentHeaders, const KDSoapAuthentication &authentication=KDSoapAuthentication()) const
Definition KDSoapMessageWriter.cpp:33
KDSoapMessageWriter::setMessageNamespace
void setMessageNamespace(const QString &ns)
Definition KDSoapMessageWriter.cpp:28
KDSoapMessageWriter::setVersion
void setVersion(KDSoap::SoapVersion version)
Definition KDSoapMessageWriter.cpp:23
KDSoapMessage
Definition KDSoapMessage.h:29
KDSoapMessage::use
Use use() const
Definition KDSoapMessage.cpp:190
KDSoapMessage::isFault
bool isFault() const
Definition KDSoapMessage.cpp:110
KDSoapMessage::hasMessageAddressingProperties
bool hasMessageAddressingProperties() const
Definition KDSoapMessage.cpp:185
KDSoapMessage::messageAddressingProperties
KDSoapMessageAddressingProperties messageAddressingProperties() const
Definition KDSoapMessage.cpp:174
KDSoapNamespaceManager::soapEncoding
static QString soapEncoding()
Definition KDSoapNamespaceManager.cpp:46
KDSoapNamespaceManager::soapEnvelope
static QString soapEnvelope()
Definition KDSoapNamespaceManager.cpp:36
KDSoapNamespaceManager::soapEnvelope200305
static QString soapEnvelope200305()
Definition KDSoapNamespaceManager.cpp:41
KDSoapNamespaceManager::soapEncoding200305
static QString soapEncoding200305()
Definition KDSoapNamespaceManager.cpp:51
KDSoapNamespacePrefixes
Definition KDSoapNamespacePrefixes_p.h:20
KDSoapNamespacePrefixes::writeStandardNamespaces
void writeStandardNamespaces(QXmlStreamWriter &writer, KDSoap::SoapVersion version=KDSoap::SOAP1_1, bool messageAddressingEnabled=false, KDSoapMessageAddressingProperties::KDSoapAddressingNamespace messageAddressingNamespace=KDSoapMessageAddressingProperties::Addressing200508)
Definition KDSoapNamespacePrefixes.cpp:14
KDSoapNamespacePrefixes::writeNamespace
void writeNamespace(QXmlStreamWriter &writer, const QString &ns, const QString &prefix)
Definition KDSoapNamespacePrefixes_p.h:26
KDSoapValue::namespaceUri
QString namespaceUri() const
Definition KDSoapValue.cpp:457
KDSoapValue::name
QString name() const
Definition KDSoapValue.cpp:94
KDSoapValue::isNil
bool isNil() const
Definition KDSoapValue.cpp:84
KDSoap
Definition KDSoapValue.h:33
KDSoap::SoapVersion
SoapVersion
Definition KDSoapValue.h:40
KDSoap::SOAP1_1
@ SOAP1_1
Definition KDSoapValue.h:42
KDSoap::SOAP1_2
@ SOAP1_2
Definition KDSoapValue.h:44
QByteArray
QLatin1String
QList::isEmpty
bool isEmpty() const const
QMap
QMap::insert
QMap::iterator insert(const Key &key, const T &value)
QMap::isEmpty
bool isEmpty() const const
QString
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QString::isEmpty
bool isEmpty() const const
QXmlStreamWriter
QXmlStreamWriter::writeEndDocument
void writeEndDocument()
QXmlStreamWriter::writeEndElement
void writeEndElement()
QXmlStreamWriter::writeStartDocument
void writeStartDocument()
QXmlStreamWriter::writeStartElement
void writeStartElement(const QString &qualifiedName)

© 2010-2024 Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
https://www.kdab.com/development-resources/qt-tools/kd-soap/
Generated on Sun Jan 7 2024 11:47:36 for KD SOAP API Documentation by doxygen 1.9.8