You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by sc...@apache.org on 2008/07/18 17:04:00 UTC

svn commit: r677926 - in /webservices/axis2/trunk/java/modules/jaxws: src/org/apache/axis2/jaxws/message/impl/ src/org/apache/axis2/jaxws/message/util/ src/org/apache/axis2/jaxws/message/util/impl/ test/org/apache/axis2/jaxws/attachments/

Author: scheu
Date: Fri Jul 18 08:04:00 2008
New Revision: 677926

URL: http://svn.apache.org/viewvc?rev=677926&view=rev
Log:
AXIS2-3925
Contributor: Rich Scheuerle
Added code to SAAJConverter.toOM that uses an existing AttachmentMap to properly associate XOPInclude nodes
with their corresponding DataHandler.
Added more trace
Changed the MTOMSerializationTest to validate this fix.

Modified:
    webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/impl/MessageImpl.java
    webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/impl/XMLPartBase.java
    webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/impl/XMLPartImpl.java
    webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/util/SAAJConverter.java
    webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/util/impl/SAAJConverterImpl.java
    webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/attachments/MTOMSerializationTests.java

Modified: webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/impl/MessageImpl.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/impl/MessageImpl.java?rev=677926&r1=677925&r2=677926&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/impl/MessageImpl.java (original)
+++ webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/impl/MessageImpl.java Fri Jul 18 08:04:00 2008
@@ -59,6 +59,7 @@
 import javax.xml.ws.WebServiceException;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -181,6 +182,9 @@
         // 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();
             
@@ -190,9 +194,15 @@
             ByteArrayOutputStream outStream = new ByteArrayOutputStream();
             element.serialize(outStream);
             
+            byte[] bytes = outStream.toByteArray();
+            
+            if (log.isDebugEnabled()) {
+                String text = new String(bytes);
+                log.debug("  inputstream = " + text);
+            }
+            
             // Create InputStream
-            ByteArrayInputStream inStream = new ByteArrayInputStream(outStream
-                    .toByteArray());
+            ByteArrayInputStream inStream = new ByteArrayInputStream(bytes);
             
             // Create MessageFactory that supports the version of SOAP in the om element
             MessageFactory mf = getSAAJConverter().createMessageFactory(ns.getNamespaceURI());
@@ -209,12 +219,20 @@
                     String key = (String) entry.getKey();
                     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]);
                         }
                     }
@@ -230,7 +248,11 @@
             }
             
             // Override the content-type
-            defaultHeaders.setHeader("Content-type", contentType +"; charset=UTF-8");
+            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.  
@@ -241,16 +263,53 @@
             // then one of the attachments is a SOAPPart.  Ignore this attachment
             String soapPartContentID = getSOAPPartContentID();  // This may be null
             
-            // Add the attachments
+            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);
-                boolean isSOAPPart = cid.equals(soapPartContentID);
-                if (!isSOAPPart) {
+                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);

Modified: webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/impl/XMLPartBase.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/impl/XMLPartBase.java?rev=677926&r1=677925&r2=677926&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/impl/XMLPartBase.java (original)
+++ webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/impl/XMLPartBase.java Fri Jul 18 08:04:00 2008
@@ -110,7 +110,7 @@
 
     boolean consumed = false;
 
-    Message parent;
+    MessageImpl parent;
 
 
     /**
@@ -673,7 +673,7 @@
      * Set the backpointer to this XMLPart's parent Message
      */
     public void setParent(Message p) {
-        parent = p;
+        parent = (MessageImpl) p;
     }
 
     /**

Modified: webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/impl/XMLPartImpl.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/impl/XMLPartImpl.java?rev=677926&r1=677925&r2=677926&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/impl/XMLPartImpl.java (original)
+++ webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/impl/XMLPartImpl.java Fri Jul 18 08:04:00 2008
@@ -19,7 +19,9 @@
 
 package org.apache.axis2.jaxws.message.impl;
 
+import org.apache.axiom.attachments.Attachments;
 import org.apache.axiom.om.OMElement;
+import org.apache.axis2.jaxws.message.Message;
 import org.apache.axis2.jaxws.message.Protocol;
 import org.apache.axis2.jaxws.message.factory.SAAJConverterFactory;
 import org.apache.axis2.jaxws.message.util.SAAJConverter;
@@ -76,7 +78,9 @@
 
     @Override
     protected OMElement _convertSE2OM(SOAPEnvelope se) throws WebServiceException {
-        return getSAAJConverter().toOM(se);
+        Attachments attachments = (parent == null) ? 
+                null : parent.attachments;
+        return getSAAJConverter().toOM(se, attachments);
     }
 
     @Override

Modified: webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/util/SAAJConverter.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/util/SAAJConverter.java?rev=677926&r1=677925&r2=677926&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/util/SAAJConverter.java (original)
+++ webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/util/SAAJConverter.java Fri Jul 18 08:04:00 2008
@@ -19,6 +19,7 @@
 
 package org.apache.axis2.jaxws.message.util;
 
+import org.apache.axiom.attachments.Attachments;
 import org.apache.axiom.om.OMElement;
 
 import javax.xml.soap.MessageFactory;
@@ -49,6 +50,18 @@
      */
     public org.apache.axiom.soap.SOAPEnvelope toOM(SOAPEnvelope saajEnvelope)
             throws WebServiceException;
