2

I'm trying to modify a SAML Response body contents (to add white space, extra attributes, etc) for testing. But can't seem to find a way to get the marshalled object converted to a string I can modify, then unmarshall back into a Response object.

doing a .toString() (as it mentions here: I want to convert an output stream into String object) Just seems to get the main node, not the entirety.

Basically I've been just messing around with

        Element el = XMLObjectProviderRegistrySupport.getMarshallerFactory().getMarshaller(response).marshall(response);

To try and get it, but thought I'd ask here since I couldn't find it in search. Once I can turn it into a string or XML object seems straight forward to just unmarshall it back (https://wiki.shibboleth.net/confluence/display/OpenSAML/OSTwoUsrManJavaCreateFromXML)

Thanks!

Th3sandm4n
  • 809
  • 4
  • 13
  • 23
  • Doh found it right after: https://stackoverflow.com/questions/32739278/convert-elementorg-w3c-dom-to-string-in-java – Th3sandm4n Aug 23 '17 at 16:55
  • posting answer found here for future people: https://stackoverflow.com/questions/32739278/convert-elementorg-w3c-dom-to-string-in-java – Th3sandm4n Feb 13 '18 at 18:00

1 Answers1

1
# Packages
import org.opensaml.xml.Configuration;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Element;


# Transform to w3c xml
Element xml = Configuration
               .getMarshallerFactory()
               .getMarshaller(authnRequest)
               .marshall(authnRequest);

# use referenced SO answer to write xml to a writer -  stackoverflow.com/questions/32739278/
StreamResult result = new StreamResult(new StringWriter());
TransformerFactory
  .newInstance()
  .newTransformer()
  .transform(new DOMSource(xml), result);

# Get the string from the writer
String samlRequest = result.getWriter().toString();

# et Voila
log.info("SAML request: {}", samlRequest);
Matyas
  • 13,473
  • 3
  • 60
  • 73