2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
24 * Implements SOAP 1.1 messaging over a transport.
28 #include "exceptions.h"
30 #include "soap/HTTPSOAPTransport.h"
31 #include "soap/SOAP.h"
32 #include "soap/SOAPClient.h"
33 #include "util/XMLHelper.h"
34 #include "validation/ValidatorSuite.h"
38 using namespace soap11;
39 using namespace xmltooling::logging;
40 using namespace xmltooling;
41 using namespace xercesc;
44 SOAPTransport::SOAPTransport()
48 SOAPTransport::~SOAPTransport()
52 bool SOAPTransport::setProviderOption(const char* provider, const char* option, const char* value)
57 bool SOAPTransport::setCacheTag(string* cacheTag)
62 void SOAPTransport::send(istream* in)
65 throw IOException("SOAP transport does not support an empty request body.");
69 long SOAPTransport::getStatusCode() const
74 HTTPSOAPTransport::HTTPSOAPTransport()
78 HTTPSOAPTransport::~HTTPSOAPTransport()
82 bool HTTPSOAPTransport::followRedirects(bool follow, unsigned int maxRedirs)
87 SOAPClient::SOAPClient(bool validate) : m_validate(validate), m_transport(nullptr)
91 SOAPClient::~SOAPClient()
96 void SOAPClient::setValidating(bool validate)
98 m_validate = validate;
101 void SOAPClient::reset()
107 void SOAPClient::send(const Envelope& env, const SOAPTransport::Address& addr)
109 // Prepare a transport object.
110 const char* pch = addr.m_endpoint ? strchr(addr.m_endpoint,':') : nullptr;
112 throw IOException("SOAP endpoint was not a URL.");
113 string scheme(addr.m_endpoint, pch-addr.m_endpoint);
114 m_transport = XMLToolingConfig::getConfig().SOAPTransportManager.newPlugin(scheme.c_str(), addr);
115 prepareTransport(*m_transport);
117 Category& log = Category::getInstance(XMLTOOLING_LOGCAT".SOAPClient");
118 if (log.isDebugEnabled())
119 log.debugStream() << "marshalled envelope:\n" << env << logging::eol;
121 // Serialize envelope.
126 m_transport->send(s);
129 Envelope* SOAPClient::receive()
132 throw IOException("No call is active.");
134 // If we can get the stream, then the call is still active.
135 istream& out = m_transport->receive();
137 return nullptr; // nothing yet
139 // Check content type.
140 string s = m_transport->getContentType();
141 if (s.find("text/xml") == string::npos)
142 throw IOException("Incorrect content type ($1) for SOAP response.", params(1,s.c_str() ? s.c_str() : "none"));
144 // Parse and bind the document into an XMLObject.
145 DOMDocument* doc = (m_validate ? XMLToolingConfig::getConfig().getValidatingParser()
146 : XMLToolingConfig::getConfig().getParser()).parse(out);
147 XercesJanitor<DOMDocument> janitor(doc);
149 Category& log = Category::getInstance(XMLTOOLING_LOGCAT".SOAPClient");
150 if (log.isDebugEnabled()) {
151 #ifdef XMLTOOLING_LOG4SHIB
152 log.debugStream() << "received XML:\n" << *(doc->getDocumentElement()) << logging::eol;
155 XMLHelper::serialize(doc->getDocumentElement(), buf);
156 log.debugStream() << "received XML:\n" << buf << logging::eol;
160 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
163 SchemaValidators.validate(xmlObject.get());
165 Envelope* env = dynamic_cast<Envelope*>(xmlObject.get());
167 throw IOException("Response was not a SOAP 1.1 Envelope.");
169 Body* body = env->getBody();
170 if (body && body->hasChildren()) {
172 const Fault* fault = dynamic_cast<Fault*>(body->getUnknownXMLObjects().front());
173 if (fault && handleFault(*fault))
174 throw IOException("SOAP client detected a Fault.");
181 void SOAPClient::prepareTransport(SOAPTransport& transport)
185 bool SOAPClient::handleFault(const Fault& fault)
187 const xmltooling::QName* code = (fault.getFaultcode() ? fault.getFaultcode()->getCode() : nullptr);
188 auto_ptr_char str((fault.getFaultstring() ? fault.getFaultstring()->getString() : nullptr));
189 Category::getInstance(XMLTOOLING_LOGCAT".SOAPClient").error(
190 "SOAP client detected a Fault: (%s) (%s)",
191 (code ? code->toString().c_str() : "no code"),
192 (str.get() ? str.get() : "no message")