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 ch...@apache.org on 2006/03/27 19:35:28 UTC

svn commit: r389204 - in /webservices/axis2/trunk/java/modules/core/src/org/apache/axis2: AxisFault.java engine/AxisEngine.java

Author: chinthaka
Date: Mon Mar 27 09:35:25 2006
New Revision: 389204

URL: http://svn.apache.org/viewcvs?rev=389204&view=rev
Log:
Hooking AxisFault information to fault handling mechanism of Axis2. Need to write some test cases for this.

Modified:
    webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/AxisFault.java
    webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/engine/AxisEngine.java

Modified: webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/AxisFault.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/AxisFault.java?rev=389204&r1=389203&r2=389204&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/AxisFault.java (original)
+++ webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/AxisFault.java Mon Mar 27 09:35:25 2006
@@ -18,18 +18,22 @@
 package org.apache.axis2;
 
 import org.apache.axiom.om.OMElement;
-import org.apache.axiom.soap.SOAPFault;
+import org.apache.axiom.soap.SOAP12Constants;
 import org.apache.axiom.soap.SOAPFaultCode;
+import org.apache.axiom.soap.SOAPFaultDetail;
+import org.apache.axiom.soap.SOAPFaultNode;
+import org.apache.axiom.soap.SOAPFaultReason;
+import org.apache.axiom.soap.SOAPFaultRole;
 import org.apache.axiom.soap.SOAPHeader;
-import org.apache.axis2.fault.FaultCode;
-import org.apache.axis2.fault.FaultReasonList;
 
 import javax.xml.namespace.QName;
 import java.lang.reflect.InvocationTargetException;
 import java.rmi.RemoteException;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.ListIterator;
