Example usage for javax.xml.soap SOAPMessage countAttachments

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

Introduction

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

Prototype

public abstract int countAttachments();

Source Link

Document

Gets a count of the number of attachments in this message.

Usage

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

@Validated
@Test//ww w  .j  av a 2s  .c  om
public void testCreateAttachmentPart() throws Exception {
    SOAPMessage message = getFactory().createMessage();
    message.createAttachmentPart();
    // The attachment part is not added to the message
    assertEquals(0, message.countAttachments());
}

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

@Validated
@Test/*from w w w. j a v a 2  s.  co m*/
public final void testRemoveAllAttachments() throws Exception {
    SOAPMessage message = getFactory().createMessage();
    for (int i = 0; i < 5; i++) {
        AttachmentPart att = message.createAttachmentPart("test" + i, "text/plain");
        message.addAttachmentPart(att);
    }
    assertEquals(5, message.countAttachments());
    message.removeAllAttachments();
    assertEquals(0, message.countAttachments());
    if (message.saveRequired()) {
        message.saveChanges();
    }
    String[] contentType = message.getMimeHeaders().getHeader("Content-Type");
    assertNotNull(contentType);
    assertEquals(1, contentType.length);
    assertEquals(messageSet.getVersion().getContentType(), new MimeType(contentType[0]).getBaseType());
}

From source file:it.cnr.icar.eric.common.SOAPMessenger.java

/**
 * @return  HashMap containing {contentId, DataHandler} entries or null
 *          if no attachments/*from   w  w  w  .j  av a  2s. c  om*/
 */
private HashMap<String, Object> processResponseAttachments(SOAPMessage response)
        throws JAXRException, SOAPException, MessagingException {
    if (response.countAttachments() == 0) {
        return null;
    }

    HashMap<String, Object> attachMap = new HashMap<String, Object>();

    for (Iterator<?> it = response.getAttachments(); it.hasNext();) {
        AttachmentPart ap = (AttachmentPart) it.next();

        String uuid = WSS4JSecurityUtilSAML.convertContentIdToUUID(ap.getContentId());
        if (log.isTraceEnabled()) {
            log.trace("Processing attachment w/ contentId=" + uuid);
        }

        DataHandler dh = ap.getDataHandler();
        attachMap.put(uuid, dh);
    }

    return attachMap;
}

From source file:org.apache.axis2.jaxws.handler.SoapMessageContext.java

/**
 * Updates information about the SOAPMessage so that
 * we can determine later if it has changed
 * @param sm SOAPMessage/*from  w  w w .  j a  v a  2s  . co m*/
 */
private void cacheSOAPMessageInfo(SOAPMessage sm) {
    cachedSoapPart = null;
    cachedSoapEnvelope = null;
    cachedAttachmentParts.clear();
    try {
        cachedSoapPart = sm.getSOAPPart();
        if (cachedSoapPart != null) {
            cachedSoapEnvelope = cachedSoapPart.getEnvelope();
        }
        if (sm.countAttachments() > 0) {
            Iterator it = sm.getAttachments();
            while (it != null && it.hasNext()) {
                AttachmentPart ap = (AttachmentPart) it.next();
                cachedAttachmentParts.add(ap);
            }
        }
    } catch (Throwable t) {
        if (log.isDebugEnabled()) {
            log.debug("Ignoring ", t);
        }
    }
}

From source file:org.apache.axis2.jaxws.handler.SoapMessageContext.java

/**
 * Checks the information in SOAPMessage sm against 
 * the information previously cached.  If an exception occurs
 * @param sm SOAPMessage/*from ww w .j a v  a 2s  . c o  m*/
 * @return true if match , (exceptions are interpeted as false)
 */
private boolean checkSOAPMessageInfo(SOAPMessage sm) {
    if (log.isDebugEnabled()) {
        log.debug("checkSOAPMessageInfo with " + JavaUtils.getObjectIdentity(sm));
    }
    // Check SOAPPart and SOAPEnvelope identity
    SOAPPart currentSoapPart = null;
    SOAPEnvelope currentSoapEnvelope = null;

    try {
        currentSoapPart = sm.getSOAPPart();
        if (currentSoapPart != null) {
            currentSoapEnvelope = cachedSoapPart.getEnvelope();
        }
        // Check object identity
        if (cachedSoapPart != currentSoapPart) {
            if (log.isDebugEnabled()) {
                log.debug("checkSOAPMessageInfo returns false due to: mismatched SOAPParts");
            }
            return false;
        }
        if (cachedSoapEnvelope != currentSoapEnvelope) {
            if (log.isDebugEnabled()) {
                log.debug("checkSOAPMessageInfo returns false due to: mismatched SOAPEnvelopes");
            }
            return false;
        }
    } catch (Throwable t) {
        if (log.isDebugEnabled()) {
            log.debug("checkSOAPMessageInfo returns false due to: ", t);
        }
    }

    // Check AttachmentParts
    try {
        int currentNumAttachmentParts = sm.countAttachments();
        if (currentNumAttachmentParts != cachedAttachmentParts.size()) {
            if (log.isDebugEnabled()) {
                log.debug("checkSOAPMessageInfo returns false due to: "
                        + "current number of AttachmentParts is " + currentNumAttachmentParts
                        + " versus cached number is " + cachedAttachmentParts.size());
            }
            return false;
        }
        if (currentNumAttachmentParts > 0) {
            if (log.isDebugEnabled()) {
                log.debug("checkSOAPMessageInfo detected " + currentNumAttachmentParts + "AttachmentParts");
            }
            Iterator cachedIT = cachedAttachmentParts.iterator();
            Iterator currentIT = sm.getAttachments();
            while (currentIT.hasNext() && cachedIT.hasNext()) {
                AttachmentPart currentAP = (AttachmentPart) currentIT.next();
                AttachmentPart cachedAP = (AttachmentPart) cachedIT.next();
                if (currentAP != cachedAP) {
                    if (log.isDebugEnabled()) {
                        log.debug("checkSOAPMessageInfo returns false due to: " + "current AttachmentParts is "
                                + JavaUtils.getObjectIdentity(currentAP) + " and cached is "
                                + JavaUtils.getObjectIdentity(cachedAP));
                    }
                    return false;
                }
            }
        }
    } catch (Throwable t) {
        if (log.isDebugEnabled()) {
            log.debug("checkSOAPMessageInfo returns false due to: ", t);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("checkSOAPMessageInfo returns true");
    }
    return true;
}

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

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

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);// w w  w  .  j a  va2s .co  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();
}

From source file:test.saaj.TestAttachmentSerialization.java

public int loadMsgWithAttachments(InputStream is) throws Exception {
    MimeHeaders headers = new MimeHeaders();
    headers.setHeader("Content-Type", MIME_MULTIPART_RELATED);
    MessageFactory mf = MessageFactory.newInstance();
    SOAPMessage msg = mf.createMessage(headers, is);
    SOAPPart sp = msg.getSOAPPart();
    SOAPEnvelope envelope = sp.getEnvelope();
    assertTrue(sp != null);/* www  .  j av  a2 s . c om*/
    assertTrue(envelope != null);
    return msg.countAttachments();
}