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