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