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 ve...@apache.org on 2009/01/12 00:02:08 UTC

svn commit: r733533 - in /webservices/axis2/trunk/java/modules/saaj: src/org/apache/axis2/saaj/SOAPConnectionImpl.java test/org/apache/axis2/saaj/integration/EchoService.java test/org/apache/axis2/saaj/integration/IntegrationTest.java

Author: veithen
Date: Sun Jan 11 15:02:07 2009
New Revision: 733533

URL: http://svn.apache.org/viewvc?rev=733533&view=rev
Log:
Initial fix for AXIS2-4202 and AXIS2-4203 (needs some more cleanup and testing).

SOAPConnectionImpl:
* Distinguish between MTOM and SwA and only insert the DataHandlers in the MTOM case.
* In the SwA case, properly add the attachments in the request and retrieve them from the response.
* Removed the nonsense code that copied "unreferenced" attachments from request to response.

IntegrationTest:
* The Echo service now echoes MTOM and SwA messages correctly (including attachments).

Removed:
    webservices/axis2/trunk/java/modules/saaj/test/org/apache/axis2/saaj/integration/EchoService.java
Modified:
    webservices/axis2/trunk/java/modules/saaj/src/org/apache/axis2/saaj/SOAPConnectionImpl.java
    webservices/axis2/trunk/java/modules/saaj/test/org/apache/axis2/saaj/integration/IntegrationTest.java

Modified: webservices/axis2/trunk/java/modules/saaj/src/org/apache/axis2/saaj/SOAPConnectionImpl.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/saaj/src/org/apache/axis2/saaj/SOAPConnectionImpl.java?rev=733533&r1=733532&r2=733533&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/saaj/src/org/apache/axis2/saaj/SOAPConnectionImpl.java (original)
+++ webservices/axis2/trunk/java/modules/saaj/src/org/apache/axis2/saaj/SOAPConnectionImpl.java Sun Jan 11 15:02:07 2009
@@ -19,6 +19,7 @@
 
 package org.apache.axis2.saaj;
 
+import org.apache.axiom.attachments.Attachments;
 import org.apache.axiom.om.OMAttribute;
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMNode;
@@ -73,8 +74,6 @@
     private boolean closed = false;
 
     private ServiceClient serviceClient;
