260736aaa0b0148cf69dd63e3d40ab919a9ebde3
[shibboleth/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 "logging.h"
26 #include "soap/SOAP.h"
27 #include "soap/SOAPClient.h"
28 #include "util/XMLHelper.h"
29 #include "validation/ValidatorSuite.h"
30
31 #include <sstream>
32
33 using namespace soap11;
34 using namespace xmltooling::logging;
35 using namespace xmltooling;
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 SOAPTransport::Address& addr)
50 {
51     // Prepare a transport object.
52     const char* pch = strchr(addr.m_endpoint,':');
53     if (!pch)
54         throw IOException("SOAP endpoint was not a URL.");
55     string scheme(addr.m_endpoint, pch-addr.m_endpoint);
56     m_transport = XMLToolingConfig::getConfig().SOAPTransportManager.newPlugin(scheme.c_str(), addr);
57     prepareTransport(*m_transport);
58     
59     Category& log = Category::getInstance(XMLTOOLING_LOGCAT".SOAPClient");
60     if (log.isDebugEnabled())
61         log.debugStream() << "marshalled envelope: " << env << logging::eol;
62     
63     // Serialize envelope.
64     stringstream s;
65     s << env;
66     
67     // Send to peer.
68     m_transport->send(s);
69 }
70
71 Envelope* SOAPClient::receive()
72 {
73     if (!m_transport)
74         throw IOException("No call is active.");
75     
76     // If we can get the stream, then the call is still active.
77     istream& out = m_transport->receive();
78     if (!out)
79         return NULL;    // nothing yet
80     
81     // Check content type.
82     string s = m_transport->getContentType();
83     if (s.find("text/xml") == string::npos)
84         throw IOException("Incorrect content type ($1) for SOAP response.", params(1,s.c_str() ? s.c_str() : "none"));
85     
86     // Parse and bind the document into an XMLObject.
87     DOMDocument* doc = (m_validate ? XMLToolingConfig::getConfig().getValidatingParser()
88         : XMLToolingConfig::getConfig().getParser()).parse(out); 
89     XercesJanitor<DOMDocument> janitor(doc);
90
91     Category& log = Category::getInstance(XMLTOOLING_LOGCAT".SOAPClient");
92     if (log.isDebugEnabled())
93         log.debugStream() << "received XML: " << *(doc->getDocumentElement()) << logging::eol;
94     
95     auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
96     janitor.release();
97     if (!m_validate)
98         SchemaValidators.validate(xmlObject.get());
99
100     Envelope* env = dynamic_cast<Envelope*>(xmlObject.get());
101     if (!env)
102         throw IOException("Response was not a SOAP 1.1 Envelope.");
103
104     Body* body = env->getBody();
105     if (body && body->hasChildren()) {
106         //Check for a Fault.
107         const Fault* fault = dynamic_cast<Fault*>(body->getUnknownXMLObjects().front());
108         if (fault && handleFault(*fault))
109             throw IOException("SOAP client detected a Fault.");
110     }
111
112     xmlObject.release();
113     return env;
114 }
115
116 bool SOAPClient::handleFault(const Fault& fault)
117 {
118     const QName* code = (fault.getFaultcode() ? fault.getFaultcode()->getCode() : NULL);
119     auto_ptr_char str((fault.getFaultstring() ? fault.getFaultstring()->getString() : NULL));
120     Category::getInstance(XMLTOOLING_LOGCAT".SOAPClient").error(
121         "SOAP client detected a Fault: (%s) (%s)",
122         (code ? code->toString().c_str() : "no code"),
123         (str.get() ? str.get() : "no message")
124         );
125     return true;
126 }