Example usage for javax.xml.soap AttachmentPart getBase64Content

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

Introduction

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

Prototype

public abstract InputStream getBase64Content() throws SOAPException;

Source Link

Document

Returns an InputStream which can be used to obtain the content of AttachmentPart as Base64 encoded character data, this method would base64 encode the raw bytes of the attachment and return.

Usage

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

@Validated
@Test//from ww  w  . j  a  v a  2 s  .com
public void testSetBase64Content() {
    try {
        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage msg = factory.createMessage();
        AttachmentPart ap = msg.createAttachmentPart();

        String urlString = "http://ws.apache.org/images/project-logo.jpg";
        if (isNetworkedResourceAvailable(urlString)) {
            URL url = new URL(urlString);
            DataHandler dh = new DataHandler(url);
            //Create InputStream from DataHandler's InputStream
            InputStream is = dh.getInputStream();

            byte buf[] = IOUtils.getStreamAsByteArray(is);
            //Setting Content via InputStream for image/jpeg mime type
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            Base64.encode(buf, 0, buf.length, bos);
            buf = bos.toByteArray();
            InputStream stream = new ByteArrayInputStream(buf);
            ap.setBase64Content(stream, "image/jpeg");

            //Getting Content.. should return InputStream object
            InputStream r = ap.getBase64Content();
            if (r != null) {
                if (r instanceof InputStream) {
                    //InputStream object was returned (ok)
                } else {
                    fail("Unexpected object was returned");
                }
            }
        }
    } catch (Exception e) {
        fail("Exception: " + e);
    }
}