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.encoding.provider;
19
20 import java.util.List;
21
22 import org.opensaml.Configuration;
23 import org.opensaml.common.SAMLObjectBuilder;
24 import org.opensaml.saml2.core.Attribute;
25 import org.opensaml.saml2.core.AttributeValue;
26 import org.opensaml.saml2.core.impl.AttributeBuilder;
27 import org.opensaml.xml.XMLObject;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
32 import edu.internet2.middleware.shibboleth.common.attribute.encoding.AttributeEncodingException;
33 import edu.internet2.middleware.shibboleth.common.attribute.encoding.SAML2AttributeEncoder;
34
35
36
37
38 public class SAML2ScopedStringAttributeEncoder extends
39 AbstractScopedAttributeEncoder<org.opensaml.saml2.core.Attribute> implements SAML2AttributeEncoder {
40
41
42 private final Logger log = LoggerFactory.getLogger(SAML2ScopedStringAttributeEncoder.class);
43
44
45 private final SAMLObjectBuilder<Attribute> attributeBuilder;
46
47
48 private String format;
49
50
51 private String friendlyName;
52
53
54 public SAML2ScopedStringAttributeEncoder() {
55 super();
56
57 attributeBuilder = (AttributeBuilder) Configuration.getBuilderFactory().getBuilder(
58 Attribute.DEFAULT_ELEMENT_NAME);
59 }
60
61
62 public String getNameFormat() {
63 return format;
64 }
65
66
67 public String getFriendlyName() {
68 return friendlyName;
69 }
70
71
72 public void setNameFormat(String newFormat) {
73 format = newFormat;
74 }
75
76
77 public void setFriendlyName(String name) {
78 friendlyName = name;
79 }
80
81
82 public Attribute encode(BaseAttribute attribute) throws AttributeEncodingException {
83 Attribute samlAttribute = attributeBuilder.buildObject();
84 samlAttribute.setName(getAttributeName());
85 samlAttribute.setNameFormat(getNameFormat());
86 samlAttribute.setFriendlyName(getFriendlyName());
87 samlAttribute.getAttributeValues()
88 .addAll(encodeAttributeValues(AttributeValue.DEFAULT_ELEMENT_NAME, attribute));
89
90 List<XMLObject> attributeValues = samlAttribute.getAttributeValues();
91 if (attributeValues == null || attributeValues.isEmpty()) {
92 log.debug("Unable to encode {} attribute. It does not contain any values", attribute.getId());
93 return null;
94 }
95
96 return samlAttribute;
97 }
98 }