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 org.opensaml.common.IdentifierGenerator;
21  import org.opensaml.common.impl.RandomIdentifierGenerator;
22  import org.opensaml.xml.util.DatatypeHelper;
23  import org.springframework.beans.factory.support.AbstractBeanDefinition;
24  import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
25  import org.springframework.beans.factory.xml.ParserContext;
26  import org.w3c.dom.Element;
27  
28  /**
29   * Base class for Spring bean definition parsers within the filter engine configuration. This base class is responsible
30   * for generating an ID for the Spring bean that is unique within all the policy components loaded.
31   */
32  public abstract class BaseFilterBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
33  
34      /** Generator of unique IDs. */
35      private static IdentifierGenerator idGen = new RandomIdentifierGenerator();
36  
37      /** {@inheritDoc} */
38      protected String resolveId(Element configElement, AbstractBeanDefinition beanDefinition, ParserContext parserContext) {
39          return getQualifiedId(configElement, configElement.getLocalName(), configElement.getAttributeNS(null, "id"));
40      }
41  
42      /**
43       * Generates an ID for a filter engine component. If the given localId is null a random one will be generated.
44       * 
45       * @param configElement component configuration element
46       * @param componentNamespace namespace for the component
47       * @param localId local id or null
48       * 
49       * @return unique ID for the componenent
50       */
51      protected String getQualifiedId(Element configElement, String componentNamespace, String localId) {
52          Element afpgElement = configElement.getOwnerDocument().getDocumentElement();
53          String policyGroupId = DatatypeHelper.safeTrimOrNullString(afpgElement.getAttributeNS(null, "id"));
54  
55          StringBuilder qualifiedId = new StringBuilder();
56          qualifiedId.append("/");
57          qualifiedId.append(AttributeFilterPolicyGroupBeanDefinitionParser.ELEMENT_NAME.getLocalPart());
58          qualifiedId.append(":");
59          qualifiedId.append(policyGroupId);
60          if (!DatatypeHelper.isEmpty(componentNamespace)) {
61              qualifiedId.append("/");
62              qualifiedId.append(componentNamespace);
63              qualifiedId.append(":");
64  
65              if (DatatypeHelper.isEmpty(localId)) {
66                  qualifiedId.append(idGen.generateIdentifier());
67              } else {
68                  qualifiedId.append(localId);
69              }
70          }
71  
72          return qualifiedId.toString();
73      }
74  
75      /**
76       * Gets the absolute refrence given a possibly relative reference.
77       * 
78       * @param configElement component configuration element
79       * @param componentNamespace namespace for the component
80       * @param reference reference to convert into absolute form
81       * 
82       * @return absolute form of the reference
83       */
84      protected String getAbsoluteReference(Element configElement, String componentNamespace, String reference) {
85          if (reference.startsWith("/")) {
86              return reference;
87          } else {
88              return getQualifiedId(configElement, componentNamespace, reference);
89          }
90      }
91  }