+import java.util.Map;
 
 /**
  * An exception which maps cleanly to a SOAP fault.
@@ -51,35 +55,31 @@
  *      a SOAP1.1 fault is created, spurious information can be discarded.
  *      Mapping
  *      <pre>
- *           SOAP1.2              SOAP1.1
- *           node                 faultactor
- *           reason(0).text       faultstring
- *           faultcode.value      faultcode
- *           faultcode.subcode    (discarded)
- *           detail               detail
- *           role                 (discarded)
- *           </pre>
+ *                                         SOAP1.2              SOAP1.1
+ *                                         node                 faultactor
+ *                                         reason(0).text       faultstring
+ *                                         faultcode.value      faultcode
+ *                                         faultcode.subcode    (discarded)
+ *                                         detail               detail
+ *                                         role                 (discarded)
+ *                                         </pre>
  */
 public class AxisFault extends RemoteException {
 
-	private static final long serialVersionUID = -374933082062124907L;
-
-	/**
-     * Contains the faultcode
-     */
-    private FaultCode faultCode = new FaultCode();
-
-    /**
-     * our failt reasons
-     */
-    private FaultReasonList reasons = new FaultReasonList();
+    private static final long serialVersionUID = -374933082062124907L;
 
     /**
      * assume headers are not used very often
      */
     private List headers = new ArrayList(0);
+
+    private List faultReasonList = new ArrayList(1);
+    private QName faultCode;
+    private String faultNode;
+    private String faultRole;
     private OMElement detail;
 
+    private Map faultElements;
     /**
      * SOAP1.2: URI of faulting node. Null for unknown.
      * <p/>
@@ -92,27 +92,63 @@
      */
     private String nodeURI;
 
+
     /**
-     * An incoming SOAPFault
+     * @param message
      */
-    private SOAPFault soapFault;
+    public AxisFault(String message) {
+        super(message);
+        addReason(message);
+    }
 
     /**
-     * Make an AxisFault from an incoming SOAPFault
+     * These are the absolute minimum to construct a meaningful SOAPFault from user's information
      *
-     * @param fault that caused the failure
+     * @param faultCode   - fault code of the message as a QName
+     * @param faultReason - the reason for the fault. The language will be defaulted to 'en'
+     * @param cause
      */
-    public AxisFault(SOAPFault fault) {
-        soapFault = fault;
-        init(soapFault);
+    public AxisFault(QName faultCode, String faultReason, Throwable cause) {
+        this(faultReason, cause);
+        setFaultCode(faultCode);
+    }
+
+    public AxisFault(QName faultCode, String faultReason, String faultNode, String faultRole, OMElement faultDetail) {
+        this(faultReason, faultCode);
+        this.faultNode = faultNode;
+        this.faultRole = faultRole;
+        setDetail(faultDetail);
     }
 
     /**
-     * @param message
-     */
-    public AxisFault(String message) {
-        super(message);
-        addReason(message);
+     * This is just a convenience method for the user. If you set these, do not use other methods
+     * in this class to get and set things.
+     * Any of the parameters can be null
+     *
+     * @param soapFaultCode
+     * @param soapFaultReason
+     * @param soapFaultNode
+     * @param soapFaultRole
+     * @param soap
+     */
+    public AxisFault(SOAPFaultCode soapFaultCode, SOAPFaultReason soapFaultReason,
+                     SOAPFaultNode soapFaultNode, SOAPFaultRole soapFaultRole, SOAPFaultDetail soapFaultDetail) {
+        if (faultElements == null) {
+            // assuming that most of the times fault code, fault string and fault details are set
+            faultElements = new HashMap(3);
+        }
+        setToElementsListIfNotNull(SOAP12Constants.SOAP_FAULT_CODE_LOCAL_NAME, soapFaultCode);
+        setToElementsListIfNotNull(SOAP12Constants.SOAP_FAULT_REASON_LOCAL_NAME, soapFaultReason);
+        setToElementsListIfNotNull(SOAP12Constants.SOAP_FAULT_NODE_LOCAL_NAME, soapFaultNode);
+        setToElementsListIfNotNull(SOAP12Constants.SOAP_FAULT_ROLE_LOCAL_NAME, soapFaultRole);
+        setToElementsListIfNotNull(SOAP12Constants.SOAP_FAULT_DETAIL_LOCAL_NAME, soapFaultDetail);
+
+    }
+
+    private void setToElementsListIfNotNull(String soapFaultElementName, OMElement soapFaultElement) {
+        if (soapFaultElement != null) {
+            faultElements.put(soapFaultElementName, soapFaultElement);
+        }
     }
 
     /**
@@ -192,7 +228,7 @@
      * @param text text message
      */
     public void addReason(String text) {
-        addReason(text, "");
+        faultReasonList.add(new FaultReason(text, ""));
     }
 
     /**
@@ -202,11 +238,19 @@
      * @param language language
      */
     public void addReason(String text, String language) {
-        reasons.add(text, language);
+        faultReasonList.add(new FaultReason(text, language));
     }
 
+    /**
+     * Returns the first fault reason, if available. If not found, returns null.
+     *
+     * @return faultReason
+     */
     public String getReason() {
-        return reasons.getFirstReasonText();
+        if (faultReasonList.size() >= 1)
+            return ((FaultReason) faultReasonList.get(0)).getText();
+
+        return null;
     }
 
     /**
@@ -228,20 +272,6 @@
     }
 
     /**
-     * Initialise from a SOAPFault. This is how incoming fault messages
-     * get turned into AxisFaults.
-     *
-     * @param fault incoming fault
-     */
-    private void init(SOAPFault fault) {
-        SOAPFaultCode faultcodesource = fault.getCode();
-
-        faultCode = new FaultCode(faultcodesource);
-        detail = fault.getDetail();
-        fault.getNode();
-    }
-
-    /**
      * Make an AxisFault based on a passed Exception.  If the Exception is
      * already an AxisFault, simply use that.  Otherwise, wrap it in an
      * AxisFault.  If the Exception is an InvocationTargetException (which
@@ -277,7 +307,7 @@
     }
 
     public QName getFaultCode() {
-        return faultCode.getValue();
+        return faultCode;
     }
 
     /**
@@ -300,11 +330,11 @@
     }
 
     public void setFaultCode(QName soapFaultCode) {
-        faultCode.setValue(soapFaultCode);
+        this.faultCode = soapFaultCode;
     }
 
     public void setFaultCode(String soapFaultCode) {
-        faultCode.setValueString(soapFaultCode);
+        faultCode = new QName(soapFaultCode);
     }
 
     /**
@@ -312,5 +342,65 @@
      */
     public void setNodeURI(String nodeURI) {
         this.nodeURI = nodeURI;
+    }
+
+
+    public Map getFaultElements() {
+        return faultElements;
+    }
+
+    public String getFaultNode() {
+        return faultNode;
+    }
+
+    public String getFaultRole() {
+        return faultRole;
+    }
+
+    class FaultReason {
+
+        /**
+         * Language of the reason.
+         * xml:lang="en" "en-GB" or just ""
+         */
+        private String language = "";
+
+        /**
+         * env:reasontext
+         */
+        private String text;
+
+        public FaultReason() {
+        }
+
+        public FaultReason(String text, String language) {
+            this.text = text;
+            this.language = language;
+        }
+
+        /**
+         * Returns a string representation of the object.
+         *
+         * @return the text value
+         */
+        public String toString() {
+            return text;
+        }
+
+        public String getLanguage() {
+            return language;
+        }
+
+        public String getText() {
+            return text;
+        }
+
+        public void setLanguage(String language) {
+            this.language = language;
+        }
+
+        public void setText(String text) {
+            this.text = text;
+        }
     }
 }

Modified: webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/engine/AxisEngine.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/engine/AxisEngine.java?rev=389204&r1=389203&r2=389204&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/engine/AxisEngine.java (original)
+++ webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/engine/AxisEngine.java Mon Mar 27 09:35:25 2006
@@ -27,7 +27,9 @@
 import org.apache.axiom.soap.SOAPFault;
 import org.apache.axiom.soap.SOAPFaultCode;
 import org.apache.axiom.soap.SOAPFaultDetail;
+import org.apache.axiom.soap.SOAPFaultNode;
 import org.apache.axiom.soap.SOAPFaultReason;
+import org.apache.axiom.soap.SOAPFaultRole;
 import org.apache.axiom.soap.SOAPHeaderBlock;
 import org.apache.axiom.soap.SOAPProcessingException;
 import org.apache.axis2.AxisFault;
@@ -270,49 +272,56 @@
     private void extractFaultInformationFromMessageContext(MessageContext context, SOAPFault fault,
                                                            Throwable e) {
         SOAPProcessingException soapException = null;
-//        String soapNamespaceURI;
-//
-//        // get the current SOAP version
-//        if (!context.isSOAP11()) {
-//
-//            // defaulting to SOAP 1.2
-//            soapNamespaceURI = SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI;
-//        } else {
-//            soapNamespaceURI = SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI;
-//        }
+        AxisFault axisFault = null;
+
+        if (e != null) {
+            if (e instanceof AxisFault) {
+                axisFault = (AxisFault) e;
+            } else if (e.getCause() instanceof AxisFault) {
+                axisFault = (AxisFault) e.getCause();
+            }
+        }
 
         if (e instanceof SOAPProcessingException) {
             soapException = (SOAPProcessingException) e;
-        } else if (e instanceof AxisFault) {
-            if (e.getCause() instanceof SOAPProcessingException) {
-                soapException = (SOAPProcessingException) e.getCause();
+        } else if (axisFault != null) {
+            if (axisFault.getCause() instanceof SOAPProcessingException) {
+                soapException = (SOAPProcessingException) axisFault.getCause();
             }
         } else {
             // we have recd an instance of just the Exception class
         }
 
+        // user can set the fault information to the message context or to the AxisFault itself.
+        // whatever user sets to the message context, supercedes eerything.
+
         Object faultCode = context.getProperty(SOAP12Constants.SOAP_FAULT_CODE_LOCAL_NAME);
         String soapFaultCode = "";
 
-        Throwable exception;
+
         if (faultCode != null) {
             fault.setCode((SOAPFaultCode) faultCode);
         } else if (soapException != null) {
             soapFaultCode = soapException.getFaultCode();
-        } else
-        if (((exception = e) instanceof AxisFault || (exception = e.getCause()) instanceof AxisFault))
-        {
-            QName faultCodeQName = ((AxisFault) exception).getFaultCode();
-            if (faultCodeQName != null) {
-                if (faultCodeQName.getLocalPart().indexOf(":") == -1) {
-                    String prefix = faultCodeQName.getPrefix();
-                    String uri = faultCodeQName.getNamespaceURI();
-                    prefix = prefix == null || "".equals(prefix) ? Constants.AXIS2_NAMESPACE_PREFIX : prefix;
-                    uri = uri == null || "".equals(uri) ? Constants.AXIS2_NAMESPACE_URI : uri;
-                    soapFaultCode = prefix + ":" + faultCodeQName.getLocalPart();
-                    fault.declareNamespace(uri, prefix);
-                } else {
-                    soapFaultCode = faultCodeQName.getLocalPart();
+        } else if (axisFault != null) {
+
+            Map faultElementsMap = axisFault.getFaultElements();
+            if (faultElementsMap != null && faultElementsMap.get(SOAP12Constants.SOAP_FAULT_CODE_LOCAL_NAME) != null)
+            {
+                fault.setCode((SOAPFaultCode) faultElementsMap.get(SOAP12Constants.SOAP_FAULT_CODE_LOCAL_NAME));
+            } else {
+                QName faultCodeQName = axisFault.getFaultCode();
+                if (faultCodeQName != null) {
+                    if (faultCodeQName.getLocalPart().indexOf(":") == -1) {
+                        String prefix = faultCodeQName.getPrefix();
+                        String uri = faultCodeQName.getNamespaceURI();
+                        prefix = prefix == null || "".equals(prefix) ? Constants.AXIS2_NAMESPACE_PREFIX : prefix;
+                        uri = uri == null || "".equals(uri) ? Constants.AXIS2_NAMESPACE_URI : uri;
+                        soapFaultCode = prefix + ":" + faultCodeQName.getLocalPart();
+                        fault.declareNamespace(uri, prefix);
+                    } else {
+                        soapFaultCode = faultCodeQName.getLocalPart();
+                    }
                 }
             }
         }
@@ -333,11 +342,16 @@
             message = fault.getReason().getSOAPText().getText();
         } else if (soapException != null) {
             message = soapException.getMessage();
-        } else
-        if (((exception = e) instanceof AxisFault || (exception = e.getCause()) instanceof AxisFault))
-        {
-            message = ((AxisFault) exception).getReason();
-            message = message != null && "".equals(message) ? message : e.getMessage();
+        } else if (axisFault != null) {
+            Map faultElementsMap = axisFault.getFaultElements();
+            if (faultElementsMap != null && faultElementsMap.get(SOAP12Constants.SOAP_FAULT_REASON_LOCAL_NAME) != null)
+            {
+                fault.setReason((SOAPFaultReason) faultElementsMap.get(SOAP12Constants.SOAP_FAULT_REASON_LOCAL_NAME));
+            } else {
+                message = axisFault.getReason();
+                message = message != null && "".equals(message) ? message : e.getMessage();
+            }
+
 
         }
 
@@ -350,22 +364,46 @@
             fault.getReason().getSOAPText().setText(message);
         }
 
+
         Object faultRole = context.getProperty(SOAP12Constants.SOAP_FAULT_ROLE_LOCAL_NAME);
         if (faultRole != null) {
             fault.getRole().setText((String) faultRole);
+        } else if (axisFault != null) {
+            Map faultElementsMap = axisFault.getFaultElements();
+            if (faultElementsMap != null && faultElementsMap.get(SOAP12Constants.SOAP_FAULT_ROLE_LOCAL_NAME) != null)
+            {
+                fault.setRole((SOAPFaultRole) faultElementsMap.get(SOAP12Constants.SOAP_FAULT_ROLE_LOCAL_NAME));
+            }
         }
 
         Object faultNode = context.getProperty(SOAP12Constants.SOAP_FAULT_NODE_LOCAL_NAME);
         if (faultNode != null) {
             fault.getNode().setText((String) faultNode);
+        } else if (axisFault != null) {
+            Map faultElementsMap = axisFault.getFaultElements();
+            if (faultElementsMap != null && faultElementsMap.get(SOAP12Constants.SOAP_FAULT_NODE_LOCAL_NAME) != null)
+            {
+                fault.setNode((SOAPFaultNode) faultElementsMap.get(SOAP12Constants.SOAP_FAULT_NODE_LOCAL_NAME));
+            }
         }
 
         Object faultDetail = context.getProperty(SOAP12Constants.SOAP_FAULT_DETAIL_LOCAL_NAME);
         if (faultDetail != null) {
             fault.setDetail((SOAPFaultDetail) faultDetail);
-        }
-
-        if (fault.getException() == null) {
+        } else if (axisFault != null) {
+            Map faultElementsMap = axisFault.getFaultElements();
+            if (faultElementsMap != null && faultElementsMap.get(SOAP12Constants.SOAP_FAULT_DETAIL_LOCAL_NAME) != null)
+            {
+                fault.setDetail((SOAPFaultDetail) faultElementsMap.get(SOAP12Constants.SOAP_FAULT_DETAIL_LOCAL_NAME));
+            }else {
+                OMElement detail = axisFault.getDetail();
+                if (detail != null) {
+                    fault.getDetail().addDetailEntry(detail);
+                }else {
+                    fault.setException(axisFault);
+                }
+            }
+        }else if (fault.getException() == null) {
             if (e instanceof Exception) {
                 fault.setException((Exception) e);
             } else {
@@ -386,7 +424,9 @@
      * @see Phase
      * @see Handler
      */
-    public void receive(MessageContext msgContext) throws AxisFault {
+    public void receive
+            (MessageContext
+                    msgContext) throws AxisFault {
         ConfigurationContext confContext = msgContext.getConfigurationContext();
         ArrayList preCalculatedPhases =
                 confContext.getAxisConfiguration().getGlobalInFlow();
@@ -417,7 +457,9 @@
      * @param msgContext
      * @throws AxisFault
      */
-    public void invoke(MessageContext msgContext) throws AxisFault {
+    public void invoke
+            (MessageContext
+                    msgContext) throws AxisFault {
         if (msgContext.getCurrentHandlerIndex() == -1) {
             msgContext.setCurrentHandlerIndex(0);
         }
@@ -440,7 +482,9 @@
      * @param msgContext
      * @throws AxisFault
      */
-    public void resumeReceive(MessageContext msgContext) throws AxisFault {
+    public void resumeReceive
+            (MessageContext
+                    msgContext) throws AxisFault {
         //invoke the phases
         invoke(msgContext);
         //invoking the MR
@@ -458,7 +502,9 @@
      *
      * @param msgContext
      */
-    public void resumeSend(MessageContext msgContext) throws AxisFault {
+    public void resumeSend
+            (MessageContext
+                    msgContext) throws AxisFault {
         //invoke the phases
         invoke(msgContext);
         //Invoking Tarnsport Sender
@@ -470,7 +516,6 @@
         }
     }
 
-
     /**
      * This is invoked when a SOAP Fault is received from a Other SOAP Node
      * Receives a SOAP fault from another SOAP node.
@@ -478,7 +523,9 @@
      * @param msgContext
      * @throws AxisFault
      */
-    public void receiveFault(MessageContext msgContext) throws AxisFault {
+    public void receiveFault
+            (MessageContext
+                    msgContext) throws AxisFault {
         log.info(Messages.getMessage("receivederrormessage",
                 msgContext.getMessageID()));
         ConfigurationContext confContext = msgContext.getConfigurationContext();
@@ -492,7 +539,9 @@
         invoke(msgContext);
     }
 
-    public void resume(MessageContext msgctx) throws AxisFault {
+    public void resume
+            (MessageContext
+                    msgctx) throws AxisFault {
         msgctx.setPaused(false);
         if (msgctx.getFLOW() == MessageContext.IN_FLOW) {
             resumeReceive(msgctx);
@@ -512,7 +561,9 @@
      * @see Phase
      * @see Handler
      */
-    public void send(MessageContext msgContext) throws AxisFault {
+    public void send
+            (MessageContext
+                    msgContext) throws AxisFault {
 
         // find and invoke the Phases
         OperationContext operationContext = msgContext.getOperationContext();
@@ -552,7 +603,9 @@
      * @param msgContext
      * @throws AxisFault
      */
-    public void sendFault(MessageContext msgContext) throws AxisFault {
+    public void sendFault
+            (MessageContext
+                    msgContext) throws AxisFault {
         OperationContext opContext = msgContext.getOperationContext();
 
         // find and execute the Fault Out Flow Handlers
@@ -579,7 +632,9 @@
         }
     }
 
-    private String getSenderFaultCode(OMNamespace soapNamespace) {
+    private String getSenderFaultCode
+            (OMNamespace
+                    soapNamespace) {
         return SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(soapNamespace.getName())
                 ? SOAP12Constants.SOAP_DEFAULT_NAMESPACE_PREFIX + ":" + SOAP12Constants.FAULT_CODE_SENDER
                 : SOAP12Constants.SOAP_DEFAULT_NAMESPACE_PREFIX + ":" + SOAP11Constants.FAULT_CODE_SENDER;