New SAMLObjects are created through a builder class. This class is responsible for creating an object using some particular implementation and setting up some initial information such as element name and schema type.
org.opensaml.Configuration#getBuilderFactory()
method.org.opensaml.xml.XMLObjectBuilderFactory#getBuilder(QName)
method. The QName constants on the interfaces for the various SAMLObjects should be used as the argument, however creating a new QName from scratch will work as well.buildObject()
method on the returned SAMLObjectBuilder
. Normally the no-argument method is sufficient but other methods allow you to explicitly set the element name and schema type of the constructed object.The following example demonstrates creating a SAML 1.1 Assertion from scratch. The try/catch code has been omitted here for readability.
import org.opensaml.Configuration; import org.opensaml.common.SAMLObjectBuilder; import org.opensaml.saml1.core.Assertion; import org.opensaml.xml.XMLObjectBuilderFactory; ... // Get the builder factory XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory(); // Get the assertion builder based on the assertion element name SAMLObjectBuilder<Assertion> builder = (SAMLObjectBuilder<Assertion>) builderFactory.getBuilder(Assertion.DEFAULT_ELEMENT_NAME); // Create the assertion Assertion assertion = builder.buildObject(); |
This more advanced example uses the schema type name and builds an element of the Assertion schema type but with a different name in a different name space.
// Get the builder factory XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory(); // Get the assertion builder based on the assertion element name SAMLObjectBuilder<Assertion> builder = builderFactory.getBuilderAssertion.TYPE_NAME); // Create the assertion Assertion assertion = builder.buildObject("someOtherNamespaceURI", "MyAssertionElementName", "someOtherNamespacePrefix", Assertion.TYPE_NAME); |