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.saml2.core.Attribute;
24 import org.opensaml.saml2.core.AttributeValue;
25 import org.opensaml.xml.XMLObject;
26 import org.opensaml.xml.XMLObjectBuilder;
27 import org.opensaml.xml.schema.XSAny;
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
34
35 public class SAML2XMLObjectAttributeEncoder extends AbstractSAML2AttributeEncoder {
36
37
38 private final Logger log = LoggerFactory.getLogger(SAML2XMLObjectAttributeEncoder.class);
39
40
41 private final XMLObjectBuilder<XSAny> attributeValueBuilder;
42
43
44 public SAML2XMLObjectAttributeEncoder() {
45 super();
46 attributeValueBuilder = Configuration.getBuilderFactory().getBuilder(XSAny.TYPE_NAME);
47 }
48
49
50 public Attribute encode(BaseAttribute attribute) throws AttributeEncodingException {
51 Attribute samlAttribute = attributeBuilder.buildObject();
52 populateAttribute(samlAttribute);
53
54 XSAny samlAttributeValue;
55 for (Object o : attribute.getValues()) {
56 if (o == null || !(o instanceof XMLObject)) {
57 continue;
58 }
59
60 samlAttributeValue = attributeValueBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME);
61 samlAttributeValue.getUnknownXMLObjects().add((XMLObject) o);
62 samlAttribute.getAttributeValues().add(samlAttributeValue);
63 }
64
65 List<XMLObject> attributeValues = samlAttribute.getAttributeValues();
66 if (attributeValues == null || attributeValues.isEmpty()) {
67 log.debug("Unable to encode {} attribute. It does not contain any values", attribute.getId());
68 return null;
69 }
70
71 return samlAttribute;
72 }
73
74 }