Java Code Examples for javax.xml.soap.SOAPBodyElement#addChildElement()

The following examples show how to use javax.xml.soap.SOAPBodyElement#addChildElement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MdwRpcWebServiceAdapter.java    From mdw with Apache License 2.0 5 votes vote down vote up
/**
 * Populate the SOAP request message.
 */
protected SOAPMessage createSoapRequest(Object requestObj) throws ActivityException {
    try {
        MessageFactory messageFactory = getSoapMessageFactory();
        SOAPMessage soapMessage = messageFactory.createMessage();
        Map<Name,String> soapReqHeaders = getSoapRequestHeaders();
        if (soapReqHeaders != null) {
            SOAPHeader header = soapMessage.getSOAPHeader();
            for (Name name : soapReqHeaders.keySet()) {
                header.addHeaderElement(name).setTextContent(soapReqHeaders.get(name));
            }
        }

        SOAPBody soapBody = soapMessage.getSOAPBody();

        Document requestDoc = null;
        if (requestObj instanceof String) {
            requestDoc = DomHelper.toDomDocument((String)requestObj);
        }
        else {
            Variable reqVar = getProcessDefinition().getVariable(getAttributeValue(REQUEST_VARIABLE));
            XmlDocumentTranslator docRefTrans = (XmlDocumentTranslator)getPackage().getTranslator(reqVar.getType());
            requestDoc = docRefTrans.toDomDocument(requestObj);
        }

        SOAPBodyElement bodyElem = soapBody.addBodyElement(getOperation());
        String requestLabel = getRequestLabelPartName();
        if (requestLabel != null) {
            SOAPElement serviceNameElem = bodyElem.addChildElement(requestLabel);
            serviceNameElem.addTextNode(getRequestLabel());
        }
        SOAPElement requestDetailsElem = bodyElem.addChildElement(getRequestPartName());
        requestDetailsElem.addTextNode("<![CDATA[" + DomHelper.toXml(requestDoc) + "]]>");

        return soapMessage;
    }
    catch (Exception ex) {
        throw new ActivityException(ex.getMessage(), ex);
    }
}
 
Example 2
Source File: SAAJTestServlet.java    From appengine-java-vm-runtime with Apache License 2.0 4 votes vote down vote up
/**
 * The main goal of this test is to exercise the SAAJ classes and make sure
 * that no exceptions are thrown. We build a SOAP message, we send it to a
 * mock SOAP server, which simply echoes the message back to us, and then we
 * check to make sure that the returned message is the same as the sent
 * message. We perform that check for equality in a very shallow way because
 * we are not seriously concerned about the possibility that the SOAP message
 * might be garbled. The check for equality should be thought of as only a
 * sanity check.
 */
private void testSAAJ(String protocol) throws Exception {
  // Create the message
  MessageFactory factory = MessageFactory.newInstance(protocol);
  SOAPMessage requestMessage = factory.createMessage();

  // Add a header
  SOAPHeader header = requestMessage.getSOAPHeader();
  QName headerName = new QName("http://ws-i.org/schemas/conformanceClaim/", "Claim", "wsi");
  SOAPHeaderElement headerElement = header.addHeaderElement(headerName);
  headerElement.addAttribute(new QName("conformsTo"), "http://ws-i.org/profiles/basic/1.1/");

  // Add a body
  QName bodyName = new QName("http://wombat.ztrade.com", "GetLastTradePrice", "m");
  SOAPBody body = requestMessage.getSOAPBody();
  SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
  QName name = new QName("symbol");
  SOAPElement symbol = bodyElement.addChildElement(name);
  symbol.addTextNode("SUNW");

  // Add an attachment
  AttachmentPart attachment = requestMessage.createAttachmentPart();
  String stringContent =
      "Update address for Sunny Skies " + "Inc., to 10 Upbeat Street, Pleasant Grove, CA 95439";
  attachment.setContent(stringContent, "text/plain");
  attachment.setContentId("update_address");
  requestMessage.addAttachmentPart(attachment);

  // Add another attachment
  URL url = new URL("http://greatproducts.com/gizmos/img.jpg");
  // URL url = new URL("file:///etc/passwords");
  DataHandler dataHandler = new DataHandler(url);
  AttachmentPart attachment2 = requestMessage.createAttachmentPart(dataHandler);
  attachment2.setContentId("attached_image");
  requestMessage.addAttachmentPart(attachment2);

  // Send the message
  SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
  SOAPConnection connection = soapConnectionFactory.createConnection();
  URL endpoint = new URL("http://wombat.ztrade.com/quotes");

  // Get the response. Our mock url-fetch handler will echo back the request
  SOAPMessage responseMessage = connection.call(requestMessage, endpoint);
  connection.close();

  assertEquals(requestMessage, responseMessage);
}
 
Example 3
Source File: TestMtomProviderImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
public SOAPMessage invoke(final SOAPMessage request) {
    try {
        System.out.println("=== Received client request ===");

        // create the SOAPMessage
        SOAPMessage message = MessageFactory.newInstance().createMessage();
        SOAPPart part = message.getSOAPPart();
        SOAPEnvelope envelope = part.getEnvelope();
        SOAPBody body = envelope.getBody();


        SOAPBodyElement testResponse = body
            .addBodyElement(envelope.createName("testXopResponse", null, "http://cxf.apache.org/mime/types"));
        SOAPElement name = testResponse.addChildElement("name", null, "http://cxf.apache.org/mime/types");
        name.setTextContent("return detail + call detail");
        SOAPElement attachinfo = testResponse.addChildElement(
                                     "attachinfo", null, "http://cxf.apache.org/mime/types");
        SOAPElement include = attachinfo.addChildElement("Include", "xop",
                                                            "http://www.w3.org/2004/08/xop/include");

        int fileSize = 0;
        try (InputStream pre = this.getClass().getResourceAsStream("/wsdl/mtom_xop.wsdl")) {
            for (int i = pre.read(); i != -1; i = pre.read()) {
                fileSize++;
            }
        }

        int count = 50;
        byte[] data = new byte[fileSize *  count];
        for (int x = 0; x < count; x++) {
            this.getClass().getResourceAsStream("/wsdl/mtom_xop.wsdl").read(data,
                                                                            fileSize * x,
                                                                            fileSize);
        }


        DataHandler dh = new DataHandler(new ByteArrayDataSource(data, "application/octet-stream"));

        // create the image attachment
        AttachmentPart attachment = message.createAttachmentPart(dh);
        attachment.setContentId("mtom_xop.wsdl");
        message.addAttachmentPart(attachment);
        System.out
            .println("Adding attachment: " + attachment.getContentId() + ":" + attachment.getSize());

        // add the reference to the image attachment
        include.addAttribute(envelope.createName("href"), "cid:" + attachment.getContentId());

        return message;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}