Add bin/build/dist to top-level svn:ignore.
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / metadata / URLMetadataProvider.java
1
2 package edu.internet2.middleware.shibboleth.metadata;
3
4 import org.apache.log4j.Logger;
5 import org.opensaml.saml2.metadata.provider.MetadataProvider;
6 import org.opensaml.saml2.metadata.provider.MetadataProviderException;
7 import org.w3c.dom.Element;
8
9 /**
10  * Loads metadata from a URL specified in the IdP configuration.
11  * 
12  * @author Walter Hoehn
13  */
14 public class URLMetadataProvider extends ShibbolethConfigurableMetadataProvider implements MetadataProvider {
15
16         private static Logger log = Logger.getLogger(URLMetadataProvider.class.getName());
17
18         public URLMetadataProvider(Element configuration) throws MetadataProviderException {
19
20                 super(configuration);
21
22                 // Grab the URL from the config
23                 String url = ((Element) configuration).getAttribute("url");
24                 if (url == null || url.equals("")) {
25                         log.error("Unable to load URL Metadata Provider.  A (url) attribute is required.  "
26                                         + "Add a (url) attribute to <MetadataProvider/>.");
27                         throw new MetadataProviderException("Required configuration not specified.");
28                 }
29
30                 // Grab the request timeout, if there is one. If not, use a reasonable default
31                 int requestTimeout = 1000 * 1 * 60; // 1 minute
32                 String rawRequestTimeout = ((Element) configuration).getAttribute("requestTimeout");
33                 if (rawRequestTimeout != null && !rawRequestTimeout.equals("")) {
34                         try {
35                                 requestTimeout = Integer.valueOf(rawRequestTimeout);
36                         } catch (NumberFormatException nfe) {
37                                 log.error("Unable to load URL Metadata Provider.  The (requestTimeout) attribute must be an integer.  "
38                                                 + "Modify the (requestTimeout) attribute on <MetadataProvider/>.");
39                                 throw new MetadataProviderException("Configuration is invalid.");
40                         }
41                 }
42
43                 // Construct provider from config
44                 try {
45                         provider = new org.opensaml.saml2.metadata.provider.URLMetadataProvider(url, requestTimeout);
46
47                         // If there is a cache duration, set it
48                         String rawMaxCacheDuration = ((Element) configuration).getAttribute("maxCacheDuration");
49                         if (rawMaxCacheDuration != null && !rawMaxCacheDuration.equals("")) {
50                                 try {
51                                         ((org.opensaml.saml2.metadata.provider.URLMetadataProvider) provider).setMaxDuration(Integer
52                                                         .valueOf(rawMaxCacheDuration));
53                                 } catch (NumberFormatException nfe) {
54                                         log.error("Unable to load URL Metadata Provider.  The (maxCacheDuration) attribute must be "
55                                                         + "an integer.  Modify the (maxCacheDuration) attribute on <MetadataProvider/>.");
56                                         throw new MetadataProviderException("Configuration is invalid.");
57                                 }
58                         }
59
60                 } catch (MetadataProviderException e) {
61                         log.error("Unable to load URL Metadata Provider: " + e);
62                         throw e;
63                 }
64         }
65 }