Author: gdaniels
Date: Tue Mar 13 18:32:10 2007
New Revision: 517967

URL: http://svn.apache.org/viewvc?view=rev&rev=517967
Log:
Improve returning of header block groups.

* Introduce a custom Iterator to do traversals.  This ensures that we're
  not walking unnecessarily through the entire structure, and also makes
  sure that only SOAPHeaderBlocks are included in the results.

* Adjust tests accordingly (remove unnecessary next() calls, etc)

* Fix a few spelling errors

Modified:
    
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/SOAPHeaderImpl.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/soap/SOAPHeaderBlockTest.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/soap/SOAPHeaderTest.java

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/SOAPHeaderImpl.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/SOAPHeaderImpl.java?view=diff&rev=517967&r1=517966&r2=517967
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/SOAPHeaderImpl.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/SOAPHeaderImpl.java
 Tue Mar 13 18:32:10 2007
@@ -31,15 +31,139 @@
 import java.util.ArrayList;
 import java.util.Iterator;
 
-/** Class SOAPHeaderImpl */
+/**
+ * A local interface we can use to make "header checker" objects which can be 
used
+ * by HeaderIterators to filter results.  This really SHOULD be done with 
anonymous
+ * classes:
+ *
+ * public void getHeadersByRole(final String role) {
+ *     return new HeaderIterator() {
+ *         public boolean checkHeader(SOAPHeaderBlock header) {
+ *             ...
+ *             if (role.equals(headerRole)) return true;
+ *             return false;
+ *         }
+ *     }
+ * }
+ *
+ * ...but there appears to be some kind of weird problem with the JVM not 
correctly
+ * scoping the passed "role" value in a situation like the above.  As such, we 
have
+ * to make Checker objects instead (sigh).
+ */
+interface Checker {
+    boolean checkHeader(SOAPHeaderBlock header);
+}
+
+/**
+ * A Checker to make sure headers match a given role.  If the role we're 
looking for is
+ * null, then everything matches.
+ */
+class RoleChecker implements Checker {
+    String role;
+
+    public RoleChecker(String role) {
+        this.role = role;
+    }
+
+    public boolean checkHeader(SOAPHeaderBlock header) {
+        if (role == null) {
+            return true;
+        }
+        String thisRole = header.getRole();
+        return (role.equals(thisRole));
+    }
+}
+
+/**
+ * A Checker to see that we both match a given role AND are mustUnderstand=true
+ */
+class MURoleChecker extends RoleChecker {
+    public MURoleChecker(String role) {
+        super(role);
+    }
+
+    public boolean checkHeader(SOAPHeaderBlock header) {
+        if (header.getMustUnderstand())
+            return super.checkHeader(header);
+        return false;
+    }
+}
+
+/**
+ * A class representing the SOAP Header, primarily allowing access to the 
contained
+ * HeaderBlocks.
+ */
 public abstract class SOAPHeaderImpl extends SOAPElement implements SOAPHeader 
{
+    /**
+     * An Iterator which walks the header list as needed, potentially filtering
+     * as we traverse.
+     */
+    class HeaderIterator implements Iterator {
+        SOAPHeaderBlock current;
+        boolean advance = false;
+        Checker checker;
+
+        public HeaderIterator() {
+            this(null);
+        }
+
+        public HeaderIterator(Checker checker) {
+            this.checker = checker;
+            current = (SOAPHeaderBlock)getFirstElement();
+            if (current != null) {
+                if (!checkHeader(current)) {
+                    advance = true;
+                    hasNext();
+                }
+            }
+        }
+
+        public void remove() {
+        }
+
+        public boolean checkHeader(SOAPHeaderBlock header) {
+            if (checker == null) return true;
+            return checker.checkHeader(header);
+        }
+
+        public boolean hasNext() {
+            if (!advance) {
+                return current != null;
+            }
+
+            advance = false;
+            OMNode sibling = current.getNextOMSibling();
+
+            while (sibling != null) {
+                if (sibling instanceof SOAPHeaderBlock) {
+                    SOAPHeaderBlock possible = (SOAPHeaderBlock)sibling;
+                    if (checkHeader(possible)) {
+                        current = (SOAPHeaderBlock)sibling;
+                        return true;
+                    }
+                }
+                sibling = sibling.getNextOMSibling();
+            }
+
+            current = null;
+            return false;
+        }
+
+        public Object next() {
+            SOAPHeaderBlock ret = current;
+            if (ret != null) {
+                advance = true;
+                hasNext();
+            }
+            return ret;
+        }
+    }
 
 
     protected SOAPHeaderImpl(OMNamespace ns, SOAPFactory factory) {
         super(SOAPConstants.HEADER_LOCAL_NAME, ns, factory);
     }
 
-    /** @param envelope  */
     public SOAPHeaderImpl(SOAPEnvelope envelope, SOAPFactory factory)
             throws SOAPProcessingException {
         super(envelope, SOAPConstants.HEADER_LOCAL_NAME, true, factory);
@@ -86,38 +210,8 @@
      *         that contain the specified actor
      * @see #extractHeaderBlocks(String) extractHeaderBlocks(java.lang.String)
      */
-    public Iterator examineHeaderBlocks(String paramRole) {
-        Iterator headerBlocksIter = this.getChildren();
-        ArrayList headersWithGivenActor = new ArrayList();
-
-        if (paramRole == null || "".equals(paramRole)) {
-            return returnAllSOAPHeaders(this.getChildren());
-        }
-
-        while (headerBlocksIter.hasNext()) {
-            Object o = headerBlocksIter.next();
-            if (o instanceof SOAPHeaderBlock) {
-                SOAPHeaderBlock soapHeaderBlock = (SOAPHeaderBlock) o;
-                String role = soapHeaderBlock.getRole();
-                if ((role != null) && role.equalsIgnoreCase(paramRole)) {
-                    headersWithGivenActor.add(soapHeaderBlock);
-                }
-            }
-        }
-        return headersWithGivenActor.iterator();
-    }
-
-    private Iterator returnAllSOAPHeaders(Iterator children) {
-        ArrayList headers = new ArrayList();
-        while (children.hasNext()) {
-            Object o = children.next();
-            if (o instanceof SOAPHeaderBlock) {
-                headers.add(o);
-            }
-        }
-
-        return headers.iterator();
-
+    public Iterator examineHeaderBlocks(final String role) {
+        return new HeaderIterator(new RoleChecker(role));
     }
 
     /**
@@ -143,22 +237,8 @@
      * @return an <code>Iterator</code> object over all the 
<code>SOAPHeaderBlock</code> objects
      *         that contain the specified actor and are marked as 
MustUnderstand
      */
-    public Iterator examineMustUnderstandHeaderBlocks(String actor) {
-        Iterator headerBlocksIter = this.getChildren();
-        ArrayList mustUnderstandHeadersWithGivenActor = new ArrayList();
-        while (headerBlocksIter.hasNext()) {
-            Object o = headerBlocksIter.next();
-            if (o instanceof SOAPHeaderBlock) {
-                SOAPHeaderBlock soapHeaderBlock = (SOAPHeaderBlock) o;
-                String role = soapHeaderBlock.getRole();
-                boolean mustUnderstand = soapHeaderBlock.getMustUnderstand();
-                if ((role != null) && role.equalsIgnoreCase(actor) &&
-                        mustUnderstand) {
-                    mustUnderstandHeadersWithGivenActor.add(soapHeaderBlock);
-                }
-            }
-        }
-        return mustUnderstandHeadersWithGivenActor.iterator();
+    public Iterator examineMustUnderstandHeaderBlocks(final String actor) {
+        return new HeaderIterator(new MURoleChecker(actor));
     }
 
     /**
@@ -170,7 +250,13 @@
      *         contained by this <code>SOAPHeader</code>
      */
     public Iterator examineAllHeaderBlocks() {
-        return this.getChildrenWithName(null);
+        class DefaultChecker implements Checker {
+            public boolean checkHeader(SOAPHeaderBlock header) {
+                return true;
+            }
+        }
+
+        return new HeaderIterator(new DefaultChecker());
     }
 
     /**
@@ -186,7 +272,7 @@
 
     public ArrayList getHeaderBlocksWithNSURI(String nsURI) {
         ArrayList headers = null;
-        OMNode node = null;
+        OMNode node;
         OMElement header = this.getFirstElement();
 
         if (header != null) {
@@ -224,5 +310,4 @@
                     "Expecting an implementation of SOAP Envelope as the 
parent. But received some other implementation");
         }
     }
-
 }

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/soap/SOAPHeaderBlockTest.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/soap/SOAPHeaderBlockTest.java?view=diff&rev=517967&r1=517966&r2=517967
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/soap/SOAPHeaderBlockTest.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/soap/SOAPHeaderBlockTest.java
 Tue Mar 13 18:32:10 2007
@@ -43,19 +43,13 @@
                                                     namespace,
                                                     soap12Header);
         Iterator iterator = soap11HeaderWithParser.examineAllHeaderBlocks();
-        iterator.next();
         soap11HeaderBlock1WithParser = (SOAPHeaderBlock) iterator.next();
-        iterator.next();
         soap11HeaderBlock2WithParser = (SOAPHeaderBlock) iterator.next();
-        iterator.next();
         soap11HeaderBlock3WithParser = (SOAPHeaderBlock) iterator.next();
 
         iterator = soap12HeaderWithParser.examineAllHeaderBlocks();
-        iterator.next();
         soap12HeaderBlock1WithParser = (SOAPHeaderBlock) iterator.next();
-        iterator.next();
         soap12HeaderBlock2WithParser = (SOAPHeaderBlock) iterator.next();
-        iterator.next();
         soap12HeaderBlock3WithParser = (SOAPHeaderBlock) iterator.next();
     }
 
@@ -108,7 +102,7 @@
             soap11HeaderBlock.setMustUnderstand("true");
         } catch (Exception e) {
             fail(
-                    "SOAP 1.1 HeaderBlock Test : - MustUnderstatnd value can 
not be set to any value rather than 1 or 0");
+                    "SOAP 1.1 HeaderBlock Test : - MustUnderstand value can 
not be set to any value rather than 1 or 0");
         }
     }
 
@@ -178,7 +172,7 @@
         try {
             soap12HeaderBlock.setMustUnderstand("otherValue");
             fail(
-                    "SOAP 1.2 HeaderBlock Test : - MustUnderstatnd value can 
not be set to any value rather than 1 , 0 , true , false");
+                    "SOAP 1.2 HeaderBlock Test : - MustUnderstand value can 
not be set to any value rather than 1 , 0 , true , false");
 
         } catch (Exception e) {
             assertTrue(true);
@@ -198,17 +192,17 @@
     //SOAP 1.1 SOAPHeaderBlock Test (With Parser)
     public void testSOAP11GetRoleWithParser() {
         assertTrue(
-                "SOAP 1.1 HeaderBlock Test Wiht Parser : - getRole method 
returns incorrect role value",
+                "SOAP 1.1 HeaderBlock Test With Parser : - getRole method 
returns incorrect role value",
                 soap11HeaderBlock1WithParser.getRole().equals(
                         "http://schemas.xmlsoap.org/soap/actor/next";));
     }
 
     public void testSOAP11GetMustUnderstandWithParser() {
         assertTrue(
-                "SOAP 1.1 HeaderBlock Test Wiht Parser : - getMustUnderstatnd 
method returns incorrect value",
+                "SOAP 1.1 HeaderBlock Test With Parser : - getMustUnderstand 
method returns incorrect value",
                 soap11HeaderBlock2WithParser.getMustUnderstand());
         assertFalse(
-                "SOAP 1.1 HeaderBlock Test Wiht Parser : - getMustUnderstatnd 
method returns incorrect value",
+                "SOAP 1.1 HeaderBlock Test With Parser : - getMustUnderstand 
method returns incorrect value",
                 soap11HeaderBlock3WithParser.getMustUnderstand());
 
     }
@@ -216,23 +210,23 @@
     //SOAP 1.2 SOAPHeaderBlock Test (With Parser)
     public void testSOAP12GetRoleWithParser() {
         assertTrue(
-                "SOAP 1.2 HeaderBlock Test Wiht Parser : - getRole method 
returns incorrect role value",
+                "SOAP 1.2 HeaderBlock Test With Parser : - getRole method 
returns incorrect role value",
                 soap12HeaderBlock1WithParser.getRole().equals(
                         
"http://www.w3.org/2003/05/soap-envelope/role/ultimateReceiver";));
     }
 
     public void testSOAP12GetMustUnderstandWithParser() {
         assertTrue(
-                "SOAP 1.2 HeaderBlock Test Wiht Parser : - getMustUnderstatnd 
method returns incorrect value",
+                "SOAP 1.2 HeaderBlock Test With Parser : - getMustUnderstand 
method returns incorrect value",
                 soap12HeaderBlock1WithParser.getMustUnderstand());
         assertFalse(
-                "SOAP 1.2 HeaderBlock Test Wiht Parser : - getMustUnderstatnd 
method returns incorrect value",
+                "SOAP 1.2 HeaderBlock Test With Parser : - getMustUnderstand 
method returns incorrect value",
                 soap12HeaderBlock2WithParser.getMustUnderstand());
         try {
             soap12HeaderBlock3WithParser.getMustUnderstand();
         } catch (Exception e) {
             fail(
-                    "SOAP 1.2 HeaderBlock Test Wiht Parser : - 
getMustUnderstatnd method should returns exception when mustunderstand value is 
incorrect");
+                    "SOAP 1.2 HeaderBlock Test With Parser : - 
getMustUnderstand method should returns exception when mustunderstand value is 
incorrect");
         }
     }
 }

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/soap/SOAPHeaderTest.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/soap/SOAPHeaderTest.java?view=diff&rev=517967&r1=517966&r2=517967
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/soap/SOAPHeaderTest.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/soap/SOAPHeaderTest.java
 Tue Mar 13 18:32:10 2007
@@ -21,6 +21,7 @@
 
 public class SOAPHeaderTest extends SOAPHeaderTestCase {
     protected boolean isThereException;
+    private static final String ROLE_URI = 
"http://schemas.xmlsoap.org/soap/envelope/actor/next";;
 
     public SOAPHeaderTest(String testName) {
         super(testName);
@@ -31,7 +32,7 @@
     }
 
     //SOAP 1.1 Header Test (Programaticaly 
Created)--------------------------------------------------------------------------------
-    public void testSOAP11AddHeadearBlock() {
+    public void testSOAP11AddHeaderBlock() {
         soap11Header.addHeaderBlock("echoOk1", namespace);
         soap11Header.addHeaderBlock("echoOk2", namespace);
         Iterator iterator = soap11Header.getChildren();
@@ -63,21 +64,19 @@
     }
 
     public void testSOAP11ExamineHeaderBlocks() {
+        System.out.println("Failing test...");
         soap11Header.addHeaderBlock("echoOk1", namespace).setRole(
                 
"http://schemas.xmlsoap.org/soap/envelope/actor/ultimateReceiver";);
-        soap11Header.addHeaderBlock("echoOk2", namespace).setRole(
-                "http://schemas.xmlsoap.org/soap/envelope/actor/next";);
-        Iterator iterator = soap11Header.examineHeaderBlocks(
-                "http://schemas.xmlsoap.org/soap/envelope/actor/next";);
+        soap11Header.addHeaderBlock("echoOk2", namespace).setRole(ROLE_URI);
+        Iterator iterator = soap11Header.examineHeaderBlocks(ROLE_URI);
         iterator.hasNext();
         SOAPHeaderBlock headerBlockWithRole1 = (SOAPHeaderBlock) 
iterator.next();
-        assertTrue(
+        assertEquals(
                 "SOAP 1.1 Header Test : - headerBlockWithRole local name 
mismatch",
-                headerBlockWithRole1.getLocalName().equals("echoOk2"));
-        assertTrue(
+                "echoOk2", headerBlockWithRole1.getLocalName());
+        assertEquals(
                 "SOAP 1.1 Header Test : - headerBlockWithRole role value 
mismatch",
-                headerBlockWithRole1.getRole().equals(
-                        
"http://schemas.xmlsoap.org/soap/envelope/actor/next";));
+                ROLE_URI, headerBlockWithRole1.getRole());
 
 
         assertFalse(
@@ -425,17 +424,14 @@
 
     public void testSOAP12ExamineAllHeaderBlocksWithParser() {
         Iterator iterator = soap12HeaderWithParser.examineAllHeaderBlocks();
-        iterator.next();
         SOAPHeaderBlock headerBlock1 = (SOAPHeaderBlock) iterator.next();
         assertTrue(
                 "SOAP 1.2 Header Test With Parser : - headerBlock1 localname 
mmismatch",
                 headerBlock1.getLocalName().equals("echoOk"));
-        iterator.next();
         SOAPHeaderBlock headerBlock2 = (SOAPHeaderBlock) iterator.next();
         assertTrue(
                 "SOAP 1.2 Header Test With Parser : - headerBlock1 localname 
mmismatch",
                 headerBlock2.getLocalName().equals("echoOk1"));
-        iterator.next();
         SOAPHeaderBlock headerBlock3 = (SOAPHeaderBlock) iterator.next();
         assertTrue(
                 "SOAP 1.2 Header Test With Parser : - headerBlock1 localname 
mmismatch",



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to