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.Collection;
21 import java.util.HashSet;
22 import java.util.Set;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25 import java.util.regex.PatternSyntaxException;
26
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30
31
32
33
34 public class ValueMap {
35
36
37 private final Logger log = LoggerFactory.getLogger(ValueMap.class);
38
39
40 private String returnValue;
41
42
43 private Collection<SourceValue> sourceValues;
44
45
46 public ValueMap() {
47 sourceValues = new HashSet<SourceValue>();
48 }
49
50
51
52
53
54
55 public String getReturnValue() {
56 return returnValue;
57 }
58
59
60
61
62
63
64 public void setReturnValue(String newReturnValue) {
65 returnValue = newReturnValue;
66 }
67
68
69
70
71
72
73 public Collection<SourceValue> getSourceValues() {
74 return sourceValues;
75 }
76
77
78
79
80
81
82
83 public Set<String> evaluate(String attributeValue) {
84 log.debug("Attempting to map attribute value '{}'", attributeValue);
85 Set<String> mappedValues = new HashSet<String>();
86 Matcher m;
87
88 String newValue;
89 for (SourceValue sourceValue : sourceValues) {
90 newValue = null;
91 if (sourceValue.isPartialMatch()) {
92 log.debug("Performing partial match comparison.");
93 if (attributeValue.contains(sourceValue.getValue())) {
94 log.debug("Attribute value '{}' matches source value '{}' it will be mapped to '{}'", new Object[] {
95 attributeValue, sourceValue.getValue(), newValue });
96 newValue = returnValue;
97 }
98 } else {
99 log.debug("Performing regular expression based comparison");
100 try {
101 int flags = sourceValue.isIgnoreCase() ? Pattern.CASE_INSENSITIVE : 0;
102 m = Pattern.compile(sourceValue.getValue(), flags).matcher(attributeValue);
103 if (m.matches()) {
104 newValue = m.replaceAll(returnValue);
105 log.debug("Attribute value '{}' matches regular expression it will be mapped to '{}'",
106 attributeValue, newValue);
107 }
108 } catch (PatternSyntaxException e) {
109 log.debug("Error matching value {}. Skipping this value.", attributeValue);
110 }
111 }
112
113 if (newValue != null) {
114 mappedValues.add(newValue);
115 }
116 }
117
118 return mappedValues;
119 }
120
121
122
123
124 public class SourceValue {
125
126
127
128
129 private String value;
130
131
132
133
134 private boolean ignoreCase;
135
136
137
138
139 private boolean partialMatch;
140
141
142
143
144
145
146
147
148 public SourceValue(String newValue, boolean newIgnoreCase, boolean newPartialMatch) {
149 value = newValue;
150 ignoreCase = newIgnoreCase;
151 partialMatch = newPartialMatch;
152 }
153
154
155
156
157
158
159 public boolean isIgnoreCase() {
160 return ignoreCase;
161 }
162
163
164
165
166
167
168 public boolean isPartialMatch() {
169 return partialMatch;
170 }
171
172
173
174
175
176
177 public String getValue() {
178 return value;
179 }
180
181
182 public String toString() {
183 return getValue();
184 }
185
186 }
187 }