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