Make OpenSAML MetadataProvider implementations loadable by the IdP configuration...
authorwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Mon, 10 Jul 2006 20:18:47 +0000 (20:18 +0000)
committerwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Mon, 10 Jul 2006 20:18:47 +0000 (20:18 +0000)
git-svn-id: https://subversion.switch.ch/svn/shibboleth/java-idp/trunk@1964 ab3bd59b-922f-494d-bb5f-6f0a3c29deca

src/edu/internet2/middleware/shibboleth/metadata/DOMMetadataProvider.java [new file with mode: 0644]
src/edu/internet2/middleware/shibboleth/metadata/FilesystemMetadataProvider.java [new file with mode: 0644]
src/edu/internet2/middleware/shibboleth/metadata/ShibbolethConfigurableMetadataProvider.java [new file with mode: 0644]
src/edu/internet2/middleware/shibboleth/metadata/URLMetadataProvider.java [new file with mode: 0644]

diff --git a/src/edu/internet2/middleware/shibboleth/metadata/DOMMetadataProvider.java b/src/edu/internet2/middleware/shibboleth/metadata/DOMMetadataProvider.java
new file mode 100644 (file)
index 0000000..3b9c9db
--- /dev/null
@@ -0,0 +1,27 @@
+
+package edu.internet2.middleware.shibboleth.metadata;
+
+import org.opensaml.saml2.metadata.provider.MetadataProvider;
+import org.opensaml.saml2.metadata.provider.MetadataProviderException;
+import org.w3c.dom.Element;
+
+/**
+ * Loads metadata directly from the IdP configuration.
+ * 
+ * @author Walter Hoehn
+ */
+/*
+ * We could do without this class, I guess, since the OpenSAML version has the right constructor; but it seems prudent
+ * to put it in the same hierarchy with its peers.
+ */
+public class DOMMetadataProvider extends ShibbolethConfigurableMetadataProvider implements MetadataProvider {
+
+       public DOMMetadataProvider(Element configuration) throws MetadataProviderException {
+
+               super(configuration);
+
+               provider = new org.opensaml.saml2.metadata.provider.DOMMetadataProvider(configuration);
+
+       }
+
+}
diff --git a/src/edu/internet2/middleware/shibboleth/metadata/FilesystemMetadataProvider.java b/src/edu/internet2/middleware/shibboleth/metadata/FilesystemMetadataProvider.java
new file mode 100644 (file)
index 0000000..b3535af
--- /dev/null
@@ -0,0 +1,51 @@
+
+package edu.internet2.middleware.shibboleth.metadata;
+
+import java.io.IOException;
+
+import org.apache.log4j.Logger;
+import org.opensaml.saml2.metadata.provider.MetadataProvider;
+import org.opensaml.saml2.metadata.provider.MetadataProviderException;
+import org.w3c.dom.Element;
+
+import edu.internet2.middleware.shibboleth.common.ShibResource;
+import edu.internet2.middleware.shibboleth.common.ShibResource.ResourceNotAvailableException;
+
+/**
+ * Loads metadata from a path specified in the IdP configuration.
+ * 
+ * @author Walter Hoehn
+ */
+public class FilesystemMetadataProvider extends ShibbolethConfigurableMetadataProvider implements MetadataProvider {
+
+       private static Logger log = Logger.getLogger(FilesystemMetadataProvider.class.getName());
+
+       public FilesystemMetadataProvider(Element configuration) throws MetadataProviderException {
+
+               super(configuration);
+
+               // Grab the path from the config
+               String path = ((Element) configuration).getAttribute("path");
+               if (path == null || path.equals("")) {
+                       log.error("Unable to load File System Metadata Provider.  A (path) attribute is required.  "
+                                       + "Add a (path) attribute to <MetadataProvider/>.");
+                       throw new MetadataProviderException("Required configuration not specified.");
+               }
+
+               // Construct provider from config
+               try {
+                       provider = new org.opensaml.saml2.metadata.provider.FilesystemMetadataProvider(new ShibResource(path)
+                                       .getFile());
+
+               } catch (MetadataProviderException e) {
+                       log.error("Unable to load URL Metadata Provider: " + e);
+                       throw e;
+               } catch (ResourceNotAvailableException e) {
+                       log.error("Unable to load File System Metadata Provider.  Could not access file at (" + path + ").");
+                       throw new MetadataProviderException("Supplied configuration is invalid.");
+               } catch (IOException e) {
+                       log.error("Unable to load File System Metadata Provider.  Error while reading file: " + e);
+                       throw new MetadataProviderException("Supplied configuration is invalid.");
+               }
+       }
+}
diff --git a/src/edu/internet2/middleware/shibboleth/metadata/ShibbolethConfigurableMetadataProvider.java b/src/edu/internet2/middleware/shibboleth/metadata/ShibbolethConfigurableMetadataProvider.java
new file mode 100644 (file)
index 0000000..60f10a5
--- /dev/null
@@ -0,0 +1,80 @@
+
+package edu.internet2.middleware.shibboleth.metadata;
+
+import java.util.List;
+
+import javax.xml.namespace.QName;
+
+import org.opensaml.saml2.metadata.EntitiesDescriptor;
+import org.opensaml.saml2.metadata.EntityDescriptor;
+import org.opensaml.saml2.metadata.RoleDescriptor;
+import org.opensaml.saml2.metadata.provider.MetadataFilter;
+import org.opensaml.saml2.metadata.provider.MetadataProvider;
+import org.opensaml.saml2.metadata.provider.MetadataProviderException;
+import org.opensaml.xml.XMLObject;
+import org.w3c.dom.Element;
+
+/**
+ * Base class for <code>MetadataProvider</code> implementations that can be loaded based on the IdP runtime
+ * configuration. Implementors should create a constructor that accepts a configuration <code>Element</code> and sets
+ * the provider field.
+ * 
+ * @author Walter Hoehn
+ */
+public abstract class ShibbolethConfigurableMetadataProvider implements MetadataProvider {
+
+       protected org.opensaml.saml2.metadata.provider.MetadataProvider provider;
+
+       public ShibbolethConfigurableMetadataProvider(Element configuration) {
+
+       }
+
+       public boolean requireValidMetadata() {
+
+               return provider.requireValidMetadata();
+       }
+
+       public void setRequireValidMetadata(boolean requireValidMetadata) {
+
+               provider.setRequireValidMetadata(requireValidMetadata);
+
+       }
+
+       public MetadataFilter getMetadataFilter() {
+
+               return provider.getMetadataFilter();
+       }
+
+       public void setMetadataFilter(MetadataFilter newFilter) throws MetadataProviderException {
+
+               provider.setMetadataFilter(newFilter);
+
+       }
+
+       public EntityDescriptor getEntityDescriptor(String entityID) throws MetadataProviderException {
+
+               return provider.getEntityDescriptor(entityID);
+       }
+
+       public List<RoleDescriptor> getRole(String entityID, QName roleName) throws MetadataProviderException {
+
+               return provider.getRole(entityID, roleName);
+       }
+
+       public List<RoleDescriptor> getRole(String entityID, QName roleName, String supportedProtocol)
+                       throws MetadataProviderException {
+
+               return provider.getRole(entityID, roleName, supportedProtocol);
+       }
+
+       public XMLObject getMetadata() throws MetadataProviderException {
+
+               return provider.getMetadata();
+       }
+
+       public EntitiesDescriptor getEntitiesDescriptor(String name) throws MetadataProviderException {
+
+               return provider.getEntitiesDescriptor(name);
+       }
+
+}
diff --git a/src/edu/internet2/middleware/shibboleth/metadata/URLMetadataProvider.java b/src/edu/internet2/middleware/shibboleth/metadata/URLMetadataProvider.java
new file mode 100644 (file)
index 0000000..70b2bc7
--- /dev/null
@@ -0,0 +1,65 @@
+
+package edu.internet2.middleware.shibboleth.metadata;
+
+import org.apache.log4j.Logger;
+import org.opensaml.saml2.metadata.provider.MetadataProvider;
+import org.opensaml.saml2.metadata.provider.MetadataProviderException;
+import org.w3c.dom.Element;
+
+/**
+ * Loads metadata from a URL specified in the IdP configuration.
+ * 
+ * @author Walter Hoehn
+ */
+public class URLMetadataProvider extends ShibbolethConfigurableMetadataProvider implements MetadataProvider {
+
+       private static Logger log = Logger.getLogger(URLMetadataProvider.class.getName());
+
+       public URLMetadataProvider(Element configuration) throws MetadataProviderException {
+
+               super(configuration);
+
+               // Grab the URL from the config
+               String url = ((Element) configuration).getAttribute("url");
+               if (url == null || url.equals("")) {
+                       log.error("Unable to load URL Metadata Provider.  A (url) attribute is required.  "
+                                       + "Add a (url) attribute to <MetadataProvider/>.");
+                       throw new MetadataProviderException("Required configuration not specified.");
+               }
+
+               // Grab the request timeout, if there is one. If not, use a reasonable default
+               int requestTimeout = 1000 * 1 * 60; // 1 minute
+               String rawRequestTimeout = ((Element) configuration).getAttribute("requestTimeout");
+               if (rawRequestTimeout != null && !rawRequestTimeout.equals("")) {
+                       try {
+                               requestTimeout = Integer.valueOf(rawRequestTimeout);
+                       } catch (NumberFormatException nfe) {
+                               log.error("Unable to load URL Metadata Provider.  The (requestTimeout) attribute must be an integer.  "
+                                               + "Modify the (requestTimeout) attribute on <MetadataProvider/>.");
+                               throw new MetadataProviderException("Configuration is invalid.");
+                       }
+               }
+
+               // Construct provider from config
+               try {
+                       provider = new org.opensaml.saml2.metadata.provider.URLMetadataProvider(url, requestTimeout);
+
+                       // If there is a cache duration, set it
+                       String rawMaxCacheDuration = ((Element) configuration).getAttribute("maxCacheDuration");
+                       if (rawMaxCacheDuration != null && !rawMaxCacheDuration.equals("")) {
+                               try {
+                                       ((org.opensaml.saml2.metadata.provider.URLMetadataProvider) provider).setMaxDuration(Integer
+                                                       .valueOf(rawMaxCacheDuration));
+                               } catch (NumberFormatException nfe) {
+                                       log.error("Unable to load URL Metadata Provider.  The (maxCacheDuration) attribute must be "
+                                                       + "an integer.  Modify the (maxCacheDuration) attribute on <MetadataProvider/>.");
+                                       throw new MetadataProviderException("Configuration is invalid.");
+                               }
+                       }
+
+               } catch (MetadataProviderException e) {
+                       log.error("Unable to load URL Metadata Provider: " + e);
+                       throw e;
+               }
+       }
+}