View Javadoc

1   /*
2    * Copyright 2007 University Corporation for Advanced Internet Development, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package edu.internet2.middleware.shibboleth.common.config.metadata;
18  
19  import java.util.List;
20  
21  import org.opensaml.xml.util.DatatypeHelper;
22  import org.opensaml.xml.util.XMLHelper;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
26  import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
27  import org.springframework.beans.factory.xml.ParserContext;
28  import org.w3c.dom.Element;
29  
30  import edu.internet2.middleware.shibboleth.common.config.SpringConfigurationUtils;
31  
32  /** Base class for metadata provider configuration parser. */
33  public abstract class BaseMetadataProviderBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
34  
35      /** Class logger. */
36      private final Logger log = LoggerFactory.getLogger(BaseMetadataProviderBeanDefinitionParser.class);
37  
38      /** {@inheritDoc} */
39      protected void doParse(Element config, ParserContext parserContext, BeanDefinitionBuilder builder) {
40          String id = getProviderId(config);
41          log.debug("Parsing configuration for '{}' metadata provider with ID: {}", XMLHelper.getXSIType(config)
42                  .getLocalPart(), id);
43  
44          boolean requireValidMetadata = getRequireValidMetadata(config);
45          log.debug("Metadata provider requires valid metadata: {}", requireValidMetadata);
46          builder.addPropertyValue("requireValidMetadata", requireValidMetadata);
47  
48          List<Element> childElems = XMLHelper.getChildElementsByTagNameNS(config, MetadataNamespaceHandler.NAMESPACE,
49                  "MetadataFilter");
50          if (childElems.size() > 0) {
51              builder.addPropertyValue("metadataFilter", SpringConfigurationUtils.parseInnerCustomElement(
52                      (Element) childElems.get(0), parserContext));
53          }
54      }
55  
56      /**
57       * Gets the valid metadata requirement for the metadata provider.
58       * 
59       * @param config metadata provider configuration
60       * 
61       * @return valid metadata requirement for the metadata provider
62       */
63      protected boolean getRequireValidMetadata(Element config) {
64          boolean requireValidMetadata = true;
65  
66          if (config.hasAttributeNS(null, "maintainExpiredMetadata")) {
67              boolean maintainedExpiredMetadata = XMLHelper.getAttributeValueAsBoolean(config.getAttributeNodeNS(null,
68                      "maintainExpiredMetadata"));
69              requireValidMetadata = !maintainedExpiredMetadata;
70              log.warn("Use of metadata provider configuration attribute 'maintainExpiredMetadata' is deprecated.  Use requireValidMetadata=\"{}\" instead.",
71                              requireValidMetadata);
72          }
73  
74          if (config.hasAttributeNS(null, "requireValidMetadata")) {
75              requireValidMetadata = XMLHelper.getAttributeValueAsBoolean(config.getAttributeNodeNS(null,
76                      "requireValidMetadata"));
77          }
78  
79          return requireValidMetadata;
80      }
81  
82      /**
83       * Gets the ID of the metadata provider.
84       * 
85       * @param config metadata provider configuration element
86       * 
87       * @return ID of the metadata provider
88       */
89      protected String getProviderId(Element config) {
90          return DatatypeHelper.safeTrimOrNullString(config.getAttributeNS(null, "id"));
91      }
92  }