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