+ // Turn off auto-backup so we can filter first.
+ m_backupIndicator = false;
+
+ // Load from source using base class.
+ pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
+
+ // If we own it, wrap it for now.
+ XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);
+
+ // Unmarshall objects, binding the document.
+ auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(raw.second, true));
+ docjanitor.release();
+
+ if (!dynamic_cast<const EntitiesDescriptor*>(xmlObject.get()) && !dynamic_cast<const EntityDescriptor*>(xmlObject.get()))
+ throw MetadataException(
+ "Root of metadata instance not recognized: $1", params(1,xmlObject->getElementQName().toString().c_str())
+ );
+
+ // Preprocess the metadata (even if we schema-validated).
+ try {
+ SchemaValidators.validate(xmlObject.get());
+ }
+ catch (exception& ex) {
+ m_log.error("metadata intance failed manual validation checking: %s", ex.what());
+ throw MetadataException("Metadata instance failed manual validation checking.");
+ }
+
+ // If the backup indicator is flipped, then this was a remote load and we need a backup.
+ // This is the best place to take a backup, since it's superficially "correct" metadata.
+ string backupKey;
+ if (m_backupIndicator) {
+ // We compute a random filename extension to the "real" location.
+ SAMLConfig::getConfig().generateRandomBytes(backupKey, 2);
+ backupKey = m_backing + '.' + SAMLArtifact::toHex(backupKey);
+ m_log.debug("backing up remote metadata resource to (%s)", backupKey.c_str());
+ try {
+ ofstream backer(backupKey.c_str());
+ backer << *raw.second->getOwnerDocument();
+ }
+ catch (exception& ex) {
+ m_log.crit("exception while backing up metadata: %s", ex.what());
+ backupKey.erase();
+ }
+ }
+