You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@synapse.apache.org by ru...@apache.org on 2009/08/09 15:03:20 UTC

svn commit: r802538 - in /synapse/trunk/java/modules/core/src/main/java/org/apache/synapse: core/axis2/SOAPUtils.java util/MessageHelper.java

Author: ruwan
Date: Sun Aug  9 13:03:20 2009
New Revision: 802538

URL: http://svn.apache.org/viewvc?rev=802538&view=rev
Log:
Fixing the SOAPFautl conversion soap11 -> soap12, https://issues.apache.org/jira/browse/SYNAPSE-566

Modified:
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/core/axis2/SOAPUtils.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/MessageHelper.java

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/core/axis2/SOAPUtils.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/core/axis2/SOAPUtils.java?rev=802538&r1=802537&r2=802538&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/core/axis2/SOAPUtils.java (original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/core/axis2/SOAPUtils.java Sun Aug  9 13:03:20 2009
@@ -24,11 +24,13 @@
 import org.apache.axiom.om.OMNode;
 import org.apache.axiom.soap.*;
 import org.apache.axis2.AxisFault;
+import org.apache.axis2.wsdl.WSDLConstants;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.synapse.SynapseException;
 import org.apache.synapse.util.MessageHelper;
 
+import javax.xml.namespace.QName;
 import java.util.Iterator;
 import java.util.List;
 
@@ -165,11 +167,13 @@
                         SOAPFaultCode newSOAPFaultCode = soap12Factory.createSOAPFaultCode();
                         newSOAPFault.setCode(newSOAPFaultCode);
 
-                        String value = code.getText();
-                        if(value != null) {
+                        QName s11Code = code.getTextAsQName();
+                        if (s11Code != null) {
+                            // get the corresponding SOAP12 fault code
+                            // for the provided SOAP11 fault code
                             SOAPFaultValue newSOAPFaultValue
                                     = soap12Factory.createSOAPFaultValue(newSOAPFaultCode);
-                            newSOAPFaultValue.setText(value);
+                            newSOAPFaultValue.setText(getMappingSOAP12Code(s11Code));
                         }
 
                     }
@@ -245,12 +249,12 @@
 
                     while(allAttributes.hasNext()) {
                         OMAttribute attr = (OMAttribute) allAttributes.next();
-                        if(attr.getNamespace() != null
+                        if (attr.getNamespace() != null
                             && SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(
                             attr.getNamespace().getNamespaceURI())) {
                             String attrName = attr.getLocalName();
 
-                            if(SOAP_ATR_ROLE.equals(attrName)) {
+                            if (SOAP_ATR_ROLE.equals(attrName)) {
                                 OMAttribute newAtr = omNode.getOMFactory().createOMAttribute(
                                     SOAP_ATR_ACTOR, newEnvelope.getNamespace(),
                                     attr.getAttributeValue());
@@ -275,63 +279,131 @@
                         }
 
                         newEnvelope.getHeader().addChild(newSOAPHeader);
-                    } // while(allAttributes.hasNext())
+                    }
 
                 } else {
                     newEnvelope.getHeader().addChild(omNode);
-                } // if (omNode instanceof SOAPHeaderBlock)
-
-            } // while (itr.hasNext())
-
-        } // if (clonedOldEnv.getHeader() != null)
+                }
+            }
+        }
 
         if (clonedOldEnv.getBody() != null) {
-            Iterator itr = clonedOldEnv.getBody().getChildren();
-            while (itr.hasNext()) {
-                OMNode omNode = (OMNode) itr.next();
-
-                if (omNode != null && omNode instanceof SOAPFault) {
-
-                    SOAPFault soapFault = (SOAPFault) omNode;
-                    SOAPFault newSOAPFault = soap11Factory.createSOAPFault();
-                    newEnvelope.getBody().addChild(newSOAPFault);
-
-                    SOAPFaultCode code = soapFault.getCode();
-                    if(code != null) {
-                        SOAPFaultCode newSOAPFaultCode
-                                = soap11Factory.createSOAPFaultCode(newSOAPFault);
-
-                        SOAPFaultValue value = code.getValue();
-                        if(value != null) {
-                            soap11Factory.createSOAPFaultValue(newSOAPFaultCode);
-                            if(value.getText() != null) {
-                                newSOAPFaultCode.setText(value.getText());
-                            }
+            if (clonedOldEnv.hasFault()) {
+                SOAPFault soapFault = clonedOldEnv.getBody().getFault();
+                SOAPFault newSOAPFault = soap11Factory.createSOAPFault();
+                newEnvelope.getBody().addChild(newSOAPFault);
+
+                SOAPFaultCode code = soapFault.getCode();
+                if(code != null) {
+                    SOAPFaultCode newSOAPFaultCode
+                            = soap11Factory.createSOAPFaultCode(newSOAPFault);
+
+                    SOAPFaultValue value = code.getValue();
+                    if(value != null) {
+                        // get the corresponding SOAP12 fault code
+                        // for the provided SOAP11 fault code
+                        soap11Factory.createSOAPFaultValue(newSOAPFaultCode);
+                        if(value.getTextAsQName() != null) {
+                            newSOAPFaultCode.setText(
+                                    getMappingSOAP11Code(value.getTextAsQName()));
                         }
                     }
+                }
 
-                    SOAPFaultReason reason = soapFault.getReason();
-                    if(reason != null) {
-                        SOAPFaultReason newSOAPFaultReason
-                                = soap11Factory.createSOAPFaultReason(newSOAPFault);
-
-                        List allSoapTexts = reason.getAllSoapTexts();
-                        Iterator iterAllSoapTexts = allSoapTexts.iterator();
-                        if (iterAllSoapTexts.hasNext()) {
-                            SOAPFaultText soapFaultText = (SOAPFaultText) iterAllSoapTexts.next();
-                            newSOAPFaultReason.setText(soapFaultText.getText());
-                        }
+                SOAPFaultReason reason = soapFault.getReason();
+                if(reason != null) {
+                    SOAPFaultReason newSOAPFaultReason
+                            = soap11Factory.createSOAPFaultReason(newSOAPFault);
+
+                    List allSoapTexts = reason.getAllSoapTexts();
+                    Iterator iterAllSoapTexts = allSoapTexts.iterator();
+                    if (iterAllSoapTexts.hasNext()) {
+                        SOAPFaultText soapFaultText = (SOAPFaultText) iterAllSoapTexts.next();
+                        newSOAPFaultReason.setText(soapFaultText.getText());
                     }
+                }
+            } else {
+                Iterator itr = clonedOldEnv.getBody().getChildren();
+                while (itr.hasNext()) {
+                    OMNode omNode = (OMNode) itr.next();
+                    if (omNode != null) {
+                        newEnvelope.getBody().addChild(omNode);
+                    }
+                }
+            }
 
-                } else {
-                    newEnvelope.getBody().addChild(omNode);
-                } // if (omNode instanceof SOAPFault)
-
-            } // while (itr.hasNext())
-
-        } // if (clonedOldEnv.getBody() != null)
+        }
         
         axisOutMsgCtx.setEnvelope(newEnvelope);
     }
 
+    /**********************************************************************
+     *                     Fault code conversions                         *
+     **********************************************************************/
+
+    private static final QName S11_FAULTCODE_VERSIONMISMATCH = new QName(
+            SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI, "VersionMismatch",
+            SOAP11Constants.SOAP_DEFAULT_NAMESPACE_PREFIX);
+    private static final QName S12_FAULTCODE_VERSIONMISMATCH = new QName(
+            SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI, "VersionMismatch",
+            SOAP12Constants.SOAP_DEFAULT_NAMESPACE_PREFIX);
+
+    private static final QName S11_FAULTCODE_MUSTUNDERSTAND = new QName(
+            SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI, "MustUnderstand",
+            SOAP11Constants.SOAP_DEFAULT_NAMESPACE_PREFIX);
+    private static final QName S12_FAULTCODE_MUSTUNDERSTAND = new QName(
+            SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI, "MustUnderstand",
+            SOAP12Constants.SOAP_DEFAULT_NAMESPACE_PREFIX);
+
+    private static final QName S11_FAULTCODE_CLIENT = new QName(
+            SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI, "Client",
+            SOAP11Constants.SOAP_DEFAULT_NAMESPACE_PREFIX);
+    private static final QName S12_FAULTCODE_SENDER = new QName(
+            SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI, "Sender",
+            SOAP12Constants.SOAP_DEFAULT_NAMESPACE_PREFIX);
+
+    private static final QName S11_FAULTCODE_SERVER = new QName(
+            SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI, "Server",
+            SOAP11Constants.SOAP_DEFAULT_NAMESPACE_PREFIX);
+    private static final QName S12_FAULTCODE_RECEIVER = new QName(
+            SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI, "Receiver",
+            SOAP12Constants.SOAP_DEFAULT_NAMESPACE_PREFIX);
+
+    private static final QName S12_FAULTCODE_DATAENCODINGUNKNOWN = new QName(
+            SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI, "DataEncodingUnknown",
+            SOAP12Constants.SOAP_DEFAULT_NAMESPACE_PREFIX);
+
+    private static QName getMappingSOAP12Code(QName soap11Code) {
+        if (S11_FAULTCODE_VERSIONMISMATCH.equals(soap11Code)) {
+            return S12_FAULTCODE_VERSIONMISMATCH;
+        } else if (S11_FAULTCODE_MUSTUNDERSTAND.equals(soap11Code)) {
+            return S12_FAULTCODE_MUSTUNDERSTAND;
+        } else if (S11_FAULTCODE_CLIENT.equals(soap11Code)) {
+            return S12_FAULTCODE_SENDER;
+        } else if (S11_FAULTCODE_SERVER.equals(soap11Code)) {
+            return S12_FAULTCODE_RECEIVER;
+        } else {
+            log.warn("An unidentified SOAP11 FaultCode encountered, returning a blank QName");
+            return new QName("", "");
+        }
+    }
+
+    private static QName getMappingSOAP11Code(QName soap12Code) {
+        if (S12_FAULTCODE_VERSIONMISMATCH.equals(soap12Code)) {
+            return S11_FAULTCODE_VERSIONMISMATCH;
+        } else if (S12_FAULTCODE_MUSTUNDERSTAND.equals(soap12Code)) {
+            return S11_FAULTCODE_MUSTUNDERSTAND;
+        } else if (S12_FAULTCODE_SENDER.equals(soap12Code)) {
+            return S11_FAULTCODE_SERVER;
+        } else if (S12_FAULTCODE_RECEIVER.equals(soap12Code)) {
+            return S11_FAULTCODE_SERVER;
+        } else if (S12_FAULTCODE_DATAENCODINGUNKNOWN.equals(soap12Code)) {
+            log.debug("There is no matching SOAP11 code value for SOAP12 fault code " +
+                    "DataEncodingUnknown, returning a blank QName");
+            return new QName("");
+        } else {
+            log.warn("An unidentified SOAP11 FaultCode encountered, returning a blank QName");
+            return new QName("");
+        }
+    }
 }

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/MessageHelper.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/MessageHelper.java?rev=802538&r1=802537&r2=802538&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/MessageHelper.java (original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/MessageHelper.java Sun Aug  9 13:03:20 2009
@@ -5,10 +5,7 @@
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMNamespace;
 import org.apache.axiom.om.OMNode;
-import org.apache.axiom.soap.SOAP11Constants;
-import org.apache.axiom.soap.SOAPEnvelope;
-import org.apache.axiom.soap.SOAPHeader;
-import org.apache.axiom.soap.SOAPHeaderBlock;
+import org.apache.axiom.soap.*;
 import org.apache.axis2.AxisFault;
 import org.apache.axis2.Constants;
 import org.apache.axis2.addressing.AddressingConstants;
@@ -223,13 +220,14 @@
      * @return cloned SOAPEnvelope from the provided one
      */
     public static SOAPEnvelope cloneSOAPEnvelope(SOAPEnvelope envelope) {
-        SOAPEnvelope newEnvelope;
+        SOAPFactory fac;
         if (SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI
-            .equals(envelope.getBody().getNamespace().getNamespaceURI())) {
-            newEnvelope = OMAbstractFactory.getSOAP11Factory().getDefaultEnvelope();
+                .equals(envelope.getBody().getNamespace().getNamespaceURI())) {
+            fac = OMAbstractFactory.getSOAP11Factory();
         } else {
-            newEnvelope = OMAbstractFactory.getSOAP12Factory().getDefaultEnvelope();
+            fac = OMAbstractFactory.getSOAP12Factory();
         }
+        SOAPEnvelope newEnvelope = fac.getDefaultEnvelope();
 
         if (envelope.getHeader() != null) {
             Iterator itr = envelope.getHeader().cloneOMElement().getChildren();
@@ -239,9 +237,16 @@
         }
 
         if (envelope.getBody() != null) {
-            Iterator itr = envelope.getBody().cloneOMElement().getChildren();
-            while (itr.hasNext()) {
-                newEnvelope.getBody().addChild((OMNode) itr.next());
+            // treat the SOAPFault cloning as a special case otherwise a cloning OMElement as the
+            // fault would lead to class cast exceptions if accessed through the getFault method
+            if (envelope.getBody().hasFault()) {
+                SOAPFault fault = envelope.getBody().getFault();
+                newEnvelope.getBody().addFault(cloneSOAPFault(fault));
+            } else {
+                Iterator itr = envelope.getBody().cloneOMElement().getChildren();
+                while (itr.hasNext()) {
+                    newEnvelope.getBody().addChild((OMNode) itr.next());
+                }
             }
         }
 
@@ -345,6 +350,7 @@
 
     /**
      * Get the Policy object for the given name from the Synapse configuration at runtime
+     * 
      * @param synCtx the current synapse configuration to get to the synapse configuration
      * @param propertyKey the name of the property which holds the Policy required
      * @return the Policy object with the given name, from the configuration
@@ -359,6 +365,75 @@
         return null;
     }
 
+    /**
+     * Clones the SOAPFault, fault cloning is not the same as cloning the OMElement because if the
+     * Fault is accessed through the SOAPEnvelope.getBody().getFault() method it will lead to a
+     * class cast because the cloned element is just an OMElement but not a Fault.
+     * 
+     * @param fault that needs to be cloned
+     * @return the cloned fault
+     */
+    public static SOAPFault cloneSOAPFault(SOAPFault fault) {
+
+        SOAPFactory fac;
+        int soapVersion;
+        final int SOAP_11 = 1;
+        final int SOAP_12 = 2;
+        if (SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI
+                .equals(fault.getNamespace().getNamespaceURI())) {
+            fac = OMAbstractFactory.getSOAP11Factory();
+            soapVersion = SOAP_11;
+        } else {
+            fac = OMAbstractFactory.getSOAP12Factory();
+            soapVersion = SOAP_12;
+        }
+        SOAPFault newFault = fac.createSOAPFault();
+
+        SOAPFaultCode code = fac.createSOAPFaultCode();
+        SOAPFaultReason reason = fac.createSOAPFaultReason();
+
+        switch (soapVersion) {
+            case SOAP_11:
+                code.setText(fault.getCode().getTextAsQName());
+                reason.setText(fault.getReason().getText());
+                break;
+            case SOAP_12:
+                SOAPFaultValue value = fac.createSOAPFaultValue(code);
+                value.setText(fault.getCode().getTextAsQName());
+                for (Object obj : fault.getReason().getAllSoapTexts()) {
+                    SOAPFaultText text = fac.createSOAPFaultText();
+                    text.setText(((SOAPFaultText) obj).getText());
+                    reason.addSOAPText(text);
+                }
+                break;
+        }
+
+        newFault.setCode(code);
+        newFault.setReason(reason);
+
+        if (fault.getNode() != null) {
+            SOAPFaultNode soapfaultNode = fac.createSOAPFaultNode();
+            soapfaultNode.setNodeValue(fault.getNode().getNodeValue());
+            newFault.setNode(soapfaultNode);
+        }
+
+        if (fault.getRole() != null) {
+            SOAPFaultRole soapFaultRole = fac.createSOAPFaultRole();
+            soapFaultRole.setRoleValue(fault.getRole().getRoleValue());
+            newFault.setRole(soapFaultRole);
+        }
+
+        if (fault.getDetail() != null) {
+            SOAPFaultDetail soapFaultDetail = fac.createSOAPFaultDetail();
+            for (Iterator itr = fault.getDetail().getAllDetailEntries(); itr.hasNext();) {
+                soapFaultDetail.addDetailEntry(((OMElement) itr.next()).cloneOMElement());
+            }
+            newFault.setDetail(soapFaultDetail);
+        }
+
+        return newFault;
+    }
+
     private static void handleException(String msg) {
         log.error(msg);
         throw new SynapseException(msg);