Missed parameter to log validator exceptions.
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / XMLMetadataProvider.cpp
index adcdee8..1809d3d 100644 (file)
@@ -27,6 +27,7 @@
 
 #include <xmltooling/util/NDC.h>
 #include <xmltooling/util/ReloadableXMLFile.h>
+#include <xmltooling/validation/ValidatorSuite.h>
 
 using namespace opensaml::saml2md;
 using namespace xmltooling::logging;
@@ -41,16 +42,12 @@ using namespace std;
 namespace opensaml {
     namespace saml2md {
 
-        static const XMLCh requireValidUntil[] = UNICODE_LITERAL_17(r,e,q,u,i,r,e,V,a,l,i,d,U,n,t,i,l);
-
         class SAML_DLLLOCAL XMLMetadataProvider : public AbstractMetadataProvider, public ReloadableXMLFile
         {
         public:
             XMLMetadataProvider(const DOMElement* e)
                 : AbstractMetadataProvider(e), ReloadableXMLFile(e, Category::getInstance(SAML_LOGCAT".MetadataProvider.XML")),
-                    m_object(NULL), m_requireValidUntil(false) {
-                const XMLCh* flag = e ? e->getAttributeNS(NULL,requireValidUntil) : NULL;
-                m_requireValidUntil = (flag && (*flag == chLatin_t || *flag == chDigit_1));
+                    m_object(NULL), m_maxCacheDuration(m_reloadInterval) {
             }
             virtual ~XMLMetadataProvider() {
                 delete m_object;
@@ -72,7 +69,7 @@ namespace opensaml {
             void index();
 
             XMLObject* m_object;
-            bool m_requireValidUntil;
+            time_t m_maxCacheDuration;
         };
 
         MetadataProvider* SAML_DLLLOCAL XMLMetadataProviderFactory(const DOMElement* const & e)
@@ -104,13 +101,16 @@ pair<bool,DOMElement*> XMLMetadataProvider::load()
             "Root of metadata instance not recognized: $1", params(1,xmlObject->getElementQName().toString().c_str())
             );
 
-    if (m_requireValidUntil) {
-        const TimeBoundSAMLObject* tbo = dynamic_cast<const TimeBoundSAMLObject*>(xmlObject.get());
-        if (!tbo || tbo->getValidUntil() == NULL)
-            throw MetadataException("Root of metadata instance does not have validUntil atttribute.");
-    }
-
     // Preprocess the metadata.
+    if (!m_validate) {
+        try {
+            SchemaValidators.validate(xmlObject.get());
+        }
+        catch (exception& ex) {
+            m_log.error("metadata intance failed manual schema validation checking: %s", ex.what());
+            throw MetadataException("Metadata instance failed manual schema validation checking.");
+        }
+    }
     doFilters(*xmlObject.get());
     xmlObject->releaseThisAndChildrenDOM();
     xmlObject->setDocument(NULL);
@@ -122,17 +122,33 @@ pair<bool,DOMElement*> XMLMetadataProvider::load()
     index();
     if (changed)
         emitChangeEvent();
+
+    // If a remote resource, reduce the reload interval if cacheDuration is set.
+    if (!m_local) {
+        const CacheableSAMLObject* cacheable = dynamic_cast<const CacheableSAMLObject*>(m_object);
+        if (cacheable && cacheable->getCacheDuration() && cacheable->getCacheDurationEpoch() < m_maxCacheDuration)
+            m_reloadInterval = cacheable->getCacheDurationEpoch();
+        else
+            m_reloadInterval = m_maxCacheDuration;
+    }
+
     return make_pair(false,(DOMElement*)NULL);
 }
 
 void XMLMetadataProvider::index()
 {
+    time_t exp = SAMLTIME_MAX;
+
     clearDescriptorIndex();
     EntitiesDescriptor* group=dynamic_cast<EntitiesDescriptor*>(m_object);
     if (group) {
-        AbstractMetadataProvider::index(group, SAMLTIME_MAX);
+        if (!m_local && group->getCacheDuration())
+            exp = time(NULL) + group->getCacheDurationEpoch();
+        AbstractMetadataProvider::index(group, exp);
         return;
     }
     EntityDescriptor* site=dynamic_cast<EntityDescriptor*>(m_object);
-    AbstractMetadataProvider::index(site, SAMLTIME_MAX);
+    if (!m_local && site->getCacheDuration())
+        exp = time(NULL) + site->getCacheDurationEpoch();
+    AbstractMetadataProvider::index(site, exp);
 }