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