2a569797e7d3f38ad6c24284f1dec088e275a73d
[shibboleth/cpp-xmltooling.git] / xmltooling / soap / impl / SOAPClient.cpp
1 /*
2  *  Copyright 2001-2007 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     delete m_transport;
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 char* peerName, 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(peerName,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     // Check content type.
78     string s = m_transport->getContentType();
79     if (s.find("text/xml") == string::npos)
80         throw IOException("Incorrect content type ($1) for SOAP response.", params(1,s.c_str() ? s.c_str() : "none"));
81     
82     // Parse and bind the document into an XMLObject.
83     DOMDocument* doc = (m_validate ? XMLToolingConfig::getConfig().getValidatingParser()
84         : XMLToolingConfig::getConfig().getParser()).parse(out); 
85     XercesJanitor<DOMDocument> janitor(doc);
86     auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
87     janitor.release();
88     if (!m_validate)
89         SchemaValidators.validate(xmlObject.get());
90
91     Envelope* env = dynamic_cast<Envelope*>(xmlObject.get());
92     if (!env)
93         throw IOException("Response was not a SOAP 1.1 Envelope.");
94
95     Body* body = env->getBody();
96     if (body && body->hasChildren()) {
97         //Check for a Fault.
98         const Fault* fault = dynamic_cast<Fault*>(body->getUnknownXMLObjects().front());
99         if (fault && handleFault(*fault))
100             throw IOException("SOAP client detected a Fault.");
101     }
102
103     xmlObject.release();
104     return env;
105 }
106
107 bool SOAPClient::handleFault(const Fault& fault)
108 {
109     const QName* code = (fault.getFaultcode() ? fault.getFaultcode()->getCode() : NULL);
110     auto_ptr_char str((fault.getFaultstring() ? fault.getFaultstring()->getString() : NULL));
111     Category::getInstance(XMLTOOLING_LOGCAT".SOAPClient").error(
112         "SOAP client detected a Fault: (%s) (%s)",
113         (code ? code->toString().c_str() : "no code"),
114         (str.get() ? str.get() : "no message")
115         );
116     return true;
117 }