-    private Map<String,AttachmentPart> unaccessedAttachments = new HashMap<String,AttachmentPart>();
-
 
     /**
      * Sends the given message to the specified endpoint and blocks until it has returned the
@@ -121,12 +120,70 @@
 
         opClient.setOptions(options);
 
-        if (request.countAttachments() != 0) { // SOAPMessage with attachments
-            opClient.getOptions().setProperty(Constants.Configuration.ENABLE_MTOM,
-                                              Constants.VALUE_TRUE);
-            return handleSOAPMessage(request, opClient);
-        } else { // simple SOAPMessage
-            return handleSOAPMessage(request, opClient);
+        MessageContext requestMsgCtx = new MessageContext();
+        org.apache.axiom.soap.SOAPEnvelope envelope =
+                SAAJUtil.toOMSOAPEnvelope(request.getSOAPPart().getDocumentElement());
+        if (isMTOM(request)) {
+            Map<String,DataHandler> attachmentMap = new HashMap<String,DataHandler>();
+            for (Iterator it = request.getAttachments(); it.hasNext(); ) {
+                AttachmentPart attachment = (AttachmentPart)it.next();
+                String contentId = attachment.getContentId();
+                if (contentId != null) {
+                    DataHandler dh = attachment.getDataHandler();
+                    if (dh == null) {
+                        throw new SOAPException("Attachment with NULL DataHandler");
+                    }
+                    attachmentMap.put(contentId, dh);
+                }
+            }
+            insertAttachmentNodes(attachmentMap, envelope);
+            options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
+        } else if (request.countAttachments() != 0) { // SOAPMessage with attachments
+            Attachments attachments = requestMsgCtx.getAttachmentMap();
+            for (Iterator it = request.getAttachments(); it.hasNext(); ) {
+                AttachmentPart attachment = (AttachmentPart)it.next();
+                String contentId = attachment.getContentId();
+                // Axiom currently doesn't support attachments without Content-ID
+                // (see WSCOMMONS-418); generate one if necessary.
+                if (contentId == null) {
+                    contentId = IDGenerator.generateID();
+                }
+                attachments.addDataHandler(contentId, attachment.getDataHandler());
+            }
+            options.setProperty(Constants.Configuration.ENABLE_SWA, Constants.VALUE_TRUE);
+        }
+        
+        MessageContext responseMsgCtx;
+        try {
+            requestMsgCtx.setEnvelope(envelope);
+            opClient.addMessageContext(requestMsgCtx);
+            opClient.execute(true);
+            responseMsgCtx =
+                    opClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
+        } catch (AxisFault ex) {
+            throw new SOAPException(ex.getMessage(), ex);
+        }
+        
+        SOAPMessage response = getSOAPMessage(responseMsgCtx.getEnvelope());
+        Attachments attachments = requestMsgCtx.getAttachmentMap();
+        for (String contentId : attachments.getAllContentIDs()) {
+            if (!contentId.equals(attachments.getSOAPPartContentID())) {
+                AttachmentPart ap = response.createAttachmentPart(
+                        attachments.getDataHandler(contentId));
+                ap.setContentId(contentId);
+                response.addAttachmentPart(ap);
+            }
+        }
+        return response;
+    }
+
+    private static boolean isMTOM(SOAPMessage soapMessage) {
+        SOAPPart soapPart = soapMessage.getSOAPPart();
+        String[] contentTypes = soapPart.getMimeHeader("Content-Type");
+        if (contentTypes != null && contentTypes.length > 0) {
+            return SAAJUtil.normalizeContentType(contentTypes[0]).equals("application/xop+xml");
+        } else {
+            return false;
         }
     }
 
@@ -175,23 +232,6 @@
         closed = true;
     }
 
-    private SOAPMessage handleSOAPMessage(SOAPMessage request,
-                                          OperationClient opClient) throws SOAPException {
-
-        MessageContext requestMsgCtx = new MessageContext();
-        try {
-            requestMsgCtx.setEnvelope(toOMSOAPEnvelope(request));
-            opClient.addMessageContext(requestMsgCtx);
-            opClient.execute(true);
-
-            MessageContext msgCtx =
-                    opClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
-            return getSOAPMessage(msgCtx.getEnvelope());
-        } catch (Exception e) {
-            throw new SOAPException(e.getMessage(), e);
-        }
-    }
-
     /**
      * This method handles the conversion of an OM SOAP Envelope to a SAAJ SOAPMessage
      *
@@ -239,12 +279,6 @@
 
         // Convert the body
         toSAAJElement(body, respOMSoapEnv.getBody(), response);
-        // if there are unrefferenced attachments, add that to response
-        if (!unaccessedAttachments.isEmpty()) {
-            for (AttachmentPart attachment : unaccessedAttachments.values()) {
-                response.addAttachmentPart(attachment);
-            }
-        }
 
         return response;
     }
@@ -315,58 +349,14 @@
     }
 
     /**
-     * Converts a SAAJ SOAPMessage to an OM SOAPEnvelope
-     *
-     * @param saajSOAPMsg
-     * @return
-     * @throws SOAPException
-     */
-    protected org.apache.axiom.soap.SOAPEnvelope toOMSOAPEnvelope(SOAPMessage saajSOAPMsg)
-            throws SOAPException {
-
-        final org.apache.axiom.soap.SOAPEnvelope omSOAPEnv =
-                SAAJUtil.toOMSOAPEnvelope(saajSOAPMsg.getSOAPPart().getDocumentElement());
-
-
-        Map<String,AttachmentPart> attachmentMap = new HashMap<String,AttachmentPart>();
-        final Iterator attachments = saajSOAPMsg.getAttachments();
-        while (attachments.hasNext()) {
-            final AttachmentPart attachment = (AttachmentPart)attachments.next();
-            if (attachment.getContentId() == null ||
-                    attachment.getContentId().trim().length() == 0) {
-                attachment.setContentId(IDGenerator.generateID());
-            }
-            if (attachment.getDataHandler() == null) {
-                throw new SOAPException("Attachment with NULL DataHandler");
-            }
-            attachmentMap.put(attachment.getContentId(), attachment);
-        }
-
-        //Get keys of attachments to a hashmap
-        //This hashmap will be updated when attachment is accessed atleast once.
-        //Doing this here instead of inside insertAttachmentNodes()is much simpler
-        //as insertAttachmentNodes() has recursive calls
-        Map<String,String> keyAccessStatus = new HashMap<String,String>();
-        for (String key : attachmentMap.keySet()) {
-            keyAccessStatus.put(key, "not-accessed");
-        }
-
-        insertAttachmentNodes(attachmentMap, omSOAPEnv, keyAccessStatus);
-        unaccessedAttachments =
-                getUnReferencedAttachmentNodes(attachmentMap, omSOAPEnv, keyAccessStatus);
-
-        return omSOAPEnv;
-    }
-
-    /**
      * Inserts the attachments in the proper places
      *
      * @param attachments
      * @param omEnvelope
      * @throws SOAPException
      */
