View Javadoc

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