Marshalling and cloning XMLObjects w/Java OpenSAML

Mark Dobrinic mdobrinic at cozmanova.com
Tue Oct 1 09:42:52 EDT 2013


Hi OpenSAML devs,

I have a question about working with the metadata, using the OpenSAML2
java library.

This is what I am trying to do:
- I use an org.opensaml.saml2.metadata.EntityDescriptor to manage the
SAML2 metadata of my system.
- To publish the metadata, I do a entityDescriptor.getDOM(), and
transform this to an XML-string. Work perfect.


The next thing I want to do, is creating a EntitiesDescriptor, in which
I manage multiple other EntityDescriptor XMLObjects.


To create the new EntityDescriptor-instances, I want to reuse parts of
the main entityDescriptor. For example: I want to re-use the
NameIDFormat elements of a IDPSSODescriptor from the main
entityDescriptor instance into a newly created entityDescriptor.

For this, I try to use the following code (exception/error handling
omitted here):

----
// The target EntityDescriptor:
XMLObjectBuilderFactory oBuilder = Configuration.getBuilderFactory();
EntityDescriptorBuilder oBuilder_ED =(EntityDescriptorBuilder)
    oBuilder.getBuilder(EntityDescriptor.DEFAULT_ELEMENT_NAME);
EntityDescriptor oED_publish = oBuilder_ED.buildObject();

IDPSSODescriptorBuilder oBuilder_IDPSSO = (IDPSSODescriptorBuilder)
    oBuilder.getBuilder(IDPSSODescriptor.DEFAULT_ELEMENT_NAME);
IDPSSODescriptor oIDPSSO_publish = oBuilder_IDPSSO.buildObject();

// The source IDPSSODescriptor:
IDPSSODescriptor oIDPSSODesc =
    entityDescriptor.getIDPSSODescriptor(SAMLConstants.SAML20P_NS);

// Now copy from the source to the destination:

List<NameIDFormat> l = oIDPSSODesc.getNameIDFormats();
if (l != null) {
  for (NameIDFormat nf: l) {
  NameIDFormat oNF_new;
  oNF_new = (NameIDFormat) XMLObjectHelper.cloneXMLObject(nf, true);

  oIDPSSO_publish.getNameIDFormats().add(oNF_new);
}
----


My questions/problems are:
1) As far as I can tell, this is the right way to do things: it works
for creating the result: the EntityDescriptor 'oED_publish' gets the
right NameIDFormats added. Can anybody confirm that using
XMLHelper.cloneXMLObject() is the way to go here?

2) Problem: if (after I run cloneXMLObject()) I do an
entityDescriptor.getDOM(), there is an empty document returned. I
narrowed this down to here:
XMLObjectHelper.cloneXMLObject() ->
  Marshaller.marshall() ->
    AbstractXMLObjectMarshaller.prepareForAdoption(XMLObject) ->
      XMLHelper.rootNamespaces(domCachingObject.getDOM());

After the rootNamespaces is called on the XMLObject-instance that is
being cloned, the DOM-element of the original XMLObject is being reset t
an empty document

Side-question:
I am not completely sure what the DOM element represents of an
XMLObject: is this a cached version? If so, is there a way to re-create
the DOM that belongs to this XMLObject?

Main question:
How can I *really* clone an XMLObject, so I can achieve that I can
create a new EntityDescriptor(XMLObject) by picking elements from
another EntityDescriptor(XMLObject), clone these, and add them to the
new EntityDescriptor(XMLObject), without affecting the source
EntityDescriptor(XMLObject)?

Thanks for your response,

Cheers!

Mark Dobrinic
Using OpenSAML with Asimba SSO (fork of OpenASelect)


More information about the dev mailing list