Example usage for javax.xml.soap AttachmentPart setContent

List of usage examples for javax.xml.soap AttachmentPart setContent

Introduction

In this page you can find the example usage for javax.xml.soap AttachmentPart setContent.

Prototype

public abstract void setContent(Object object, String contentType);

Source Link

Document

Sets the content of this attachment part to that of the given Object and sets the value of the Content-Type header to the given type.

Usage

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

@Validated
@Test//www .  j  av a2  s . c o  m
public void testStringAttachment() throws Exception {

    MessageFactory factory = MessageFactory.newInstance();
    SOAPMessage message = factory.createMessage();
    AttachmentPart attachment = message.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");
    message.addAttachmentPart(attachment);

    assertTrue(message.countAttachments() == 1);

    Iterator it = message.getAttachments();
    while (it.hasNext()) {
        attachment = (AttachmentPart) it.next();
        Object content = attachment.getContent();
        String id = attachment.getContentId();
        assertEquals(content, stringContent);
    }
    message.removeAllAttachments();
    assertTrue(message.countAttachments() == 0);
}

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

@Validated
@Test//from  w  w w  . jav  a2  s . c o  m
public void testClearContent() throws Exception {
    try {
        InputStream in1 = TestUtils.getTestFile("attach.xml");

        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage message = factory.createMessage();
        AttachmentPart ap = message.createAttachmentPart();
        MimeHeader mh = null;

        //Setting Mime Header
        ap.setMimeHeader("Content-Description", "some text");

        //Setting Content Id Header
        ap.setContentId("id@abc.com");

        //Setting Content
        ap.setContent(new StreamSource(in1), "text/xml");

        //Clearing Content
        ap.clearContent();

        try {

            //Getting Content
            InputStream is = (InputStream) ap.getContent();
            fail("Error: SOAPException should have been thrown");
        } catch (SOAPException e) {
            //Error thrown.(expected)
        }

        Iterator iterator = ap.getAllMimeHeaders();
        int cnt = 0;
        boolean foundHeader1 = false;
        boolean foundHeader2 = false;
        boolean foundDefaultHeader = false;
        while (iterator.hasNext()) {
            cnt++;
            mh = (MimeHeader) iterator.next();
            String name = mh.getName();
            String value = mh.getValue();
            if (name.equals("Content-Description") && value.equals("some text")) {
                if (!foundHeader1) {
                    foundHeader1 = true;
                    //MimeHeaders do match for header1
                } else {
                    fail("Error: Received the same header1 header twice");
                }
            } else if (name.equalsIgnoreCase("Content-Id") && value.equals("id@abc.com")) {
                if (!foundHeader2) {
                    foundHeader2 = true;
                    //MimeHeaders do match for header2
                } else {
                    fail("Error: Received the same header2 header twice");
                }
            } else if (name.equals("Content-Type") && value.equals("text/xml")) {
                if (!foundDefaultHeader) {
                    foundDefaultHeader = true;
                    //MimeHeaders do match for default header
                } else {
                    fail("Error: Received the same default header header twice");
                }
            } else {
                fail("Error: Received an invalid header");
            }
        }

        if (!(foundHeader1 && foundHeader2)) {
            fail("Error: did not receive both headers");
        }

    } catch (Exception e) {
        fail("Exception: " + e);
    }

}

From source file:test.saaj.TestAttachmentSerialization.java

public int saveMsgWithAttachments(OutputStream os) throws Exception {
    MessageFactory mf = MessageFactory.newInstance();
    SOAPMessage msg = mf.createMessage();

    SOAPPart sp = msg.getSOAPPart();
    SOAPEnvelope envelope = sp.getEnvelope();
    SOAPHeader header = envelope.getHeader();
    SOAPBody body = envelope.getBody();

    SOAPElement el = header.addHeaderElement(envelope.createName("field4", NS_PREFIX, NS_URI));
    SOAPElement el2 = el.addChildElement("field4b", NS_PREFIX);
    SOAPElement el3 = el2.addTextNode("field4value");

    el = body.addBodyElement(envelope.createName("bodyfield3", NS_PREFIX, NS_URI));
    el2 = el.addChildElement("bodyfield3a", NS_PREFIX);
    el2.addTextNode("bodyvalue3a");
    el2 = el.addChildElement("bodyfield3b", NS_PREFIX);
    el2.addTextNode("bodyvalue3b");
    el2 = el.addChildElement("datefield", NS_PREFIX);

    AttachmentPart ap = msg.createAttachmentPart();
    ap.setContent("some attachment text...", "text/plain");
    msg.addAttachmentPart(ap);//from  w ww.  ja v a 2 s.  c  o m

    String jpgfilename = "docs/images/axis.jpg";
    File myfile = new File(jpgfilename);
    FileDataSource fds = new FileDataSource(myfile);
    DataHandler dh = new DataHandler(fds);
    AttachmentPart ap2 = msg.createAttachmentPart(dh);
    ap2.setContentType("image/jpg");
    msg.addAttachmentPart(ap2);

    // Test for Bug #17664
    if (msg.saveRequired()) {
        msg.saveChanges();
    }
    MimeHeaders headers = msg.getMimeHeaders();
    assertTrue(headers != null);
    String[] contentType = headers.getHeader("Content-Type");
    assertTrue(contentType != null);

    msg.writeTo(os);
    os.flush();
    return msg.countAttachments();
}