Example usage for org.w3c.dom.ls LSSerializer write

List of usage examples for org.w3c.dom.ls LSSerializer write

Introduction

In this page you can find the example usage for org.w3c.dom.ls LSSerializer write.

Prototype

public boolean write(Node nodeArg, LSOutput destination) throws LSException;

Source Link

Document

Serialize the specified node as described above in the general description of the LSSerializer interface.

Usage

From source file:org.wso2.carbon.identity.query.saml.util.SAMLQueryRequestUtil.java

/**
 * This method is used to serialize response message
 *
 * @param xmlObject well formed XML object
 * @return String serialized response/*from   ww w. ja  va  2 s. com*/
 * @throws IdentitySAML2QueryException If unable to marshall response
 */
public static String marshall(XMLObject xmlObject) throws IdentitySAML2QueryException {

    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);
        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(SAMLQueryRequestConstants.GenericConstants.UTF8_ENC);
    } catch (IdentityException e) {
        log.error("Error de-serializing the SAML Response", e);
        throw new IdentitySAML2QueryException("Error de-serializing the SAML Response");
    } catch (UnsupportedEncodingException e) {
        log.error("XML message contain invalid Encoding", e);
        throw new IdentitySAML2QueryException("XML message contain invalid Encoding");
    } catch (MarshallingException e) {
        log.error("Unable to marshall", e);
        throw new IdentitySAML2QueryException("Unable to marshall");
    } catch (IllegalAccessException e) {
        log.error("Illegal Access", e);
        throw new IdentitySAML2QueryException("Illegal Access");
    } catch (InstantiationException e) {
        log.error("Unable to initialize", e);
        throw new IdentitySAML2QueryException("Unable to initialize");
    } catch (ClassNotFoundException e) {
        log.error("Class not found", e);
        throw new IdentitySAML2QueryException("Class not found");
    } catch (NullPointerException e) {
        log.error("Marshall throw null pointer exception", e);
        throw new IdentitySAML2QueryException("Marshall throw null pointer exception");
    } 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.saml.inbound.util.SAMLSSOUtil.java

/**
 * Serialize the Auth. Request//from  w  ww .jav  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(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.agent.saml.SAML2SSOManager.java

/**
 * Serialize the Auth. Request//from   ww  w . java2 s . c  o  m
 *
 * @param xmlObject
 * @return serialized auth. req
 */
protected String marshall(XMLObject xmlObject) throws SSOAgentException {

    try {
        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 new String(byteArrayOutputStrm.toByteArray(), Charset.forName("UTF-8"));
    } catch (ClassNotFoundException e) {
        throw new SSOAgentException("Error in marshalling SAML2 Assertion", e);
    } catch (InstantiationException e) {
        throw new SSOAgentException("Error in marshalling SAML2 Assertion", e);
    } catch (MarshallingException e) {
        throw new SSOAgentException("Error in marshalling SAML2 Assertion", e);
    } catch (IllegalAccessException e) {
        throw new SSOAgentException("Error in marshalling SAML2 Assertion", e);
    }
}

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

/**
 * Serialize the Auth. Request//  www . j a va2  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(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 {/*  ww  w  .ja va  2s .  co m*/
        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/*ww w .  j av  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.carbon.mediation.configadmin.util.XMLPrettyPrinter.java

/**
 * XML Pretty Print method with XML Comments support.
 *
 * @return XML formatted String//from  w ww  . ja  va 2s .  com
 */
public String xmlFormatWithComments() {
    String xmlOutput = null;
    Document doc;
    LSSerializer lsSerializer;

    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        doc = documentBuilderFactory.newDocumentBuilder().parse(in);

        DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
        lsSerializer = domImplementation.createLSSerializer();
        lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);

        LSOutput lsOutput = domImplementation.createLSOutput();
        lsOutput.setEncoding(encoding);
        Writer stringWriter = new StringWriter();
        lsOutput.setCharacterStream(stringWriter);
        lsSerializer.write(doc, lsOutput);

        xmlOutput = stringWriter.toString();

    } catch (IOException e) {
        log.error("XML Pretty Printer Error reading data from given InputStream to XML Document ", e);
    } catch (SAXException e) {
        log.error("XML Pretty Printer Error parsing the given InputStream to XML Document", e);
    } catch (Exception e) {
        log.error("XML Pretty Printer failed. ", e);
    }
    return xmlOutput;
}

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

private String marshall(XMLObject xmlObject) throws ConfigurationException {

    IMLUtils.doBootstrap();//from  w ww .j a  va2  s  . co 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 . ja  v a 2  s  . co  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 {//  w  w w  . j  a v  a 2s  .  c o  m
        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;
}