2 * Copyright 2001-2006 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"
25 #include "soap/SOAP.h"
26 #include "soap/SOAPClient.h"
27 #include "util/XMLHelper.h"
28 #include "validation/ValidatorSuite.h"
31 #include <log4cpp/Category.hh>
33 using namespace soap11;
34 using namespace xmltooling;
35 using namespace log4cpp;
38 SOAPClient::~SOAPClient()
43 void SOAPClient::reset()
49 void SOAPClient::send(const Envelope* env, const KeyInfoSource& peer, const char* endpoint)
51 // Prepare a transport object.
52 const char* pch = strchr(endpoint,':');
54 throw IOException("SOAP endpoint was not a URL.");
55 string scheme(endpoint, pch-endpoint);
56 m_transport = XMLToolingConfig::getConfig().SOAPTransportManager.newPlugin(scheme.c_str(), make_pair(&peer,endpoint));
57 prepareTransport(*m_transport);
59 // Serialize envelope.
67 Envelope* SOAPClient::receive()
70 throw IOException("No call is active.");
72 // If we can get the stream, then the call is still active.
73 istream& out = m_transport->receive();
75 return NULL; // nothing yet
77 // Parse and bind the document into an XMLObject.
78 DOMDocument* doc = (m_validate ? XMLToolingConfig::getConfig().getValidatingParser()
79 : XMLToolingConfig::getConfig().getParser()).parse(out);
80 XercesJanitor<DOMDocument> janitor(doc);
81 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
84 SchemaValidators.validate(xmlObject.get());
86 Envelope* env = dynamic_cast<Envelope*>(xmlObject.get());
88 throw IOException("Response was not a SOAP 1.1 Envelope.");
90 Body* body = env->getBody();
91 if (body && body->hasChildren()) {
93 const Fault* fault = dynamic_cast<Fault*>(body->getUnknownXMLObjects().front());
94 if (fault && handleFault(*fault))
95 throw IOException("SOAP client detected a Fault.");
102 bool SOAPClient::handleFault(const Fault& fault)
104 const QName* code = (fault.getFaultcode() ? fault.getFaultcode()->getCode() : NULL);
105 auto_ptr_char str((fault.getFaultstring() ? fault.getFaultstring()->getString() : NULL));
106 Category::getInstance(XMLTOOLING_LOGCAT".SOAPClient").error(
107 "SOAP client detected a Fault: (%s) (%s)",
108 (code ? code->toString().c_str() : "no code"),
109 (str.get() ? str.get() : "no message")