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