Add call to ValidatorSuite.
[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"\r
29
30 #include <sstream>
31
32 using namespace soap11;
33 using namespace xmltooling;
34 using namespace std;
35
36 SOAPClient::~SOAPClient()
37 {
38     reset();
39 }
40
41 void SOAPClient::reset()
42 {
43     delete m_transport;
44     m_transport=NULL;
45 }
46
47 void SOAPClient::send(const Envelope* env, const KeyInfoSource& peer, const char* endpoint)
48 {
49     // Prepare a transport object.
50     const char* pch = strchr(endpoint,':');
51     if (!pch)
52         throw IOException("SOAP endpoint was not a URL.");
53     string scheme(endpoint, pch-endpoint);
54     m_transport = XMLToolingConfig::getConfig().SOAPTransportManager.newPlugin(scheme.c_str(), make_pair(&peer,endpoint));
55     prepareTransport(*m_transport);
56     
57     // Serialize envelope.
58     stringstream s;
59     XMLHelper::serialize(env->marshall(), s);
60     
61     // Send to peer.
62     m_transport->send(s);
63 }
64
65 Envelope* SOAPClient::receive()
66 {
67     if (!m_transport)
68         throw IOException("No call is active.");
69     
70     // If we can get the stream, then the call is still active.
71     istream& out = m_transport->receive();
72     if (!out)
73         return NULL;    // nothing yet
74     
75     // Parse and bind the document into an XMLObject.
76     DOMDocument* doc = (m_validate ? XMLToolingConfig::getConfig().getValidatingParser()
77         : XMLToolingConfig::getConfig().getParser()).parse(out); 
78     XercesJanitor<DOMDocument> janitor(doc);
79     auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
80     janitor.release();
81     if (!m_validate)\r
82         SchemaValidators.validate(xmlObject.get());\r
83
84     Envelope* env = dynamic_cast<Envelope*>(xmlObject.get());
85     if (!env)
86         throw IOException("Response was not a SOAP 1.1 Envelope.");
87
88     reset();
89     xmlObject.release();
90     return env;
91 }