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