Example usage for javax.xml.soap SOAPMessage WRITE_XML_DECLARATION

List of usage examples for javax.xml.soap SOAPMessage WRITE_XML_DECLARATION

Introduction

In this page you can find the example usage for javax.xml.soap SOAPMessage WRITE_XML_DECLARATION.

Prototype

String WRITE_XML_DECLARATION

To view the source code for javax.xml.soap SOAPMessage WRITE_XML_DECLARATION.

Click Source Link

Document

Specifies whether the SOAP Message will contain an XML declaration when it is sent.

Usage

From source file:ee.ria.xroad.common.message.SoapUtils.java

/**
 * Returns the XML representing the SOAP message.
 * @param soap message to be converted into XML
 * @param charset charset to use when generating the XML string
 * @return XML String representing the soap message
 * @throws IOException in case of errors
 *///from  www  .  j  av  a 2  s  .co m
public static String getXml(SOAPMessage soap, String charset) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        soap.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
        soap.writeTo(out);
    } catch (SOAPException e) {
        // Avoid throwing SOAPException, since it is essentially an
        // IOException if we cannot produce the XML
        throw new IOException(e);
    }

    return out.toString(charset);
}

From source file:ee.ria.xroad.common.message.SoapUtils.java

/**
 * Returns the XML representing the SOAP message as bytes.
 * @param soap message to be converted to byte content
 * @return byte[]//from w  w  w .j  a  v  a  2s. c  o m
 * @throws IOException if errors occur while writing message bytes
 */
public static byte[] getBytes(SOAPMessage soap) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        soap.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
        soap.writeTo(out);
    } catch (SOAPException e) {
        // Avoid throwing SOAPException, since it is essentially an
        // IOException if we cannot produce the XML
        throw new IOException(e);
    }

    return out.toByteArray();
}

From source file:org.apache.axis.SOAPPart.java

/**
 * Write the contents to the specified writer.
 *
 * @param writer  the <code>Writer</code> to write to
 *//*from   w  ww  .ja va  2s .  co m*/
public void writeTo(Writer writer) throws IOException {
    boolean inclXmlDecl = false;

    if (msgObject.getMessageContext() != null) { // if we have message context (JAX-RPC), write xml decl always. 
        inclXmlDecl = true;
    } else { // if we have no message context (SAAJ), write xml decl according to property.
        try {
            String xmlDecl = (String) msgObject.getProperty(SOAPMessage.WRITE_XML_DECLARATION);
            if (xmlDecl != null && xmlDecl.equals("true")) {
                inclXmlDecl = true;
            }
        } catch (SOAPException e) {
            throw new IOException(e.getMessage());
        }
    }

    if (currentForm == FORM_FAULT) {
        AxisFault env = (AxisFault) currentMessage;
        try {
            SerializationContext serContext = new SerializationContext(writer,
                    getMessage().getMessageContext());
            serContext.setSendDecl(inclXmlDecl);
            serContext.setEncoding(currentEncoding);
            env.output(serContext);
        } catch (Exception e) {
            log.error(Messages.getMessage("exception00"), e);
            throw env;
        }
        return;
    }

    if (currentForm == FORM_SOAPENVELOPE) {
        SOAPEnvelope env = (SOAPEnvelope) currentMessage;
        try {
            SerializationContext serContext = new SerializationContext(writer,
                    getMessage().getMessageContext());
            serContext.setSendDecl(inclXmlDecl);
            serContext.setEncoding(currentEncoding);
            env.output(serContext);
        } catch (Exception e) {
            throw AxisFault.makeFault(e);
        }
        return;
    }

    String xml = this.getAsString();
    if (inclXmlDecl) {
        if (!xml.startsWith("<?xml")) {
            writer.write("<?xml version=\"1.0\" encoding=\"");
            writer.write(currentEncoding);
            writer.write("\"?>");
        }
    }
    writer.write(xml);
}

From source file:org.openhab.binding.fritzboxtr064.internal.Tr064Comm.java

/**
 * Sets all required namespaces and prepares the SOAP message to send.
 * Creates skeleton + body data.//from  w ww .  ja  v a  2 s .c o m
 *
 * @param bodyData
 *            is attached to skeleton to form entire SOAP message
 * @return ready to send SOAP message
 */
private SOAPMessage constructTr064Msg(SOAPBodyElement bodyData) {
    SOAPMessage soapMsg = null;

    try {
        MessageFactory msgFac;
        msgFac = MessageFactory.newInstance();
        soapMsg = msgFac.createMessage();
        soapMsg.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
        soapMsg.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-8");
        SOAPPart part = soapMsg.getSOAPPart();

        // valid for entire SOAP msg
        String namespace = "s";

        // create suitable fbox envelope
        SOAPEnvelope envelope = part.getEnvelope();
        envelope.setPrefix(namespace);
        envelope.removeNamespaceDeclaration("SOAP-ENV"); // delete standard namespace which was already set
        envelope.addNamespaceDeclaration(namespace, "http://schemas.xmlsoap.org/soap/envelope/");
        Name nEncoding = envelope.createName("encodingStyle", namespace,
                "http://schemas.xmlsoap.org/soap/encoding/");
        envelope.addAttribute(nEncoding, "http://schemas.xmlsoap.org/soap/encoding/");

        // create empty header
        SOAPHeader header = envelope.getHeader();
        header.setPrefix(namespace);

        // create body with command based on parameter
        SOAPBody body = envelope.getBody();
        body.setPrefix(namespace);
        body.addChildElement(bodyData); // bodyData already prepared. Needs only be added

    } catch (Exception e) {
        logger.error("Error creating SOAP message for fbox request with data {}", bodyData);
        e.printStackTrace();
    }

    return soapMsg;
}