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.regex.Matcher;
22 import java.util.regex.Pattern;
23
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
28 import edu.internet2.middleware.shibboleth.common.attribute.provider.BasicAttribute;
29 import edu.internet2.middleware.shibboleth.common.attribute.resolver.AttributeResolutionException;
30 import edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.ShibbolethResolutionContext;
31
32
33 public class RegexSplitAttributeDefinition extends BaseAttributeDefinition {
34
35
36 private final Logger log = LoggerFactory.getLogger(RegexSplitAttributeDefinition.class);
37
38
39 private Pattern regex;
40
41
42
43
44
45
46
47 public RegexSplitAttributeDefinition(String regularExpression, boolean caseSensitive) {
48 if(!caseSensitive){
49 regex = Pattern.compile(regularExpression, Pattern.CASE_INSENSITIVE);
50 }else{
51 regex = Pattern.compile(regularExpression);
52 }
53 }
54
55
56 protected BaseAttribute<?> doResolve(ShibbolethResolutionContext resolutionContext)
57 throws AttributeResolutionException {
58 BasicAttribute<Object> attribute = new BasicAttribute<Object>();
59 attribute.setId(getId());
60
61 Collection<?> values = getValuesFromAllDependencies(resolutionContext);
62 if (values == null || values.isEmpty()) {
63 return attribute;
64 }
65
66 Matcher matcher;
67 for (Object value : values) {
68 if (value instanceof String) {
69 matcher = regex.matcher((String) value);
70 if(matcher.matches()){
71 attribute.getValues().add(matcher.group(1));
72 } else {
73 log.debug("Value {} did not result in any values when split by regular expression {}", value, regex
74 .toString());
75 }
76 } else {
77 log.debug("Ignoring non-string attribute value");
78 }
79 }
80
81 return attribute;
82 }
83
84
85 public void validate() throws AttributeResolutionException {
86 if(getSourceAttributeID() == null){
87 throw new AttributeResolutionException("Source attribute ID is required but none was given.");
88 }
89 }
90 }