Example usage for javax.xml.soap SOAPMessage getAttachments

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

Introduction

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

Prototype

public abstract Iterator<AttachmentPart> getAttachments();

Source Link

Document

Retrieves all the AttachmentPart objects that are part of this SOAPMessage object.

Usage

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/*  w w  w.j ava  2  s.  co  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.jaxws.message.impl.MessageImpl.java

public SOAPMessage getAsSOAPMessage() throws WebServiceException {

    // TODO: /* w ww.j  av  a 2  s .  c o m*/
    // This is a non performant way to create SOAPMessage. I will serialize
    // the xmlpart content and then create an InputStream of byte.
    // Finally create SOAPMessage using this InputStream.
    // The real solution may involve using non-spec, implementation
    // constructors to create a Message from an Envelope
    try {
        if (log.isDebugEnabled()) {
            log.debug("start getAsSOAPMessage");
        }
        // Get OMElement from XMLPart.
        OMElement element = xmlPart.getAsOMElement();

        // Get the namespace so that we can determine SOAP11 or SOAP12
        OMNamespace ns = element.getNamespace();

        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        element.serialize(outStream);

        // In some cases (usually inbound) the builder will not be closed after
        // serialization.  In that case it should be closed manually.
        if (element.getBuilder() != null && !element.getBuilder().isCompleted()) {
            element.close(false);
        }

        byte[] bytes = outStream.toByteArray();

        if (log.isDebugEnabled()) {
            String text = new String(bytes);
            log.debug("  inputstream = " + text);
        }

        // Create InputStream
        ByteArrayInputStream inStream = new ByteArrayInputStream(bytes);

        // Create MessageFactory that supports the version of SOAP in the om element
        MessageFactory mf = getSAAJConverter().createMessageFactory(ns.getNamespaceURI());

        // Create soapMessage object from Message Factory using the input
        // stream created from OM.

        // Get the MimeHeaders from the transportHeaders map
        MimeHeaders defaultHeaders = new MimeHeaders();
        if (transportHeaders != null) {
            Iterator it = transportHeaders.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry entry = (Map.Entry) it.next();
                String key = (String) entry.getKey();
                if (entry.getValue() == null) {
                    // This is not necessarily a problem; log it and make sure not to NPE
                    if (log.isDebugEnabled()) {
                        log.debug(
                                "  Not added to transport header. header =" + key + " because value is null;");
                    }
                } else if (entry.getValue() instanceof String) {
                    // Normally there is one value per key
                    if (log.isDebugEnabled()) {
                        log.debug("  add transport header. header =" + key + " value = " + entry.getValue());
                    }
                    defaultHeaders.addHeader(key, (String) entry.getValue());
                } else {
                    // There may be multiple values for each key.  This code
                    // assumes the value is an array of String.
                    String values[] = (String[]) entry.getValue();
                    for (int i = 0; i < values.length; i++) {
                        if (log.isDebugEnabled()) {
                            log.debug("  add transport header. header =" + key + " value = " + values[i]);
                        }
                        defaultHeaders.addHeader(key, values[i]);
                    }
                }
            }
        }

        // Toggle based on SOAP 1.1 or SOAP 1.2
        String contentType = null;
        if (ns.getNamespaceURI().equals(SOAPConstants.URI_NS_SOAP_1_1_ENVELOPE)) {
            contentType = SOAPConstants.SOAP_1_1_CONTENT_TYPE;
        } else {
            contentType = SOAPConstants.SOAP_1_2_CONTENT_TYPE;
        }

        // Override the content-type
        String ctValue = contentType + "; charset=UTF-8";
        defaultHeaders.setHeader("Content-type", ctValue);
        if (log.isDebugEnabled()) {
            log.debug("  setContentType =" + ctValue);
        }
        SOAPMessage soapMessage = mf.createMessage(defaultHeaders, inStream);

        // At this point the XMLPart is still an OMElement.  
        // We need to change it to the new SOAPEnvelope.
        createXMLPart(soapMessage.getSOAPPart().getEnvelope());

        // If axiom read the message from the input stream, 
        // then one of the attachments is a SOAPPart.  Ignore this attachment
        String soapPartContentID = getSOAPPartContentID(); // This may be null

        if (log.isDebugEnabled()) {
            log.debug("  soapPartContentID =" + soapPartContentID);
        }

        List<String> dontCopy = new ArrayList<String>();
        if (soapPartContentID != null) {
            dontCopy.add(soapPartContentID);
        }

        // Add any new attachments from the SOAPMessage to this Message
        Iterator it = soapMessage.getAttachments();
        while (it.hasNext()) {

            AttachmentPart ap = (AttachmentPart) it.next();
            String cid = ap.getContentId();
            if (log.isDebugEnabled()) {
                log.debug("  add SOAPMessage attachment to Message.  cid = " + cid);
            }
            addDataHandler(ap.getDataHandler(), cid);
            dontCopy.add(cid);
        }

        // Add the attachments from this Message to the SOAPMessage
        for (String cid : getAttachmentIDs()) {
            DataHandler dh = attachments.getDataHandler(cid);
            if (!dontCopy.contains(cid)) {
                if (log.isDebugEnabled()) {
                    log.debug("  add Message attachment to SoapMessage.  cid = " + cid);
                }
                AttachmentPart ap = MessageUtils.createAttachmentPart(cid, dh, soapMessage);
                soapMessage.addAttachmentPart(ap);
            }
        }

        if (log.isDebugEnabled()) {
            log.debug("  The SOAPMessage has the following attachments");
            Iterator it2 = soapMessage.getAttachments();
            while (it2.hasNext()) {
                AttachmentPart ap = (AttachmentPart) it2.next();
                log.debug("    AttachmentPart cid=" + ap.getContentId());
                log.debug("        contentType =" + ap.getContentType());
            }
        }

        if (log.isDebugEnabled()) {
            log.debug("end getAsSOAPMessage");
        }
        return soapMessage;
    } catch (Exception e) {
        throw ExceptionFactory.makeWebServiceException(e);
    }

}

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

@Validated
@Test//from  w  ww .  jav  a 2  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.mule.transport.soap.axis.AxisMuleMessageFactory.java

@Override
protected void addAttachments(DefaultMuleMessage message, Object transportMessage) throws Exception {
    MessageContext ctx = MessageContext.getCurrentContext();
    if (ctx == null) {
        return;/*from w w w .  j av  a  2  s.c  o  m*/
    }

    try {
        SOAPMessage soapMessage = ctx.getMessage();
        int x = 1;
        for (Iterator<?> i = soapMessage.getAttachments(); i.hasNext(); x++) {
            String name = String.valueOf(x);
            AttachmentPart attachmentPart = (AttachmentPart) i.next();
            message.addOutboundAttachment(name, attachmentPart.getActivationDataHandler());
        }
    } catch (Exception e) {
        // this will not happen
        log.fatal("Failed to read attachments", e);
    }
}