The OpenSAML V2 software has reached its End of Life and is no longer supported. This documentation is available for historical purposes only.

OSTwoUsrManJavaCreateFromScratch

Creating SAML Objects from Scratch

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.

Building Basics

  1. Retrieve the builder factory using the org.opensaml.Configuration#getBuilderFactory() method.
  2. Retrieve the builder for the SAMLObject you're trying to create using the 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.
  3. Invoke the appropriate 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.

Building Example

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);

Additional Building Information

  • While every SAMLObjectBuilder has a no-argument builder method the more generic XMLObjectBuilder need not have this method.
  • Builders are thread-safe and stateless. You may use an instance to create any number of objects.