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