Java Code Examples for javax.xml.soap.SOAPMessage#getAttachments()

The following examples show how to use javax.xml.soap.SOAPMessage#getAttachments() . 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: SAAJTestServlet.java    From appengine-java-vm-runtime with Apache License 2.0 6 votes vote down vote up
private static void assertAttachmentsAreEqual(SOAPMessage expected, SOAPMessage actual)
    throws Exception {
  int expectedNumAttachments = expected.countAttachments();
  int actualNumAttachments = actual.countAttachments();
  if (expectedNumAttachments != actualNumAttachments) {
    throw new Exception(
        "expectedNumAttachments="
            + expectedNumAttachments
            + "actualNumAttachments="
            + actualNumAttachments);
  }
  Iterator<?> expectedAttachmentIterator = expected.getAttachments();
  Iterator<?> actualAttachmentIterator = actual.getAttachments();
  for (int attachmentIndex = 0; attachmentIndex < actualNumAttachments; attachmentIndex++) {
    AttachmentPart expectedAttachment = (AttachmentPart) expectedAttachmentIterator.next();
    AttachmentPart actualAttachment = (AttachmentPart) actualAttachmentIterator.next();
    assertEquals(expectedAttachment, actualAttachment, attachmentIndex);
  }
}
 
Example 2
Source File: SoapProviderTest.java    From iaf with Apache License 2.0 6 votes vote down vote up
private void assertAttachmentInReceivedMessage(SOAPMessage message) throws SOAPException, IOException {
	assertEquals(1, message.countAttachments());

	Iterator<?> attachmentParts = message.getAttachments();
	while (attachmentParts.hasNext()) {
		AttachmentPart soapAttachmentPart = (AttachmentPart)attachmentParts.next();
		InputStreamAttachmentPart attachmentPart = new InputStreamAttachmentPart(soapAttachmentPart);
		String attachment = Misc.streamToString(attachmentPart.getInputStream());
		//ContentID should be equal to the filename
		assertEquals(ATTACHMENT2_NAME, attachmentPart.getContentId());

		//Validate the attachment's content
		assertEquals(ATTACHMENT2_CONTENT, attachment);

		//Make sure at least the content-type header has been set
		Iterator<?> headers = attachmentPart.getAllMimeHeaders();
		String contentType = null;
		while (headers.hasNext()) {
			MimeHeader header = (MimeHeader) headers.next();
			if("Content-Type".equalsIgnoreCase(header.getName()))
				contentType = header.getValue();
		}
		assertEquals(ATTACHMENT2_MIMETYPE, contentType);
	}
}
 
Example 3
Source File: XmlTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void test() throws Exception {

        File file = new File("message.xml");
        file.deleteOnExit();

        MessageFactory mf = MessageFactory.newInstance();
        SOAPMessage msg = createMessage(mf);

        // Save the soap message to file
        try (FileOutputStream sentFile = new FileOutputStream(file)) {
            msg.writeTo(sentFile);
        }

        // See if we get the image object back
        try (FileInputStream fin = new FileInputStream(file)) {
            SOAPMessage newMsg = mf.createMessage(msg.getMimeHeaders(), fin);

            newMsg.writeTo(new ByteArrayOutputStream());

            Iterator<?> i = newMsg.getAttachments();
            while (i.hasNext()) {
                AttachmentPart att = (AttachmentPart) i.next();
                Object obj = att.getContent();
                if (!(obj instanceof StreamSource)) {
                    fail("Got incorrect attachment type [" + obj.getClass() + "], " +
                         "expected [javax.xml.transform.stream.StreamSource]");
                }
            }
        }

    }
 
Example 4
Source File: AbstractXTeeAxisEndpoint.java    From j-road with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected void invokeInternalEx(XTeeMessage<Document> request,
                                XTeeMessage<Element> response,
                                SOAPMessage requestMessage,
                                SOAPMessage responseMessage) throws Exception {
  requestMessage.getSOAPHeader().detachNode();
  Node bodyNode;
  if (XRoadProtocolVersion.V2_0 == version) {
    bodyNode = SOAPUtil.getNodeByXPath(requestMessage.getSOAPBody(), "//keha");
  } else {
    bodyNode = requestMessage.getSOAPBody();
  }
  Node reqNode = bodyNode.getParentNode();
  NodeList nl = bodyNode.getChildNodes();
  for (int i = 0; i < nl.getLength(); i++) {
    Node curNode = nl.item(i);
    reqNode.appendChild(curNode.cloneNode(true));
  }
  reqNode.removeChild(bodyNode);

  // Since Axis needs the XML as a String a transformation is required.
  StringResult result = new StringResult();
  Transformer transformer = TransformerFactory.newInstance().newTransformer();
  transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
  transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
  transformer.transform(new DOMSource(requestMessage.getSOAPPart().getEnvelope()), result);

  Message axisMessage = new Message(result.toString());
  // The context is very important, as the binding stub creates all the type bindings for proper unmarshalling.
  axisMessage.setMessageContext(getContextHelper().getMessageContext());

  // Adding the attachments is needed to handle "href" attributes where the data is in an attachment.
  for (Iterator<AttachmentPart> i = requestMessage.getAttachments(); i.hasNext();) {
    axisMessage.addAttachmentPart(i.next());
  }

  XTeeMessage<P> axisRequestMessage = new BeanXTeeMessage<P>(request.getHeader(),
                                                             (P) axisMessage.getSOAPEnvelope().getFirstBody().getObjectValue(getParingKehaClass()),
                                                             request.getAttachments());
  XTeeMessage<V> axisResponseMessage =
      new BeanXTeeMessage<V>(response.getHeader(), null, new ArrayList<XTeeAttachment>());

  invoke(axisRequestMessage, axisResponseMessage);

  V responseBean = axisResponseMessage.getContent();
  // If response is null we return <keha/>, otherwise some marshalling needs to be done.
  if (responseBean != null) {
    String responseXml = AxisUtil.serialize(responseBean);
    Document doc =
        DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(responseXml.getBytes("UTF-8")));
    Node parent = response.getContent().getParentNode();
    parent.removeChild(response.getContent());
    parent.appendChild(parent.getOwnerDocument().importNode(doc.getFirstChild(), true));
  }
}