Example usage for javax.xml.soap AttachmentPart getContent

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

Introduction

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

Prototype

public abstract Object getContent() throws SOAPException;

Source Link

Document

Gets the content of this AttachmentPart object as a Java object.

Usage

From source file:it.cnr.icar.eric.common.cms.AbstractService.java

protected HashMap<String, DataHandler> getRepositoryItemDHMap() throws Exception {
    HashMap<String, DataHandler> repositoryItemDHMap = new HashMap<String, DataHandler>();
    MessageContext mc = servletEndpointContext.getMessageContext();
    Collection<?> attachments = (Collection<?>) mc
            .getProperty(com.sun.xml.rpc.server.ServerPropertyConstants.GET_ATTACHMENT_PROPERTY);

    Iterator<?> attachmentsIter = attachments.iterator();

    while (attachmentsIter.hasNext()) {
        Object obj = attachmentsIter.next();

        System.err.println("getRepositoryItems:: Attachment: " + obj.getClass().getName());

        if (obj instanceof AttachmentPart) {
            AttachmentPart ap = (AttachmentPart) obj;

            String contentId = WSS4JSecurityUtilBST.convertContentIdToUUID(ap.getContentId());
            String contentType = ap.getContentType();

            System.err/*from   ww w.  j  a va 2s. c om*/
                    .println("getRepositoryItems:: contentId: " + contentId + "; contentType: " + contentType);
            if (log.isDebugEnabled()) {
                log.debug("Processing attachment (RepositoryItem):\n" + ap.getContent().toString());
            }

            DataHandler dh = ap.getDataHandler();

            repositoryItemDHMap.put(contentId, dh);
        }
    }

    return repositoryItemDHMap;
}

From source file:it.cnr.icar.eric.server.cms.ContentCatalogingServiceImpl.java

private RepositoryItem processIncomingAttachment(AttachmentPart ap) throws Exception {
    DataHandler dh = null;// ww w .  jav  a 2s . c  om

    //ContentId is the id of the repositoryItem (CID UIR
    String id = WSS4JSecurityUtilBST.convertContentIdToUUID(ap.getContentId());

    if (log.isInfoEnabled()) {
        log.info(ServerResourceBundle.getInstance().getString("message.ProcessingAttachmentWithContentId",
                new Object[] { id }));
    }

    if (log.isDebugEnabled()) {
        log.debug("Processing attachment (RepositoryItem):\n" + ap.getContent().toString());
    }

    dh = ap.getDataHandler();
    return new RepositoryItemImpl(id, dh);
}

From source file:it.cnr.icar.eric.server.interfaces.soap.RegistryBSTServlet.java

private RepositoryItem processIncomingAttachment(AttachmentPart ap) throws RegistryException {
    RepositoryItem ri = null;/*  w  ww  .j a va  2 s  . c  om*/

    try {
        //ContentId is the id of the repositoryItem
        String id = WSS4JSecurityUtilBST.convertContentIdToUUID(ap.getContentId());
        if (log.isInfoEnabled()) {
            log.info(ServerResourceBundle.getInstance().getString("message.ProcessingAttachmentWithContentId",
                    new Object[] { id }));
        }

        if (log.isDebugEnabled()) {
            log.debug("Processing attachment (RepositoryItem):\n" + ap.getContent().toString());
        }

        DataHandler dh = ap.getDataHandler();
        ri = new RepositoryItemImpl(id, dh);
    } catch (SOAPException e) {
        RegistryException toThrow = new RegistryException(e);
        toThrow.initCause(e);
        throw toThrow;
    }

    return ri;
}

From source file:it.cnr.icar.eric.server.interfaces.soap.RegistrySAMLServlet.java

/**
 * This method is a copy of the respective method from RegistrySOAPServlet.
 *///from  w  w w.  ja va  2s . c o m

private RepositoryItem processIncomingAttachment(AttachmentPart ap) throws RegistryException {

    RepositoryItem ri = null;
    try {
        // ContentId is the id of the repositoryItem
        String id = WSS4JSecurityUtilSAML.convertContentIdToUUID(ap.getContentId());
        if (log.isInfoEnabled()) {
            log.info(ServerResourceBundle.getInstance().getString("message.ProcessingAttachmentWithContentId",
                    new Object[] { id }));
        }

        if (log.isDebugEnabled()) {
            log.debug("Processing attachment (RepositoryItem):\n" + ap.getContent().toString());
        }

        DataHandler dh = ap.getDataHandler();
        ri = new RepositoryItemImpl(id, dh);

    } catch (SOAPException e) {

        RegistryException toThrow = new RegistryException(e);
        toThrow.initCause(e);
        throw toThrow;

    }

    return ri;
}

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

@Validated
@Test/*from   w  w w  . j  a v  a2  s.co  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/*  ww w . j  av  a 2s.  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:org.apache.axis2.saaj.AttachmentTest.java

@Validated
@Test/* www . jav  a  2 s .  c  om*/
public void testGetContent() throws Exception {
    try {
        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage msg = factory.createMessage();
        AttachmentPart ap = msg.createAttachmentPart();
        Image image = ImageIO.read(TestUtils.getTestFileURL("attach.gif"));
        ap = msg.createAttachmentPart(image, "image/gif");

        //Getting Content should return an Image object
        Object o = ap.getContent();
        if (o != null) {
            if (o instanceof Image) {
                //Image object was returned (ok)
            } else {
                fail("Unexpected object was returned");
            }
        }
    } catch (Exception e) {
        fail("Exception: " + e);
    }
}