+    
+    /**
+     * Convert SAAJ SOAPEnvelope to OM SOAPEnvelope
+     *
+     * @param saajEnvelope
+     * @param Attachments
+     * @return OM Envelope
+     * @throws WebServiceException
+     */
+    public org.apache.axiom.soap.SOAPEnvelope toOM(SOAPEnvelope saajEnvelope, 
+                                                   Attachments attachments)
+            throws WebServiceException;
 
     /**
      * Convert SOAPElement into an OMElement

Modified: webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/util/impl/SAAJConverterImpl.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/util/impl/SAAJConverterImpl.java?rev=677926&r1=677925&r2=677926&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/util/impl/SAAJConverterImpl.java (original)
+++ webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/util/impl/SAAJConverterImpl.java Fri Jul 18 08:04:00 2008
@@ -19,6 +19,7 @@
 
 package org.apache.axis2.jaxws.message.util.impl;
 
+import org.apache.axiom.attachments.Attachments;
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMException;
 import org.apache.axiom.om.OMNamespace;
@@ -27,6 +28,7 @@
 import org.apache.axiom.om.util.StAXUtils;
 import org.apache.axiom.soap.SOAP11Constants;
 import org.apache.axiom.soap.SOAP12Constants;
+import org.apache.axiom.soap.impl.builder.MTOMStAXSOAPModelBuilder;
 import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
 import org.apache.axis2.jaxws.ExceptionFactory;
 import org.apache.axis2.jaxws.i18n.Messages;
@@ -116,18 +118,28 @@
     /* (non-Javadoc)
       * @see org.apache.axis2.jaxws.message.util.SAAJConverter#toOM(javax.xml.soap.SOAPEnvelope)
       */
-    public org.apache.axiom.soap.SOAPEnvelope toOM(SOAPEnvelope saajEnvelope)
+    public org.apache.axiom.soap.SOAPEnvelope toOM(SOAPEnvelope saajEnvelope) {
+        return toOM(saajEnvelope, null);
+    }
+    public org.apache.axiom.soap.SOAPEnvelope toOM(SOAPEnvelope saajEnvelope, 
+                                                   Attachments attachments)
             throws WebServiceException {
     	if (log.isDebugEnabled()) {
-    		log.debug("Converting SAAJ SOAPEnvelope to an OM SOAPEnvelope");
+    	    log.debug("Converting SAAJ SOAPEnvelope to an OM SOAPEnvelope");
     	}    	
     	
     	// Before we do the conversion, we have to fix the QNames for fault elements
         _fixFaultElements(saajEnvelope);        
         // Get a XMLStreamReader backed by a SOAPElement tree
         XMLStreamReader reader = new SOAPElementReader(saajEnvelope);
+        
         // Get a SOAP OM Builder.  Passing null causes the version to be automatically triggered
-        StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(reader, null);
+        StAXSOAPModelBuilder builder = null;
+        if (attachments == null) {
+            builder = new StAXSOAPModelBuilder(reader, null);
+        } else {
+            builder = new MTOMStAXSOAPModelBuilder(reader, attachments, null);
+        }
         // Create and return the OM Envelope
         org.apache.axiom.soap.SOAPEnvelope omEnvelope = builder.getSOAPEnvelope();
         

Modified: webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/attachments/MTOMSerializationTests.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/attachments/MTOMSerializationTests.java?rev=677926&r1=677925&r2=677926&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/attachments/MTOMSerializationTests.java (original)
+++ webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/attachments/MTOMSerializationTests.java Fri Jul 18 08:04:00 2008
@@ -159,9 +159,63 @@
                
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         soapOM.serializeAndConsume(baos, format);
+        String outputText = baos.toString();
+        // Make sure the attachment is serialized
+        assertTrue(outputText.indexOf("Content-Type: image/jpeg") > 0);
 
         TestLogger.logger.debug("==================================");
-        TestLogger.logger.debug(baos.toString());
+        TestLogger.logger.debug(outputText);
+        TestLogger.logger.debug("==================================");
+    }
+    
+    public void testMTOMAttachmentWriter2() throws Exception {
+        TestLogger.logger.debug("---------------------------------------");
+        TestLogger.logger.debug("test: " + getName());
+        
+        //Create a DataHandler with the String DataSource object
+        DataHandler dataHandler = new DataHandler(imageDS);
+                        
+        //Store the data handler in ImageDepot bean
+        ImageDepot imageDepot = new ObjectFactory().createImageDepot();
+        imageDepot.setImageData(dataHandler);
+        
+        //JAXBContext jbc = JAXBContext.newInstance("org.test.mtom");
+        JAXBBlockContext context = new JAXBBlockContext(SendImage.class.getPackage().getName());
+        
+        //Create a request bean with imagedepot bean as value
+        ObjectFactory factory = new ObjectFactory();
+        SendImage request = factory.createSendImage();
+        request.setInput(imageDepot);
+        
+        BlockFactory blkFactory = (JAXBBlockFactory) FactoryRegistry.getFactory(JAXBBlockFactory.class);
+        Block block = blkFactory.createFrom(request, context, null);
+        
+        MessageFactory msgFactory = (MessageFactory) FactoryRegistry.getFactory(MessageFactory.class);
+        Message msg = msgFactory.create(Protocol.soap11);
+        
+        msg.setBodyBlock(block);
+        
+        msg.setMTOMEnabled(true);
+        
+        // Convert message to SAAJ to simulate an outbound handler
+        msg.getAsSOAPMessage();
+        
+        // Now convert it back to AXIOM
+        
+        SOAPEnvelope soapOM = (SOAPEnvelope) msg.getAsOMElement();
+        
+        OMOutputFormat format = new OMOutputFormat();
+        format.setDoOptimize(true);
+        format.setSOAP11(true);
+               
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        soapOM.serializeAndConsume(baos, format);
+        String outputText = baos.toString();
+        // Make sure the attachment is serialized
+        assertTrue(outputText.indexOf("Content-Type: image/jpeg") > 0);
+
+        TestLogger.logger.debug("==================================");
+        TestLogger.logger.debug(outputText);
         TestLogger.logger.debug("==================================");
     }