Example usage for javax.xml.soap SOAPMessage getAttachments

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

Introduction

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

Prototype

public abstract Iterator<AttachmentPart> getAttachments(MimeHeaders headers);

Source Link

Document

Retrieves all the AttachmentPart objects that have header entries that match the specified headers.

Usage

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

@Validated
@Test/*from  w  ww. j  a v a2 s  .c  o  m*/
public void testGetAttachmentsFiltered() throws Exception {
    SOAPMessage message = getFactory().createMessage();

    AttachmentPart att1 = message.createAttachmentPart();
    att1.addMimeHeader("Content-Type", "text/plain");
    message.addAttachmentPart(att1);

    AttachmentPart att2 = message.createAttachmentPart();
    att2.addMimeHeader("Content-Type", "application/octet-stream");
    message.addAttachmentPart(att2);

    AttachmentPart att3 = message.createAttachmentPart();
    att3.addMimeHeader("Content-ID", "<123456@example.com>");
    att3.addMimeHeader("Content-Type", "text/plain");
    message.addAttachmentPart(att3);

    MimeHeaders headers = new MimeHeaders();
    headers.addHeader("Content-Type", "text/plain");
    Iterator it = message.getAttachments(headers);
    assertTrue(it.hasNext());
    assertSame(att1, it.next());
    assertTrue(it.hasNext());
    assertSame(att3, it.next());
    assertFalse(it.hasNext());
}

From source file:org.apache.axis2.saaj.AttachmentTest.java

@Validated
@Test//from www. j av  a 2  s. com
public void testMultipleAttachments() throws Exception {

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

    AttachmentPart a1 = msg.createAttachmentPart(new DataHandler("<some_xml/>", "text/xml"));
    a1.setContentType("text/xml");
    msg.addAttachmentPart(a1);
    AttachmentPart a2 = msg.createAttachmentPart(new DataHandler("<some_xml/>", "text/xml"));
    a2.setContentType("text/xml");
    msg.addAttachmentPart(a2);
    AttachmentPart a3 = msg.createAttachmentPart(new DataHandler("text", "text/plain"));
    a3.setContentType("text/plain");
    msg.addAttachmentPart(a3);

    assertTrue(msg.countAttachments() == 3);

    MimeHeaders mimeHeaders = new MimeHeaders();
    mimeHeaders.addHeader("Content-Type", "text/xml");

    int nAttachments = 0;
    Iterator iterator = msg.getAttachments(mimeHeaders);
    while (iterator.hasNext()) {
        nAttachments++;
        AttachmentPart ap = (AttachmentPart) iterator.next();
        assertTrue(ap.equals(a1) || ap.equals(a2));
    }
    assertTrue(nAttachments == 2);
}