Refined ElementProxy/ElementExtensible interfaces to match Java.
[shibboleth/cpp-xmltooling.git] / xmltooling / soap / impl / SOAPClient.cpp
index f8a84b5..d785b63 100644 (file)
 #include "soap/SOAP.h"
 #include "soap/SOAPClient.h"
 #include "util/XMLHelper.h"
-#include "validation/ValidatorSuite.h"\r
+#include "validation/ValidatorSuite.h"
 
 #include <sstream>
+#include <log4cpp/Category.hh>
 
 using namespace soap11;
 using namespace xmltooling;
+using namespace log4cpp;
 using namespace std;
 
 SOAPClient::~SOAPClient()
@@ -56,7 +58,7 @@ void SOAPClient::send(const Envelope* env, const KeyInfoSource& peer, const char
     
     // Serialize envelope.
     stringstream s;
-    XMLHelper::serialize(env->marshall(), s);
+    s << *env;
     
     // Send to peer.
     m_transport->send(s);
@@ -78,14 +80,33 @@ Envelope* SOAPClient::receive()
     XercesJanitor<DOMDocument> janitor(doc);
     auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
     janitor.release();
-    if (!m_validate)\r
-        SchemaValidators.validate(xmlObject.get());\r
+    if (!m_validate)
+        SchemaValidators.validate(xmlObject.get());
 
     Envelope* env = dynamic_cast<Envelope*>(xmlObject.get());
     if (!env)
         throw IOException("Response was not a SOAP 1.1 Envelope.");
 
-    reset();
+    Body* body = env->getBody();
+    if (body && body->hasChildren()) {
+        //Check for a Fault.
+        const Fault* fault = dynamic_cast<Fault*>(body->getUnknownXMLObjects().front());
+        if (fault && handleFault(*fault))
+            throw IOException("SOAP client detected a Fault.");
+    }
+
     xmlObject.release();
     return env;
 }
+
+bool SOAPClient::handleFault(const Fault& fault)
+{
+    const QName* code = (fault.getFaultcode() ? fault.getFaultcode()->getCode() : NULL);
+    auto_ptr_char str((fault.getFaultstring() ? fault.getFaultstring()->getString() : NULL));
+    Category::getInstance(XMLTOOLING_LOGCAT".SOAPClient").error(
+        "SOAP client detected a Fault: (%s) (%s)",
+        (code ? code->toString().c_str() : "no code"),
+        (str.get() ? str.get() : "no message")
+        );
+    return true;
+}