-    private void insertAttachmentNodes(Map<String,AttachmentPart> attachments,
-                                       OMElement omEnvelope, Map<String,String> keyAccessStatus)
+    private void insertAttachmentNodes(Map<String,DataHandler> attachments,
+                                       OMElement omEnvelope)
             throws SOAPException {
 
         Iterator childIter = omEnvelope.getChildElements();
@@ -377,42 +367,18 @@
 
             if (contentID != null) {//This is an omEnvelope referencing an attachment
                 child.build();
-                AttachmentPart ap = attachments.get(contentID.trim());
+                DataHandler dh = attachments.get(contentID.trim());
                 //update the key status as accessed
-                keyAccessStatus.put(contentID.trim(), "accessed");
-                OMText text = new OMTextImpl(ap.getDataHandler(), true,
+                OMText text = new OMTextImpl(dh, true,
                                              omEnvelope.getOMFactory());
                 child.removeAttribute(hrefAttr);
                 child.addChild(text);
             } else {
                 //possibly there can be references in the children of this omEnvelope
                 //so recurse through.
-                insertAttachmentNodes(attachments, child, keyAccessStatus);
-            }
-        }
-    }
-
-
-    private Map<String,AttachmentPart> getUnReferencedAttachmentNodes(
-                    Map<String,AttachmentPart> attachments, OMElement omEnvelope,
-                    Map<String,String> keyAccessStatus)
-            throws SOAPException {
-
-        Map<String,AttachmentPart> unaccessedAttachments = new HashMap<String,AttachmentPart>();
-        //now check for unaccessed keys
-        for (Map.Entry<String,String> entry : keyAccessStatus.entrySet()) {
-            String key = entry.getKey();
-            String keyStatus = entry.getValue();
-            if ("not-accessed".equals(keyStatus)) {
-                //The value for this key has not been accessed in the
-                //referencing attachment scenario.Hence it must be an
-                //unreferenced one.
-                AttachmentPart ap = attachments.get(key);
-                unaccessedAttachments.put(key, ap);
-                entry.setValue("accessed");
+                insertAttachmentNodes(attachments, child);
             }
         }
-        return unaccessedAttachments;
     }
 
     /**

Modified: webservices/axis2/trunk/java/modules/saaj/test/org/apache/axis2/saaj/integration/IntegrationTest.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/saaj/test/org/apache/axis2/saaj/integration/IntegrationTest.java?rev=733533&r1=733532&r2=733533&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/saaj/test/org/apache/axis2/saaj/integration/IntegrationTest.java (original)
+++ webservices/axis2/trunk/java/modules/saaj/test/org/apache/axis2/saaj/integration/IntegrationTest.java Sun Jan 11 15:02:07 2009
@@ -23,8 +23,13 @@
 import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
-import org.apache.axis2.description.AxisService;
+import org.apache.axiom.attachments.Attachments;
+import org.apache.axis2.AxisFault;
+import org.apache.axis2.context.MessageContext;
 import org.apache.axis2.description.Parameter;
+import org.apache.axis2.engine.AxisConfiguration;
+import org.apache.axis2.engine.MessageReceiver;
+import org.apache.axis2.receivers.AbstractInOutMessageReceiver;
 import org.apache.axis2.util.Utils;
 
 import javax.activation.DataHandler;
@@ -76,9 +81,10 @@
         return new TestSetup(new TestSuite(IntegrationTest.class)) {
             public void setUp() throws Exception {
                 port = UtilServer.start(SAAJ_REPO);
-                Parameter eneblemtom = new Parameter("enableMTOM", "true");
-                UtilServer.getConfigurationContext().getAxisConfiguration()
-                        .addParameter(eneblemtom);
+                AxisConfiguration axisCfg =
+                        UtilServer.getConfigurationContext().getAxisConfiguration();
+                axisCfg.addParameter(new Parameter("enableMTOM", "optional"));
+                axisCfg.addParameter(new Parameter("enableSwA", "optional"));
             }
 
             public void tearDown() throws Exception {
@@ -88,10 +94,26 @@
     }
 
     protected void setUp() throws Exception {
-        final AxisService service = Utils.createSimpleService(SERVICE_NAME,
-                                                              EchoService.class.getName(),
-                                                              OPERATION_NAME);
-        UtilServer.deployService(service);
+        MessageReceiver mr = new AbstractInOutMessageReceiver() {
+            @Override
+            public void invokeBusinessLogic(MessageContext inMessage, MessageContext outMessage)
+                    throws AxisFault {
+                
+                outMessage.setEnvelope(inMessage.getEnvelope());
+                Attachments inAttachments = inMessage.getAttachmentMap();
+                Attachments outAttachments = outMessage.getAttachmentMap();
+                for (String contentId : inAttachments.getAllContentIDs()) {
+                    if (!contentId.equals(inAttachments.getSOAPPartContentID())) {
+                        outAttachments.addDataHandler(contentId,
+                                inAttachments.getDataHandler(contentId));
+                    }
+                }
+                outMessage.setDoingSwA(inMessage.isDoingSwA());
+                outMessage.setDoingMTOM(inMessage.isDoingMTOM());
+            }
+        };
+        UtilServer.deployService(
+                Utils.createSimpleService(SERVICE_NAME, mr, null, OPERATION_NAME));
     }
 
     protected void tearDown() throws Exception {