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