Example usage for javax.xml.soap AttachmentPart clearContent

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

Introduction

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

Prototype

public abstract void clearContent();

Source Link

Document

Clears out the content of this AttachmentPart object.

Usage

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

@Validated
@Test/*from  w ww.j  a  v a 2  s.c om*/
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);
    }

}