1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.attributeDefinition;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Set;
23
24 import org.opensaml.xml.util.DatatypeHelper;
25 import org.opensaml.xml.util.LazySet;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
30 import edu.internet2.middleware.shibboleth.common.attribute.provider.BasicAttribute;
31 import edu.internet2.middleware.shibboleth.common.attribute.resolver.AttributeResolutionException;
32 import edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.ShibbolethResolutionContext;
33
34
35
36
37
38 public class MappedAttributeDefinition extends BaseAttributeDefinition {
39
40
41 private static Logger log = LoggerFactory.getLogger(MappedAttributeDefinition.class);
42
43
44 private String defaultValue;
45
46
47 private boolean passThru;
48
49
50 private Collection<ValueMap> valueMaps;
51
52
53 public MappedAttributeDefinition() {
54 valueMaps = new ArrayList<ValueMap>(5);
55 }
56
57
58 protected BaseAttribute doResolve(ShibbolethResolutionContext resolutionContext)
59 throws AttributeResolutionException {
60 BasicAttribute<String> attribute = new BasicAttribute<String>();
61 attribute.setId(getId());
62
63 Collection<?> values = getValuesFromAllDependencies(resolutionContext);
64 if (values == null || values.isEmpty()) {
65 log.debug("Attribute Definition {}: No values from dependency attribute attribute {}", getId(),
66 getDependencyIds());
67 if (!DatatypeHelper.isEmpty(getDefaultValue())) {
68 log.debug(
69 "Attribute Definition {}: Default value is not empty, adding it as the value for this attribute",
70 getId());
71 attribute.getValues().add(getDefaultValue());
72 }
73 return attribute;
74 }
75
76 Set<String> mappedValues;
77 for (Object o : values) {
78 if (o == null) {
79 log.debug("Attribute Definition {}: null attribute value, skipping it", getId());
80 continue;
81 }
82 mappedValues = mapValue(o.toString());
83 attribute.getValues().addAll(mappedValues);
84 }
85
86 return attribute;
87 }
88
89
90
91
92
93
94
95
96 protected Set<String> mapValue(String value) {
97 log.debug("Attribute Definition {}: mapping depdenency attribute value {}", getId(), value);
98
99 LazySet<String> mappedValues = new LazySet<String>();
100
101 boolean valueMapMatch = false;
102 if (!DatatypeHelper.isEmpty(value)) {
103 for (ValueMap valueMap : valueMaps) {
104 mappedValues.addAll(valueMap.evaluate(value));
105 if (!mappedValues.isEmpty()) {
106 valueMapMatch = true;
107 }
108 }
109
110 if (!valueMapMatch) {
111 if (passThru) {
112 mappedValues.add(value);
113 } else if (getDefaultValue() != null) {
114 mappedValues.add(getDefaultValue());
115 }
116 }
117 }
118
119 log.debug("Attribute Definition {}: mapped depdenency attribute value {} to the values {}", new Object[] {
120 getId(), value, mappedValues, });
121
122 return mappedValues;
123 }
124
125
126 public void validate() throws AttributeResolutionException {
127 if (passThru && !DatatypeHelper.isEmpty(defaultValue)) {
128 log.error("MappedAttributeDefinition (" + getId()
129 + ") may not have a DefaultValue string with passThru enabled.");
130 throw new AttributeResolutionException("MappedAttributeDefinition (" + getId()
131 + ") may not have a DefaultValue string with passThru enabled.");
132 }
133 }
134
135
136
137
138
139
140 public String getDefaultValue() {
141 return defaultValue;
142 }
143
144
145
146
147
148
149 public void setDefaultValue(String newDefaultValue) {
150 defaultValue = newDefaultValue;
151 }
152
153
154
155
156
157
158 public boolean isPassThru() {
159 return passThru;
160 }
161
162
163
164
165
166
167 public void setPassThru(boolean newPassThru) {
168 passThru = newPassThru;
169 }
170
171
172
173
174
175
176 public Collection<ValueMap> getValueMaps() {
177 return valueMaps;
178 }
179
180 }