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.attribute.filtering;
19  
20  import java.util.List;
21  import java.util.Map;
22  
23  import javax.xml.namespace.QName;
24  
25  import org.opensaml.xml.util.DatatypeHelper;
26  import org.opensaml.xml.util.XMLHelper;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  import org.springframework.beans.factory.config.RuntimeBeanReference;
30  import org.springframework.beans.factory.support.AbstractBeanDefinition;
31  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
32  import org.springframework.beans.factory.support.ManagedList;
33  import org.springframework.beans.factory.xml.ParserContext;
34  import org.w3c.dom.Element;
35  
36  import edu.internet2.middleware.shibboleth.common.config.SpringConfigurationUtils;
37  
38  /**
39   * Spring bean definition parser to configure an {@link AttributeFilterPolicyFactoryBean}.
40   */
41  public class AttributeFilterPolicyBeanDefinitionParser extends BaseFilterBeanDefinitionParser {
42  
43      /** Element name. */
44      public static final QName ELEMENT_NAME = new QName(AttributeFilterNamespaceHandler.NAMESPACE,
45              "AttributeFilterPolicy");
46  
47      /** Schema type name. */
48      public static final QName TYPE_NAME = new QName(AttributeFilterNamespaceHandler.NAMESPACE,
49              "AttributeFilterPolicyType");
50  
51      /** Class logger. */
52      private static Logger log = LoggerFactory.getLogger(AttributeFilterPolicyBeanDefinitionParser.class);
53  
54      /** {@inheritDoc} */
55      protected Class getBeanClass(Element arg0) {
56          return AttributeFilterPolicyFactoryBean.class;
57      }
58      
59      /** {@inheritDoc} */
60      protected String resolveId(Element configElement, AbstractBeanDefinition beanDefinition, ParserContext parserContext) {
61          if(!configElement.hasAttributeNS(null, "id")){
62              log.warn("AttributeFilterPolicy elements should include an 'id' attribute.  This is not currently required but will be in future versions.");
63          }
64          return getQualifiedId(configElement, configElement.getLocalName(), configElement.getAttributeNS(null, "id"));
65      }
66  
67      /** {@inheritDoc} */
68      protected void doParse(Element config, ParserContext parserContext, BeanDefinitionBuilder builder) {
69          super.doParse(config, parserContext, builder);
70  
71          String policyId = DatatypeHelper.safeTrimOrNullString(config.getAttributeNS(null, "id"));
72          log.info("Parsing configuration for attribute filter policy {}", policyId);
73          builder.addPropertyValue("policyId", policyId);
74          List<Element> children;
75          Map<QName, List<Element>> childrenMap = XMLHelper.getChildElements(config);
76  
77          children = childrenMap.get(new QName(AttributeFilterNamespaceHandler.NAMESPACE, "PolicyRequirementRule"));
78          if (children != null && children.size() > 0) {
79              builder.addPropertyValue("policyRequirement", SpringConfigurationUtils.parseInnerCustomElement(children
80                      .get(0), parserContext));
81          } else {
82              children = childrenMap.get(new QName(AttributeFilterNamespaceHandler.NAMESPACE,
83                      "PolicyRequirementRuleReference"));
84              String reference = getAbsoluteReference(config, "PolicyRequirementRule", children.get(0).getTextContent());
85              builder.addPropertyReference("policyRequirement", reference);
86          }
87  
88          ManagedList attributeRules = new ManagedList();
89          children = childrenMap.get(new QName(AttributeFilterNamespaceHandler.NAMESPACE, "AttributeRule"));
90          if (children != null && children.size() > 0) {
91              attributeRules.addAll(SpringConfigurationUtils.parseInnerCustomElements(children, parserContext));
92          }
93  
94          children = childrenMap.get(new QName(AttributeFilterNamespaceHandler.NAMESPACE, "AttributeRuleReference"));
95          if (children != null && children.size() > 0) {
96              String reference;
97              for (Element child : children) {
98                  reference = getAbsoluteReference(config, "AttributeRule", child.getTextContent());
99                  attributeRules.add(new RuntimeBeanReference(reference));
100             }
101         }
102 
103         builder.addPropertyValue("attributeRules", attributeRules);
104     }
105 }