Example usage for org.w3c.dom.bootstrap DOMImplementationRegistry getDOMImplementation

List of usage examples for org.w3c.dom.bootstrap DOMImplementationRegistry getDOMImplementation

Introduction

In this page you can find the example usage for org.w3c.dom.bootstrap DOMImplementationRegistry getDOMImplementation.

Prototype

public DOMImplementation getDOMImplementation(final String features) 

Source Link

Document

Return the first implementation that has the desired features, or null if none is found.

Usage

From source file:org.wso2.carbon.identity.sso.saml.cloud.util.SAMLSSOUtil.java

/**
 * Serialize the Auth. Request//w w  w .  j  a v  a2  s.  c  om
 *
 * @param xmlObject
 * @return serialized auth. req
 */
public static String marshall(XMLObject xmlObject) throws IdentityException {

    ByteArrayOutputStream byteArrayOutputStrm = null;
    try {
        doBootstrap();
        System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
                "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");

        MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration.getMarshallerFactory();
        Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject);
        Element element = marshaller.marshall(xmlObject);

        byteArrayOutputStrm = new ByteArrayOutputStream();
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();
        LSOutput output = impl.createLSOutput();
        output.setByteStream(byteArrayOutputStrm);
        writer.write(element, output);
        return byteArrayOutputStrm.toString(StandardCharsets.UTF_8.name());
    } catch (Exception e) {
        log.error("Error Serializing the SAML Response");
        throw IdentityException.error("Error Serializing the SAML Response", e);
    } finally {
        if (byteArrayOutputStrm != null) {
            try {
                byteArrayOutputStrm.close();
            } catch (IOException e) {
                log.error("Error while closing the stream", e);
            }
        }
    }
}

From source file:org.wso2.carbon.identity.sso.saml.ui.ErrorResponseBuilder.java

private static String marshall(XMLObject xmlObject) throws org.wso2.carbon.identity.base.IdentityException {
    try {/*from w  w  w . ja v a2  s. c  om*/
        System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
                "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");

        MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration.getMarshallerFactory();
        Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject);
        Element element = marshaller.marshall(xmlObject);

        ByteArrayOutputStream byteArrayOutputStrm = new ByteArrayOutputStream();
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();
        LSOutput output = impl.createLSOutput();
        output.setByteStream(byteArrayOutputStrm);
        writer.write(element, output);
        return byteArrayOutputStrm.toString("UTF-8");
    } catch (Exception e) {
        log.error("Error Serializing the SAML Response");
        throw new IdentityException("Error Serializing the SAML Response", e);
    }
}

From source file:org.wso2.carbon.identity.sso.saml.util.SAMLSSOUtil.java

/**
 * Serialize the Auth. Request/*from w w w . ja v  a 2  s .  c o  m*/
 *
 * @param xmlObject
 * @return serialized auth. req
 */
public static String marshall(XMLObject xmlObject) throws IdentityException {

    ByteArrayOutputStream byteArrayOutputStrm = null;
    try {
        doBootstrap();
        System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
                "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");

        MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration.getMarshallerFactory();
        Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject);
        Element element = marshaller.marshall(xmlObject);

        byteArrayOutputStrm = new ByteArrayOutputStream();
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();
        LSOutput output = impl.createLSOutput();
        output.setByteStream(byteArrayOutputStrm);
        writer.write(element, output);
        return byteArrayOutputStrm.toString("UTF-8");
    } catch (Exception e) {
        log.error("Error Serializing the SAML Response");
        throw new IdentityException("Error Serializing the SAML Response", e);
    } finally {
        if (byteArrayOutputStrm != null) {
            try {
                byteArrayOutputStrm.close();
            } catch (IOException e) {
                log.error("Error while closing the stream", e);
            }
        }
    }
}

From source file:org.wso2.developerstudio.eclipse.security.project.ui.form.SecurityFormPage.java

/**
 * Gets the updated content of the source
 *
 * @return content/*  w w w  . j a  v  a2  s . com*/
 * @throws TransformerException
 */
public String getUpdatedContent()
        throws TransformerException, InstantiationException, IllegalAccessException, ClassNotFoundException {
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
    final LSSerializer writer = impl.createLSSerializer();

    // Set this to true if the output needs to be beautified.
    writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
    // Set this to true if the declaration is needed to be outputted.
    writer.getDomConfig().setParameter("xml-declaration", false);

    return writer.writeToString(policyFileDOMDocument);
}

From source file:org.wso2.identity.iml.dsl.mediators.SAMLResponseBuilder.java

