Example usage for javax.xml.soap SOAPMessage setProperty

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

Introduction

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

Prototype

public void setProperty(String property, Object value) throws SOAPException 

Source Link

Document

Associates the specified value with the specified property.

Usage

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 va2  s  .c  om
 * @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: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  w ww  . j  a  v a2s  . c o  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:com.googlecode.ddom.saaj.SOAPMessageTest.java

/**
 * Tests that calling {@link SOAPMessage#setProperty(String, Object)} doesn't throw an exception
 * for unknown property names. Although that the Javadoc of that method suggests that a
 * {@link SOAPException} should be thrown, this is not the case for the reference
 * implementation./* w ww .j  a  va 2 s. com*/
 * 
 * @throws Exception
 */
@Validated
@Test
public void testSetPropertyUnknown() throws Exception {
    SOAPMessage message = getFactory().createMessage();
    message.setProperty("some.unknown.property", "test");
}

From source file:com.googlecode.ddom.saaj.SOAPMessageTest.java

@Validated
@Test/*from w w w  .ja v  a 2s. c o  m*/
public void testGetSetCharacterSetEncoding() throws Exception {
    SOAPMessage message = getFactory().createMessage();
    String encoding = "ISO-8859-15";
    message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, encoding);
    assertEquals(encoding, message.getProperty(SOAPMessage.CHARACTER_SET_ENCODING));
}

From source file:com.twinsoft.convertigo.engine.translators.WebServiceTranslator.java

public Object buildOutputData(Context context, Object convertigoResponse) throws Exception {
    Engine.logBeans.debug("[WebServiceTranslator] Encoding the SOAP response...");

    SOAPMessage responseMessage = null;
    String sResponseMessage = "";
    String encodingCharSet = "UTF-8";
    if (context.requestedObject != null)
        encodingCharSet = context.requestedObject.getEncodingCharSet();

    if (convertigoResponse instanceof Document) {
        Engine.logBeans.debug("[WebServiceTranslator] The Convertigo response is a XML document.");
        Document document = Engine.theApp.schemaManager.makeResponse((Document) convertigoResponse);

        //MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
        MessageFactory messageFactory = MessageFactory.newInstance();
        responseMessage = messageFactory.createMessage();

        responseMessage.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, encodingCharSet);

        SOAPPart sp = responseMessage.getSOAPPart();
        SOAPEnvelope se = sp.getEnvelope();
        SOAPBody sb = se.getBody();

        sb.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");

        //se.addNamespaceDeclaration(prefix, targetNameSpace);
        se.addNamespaceDeclaration("soapenc", "http://schemas.xmlsoap.org/soap/encoding/");
        se.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        se.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");

        // Remove header as it not used. Seems that empty headers causes the WS client of Flex 4 to fail 
        se.getHeader().detachNode();/*w w w.j  av a  2 s .c om*/

        addSoapElement(context, se, sb, document.getDocumentElement());

        sResponseMessage = SOAPUtils.toString(responseMessage, encodingCharSet);

        // Correct missing "xmlns" (Bug AXA POC client .NET)
        //sResponseMessage = sResponseMessage.replaceAll("<soapenv:Envelope", "<soapenv:Envelope xmlns=\""+targetNameSpace+"\"");
    } else {
        Engine.logBeans.debug("[WebServiceTranslator] The Convertigo response is not a XML document.");
        sResponseMessage = convertigoResponse.toString();
    }

    if (Engine.logBeans.isDebugEnabled()) {
        Engine.logBeans.debug("[WebServiceTranslator] SOAP response:\n" + sResponseMessage);
    }

    return responseMessage == null ? sResponseMessage.getBytes(encodingCharSet) : responseMessage;
}

From source file:com.twinsoft.convertigo.engine.translators.WebServiceTranslator.java

