Refined ElementProxy/ElementExtensible interfaces to match Java.
[shibboleth/cpp-xmltooling.git] / xmltooling / soap / impl / SOAPClient.cpp
1 /*
2  *  Copyright 2001-2006 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * SOAPClient.cpp
19  * 
20  * Implements SOAP 1.1 messaging over a transport.
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "soap/SOAP.h"
26 #include "soap/SOAPClient.h"
27 #include "util/XMLHelper.h"
28 #include "validation/ValidatorSuite.h"
29
30 #include <sstream>
31 #include <log4cpp/Category.hh>
32
33 using namespace soap11;
34 using namespace xmltooling;
35 using namespace log4cpp;
36 using namespace std;
37
38 SOAPClient::~SOAPClient()
39 {
40     reset();
41 }
42
43 void SOAPClient::reset()
44 {
45     delete m_transport;
46     m_transport=NULL;
47 }
48
49 void SOAPClient::send(const Envelope* env, const KeyInfoSource& peer, const char* endpoint)
50 {
51     // Prepare a transport object.
52     const char* pch = strchr(endpoint,':');
53     if (!pch)
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);
58     
59     // Serialize envelope.
60     stringstream s;
61     s << *env;
62     
63     // Send to peer.
64     m_transport->send(s);
65 }
66
67 Envelope* SOAPClient::receive()
68 {
69     if (!m_transport)
70         throw IOException("No call is active.");
71     
72     // If we can get the stream, then the call is still active.
73     istream& out = m_transport->receive();
74     if (!out)
75         return NULL;    // nothing yet
76     
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));
82     janitor.release();
83     if (!m_validate)
84         SchemaValidators.validate(xmlObject.get());
85
86     Envelope* env = dynamic_cast<Envelope*>(xmlObject.get());
87     if (!env)
88         throw IOException("Response was not a SOAP 1.1 Envelope.");
89
90     Body* body = env->getBody();
91     if (body && body->hasChildren()) {
92         //Check for a Fault.
93         const Fault* fault = dynamic_cast<Fault*>(body->getUnknownXMLObjects().front());
94         if (fault && handleFault(*fault))
95             throw IOException("SOAP client detected a Fault.");
96     }
97
98     xmlObject.release();
99     return env;
100 }
101
102 bool SOAPClient::handleFault(const Fault& fault)
103 {
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")
110         );
111     return true;
112 }