Logan
Logan

Reputation: 101

SAML getUnmarshaller(element) returns null

I am trying to construct XML object from XmlString but getUnmarshaller(element) is returning null :

This is the code :

     try {
        InitializationService.initialize();
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = docBuilder.parse(new ByteArrayInputStream(xmlString.trim().getBytes()));
        Element element = document.getDocumentElement();
        UnmarshallerFactory unmarshallerFactory = XMLObjectProviderRegistrySupport.getUnmarshallerFactory();
        org.opensaml.core.xml.io.Unmarshaller unmarshaller = unmarshallerFactory
              .getUnmarshaller(element); //This is coming out be null
        System.out.println(unmarshaller);
     } catch (Exception e) {
        e.printStackTrace();
     }

This is the XMLString

static String xmlString ="<?xml version=\"1.0\"?>" +
          "  <!DOCTYPE address" +
          "  [" +
          "     <!ELEMENT address (buildingnumber, street, city, state, zip)>" +
          "     <!ATTLIST address xmlns CDATA #IMPLIED>" +
          "     <!ELEMENT buildingnumber (#PCDATA)>" +
          "     <!ELEMENT street (#PCDATA)>" +
          "     <!ELEMENT city (#PCDATA)>" +
          "     <!ELEMENT state (#PCDATA)>" +
          "     <!ELEMENT zip (#PCDATA)>" +
          "  ]>" +
          "" +
          "  <address>" +
          "    <buildingnumber> 29 </buildingnumber>" +
          "    <street> South Street</street>" +
          "    <city>Vancouver</city>" +
          "" +
          "    <state>BC</state>" +
          "    <zip>V6V 4U7</zip>" +
          "  </address>";

Any help would be appreciated.

Upvotes: 0

Views: 1835

Answers (2)

Deshan Koswatte
Deshan Koswatte

Reputation: 138

Most probably there's an issue with the initialization process of OpenSAML3. My guess is that unmarshallerFactory is null. First check if you have the below dependency:

<dependency>
    <groupId>org.opensaml</groupId>
    <artifactId>opensaml-saml-impl</artifactId>
    <version>3.0.x</version>
</dependency>

If it is there then try to initialize the OpenSAML library manually, just like https://github.com/wso2-extensions/carbon-identity-saml-common/blob/931e6164ee64350b15c6aa6ae080a0c5fb280828/components/org.wso2.carbon.identity.saml.common.util/src/main/java/org/wso2/carbon/identity/saml/common/util/SAMLInitializer.java#L41-L85 and then you'll get the correct unmarshaller.

Upvotes: 2

Matteo Pratesi
Matteo Pratesi

Reputation: 11

I don't know if it's still needed but

DefaultBootstrap.bootstrap();

at the beginning resolved the problem for me.

Upvotes: 1

Related Questions