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/OpenSSLSOAPTransport.h"
32 #include "soap/SOAP.h"
33 #include "soap/SOAPClient.h"
34 #include "util/XMLHelper.h"
35 #include "validation/ValidatorSuite.h"
39 using namespace soap11;
40 using namespace xmltooling::logging;
41 using namespace xmltooling;
42 using namespace xercesc;
45 #if !defined(XMLTOOLING_NO_XMLSEC) && !defined(XMLTOOLING_LITE)
46 namespace xmltooling {
47 PluginManager<SOAPTransport,string,SOAPTransport::Address>::Factory CURLSOAPTransportFactory;
51 void xmltooling::registerSOAPTransports()
53 #if !defined(XMLTOOLING_NO_XMLSEC) && !defined(XMLTOOLING_LITE)
54 XMLToolingConfig& conf=XMLToolingConfig::getConfig();
55 conf.SOAPTransportManager.registerFactory("http", CURLSOAPTransportFactory);
56 conf.SOAPTransportManager.registerFactory("https", CURLSOAPTransportFactory);
61 #ifdef XMLTOOLING_NO_XMLSEC
62 void xmltooling::initSOAPTransports()
66 void xmltooling::termSOAPTransports()
71 SOAPTransport::SOAPTransport()
75 SOAPTransport::~SOAPTransport()
79 bool SOAPTransport::setProviderOption(const char* provider, const char* option, const char* value)
84 bool SOAPTransport::setCacheTag(string* cacheTag)
89 void SOAPTransport::send(istream* in)
92 throw IOException("SOAP transport does not support an empty request body.");
96 long SOAPTransport::getStatusCode() const
101 HTTPSOAPTransport::HTTPSOAPTransport()
105 HTTPSOAPTransport::~HTTPSOAPTransport()
109 bool HTTPSOAPTransport::followRedirects(bool follow, unsigned int maxRedirs)
114 #ifndef XMLTOOLING_NO_XMLSEC
115 OpenSSLSOAPTransport::OpenSSLSOAPTransport()
119 OpenSSLSOAPTransport::~OpenSSLSOAPTransport()
124 SOAPClient::SOAPClient(bool validate) : m_validate(validate), m_transport(nullptr)
128 SOAPClient::~SOAPClient()
133 void SOAPClient::setValidating(bool validate)
135 m_validate = validate;
138 void SOAPClient::reset()
144 void SOAPClient::send(const Envelope& env, const SOAPTransport::Address& addr)
146 // Prepare a transport object.
147 const char* pch = addr.m_endpoint ? strchr(addr.m_endpoint,':') : nullptr;
149 throw IOException("SOAP endpoint was not a URL.");
150 string scheme(addr.m_endpoint, pch-addr.m_endpoint);
151 m_transport = XMLToolingConfig::getConfig().SOAPTransportManager.newPlugin(scheme.c_str(), addr);
152 prepareTransport(*m_transport);
154 Category& log = Category::getInstance(XMLTOOLING_LOGCAT".SOAPClient");
155 if (log.isDebugEnabled())
156 log.debugStream() << "marshalled envelope:\n" << env << logging::eol;
158 // Serialize envelope.
163 m_transport->send(s);
166 Envelope* SOAPClient::receive()
169 throw IOException("No call is active.");
171 // If we can get the stream, then the call is still active.
172 istream& out = m_transport->receive();
174 return nullptr; // nothing yet
176 // Check content type.
177 string s = m_transport->getContentType();
178 if (s.find("text/xml") == string::npos)
179 throw IOException("Incorrect content type ($1) for SOAP response.", params(1,s.c_str() ? s.c_str() : "none"));
181 // Parse and bind the document into an XMLObject.
182 DOMDocument* doc = (m_validate ? XMLToolingConfig::getConfig().getValidatingParser()
183 : XMLToolingConfig::getConfig().getParser()).parse(out);
184 XercesJanitor<DOMDocument> janitor(doc);
186 Category& log = Category::getInstance(XMLTOOLING_LOGCAT".SOAPClient");
187 if (log.isDebugEnabled()) {
189 XMLHelper::serialize(doc->getDocumentElement(), buf);
190 log.debugStream() << "received XML:\n" << buf << logging::eol;
193 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
196 SchemaValidators.validate(xmlObject.get());
198 Envelope* env = dynamic_cast<Envelope*>(xmlObject.get());
200 throw IOException("Response was not a SOAP 1.1 Envelope.");
202 Body* body = env->getBody();
203 if (body && body->hasChildren()) {
205 const Fault* fault = dynamic_cast<Fault*>(body->getUnknownXMLObjects().front());
206 if (fault && handleFault(*fault))
207 throw IOException("SOAP client detected a Fault.");
214 void SOAPClient::prepareTransport(SOAPTransport& transport)
218 bool SOAPClient::handleFault(const Fault& fault)
220 const xmltooling::QName* code = (fault.getFaultcode() ? fault.getFaultcode()->getCode() : nullptr);
221 auto_ptr_char str((fault.getFaultstring() ? fault.getFaultstring()->getString() : nullptr));
222 Category::getInstance(XMLTOOLING_LOGCAT".SOAPClient").error(
223 "SOAP client detected a Fault: (%s) (%s)",
224 (code ? code->toString().c_str() : "no code"),
225 (str.get() ? str.get() : "no message")