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