public Object __buildOutputData(Context context, Object convertigoResponse) throws Exception {
    Engine.logBeans.debug("[WebServiceTranslator] Encoding the SOAP response...");

    SOAPMessage responseMessage = null;
    String sResponseMessage = "";
    String encodingCharSet = "UTF-8";
    if (context.requestedObject != null)
        encodingCharSet = context.requestedObject.getEncodingCharSet();

    if (convertigoResponse instanceof Document) {
        Engine.logBeans.debug("[WebServiceTranslator] The Convertigo response is a XML document.");
        Document document = (Document) convertigoResponse;

        //MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
        MessageFactory messageFactory = MessageFactory.newInstance();
        responseMessage = messageFactory.createMessage();

        responseMessage.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, encodingCharSet);

        SOAPPart sp = responseMessage.getSOAPPart();
        SOAPEnvelope se = sp.getEnvelope();
        SOAPBody sb = se.getBody();

        sb.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");

        String targetNamespace = context.project.getTargetNamespace();
        String prefix = getPrefix(context.projectName, targetNamespace);

        //se.addNamespaceDeclaration(prefix, targetNameSpace);
        se.addNamespaceDeclaration("soapenc", "http://schemas.xmlsoap.org/soap/encoding/");
        se.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        se.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");

        // Remove header as it not used. Seems that empty headers causes the WS client of Flex 4 to fail 
        se.getHeader().detachNode();//from www  . j a va 2  s  . c o  m

        // Add the method response element
        SOAPElement soapMethodResponseElement = null;
        String soapElementName = context.sequenceName != null ? context.sequenceName
                : context.connectorName + "__" + context.transactionName;
        soapElementName += "Response";

        soapMethodResponseElement = sb.addChildElement(se.createName(soapElementName, prefix, targetNamespace));

        if (XsdForm.qualified == context.project.getSchemaElementForm()) {
            soapMethodResponseElement.addAttribute(se.createName("xmlns"), targetNamespace);
        }

        // Add a 'response' root child element or not
        SOAPElement soapResponseElement;
        if (context.sequenceName != null) {
            Sequence sequence = (Sequence) context.requestedObject;
            if (sequence.isIncludeResponseElement()) {
                soapResponseElement = soapMethodResponseElement.addChildElement("response");
            } else {
                soapResponseElement = soapMethodResponseElement;
            }
        } else {
            soapResponseElement = soapMethodResponseElement.addChildElement("response");
        }

        if (soapResponseElement.getLocalName().equals("response")) {
            if (document.getDocumentElement().hasAttributes()) {
                addAttributes(responseMessage, se, context, document.getDocumentElement().getAttributes(),
                        soapResponseElement);
            }
        }

        NodeList childNodes = document.getDocumentElement().getChildNodes();
        int len = childNodes.getLength();
        org.w3c.dom.Node node;
        for (int i = 0; i < len; i++) {
            node = childNodes.item(i);
            if (node instanceof Element) {
                addElement(responseMessage, se, context, (Element) node, soapResponseElement);
            }
        }

        sResponseMessage = SOAPUtils.toString(responseMessage, encodingCharSet);

        // Correct missing "xmlns" (Bug AXA POC client .NET)
        //sResponseMessage = sResponseMessage.replaceAll("<soapenv:Envelope", "<soapenv:Envelope xmlns=\""+targetNameSpace+"\"");
    } else {
        Engine.logBeans.debug("[WebServiceTranslator] The Convertigo response is not a XML document.");
        sResponseMessage = convertigoResponse.toString();
    }

    if (Engine.logBeans.isDebugEnabled()) {
        Engine.logBeans.debug("[WebServiceTranslator] SOAP response:\n" + sResponseMessage);
    }

    return responseMessage == null ? sResponseMessage.getBytes(encodingCharSet) : responseMessage;
}

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 w w . j  ava2s  .co  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;
}

From source file:org.springframework.ws.soap.saaj.SaajSoapMessageFactory.java

/**
 * Template method that allows for post-processing of the given {@link SOAPMessage}.
 * <p>Default implementation sets {@linkplain SOAPMessage#setProperty(String, Object) message properties}, if any.
 * @param soapMessage the message to post process
 * @see #setMessageProperties(java.util.Map)
 *///from  w ww . j ava  2 s .c o  m
protected void postProcess(SOAPMessage soapMessage) throws SOAPException {
    if (!CollectionUtils.isEmpty(messageProperties)) {
        for (Map.Entry<String, ?> entry : messageProperties.entrySet()) {
            soapMessage.setProperty(entry.getKey(), entry.getValue());
        }
    }
}