Search in sources :

Example 1 with MarshallerFactory

use of org.opensaml.core.xml.io.MarshallerFactory in project cas by apereo.

the class AbstractSamlObjectBuilder method marshalSamlXmlObject.

/**
     * Marshal the saml xml object to raw xml.
     *
     * @param object the object
     * @param writer the writer
     * @return the xml string
     */
public String marshalSamlXmlObject(final XMLObject object, final StringWriter writer) {
    try {
        final MarshallerFactory marshallerFactory = XMLObjectProviderRegistrySupport.getMarshallerFactory();
        final Marshaller marshaller = marshallerFactory.getMarshaller(object);
        if (marshaller == null) {
            throw new IllegalArgumentException("Cannot obtain marshaller for object " + object.getElementQName());
        }
        final Element element = marshaller.marshall(object);
        element.setAttributeNS(NAMESPACE_URI, "xmlns", SAMLConstants.SAML20_NS);
        element.setAttributeNS(NAMESPACE_URI, "xmlns:xenc", "http://www.w3.org/2001/04/xmlenc#");
        final TransformerFactory transFactory = TransformerFactory.newInstance();
        final Transformer transformer = transFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(new DOMSource(element), new StreamResult(writer));
        return writer.toString();
    } catch (final Exception e) {
        throw new IllegalStateException("An error has occurred while marshalling SAML object to xml", e);
    }
}
Also used : MarshallerFactory(org.opensaml.core.xml.io.MarshallerFactory) Marshaller(org.opensaml.core.xml.io.Marshaller) DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element)

Aggregations

Transformer (javax.xml.transform.Transformer)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 Marshaller (org.opensaml.core.xml.io.Marshaller)1 MarshallerFactory (org.opensaml.core.xml.io.MarshallerFactory)1 Element (org.w3c.dom.Element)1