2 * Copyright 2010 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
18 * XMLProtocolProvider.cpp
\r
20 * XML-based protocol provider.
\r
23 #include "internal.h"
\r
24 #include "exceptions.h"
\r
25 #include "binding/ProtocolProvider.h"
\r
26 #include "util/DOMPropertySet.h"
\r
27 #include "util/SPConstants.h"
\r
30 #include <xmltooling/io/HTTPResponse.h>
\r
31 #include <xmltooling/util/NDC.h>
\r
32 #include <xmltooling/util/ReloadableXMLFile.h>
\r
33 #include <xmltooling/util/Threads.h>
\r
34 #include <xmltooling/util/XMLHelper.h>
\r
35 #include <xercesc/util/XMLUniDefs.hpp>
\r
37 using shibspconstants::SHIB2SPPROTOCOLS_NS;
\r
38 using namespace shibsp;
\r
39 using namespace xmltooling;
\r
40 using namespace std;
\r
44 static const XMLCh _id[] = UNICODE_LITERAL_2(i,d);
\r
45 static const XMLCh Binding[] = UNICODE_LITERAL_7(B,i,n,d,i,n,g);
\r
46 static const XMLCh Protocol[] = UNICODE_LITERAL_8(P,r,o,t,o,c,o,l);
\r
47 static const XMLCh Protocols[] = UNICODE_LITERAL_9(P,r,o,t,o,c,o,l,s);
\r
48 static const XMLCh Service[] = UNICODE_LITERAL_7(S,e,r,v,i,c,e);
\r
50 #if defined (_MSC_VER)
\r
51 #pragma warning( push )
\r
52 #pragma warning( disable : 4250 )
\r
55 class SHIBSP_DLLLOCAL XMLProtocolProviderImpl : public DOMNodeFilter, DOMPropertySet
\r
58 XMLProtocolProviderImpl(const DOMElement* e, Category& log);
\r
59 ~XMLProtocolProviderImpl() {
\r
60 for (protmap_t::iterator i = m_map.begin(); i != m_map.end(); ++i) {
\r
61 delete i->second.first;
\r
62 for_each(i->second.second.begin(), i->second.second.end(), xmltooling::cleanup<PropertySet>());
\r
65 m_document->release();
\r
68 void setDocument(DOMDocument* doc) {
\r
72 #ifdef SHIBSP_XERCESC_SHORT_ACCEPTNODE
\r
77 acceptNode(const DOMNode* node) const {
\r
78 return FILTER_REJECT;
\r
82 DOMDocument* m_document;
\r
83 // Map of protocol/service pair to a service propset plus an array of Binding propsets.
\r
84 typedef map< pair<string,string>, pair< PropertySet*,vector<const PropertySet*> > > protmap_t;
\r
87 friend class SHIBSP_DLLLOCAL XMLProtocolProvider;
\r
90 class XMLProtocolProvider : public ProtocolProvider, public ReloadableXMLFile
\r
93 XMLProtocolProvider(const DOMElement* e)
\r
94 : ReloadableXMLFile(e, Category::getInstance(SHIBSP_LOGCAT".ProtocolProvider.XML")), m_impl(nullptr) {
\r
95 background_load(); // guarantees an exception or the policy is loaded
\r
98 ~XMLProtocolProvider() {
\r
103 const PropertySet* getService(const char* protocol, const char* service) const {
\r
104 XMLProtocolProviderImpl::protmap_t::const_iterator i = m_impl->m_map.find(pair<string,string>(protocol,service));
\r
105 return (i != m_impl->m_map.end()) ? i->second.first : nullptr;
\r
108 const vector<const PropertySet*>& getBindings(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 if (i != m_impl->m_map.end())
\r
111 return i->second.second;
\r
112 throw ConfigurationException("ProtocolProvider can't return bindings for undefined protocol and service.");
\r
116 pair<bool,DOMElement*> load(bool backup);
\r
117 pair<bool,DOMElement*> background_load();
\r
120 XMLProtocolProviderImpl* m_impl;
\r
123 #if defined (_MSC_VER)
\r
124 #pragma warning( pop )
\r
127 ProtocolProvider* SHIBSP_DLLLOCAL XMLProtocolProviderFactory(const DOMElement* const & e)
\r
129 return new XMLProtocolProvider(e);
\r
133 void SHIBSP_API shibsp::registerProtocolProviders()
\r
135 SPConfig::getConfig().ProtocolProviderManager.registerFactory(XML_PROTOCOL_PROVIDER, XMLProtocolProviderFactory);
\r
138 ProtocolProvider::ProtocolProvider()
\r
142 ProtocolProvider::~ProtocolProvider()
\r
146 XMLProtocolProviderImpl::XMLProtocolProviderImpl(const DOMElement* e, Category& log) : m_document(nullptr)
\r
149 xmltooling::NDC ndc("XMLProtocolProviderImpl");
\r
151 //typedef map< pair<string,string>, pair< PropertySet*,vector<const PropertySet*> > > protmap_t;
\r
153 if (!XMLHelper::isNodeNamed(e, SHIB2SPPROTOCOLS_NS, Protocols))
\r
154 throw ConfigurationException("XML ProtocolProvider requires prot:Protocols at root of configuration.");
\r
156 e = XMLHelper::getFirstChildElement(e, SHIB2SPPROTOCOLS_NS, Protocol);
\r
158 string id = XMLHelper::getAttrString(e, nullptr, _id);
\r
160 const DOMElement* svc = XMLHelper::getFirstChildElement(e, SHIB2SPPROTOCOLS_NS, Service);
\r
162 string svcid = XMLHelper::getAttrString(svc, nullptr, _id);
\r
163 if (!svcid.empty()) {
\r
164 pair< PropertySet*,vector<const PropertySet*> >& entry = m_map[make_pair(id,svcid)];
\r
165 if (!entry.first) {
\r
166 // Wrap the Service in a propset.
\r
167 DOMPropertySet* svcprop = new DOMPropertySet();
\r
168 entry.first = svcprop;
\r
169 svcprop->load(svc, &log, this);
\r
171 // Walk the Bindings.
\r
172 const DOMElement* bind = XMLHelper::getFirstChildElement(svc, SHIB2SPPROTOCOLS_NS, Binding);
\r
174 DOMPropertySet* bindprop = new DOMPropertySet();
\r
175 entry.second.push_back(bindprop);
\r
176 bindprop->load(bind, &log, this);
\r
177 bind = XMLHelper::getNextSiblingElement(bind, SHIB2SPPROTOCOLS_NS, Binding);
\r
181 svc = XMLHelper::getNextSiblingElement(svc, SHIB2SPPROTOCOLS_NS, Service);
\r
184 e = XMLHelper::getNextSiblingElement(e, SHIB2SPPROTOCOLS_NS, Protocol);
\r
189 pair<bool,DOMElement*> XMLProtocolProvider::load(bool backup)
\r
191 // Load from source using base class.
\r
192 pair<bool,DOMElement*> raw = ReloadableXMLFile::load(backup);
\r
194 // If we own it, wrap it.
\r
195 XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : nullptr);
\r
197 XMLProtocolProviderImpl* impl = new XMLProtocolProviderImpl(raw.second, m_log);
\r
199 // If we held the document, transfer it to the impl. If we didn't, it's a no-op.
\r
200 impl->setDocument(docjanitor.release());
\r
202 // Perform the swap inside a lock.
\r
205 SharedLock locker(m_lock, false);
\r
210 return make_pair(false,(DOMElement*)nullptr);
\r
213 pair<bool,DOMElement*> XMLProtocolProvider::background_load()
\r
216 return load(false);
\r
219 if (ex == HTTPResponse::XMLTOOLING_HTTP_STATUS_NOTMODIFIED)
\r
220 m_log.info("remote resource (%s) unchanged", m_source.c_str());
\r
221 if (!m_loaded && !m_backing.empty())
\r
225 catch (exception&) {
\r
226 if (!m_loaded && !m_backing.empty())
\r