2 * Copyright 2001-2010 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 * Implements SOAP 1.1 messaging over a transport.
24 #include "exceptions.h"
26 #include "soap/HTTPSOAPTransport.h"
27 #include "soap/SOAP.h"
28 #include "soap/SOAPClient.h"
29 #include "util/XMLHelper.h"
30 #include "validation/ValidatorSuite.h"
34 using namespace soap11;
35 using namespace xmltooling::logging;
36 using namespace xmltooling;
37 using namespace xercesc;
40 SOAPTransport::SOAPTransport()
44 SOAPTransport::~SOAPTransport()
48 bool SOAPTransport::setProviderOption(const char* provider, const char* option, const char* value)
53 bool SOAPTransport::setCacheTag(string* cacheTag)
58 void SOAPTransport::send(istream* in)
61 throw IOException("SOAP transport does not support an empty request body.");
65 long SOAPTransport::getStatusCode() const
70 HTTPSOAPTransport::HTTPSOAPTransport()
74 HTTPSOAPTransport::~HTTPSOAPTransport()
78 bool HTTPSOAPTransport::followRedirects(bool follow, unsigned int maxRedirs)
83 SOAPClient::SOAPClient(bool validate) : m_validate(validate), m_transport(nullptr)
87 SOAPClient::~SOAPClient()
92 void SOAPClient::setValidating(bool validate)
94 m_validate = validate;
97 void SOAPClient::reset()
103 void SOAPClient::send(const Envelope& env, const SOAPTransport::Address& addr)
105 // Prepare a transport object.
106 const char* pch = addr.m_endpoint ? strchr(addr.m_endpoint,':') : nullptr;
108 throw IOException("SOAP endpoint was not a URL.");
109 string scheme(addr.m_endpoint, pch-addr.m_endpoint);
110 m_transport = XMLToolingConfig::getConfig().SOAPTransportManager.newPlugin(scheme.c_str(), addr);
111 prepareTransport(*m_transport);
113 Category& log = Category::getInstance(XMLTOOLING_LOGCAT".SOAPClient");
114 if (log.isDebugEnabled())
115 log.debugStream() << "marshalled envelope:\n" << env << logging::eol;
117 // Serialize envelope.
122 m_transport->send(s);
125 Envelope* SOAPClient::receive()
128 throw IOException("No call is active.");
130 // If we can get the stream, then the call is still active.
131 istream& out = m_transport->receive();
133 return nullptr; // nothing yet
135 // Check content type.
136 string s = m_transport->getContentType();
137 if (s.find("text/xml") == string::npos)
138 throw IOException("Incorrect content type ($1) for SOAP response.", params(1,s.c_str() ? s.c_str() : "none"));
140 // Parse and bind the document into an XMLObject.
141 DOMDocument* doc = (m_validate ? XMLToolingConfig::getConfig().getValidatingParser()
142 : XMLToolingConfig::getConfig().getParser()).parse(out);
143 XercesJanitor<DOMDocument> janitor(doc);
145 Category& log = Category::getInstance(XMLTOOLING_LOGCAT".SOAPClient");
146 if (log.isDebugEnabled()) {
147 #ifdef XMLTOOLING_LOG4SHIB
148 log.debugStream() << "received XML:\n" << *(doc->getDocumentElement()) << logging::eol;
151 XMLHelper::serialize(doc->getDocumentElement(), buf);
152 log.debugStream() << "received XML:\n" << buf << logging::eol;
156 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
159 SchemaValidators.validate(xmlObject.get());
161 Envelope* env = dynamic_cast<Envelope*>(xmlObject.get());
163 throw IOException("Response was not a SOAP 1.1 Envelope.");
165 Body* body = env->getBody();
166 if (body && body->hasChildren()) {
168 const Fault* fault = dynamic_cast<Fault*>(body->getUnknownXMLObjects().front());
169 if (fault && handleFault(*fault))
170 throw IOException("SOAP client detected a Fault.");
177 void SOAPClient::prepareTransport(SOAPTransport& transport)
181 bool SOAPClient::handleFault(const Fault& fault)
183 const xmltooling::QName* code = (fault.getFaultcode() ? fault.getFaultcode()->getCode() : nullptr);
184 auto_ptr_char str((fault.getFaultstring() ? fault.getFaultstring()->getString() : nullptr));
185 Category::getInstance(XMLTOOLING_LOGCAT".SOAPClient").error(
186 "SOAP client detected a Fault: (%s) (%s)",
187 (code ? code->toString().c_str() : "no code"),
188 (str.get() ? str.get() : "no message")