2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * XMLProtocolProvider.cpp
\r
24 * XML-based protocol provider.
\r
27 #include "internal.h"
\r
28 #include "exceptions.h"
\r
29 #include "binding/ProtocolProvider.h"
\r
30 #include "util/DOMPropertySet.h"
\r
31 #include "util/SPConstants.h"
\r
34 #include <xmltooling/io/HTTPResponse.h>
\r
35 #include <xmltooling/util/NDC.h>
\r
36 #include <xmltooling/util/ReloadableXMLFile.h>
\r
37 #include <xmltooling/util/Threads.h>
\r
38 #include <xmltooling/util/XMLHelper.h>
\r
39 #include <xercesc/util/XMLUniDefs.hpp>
\r
41 using shibspconstants::SHIB2SPPROTOCOLS_NS;
\r
42 using namespace shibsp;
\r
43 using namespace xmltooling;
\r
44 using namespace std;
\r
48 static const XMLCh _id[] = UNICODE_LITERAL_2(i,d);
\r
49 static const XMLCh Binding[] = UNICODE_LITERAL_7(B,i,n,d,i,n,g);
\r
50 static const XMLCh Initiator[] = UNICODE_LITERAL_9(I,n,i,t,i,a,t,o,r);
\r
51 static const XMLCh Protocol[] = UNICODE_LITERAL_8(P,r,o,t,o,c,o,l);
\r
52 static const XMLCh Protocols[] = UNICODE_LITERAL_9(P,r,o,t,o,c,o,l,s);
\r
53 static const XMLCh Service[] = UNICODE_LITERAL_7(S,e,r,v,i,c,e);
\r
55 #if defined (_MSC_VER)
\r
56 #pragma warning( push )
\r
57 #pragma warning( disable : 4250 )
\r
60 class SHIBSP_DLLLOCAL XMLProtocolProviderImpl : public DOMNodeFilter, DOMPropertySet
\r
63 XMLProtocolProviderImpl(const DOMElement* e, Category& log);
\r
64 ~XMLProtocolProviderImpl() {
\r
65 for (protmap_t::iterator i = m_map.begin(); i != m_map.end(); ++i) {
\r
66 delete i->second.first;
\r
67 for_each(i->second.second.begin(), i->second.second.end(), xmltooling::cleanup<PropertySet>());
\r
70 m_document->release();
\r
73 void setDocument(DOMDocument* doc) {
\r
77 #ifdef SHIBSP_XERCESC_SHORT_ACCEPTNODE
\r
82 acceptNode(const DOMNode* node) const {
\r
83 return FILTER_REJECT;
\r
87 DOMDocument* m_document;
\r
88 // Map of protocol/service pair to an Initiator propset plus an array of Binding propsets.
\r
89 typedef map< pair<string,string>, pair< PropertySet*,vector<const PropertySet*> > > protmap_t;
\r
92 friend class SHIBSP_DLLLOCAL XMLProtocolProvider;
\r
95 class XMLProtocolProvider : public ProtocolProvider, public ReloadableXMLFile
\r
98 XMLProtocolProvider(const DOMElement* e)
\r
99 : ReloadableXMLFile(e, Category::getInstance(SHIBSP_LOGCAT".ProtocolProvider.XML")), m_impl(nullptr) {
\r
100 background_load(); // guarantees an exception or the policy is loaded
\r
103 ~XMLProtocolProvider() {
\r
108 const PropertySet* getInitiator(const char* protocol, const char* service) const {
\r
109 XMLProtocolProviderImpl::protmap_t::const_iterator i = m_impl->m_map.find(pair<string,string>(protocol,service));
\r
110 return (i != m_impl->m_map.end()) ? i->second.first : nullptr;
\r
113 const vector<const PropertySet*>& getBindings(const char* protocol, const char* service) const {
\r
114 XMLProtocolProviderImpl::protmap_t::const_iterator i = m_impl->m_map.find(pair<string,string>(protocol,service));
\r
115 return (i != m_impl->m_map.end()) ? i->second.second : m_noBindings;
\r
119 pair<bool,DOMElement*> load(bool backup);
\r
120 pair<bool,DOMElement*> background_load();
\r
123 static vector<const PropertySet*> m_noBindings;
\r
124 XMLProtocolProviderImpl* m_impl;
\r
127 #if defined (_MSC_VER)
\r
128 #pragma warning( pop )
\r
131 ProtocolProvider* SHIBSP_DLLLOCAL XMLProtocolProviderFactory(const DOMElement* const & e)
\r
133 return new XMLProtocolProvider(e);
\r
137 void SHIBSP_API shibsp::registerProtocolProviders()
\r
139 SPConfig::getConfig().ProtocolProviderManager.registerFactory(XML_PROTOCOL_PROVIDER, XMLProtocolProviderFactory);
\r
142 ProtocolProvider::ProtocolProvider()
\r
146 ProtocolProvider::~ProtocolProvider()
\r
150 vector<const PropertySet*> XMLProtocolProvider::m_noBindings;
\r
152 XMLProtocolProviderImpl::XMLProtocolProviderImpl(const DOMElement* e, Category& log) : m_document(nullptr)
\r
155 xmltooling::NDC ndc("XMLProtocolProviderImpl");
\r
157 //typedef map< pair<string,string>, pair< PropertySet*,vector<const PropertySet*> > > protmap_t;
\r
159 if (!XMLHelper::isNodeNamed(e, SHIB2SPPROTOCOLS_NS, Protocols))
\r
160 throw ConfigurationException("XML ProtocolProvider requires prot:Protocols at root of configuration.");
\r
162 e = XMLHelper::getFirstChildElement(e, SHIB2SPPROTOCOLS_NS, Protocol);
\r
164 string id = XMLHelper::getAttrString(e, nullptr, _id);
\r
166 const DOMElement* svc = XMLHelper::getFirstChildElement(e, SHIB2SPPROTOCOLS_NS, Service);
\r
168 string svcid = XMLHelper::getAttrString(svc, nullptr, _id);
\r
169 if (!svcid.empty() && m_map.count(make_pair(id,svcid)) == 0) {
\r
170 pair< PropertySet*,vector<const PropertySet*> >& entry = m_map[make_pair(id,svcid)];
\r
171 // Wrap the Initiator in a propset, if any.
\r
172 const DOMElement* child = XMLHelper::getFirstChildElement(svc, SHIB2SPPROTOCOLS_NS, Initiator);
\r
174 DOMPropertySet* initprop = new DOMPropertySet();
\r
175 entry.first = initprop;
\r
176 initprop->load(child, nullptr, this);
\r
179 entry.first = nullptr;
\r
182 // Walk the Bindings.
\r
183 child = XMLHelper::getFirstChildElement(svc, SHIB2SPPROTOCOLS_NS, Binding);
\r
185 DOMPropertySet* bindprop = new DOMPropertySet();
\r
186 entry.second.push_back(bindprop);
\r
187 bindprop->load(child, nullptr, this);
\r
188 child = XMLHelper::getNextSiblingElement(child, SHIB2SPPROTOCOLS_NS, Binding);
\r
191 svc = XMLHelper::getNextSiblingElement(svc, SHIB2SPPROTOCOLS_NS, Service);
\r
194 e = XMLHelper::getNextSiblingElement(e, SHIB2SPPROTOCOLS_NS, Protocol);
\r
199 pair<bool,DOMElement*> XMLProtocolProvider::load(bool backup)
\r
201 // Load from source using base class.
\r
202 pair<bool,DOMElement*> raw = ReloadableXMLFile::load(backup);
\r
204 // If we own it, wrap it.
\r
205 XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : nullptr);
\r
207 XMLProtocolProviderImpl* impl = new XMLProtocolProviderImpl(raw.second, m_log);
\r
209 // If we held the document, transfer it to the impl. If we didn't, it's a no-op.
\r
210 impl->setDocument(docjanitor.release());
\r
212 // Perform the swap inside a lock.
\r
215 SharedLock locker(m_lock, false);
\r
220 return make_pair(false,(DOMElement*)nullptr);
\r
223 pair<bool,DOMElement*> XMLProtocolProvider::background_load()
\r
226 return load(false);
\r
229 if (ex == HTTPResponse::XMLTOOLING_HTTP_STATUS_NOTMODIFIED)
\r
230 m_log.info("remote resource (%s) unchanged", m_source.c_str());
\r
231 if (!m_loaded && !m_backing.empty())
\r
235 catch (exception&) {
\r
236 if (!m_loaded && !m_backing.empty())
\r