Java Code Examples for javax.xml.soap.SOAPBody#addBodyElement()

The following examples show how to use javax.xml.soap.SOAPBody#addBodyElement() . 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: ClientServerSwaTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testSwaTypesWithDispatchAPI() throws Exception {
    URL url1 = this.getClass().getResource("resources/attach.text");
    URL url2 = this.getClass().getResource("resources/attach.html");
    URL url3 = this.getClass().getResource("resources/attach.xml");
    URL url4 = this.getClass().getResource("resources/attach.jpeg1");
    URL url5 = this.getClass().getResource("resources/attach.jpeg2");


    byte[] bytes = IOUtils.readBytesFromStream(url1.openStream());
    byte[] bigBytes = new byte[bytes.length * 50];
    for (int x = 0; x < 50; x++) {
        System.arraycopy(bytes, 0, bigBytes, x * bytes.length, bytes.length);
    }

    DataHandler dh1 = new DataHandler(new ByteArrayDataSource(bigBytes, "text/plain"));
    DataHandler dh2 = new DataHandler(url2);
    DataHandler dh3 = new DataHandler(url3);
    DataHandler dh4 = new DataHandler(url4);
    DataHandler dh5 = new DataHandler(url5);

    SwAService service = new SwAService();

    Dispatch<SOAPMessage> disp = service
        .createDispatch(SwAService.SwAServiceHttpPort,
                        SOAPMessage.class,
                        Service.Mode.MESSAGE);
    setAddress(disp, "http://localhost:" + serverPort + "/swa");


    SOAPMessage msg = MessageFactory.newInstance().createMessage();
    SOAPBody body = msg.getSOAPPart().getEnvelope().getBody();
    body.addBodyElement(new QName("http://cxf.apache.org/swa/types",
                                  "VoidRequest"));

    AttachmentPart att = msg.createAttachmentPart(dh1);
    att.setContentId("<[email protected]>");
    msg.addAttachmentPart(att);

    att = msg.createAttachmentPart(dh2);
    att.setContentId("<[email protected]>");
    msg.addAttachmentPart(att);

    att = msg.createAttachmentPart(dh3);
    att.setContentId("<[email protected]>");
    msg.addAttachmentPart(att);

    att = msg.createAttachmentPart(dh4);
    att.setContentId("<[email protected]>");
    msg.addAttachmentPart(att);

    att = msg.createAttachmentPart(dh5);
    att.setContentId("<[email protected]>");
    msg.addAttachmentPart(att);

    //Test for CXF-
    msg = disp.invoke(msg);
    assertEquals(5, msg.countAttachments());

}
 
Example 4
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;
}