private String marshall(XMLObject xmlObject) throws ConfigurationException {

    IMLUtils.doBootstrap();/*from ww w.  ja v  a  2  s .c  o  m*/

    try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
        System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
                "org.apache.xerces.jaxp" + ".DocumentBuilderFactoryImpl");
        MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration.getMarshallerFactory();
        Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject);
        Element element = marshaller.marshall(xmlObject);

        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");

        LSSerializer serializer = impl.createLSSerializer();
        LSOutput output = impl.createLSOutput();

        output.setByteStream(byteArrayOutputStream);
        serializer.write(element, output);

        return byteArrayOutputStream.toString(StandardCharsets.UTF_8.name());

    } catch (MarshallingException | IOException | ClassNotFoundException | InstantiationException
            | IllegalAccessException e) {
        //TODO Build SAML Error Resp and do proper logging
        log.error("Error while marshalling the SAML response", e);
        return null;
    }
}

From source file:org.wso2.identity.integration.common.clients.sso.saml.query.QueryClientUtils.java

/**
 * This method is used to serialize response message
 *
 * @param xmlObject well formed XML object
 * @return String serialized response/*from  ww w.j  av  a 2  s  .  c o m*/
 */
public static String marshall(XMLObject xmlObject) {

    ByteArrayOutputStream byteArrayOutputStrm = null;
    try {
        doBootstrap();
        System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
                "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
        MarshallerFactory marshallerFactory = XMLObjectProviderRegistrySupport.getMarshallerFactory();
        Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject);
        if (marshaller != null) {
            Element element = marshaller.marshall(xmlObject);
            byteArrayOutputStrm = new ByteArrayOutputStream();
            DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
            DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
            LSSerializer writer = impl.createLSSerializer();
            LSOutput output = impl.createLSOutput();
            output.setByteStream(byteArrayOutputStrm);
            writer.write(element, output);
            return byteArrayOutputStrm.toString("UTF-8");
        } else {
            log.error("Error can not find marshaller");
        }
    } catch (InstantiationException e) {
        log.error("Unable to initiate DOM implementation registry", e);
    } catch (MarshallingException e) {
        log.error("Unable to marshall element", e);
    } catch (IllegalAccessException e) {
        log.error("Illegal access on DOM registry ", e);
    } catch (UnsupportedEncodingException e) {
        log.error("Unsupported encoding scheme", e);
    } catch (ClassNotFoundException e) {
        log.error("Class not found", e);
    } finally {
        if (byteArrayOutputStrm != null) {
            try {
                byteArrayOutputStrm.close();
            } catch (IOException e) {
                log.error("Error while closing the stream", e);
            }
        }
    }

    return null;
}

From source file:org.wso2.is.saml.TestUtils.java

public static String marshall(XMLObject xmlObject) {

    ByteArrayOutputStream byteArrayOutputStrm = null;
    try {/*from  w w  w  . jav  a  2  s  .c  om*/
        doBootstrap();
        MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration.getMarshallerFactory();
        Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject);
        Element element = marshaller.marshall(xmlObject);

        byteArrayOutputStrm = new ByteArrayOutputStream();
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();
        LSOutput output = impl.createLSOutput();
        output.setByteStream(byteArrayOutputStrm);
        writer.write(element, output);
        return byteArrayOutputStrm.toString(StandardCharsets.UTF_8.name());
    } catch (Exception e) {
        logger.error("Error while marshalling xml object", e);
    } finally {
        if (byteArrayOutputStrm != null) {
            try {
                byteArrayOutputStrm.close();
            } catch (IOException e) {
                logger.error("Error while closing byteArrayOutputStream", e);
            }
        }
    }
    return null;
}

From source file:test.framework.TestBase.java

/**
 * Serialize the given Dom Object to a String.
 * /*from w  ww  . j  ava 2  s.co m*/
 * @param xml The Xml Node to serialize.
 * @param omitXMLDeclaration Indicates if XML declaration will be omitted.
 * @return The String representation of the Xml Node.
 * @throws Exception If anything fails.
 */
protected static String toString(final Node xml, final boolean omitXMLDeclaration) throws Exception {
    String result = new String();
    if (xml instanceof AttrImpl) {
        result = xml.getTextContent();
    } else if (xml instanceof Document) {
        StringWriter stringOut = new StringWriter();
        // format
        OutputFormat format = new OutputFormat((Document) xml);
        format.setIndenting(true);
        format.setPreserveSpace(false);
        format.setOmitXMLDeclaration(omitXMLDeclaration);
        format.setEncoding("UTF-8");
        // serialize
        XMLSerializer serial = new XMLSerializer(stringOut, format);
        serial.asDOMSerializer();
        serial.serialize((Document) xml);
        result = stringOut.toString();
    } else {
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSOutput lsOutput = impl.createLSOutput();
        lsOutput.setEncoding("UTF-8");
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        lsOutput.setByteStream(os);
        LSSerializer writer = impl.createLSSerializer();
        // result = writer.writeToString(xml);
        writer.write(xml, lsOutput);
        result = ((ByteArrayOutputStream) lsOutput.getByteStream()).toString();
        if ((omitXMLDeclaration) && (result.indexOf("?>") != -1)) {
            result = result.substring(result.indexOf("?>") + 2);
        }
        // result = toString(getDocument(writer.writeToString(xml)),
        // true);
    }
    return result;
}