7cf17f6f7969005b0e2b05337962e3f485a344fc
[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 "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 void SOAPTransport::send(istream* in)
39 {
40     if (!in)
41         throw IOException("SOAP transport does not support an empty request body.");
42     return send(*in);
43 }
44
45 SOAPClient::~SOAPClient()
46 {
47     delete m_transport;
48 }
49
50 void SOAPClient::reset()
51 {
52     delete m_transport;
53     m_transport=NULL;
54 }
55
56 void SOAPClient::send(const Envelope& env, const SOAPTransport::Address& addr)
57 {
58     // Prepare a transport object.
59     const char* pch = strchr(addr.m_endpoint,':');
60     if (!pch)
61         throw IOException("SOAP endpoint was not a URL.");
62     string scheme(addr.m_endpoint, pch-addr.m_endpoint);
63     m_transport = XMLToolingConfig::getConfig().SOAPTransportManager.newPlugin(scheme.c_str(), addr);
64     prepareTransport(*m_transport);
65     
66     Category& log = Category::getInstance(XMLTOOLING_LOGCAT".SOAPClient");
67     if (log.isDebugEnabled())
68         log.debugStream() << "marshalled envelope:\n" << env << logging::eol;
69     
70     // Serialize envelope.
71     stringstream s;
72     s << env;
73     
74     // Send to peer.
75     m_transport->send(s);
76 }
77
78 Envelope* SOAPClient::receive()
79 {
80     if (!m_transport)
81         throw IOException("No call is active.");
82     
83     // If we can get the stream, then the call is still active.
84     istream& out = m_transport->receive();
85     if (!out)
86         return NULL;    // nothing yet
87     
88     // Check content type.
89     string s = m_transport->getContentType();
90     if (s.find("text/xml") == string::npos)
91         throw IOException("Incorrect content type ($1) for SOAP response.", params(1,s.c_str() ? s.c_str() : "none"));
92     
93     // Parse and bind the document into an XMLObject.
94     DOMDocument* doc = (m_validate ? XMLToolingConfig::getConfig().getValidatingParser()
95         : XMLToolingConfig::getConfig().getParser()).parse(out); 
96     XercesJanitor<DOMDocument> janitor(doc);
97
98     Category& log = Category::getInstance(XMLTOOLING_LOGCAT".SOAPClient");
99     if (log.isDebugEnabled()) {
100 #ifdef XMLTOOLING_LOG4SHIB
101         log.debugStream() << "received XML:\n" << *(doc->getDocumentElement()) << logging::eol;
102 #else
103         string buf;
104         XMLHelper::serialize(doc->getDocumentElement(), buf);
105         log.debugStream() << "received XML:\n" << buf << logging::eol;
106 #endif
107     }
108     
109     auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
110     janitor.release();
111     if (!m_validate)
112         SchemaValidators.validate(xmlObject.get());
113
114     Envelope* env = dynamic_cast<Envelope*>(xmlObject.get());
115     if (!env)
116         throw IOException("Response was not a SOAP 1.1 Envelope.");
117
118     Body* body = env->getBody();
119     if (body && body->hasChildren()) {
120         //Check for a Fault.
121         const Fault* fault = dynamic_cast<Fault*>(body->getUnknownXMLObjects().front());
122         if (fault && handleFault(*fault))
123             throw IOException("SOAP client detected a Fault.");
124     }
125
126     xmlObject.release();
127     return env;
128 }
129
130 bool SOAPClient::handleFault(const Fault& fault)
131 {
132     const QName* code = (fault.getFaultcode() ? fault.getFaultcode()->getCode() : NULL);
133     auto_ptr_char str((fault.getFaultstring() ? fault.getFaultstring()->getString() : NULL));
134     Category::getInstance(XMLTOOLING_LOGCAT".SOAPClient").error(
135         "SOAP client detected a Fault: (%s) (%s)",
136         (code ? code->toString().c_str() : "no code"),
137         (str.get() ? str.get() : "no message")
138         );
139     return true;
140 }