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.ArrayList;
21  import java.util.List;
22  import java.util.Map;
23  
24  import javax.xml.namespace.QName;
25  
26  import org.opensaml.xml.util.DatatypeHelper;
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.attribute.resolver.provider.attributeDefinition.ValueMap;
35  
36  /**
37   * Spring bean definition parser for mapped attribute definition.
38   */
39  public class MappedAttributeDefinitionBeanDefinitionParser extends BaseAttributeDefinitionBeanDefinitionParser {
40  
41      /** Schema type name. */
42      public static final QName TYPE_NAME = new QName(AttributeDefinitionNamespaceHandler.NAMESPACE, "Mapped");
43  
44      /** ValueMap element name. */
45      public static final QName VALUEMAP_ELEMENT_NAME = new QName(AttributeDefinitionNamespaceHandler.NAMESPACE,
46              "ValueMap");
47  
48      /** SourceValue element name. */
49      public static final QName SOURCE_VALUE_ELEMENT_NAME = new QName(AttributeDefinitionNamespaceHandler.NAMESPACE,
50              "SourceValue");
51  
52      /** ReturnValue element name. */
53      public static final QName RETURN_VALUE_ELEMENT_NAME = new QName(AttributeDefinitionNamespaceHandler.NAMESPACE,
54              "ReturnValue");
55  
56      /** DefaultValue element name. */
57      public static final QName DEFAULT_VALUE_ELEMENT_NAME = new QName(AttributeDefinitionNamespaceHandler.NAMESPACE,
58              "DefaultValue");
59  
60      /** Class logger. */
61      private final Logger log = LoggerFactory.getLogger(MappedAttributeDefinitionBeanDefinitionParser.class);
62  
63      /** {@inheritDoc} */
64      protected Class getBeanClass(Element element) {
65          return MappedAttributeDefinitionFactoryBean.class;
66      }
67  
68      /** {@inheritDoc} */
69      protected void doParse(String pluginId, Element pluginConfig, Map<QName, List<Element>> pluginConfigChildren,
70              BeanDefinitionBuilder pluginBuilder, ParserContext parserContext) {
71          super.doParse(pluginId, pluginConfig, pluginConfigChildren, pluginBuilder, parserContext);
72  
73          List<ValueMap> valueMaps = processValueMaps(pluginId, pluginConfigChildren, pluginBuilder);
74          pluginBuilder.addPropertyValue("valueMaps", valueMaps);
75  
76          if (pluginConfigChildren.containsKey(DEFAULT_VALUE_ELEMENT_NAME)) {
77              Element defaultValueElem = pluginConfigChildren.get(DEFAULT_VALUE_ELEMENT_NAME).get(0);
78              String defaultValue = DatatypeHelper.safeTrimOrNullString(defaultValueElem.getTextContent());
79              pluginBuilder.addPropertyValue("defaultValue", defaultValue);
80              if (log.isDebugEnabled()) {
81                  log.debug("Attribute definition {} default value: {}", pluginId, defaultValue);
82              }
83  
84              boolean passThru = false;
85              if (defaultValueElem.hasAttributeNS(null, "passThru")) {
86                  passThru = XMLHelper.getAttributeValueAsBoolean(defaultValueElem.getAttributeNodeNS(null, "passThru"));
87              }
88              pluginBuilder.addPropertyValue("passThru", passThru);
89              if (log.isDebugEnabled()) {
90                  log.debug("Attribute definition {} uses default value pass thru: {}", pluginId, passThru);
91              }
92          }
93  
94      }
95  
96      /**
97       * Process the value map elements.
98       * 
99       * @param pluginId ID of this data connector
100      * @param pluginConfigChildren configuration elements
101      * @param pluginBuilder the bean definition parser
102      * 
103      * @return the list of value maps
104      */
105     protected List<ValueMap> processValueMaps(String pluginId, Map<QName, List<Element>> pluginConfigChildren,
106             BeanDefinitionBuilder pluginBuilder) {
107         List<ValueMap> maps = new ArrayList<ValueMap>(5);
108 
109         ValueMap valueMap;
110         String returnValue;
111         String sourceValue;
112         boolean ignoreCase;
113         boolean partialMatch;
114         if (pluginConfigChildren.containsKey(VALUEMAP_ELEMENT_NAME)) {
115             for (Element valueMapElem : pluginConfigChildren.get(VALUEMAP_ELEMENT_NAME)) {
116                 valueMap = new ValueMap();
117 
118                 Map<QName, List<Element>> children = XMLHelper.getChildElements(valueMapElem);
119 
120                 if (children.containsKey(RETURN_VALUE_ELEMENT_NAME)) {
121                     List<Element> returnValueElems = children.get(RETURN_VALUE_ELEMENT_NAME);
122                     returnValue = DatatypeHelper.safeTrimOrNullString(returnValueElems.get(0).getTextContent());
123                     valueMap.setReturnValue(returnValue);
124                 }
125 
126                 if (children.containsKey(SOURCE_VALUE_ELEMENT_NAME)) {
127                     for (Element sourceValueElem : children.get(SOURCE_VALUE_ELEMENT_NAME)) {
128                         sourceValue = DatatypeHelper.safeTrim(sourceValueElem.getTextContent());
129 
130                         if (sourceValueElem.hasAttributeNS(null, "ignoreCase")) {
131                             ignoreCase = XMLHelper.getAttributeValueAsBoolean(sourceValueElem.getAttributeNodeNS(null,
132                                     "ignoreCase"));
133                         } else {
134                             ignoreCase = false;
135                         }
136 
137                         if (sourceValueElem.hasAttributeNS(null, "partialMatch")) {
138                             partialMatch = XMLHelper.getAttributeValueAsBoolean(sourceValueElem.getAttributeNodeNS(
139                                     null, "partialMatch"));
140                         } else {
141                             partialMatch = false;
142                         }
143 
144                         valueMap.getSourceValues().add(valueMap.new SourceValue(sourceValue, ignoreCase, partialMatch));
145                     }
146                 }
147 
148                 maps.add(valueMap);
149             }
150         }
151 
152         return maps;
153     }
154 
155 }