1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
38
39 public class MappedAttributeDefinitionBeanDefinitionParser extends BaseAttributeDefinitionBeanDefinitionParser {
40
41
42 public static final QName TYPE_NAME = new QName(AttributeDefinitionNamespaceHandler.NAMESPACE, "Mapped");
43
44
45 public static final QName VALUEMAP_ELEMENT_NAME = new QName(AttributeDefinitionNamespaceHandler.NAMESPACE,
46 "ValueMap");
47
48
49 public static final QName SOURCE_VALUE_ELEMENT_NAME = new QName(AttributeDefinitionNamespaceHandler.NAMESPACE,
50 "SourceValue");
51
52
53 public static final QName RETURN_VALUE_ELEMENT_NAME = new QName(AttributeDefinitionNamespaceHandler.NAMESPACE,
54 "ReturnValue");
55
56
57 public static final QName DEFAULT_VALUE_ELEMENT_NAME = new QName(AttributeDefinitionNamespaceHandler.NAMESPACE,
58 "DefaultValue");
59
60
61 private final Logger log = LoggerFactory.getLogger(MappedAttributeDefinitionBeanDefinitionParser.class);
62
63
64 protected Class getBeanClass(Element element) {
65 return MappedAttributeDefinitionFactoryBean.class;
66 }
67
68
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
98
99
100
101
102
103
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 }