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.resolver.attributeDefinition;
18  
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Locale;
22  import java.util.Map;
23  
24  import javax.xml.namespace.QName;
25  
26  import org.opensaml.xml.util.XMLHelper;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
30  import org.springframework.beans.factory.xml.ParserContext;
31  import org.w3c.dom.Element;
32  
33  import edu.internet2.middleware.shibboleth.common.config.SpringConfigurationUtils;
34  import edu.internet2.middleware.shibboleth.common.config.attribute.resolver.AbstractResolutionPlugInBeanDefinitionParser;
35  import edu.internet2.middleware.shibboleth.common.config.attribute.resolver.AttributeResolverNamespaceHandler;
36  
37  /**
38   * Base spring bean definition parser for attribute definitions. AttributeDefinition implementations should provide a
39   * custom BeanDefinitionParser by extending this class and overriding the doParse() method to parse any additional
40   * attributes or elements it requires. Standard attributes and elements defined by the ResolutionPlugIn and
41   * AttributeDefinition schemas will automatically attempt to be parsed.
42   */
43  public abstract class BaseAttributeDefinitionBeanDefinitionParser extends AbstractResolutionPlugInBeanDefinitionParser {
44  
45      /** Local name of attribute encoder. */
46      public static final QName ATTRIBUTE_ENCODER_ELEMENT_NAME = new QName(AttributeResolverNamespaceHandler.NAMESPACE,
47              "AttributeEncoder");
48  
49      /** Class logger. */
50      private Logger log = LoggerFactory.getLogger(BaseAttributeDefinitionBeanDefinitionParser.class);
51  
52      /** {@inheritDoc} */
53      protected void doParse(String pluginId, Element pluginConfig, Map<QName, List<Element>> pluginConfigChildren,
54              BeanDefinitionBuilder pluginBuilder, ParserContext parserContext) {
55  
56          String sourceAttributeId = pluginConfig.getAttributeNS(null, "sourceAttributeID");
57          log.debug("Setting source attribute ID for attribute definition {} to: {}", pluginId, sourceAttributeId);
58          pluginBuilder.addPropertyValue("sourceAttributeId", sourceAttributeId);
59  
60          List<Element> displayNames = pluginConfigChildren.get(new QName(AttributeResolverNamespaceHandler.NAMESPACE,
61                  "DisplayName"));
62          if (displayNames != null) {
63              log.debug("Setting {} display names for attribute definition {}", displayNames.size(), pluginId);
64              pluginBuilder.addPropertyValue("displayNames", processLocalizedElement(displayNames));
65          }
66  
67          List<Element> displayDescriptions = pluginConfigChildren.get(new QName(
68                  AttributeResolverNamespaceHandler.NAMESPACE, "DisplayDescription"));
69          if (displayDescriptions != null) {
70              log.debug("Setting {} display descriptions for attribute definition {}", displayDescriptions.size(),
71                      pluginId);
72              pluginBuilder.addPropertyValue("displayDescriptions", processLocalizedElement(displayDescriptions));
73          }
74  
75          boolean dependencyOnly = false;
76          if (pluginConfig.hasAttributeNS(null, "dependencyOnly")) {
77              dependencyOnly = XMLHelper.getAttributeValueAsBoolean(pluginConfig.getAttributeNodeNS(null,
78                      "dependencyOnly"));
79          }
80          if (log.isDebugEnabled()) {
81              log.debug("Attribute definition {} produces attributes that are only dependencies: {}", pluginId,
82                      dependencyOnly);
83          }
84          pluginBuilder.addPropertyValue("dependencyOnly", dependencyOnly);
85  
86          pluginBuilder.addPropertyValue("attributeEncoders", SpringConfigurationUtils.parseInnerCustomElements(
87                  pluginConfigChildren.get(ATTRIBUTE_ENCODER_ELEMENT_NAME), parserContext));
88      }
89  
90      /**
91       * Used to process string elements that contain an xml:lang attribute expressing localization.
92       * 
93       * @param elements list of elements, must not be null, may be empty
94       * 
95       * @return the localized string indexed by locale
96       */
97      protected Map<Locale, String> processLocalizedElement(List<Element> elements) {
98          HashMap<Locale, String> localizedString = new HashMap<Locale, String>();
99          for (Element element : elements) {
100             localizedString.put(XMLHelper.getLanguage(element), element.getTextContent());
101         }
102 
103         return localizedString;
104     }
105 }