You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commons-dev@ws.apache.org by sc...@apache.org on 2007/11/06 12:19:31 UTC

svn commit: r592375 - in /webservices/commons/trunk/modules/axiom/modules: axiom-api/src/main/java/org/apache/axiom/om/ axiom-api/src/main/java/org/apache/axiom/om/ds/ axiom-api/src/main/java/org/apache/axiom/soap/ axiom-impl/src/main/java/org/apache/a...

Author: scheu
Date: Tue Nov  6 03:19:28 2007
New Revision: 592375

URL: http://svn.apache.org/viewvc?rev=592375&view=rev
Log:
WSCOMMONS-267
Contributor:Rich Scheuerle
Add property support to OMDataSourceExt.  This allows the developer of an OMDataSource
to expose MUST_UNDERSTAND, ROLE, and RELAY properties which can be read by the SOAPHeaderBlock processing.

Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMDataSourceExt.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/ByteArrayDataSource.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/CharArrayDataSource.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/InputStreamDataSource.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAPHeaderBlock.java
    webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/SOAPHeaderBlockImpl.java
    webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/soap11/SOAP11HeaderBlockImpl.java
    webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/soap12/SOAP12HeaderBlockImpl.java
    webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/OMSourcedElementTest.java

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMDataSourceExt.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMDataSourceExt.java?rev=592375&r1=592374&r2=592375&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMDataSourceExt.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMDataSourceExt.java Tue Nov  6 03:19:28 2007
@@ -133,4 +133,26 @@
      * @return OMDataSourceExt
      */
     public OMDataSourceExt copy();
+    
+    /**
+     * Returns true if property is set
+     * @param key
+     * @return
+     */
+    public boolean hasProperty(String key);
+    
+    /**
+     * Query a property stored on the OMDataSource
+     * @param key
+     * @return value or null
+     */
+    public Object getProperty(String key);
+    
+    /**
+     * Set a property on the OMDataSource
+     * @param key
+     * @param value
+     * @return old property object or null
+     */
+    public Object setProperty(String key, Object value);
 }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/ByteArrayDataSource.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/ByteArrayDataSource.java?rev=592375&r1=592374&r2=592375&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/ByteArrayDataSource.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/ByteArrayDataSource.java Tue Nov  6 03:19:28 2007
@@ -33,6 +33,7 @@
 import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 import java.io.Writer;
+import java.util.HashMap;
 import java.util.Iterator;
 
 /**
@@ -45,6 +46,8 @@
 
     ByteArray byteArray = null;
     
+    HashMap map = null;  // Map of properties
+    
     /**
      * Constructor
      * @param bytes 
@@ -196,6 +199,27 @@
     public class ByteArray {
         public byte[] bytes;
         public String encoding;
+    }
+
+    public Object getProperty(String key) {
+        if (map == null) {
+            return null;
+        }
+        return map.get(key);
+    }
+
+    public Object setProperty(String key, Object value) {
+        if (map == null) {
+            map = new HashMap();
+        }
+        return map.put(key, value);
+    }
+
+    public boolean hasProperty(String key) {
+        if (map == null) {
+            return false;
+        } 
+        return map.containsKey(key);
     }
 
     

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/CharArrayDataSource.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/CharArrayDataSource.java?rev=592375&r1=592374&r2=592375&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/CharArrayDataSource.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/CharArrayDataSource.java Tue Nov  6 03:19:28 2007
@@ -34,6 +34,7 @@
 import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 import java.io.Writer;
+import java.util.HashMap;
 import java.util.Iterator;
 
 /**
@@ -46,6 +47,7 @@
 
     char[] chars = null;
     
+    HashMap map = null;  // Map of properties
     /**
      * Constructor
      * @param bytes 
@@ -179,5 +181,24 @@
             builder.close();
         }
     }
+    public Object getProperty(String key) {
+        if (map == null) {
+            return null;
+        }
+        return map.get(key);
+    }
+
+    public Object setProperty(String key, Object value) {
+        if (map == null) {
+            map = new HashMap();
+        }
+        return map.put(key, value);
+    }
     
+    public boolean hasProperty(String key) {
+        if (map == null) {
+            return false;
+        } 
+        return map.containsKey(key);
+    }
 }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/InputStreamDataSource.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/InputStreamDataSource.java?rev=592375&r1=592374&r2=592375&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/InputStreamDataSource.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/InputStreamDataSource.java Tue Nov  6 03:19:28 2007
@@ -35,6 +35,7 @@
 import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 import java.io.Writer;
+import java.util.HashMap;
 import java.util.Iterator;
 
 /**
@@ -48,6 +49,8 @@
     Data data = null;
     private static final int BUFFER_LEN = 4096;
     
+    HashMap map = null;  // Map of properties
+    
     /**
      * Constructor
      * @param bytes 
@@ -265,5 +268,24 @@
         public InputStream is;
     }
 
+    public Object getProperty(String key) {
+        if (map == null) {
+            return null;
+        }
+        return map.get(key);
+    }
+
+    public Object setProperty(String key, Object value) {
+        if (map == null) {
+            map = new HashMap();
+        }
+        return map.put(key, value);
+    }
     
+    public boolean hasProperty(String key) {
+        if (map == null) {
+            return false;
+        } 
+        return map.containsKey(key);
+    }
 }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAPHeaderBlock.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAPHeaderBlock.java?rev=592375&r1=592374&r2=592375&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAPHeaderBlock.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAPHeaderBlock.java Tue Nov  6 03:19:28 2007
@@ -28,6 +28,16 @@
  * <CODE>OMElement</CODE> objects as its children.</P>
  */
 public interface SOAPHeaderBlock extends OMSourcedElement {
+    
+    /**
+     * A SOAPHeaderBlock may be represented as an unexpanded OMSourcedElement.
+     * In such cases, the underlying OMDataSource may have a property that contains
+     * the value of the ROLE/ACTOR, RELAY or MUST_UNDERSTAND setting.
+     */
+    public String ROLE_PROPERTY = "org.apache.axiom.soap.SOAPHeader.ROLE";
+    public String RELAY_PROPERTY = "org.apache.axiom.soap.SOAPHeader.RELAY";
+    public String MUST_UNDERSTAND_PROPERTY = "org.apache.axiom.soap.SOAPHeader.MUST_UNDERSTAND";
+    
     /**
      * Sets the actor associated with this <CODE> SOAPHeaderBlock</CODE> object to the specified
      * actor.

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/SOAPHeaderBlockImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/SOAPHeaderBlockImpl.java?rev=592375&r1=592374&r2=592375&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/SOAPHeaderBlockImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/SOAPHeaderBlockImpl.java Tue Nov  6 03:19:28 2007
@@ -21,6 +21,7 @@
 
 import org.apache.axiom.om.OMAttribute;
 import org.apache.axiom.om.OMDataSource;
+import org.apache.axiom.om.OMDataSourceExt;
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMNamespace;
 import org.apache.axiom.om.OMXMLParserWrapper;
@@ -124,5 +125,30 @@
 
     public void setProcessed() {
         processed = true;
+    }
+    
+    /**
+     * @param key
+     * @return requested OMDataSourceExt property or null
+     */
+    protected String getOMDataSourceProperty(String key) {
+        if (this.hasOMDataSourceProperty(key)) {
+            return (String) ((OMDataSourceExt) getDataSource()).getProperty(key);
+        }
+        return null;
+    }
+    
+    /**
+     * @param key
+     * @return requested OMDataSourceExt property or null
+     */
+    protected boolean hasOMDataSourceProperty(String key) {
+        if (!this.isExpanded()) {
+            OMDataSource ds = this.getDataSource();
+            if (ds instanceof OMDataSourceExt) {
+                return ((OMDataSourceExt)ds).hasProperty(key);
+            }
+        }
+        return false;
     }
 }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/soap11/SOAP11HeaderBlockImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/soap11/SOAP11HeaderBlockImpl.java?rev=592375&r1=592374&r2=592375&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/soap11/SOAP11HeaderBlockImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/soap11/SOAP11HeaderBlockImpl.java Tue Nov  6 03:19:28 2007
@@ -90,8 +90,15 @@
     }
 
     public String getRole() {
-        return getAttribute(SOAP11Constants.ATTR_ACTOR,
-                            SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
+//      Get the property or attribute
+        String val;
+        if (this.hasOMDataSourceProperty(ROLE_PROPERTY)) {
+            val = this.getOMDataSourceProperty(ROLE_PROPERTY);
+        } else {
+            val = getAttribute(SOAP11Constants.ATTR_ACTOR,
+                               SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
+        }
+        return val;
     }
 
     public void setMustUnderstand(boolean mustUnderstand) {
@@ -122,11 +129,19 @@
      *         <CODE>SOAPHeaderBlock</CODE> object is turned on; <CODE>false</CODE> otherwise
      */
     public boolean getMustUnderstand() throws SOAPProcessingException {
+        // First, try getting the information from the property
+        // Fallback to getting the information from the attribute
         String mustUnderstand;
-        if ((mustUnderstand =
+        if (this.hasOMDataSourceProperty(MUST_UNDERSTAND_PROPERTY)) {
+            mustUnderstand = this.getOMDataSourceProperty(this.MUST_UNDERSTAND_PROPERTY);
+        } else {
+            mustUnderstand =
                 getAttribute(SOAPConstants.ATTR_MUSTUNDERSTAND,
-                             SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI))
-                != null) {
+                             SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
+        }
+        
+        // Parse the value
+        if (mustUnderstand != null) {
             if (SOAPConstants.ATTR_MUSTUNDERSTAND_TRUE.equals(mustUnderstand) ||
                     SOAPConstants.ATTR_MUSTUNDERSTAND_1.equals(mustUnderstand)) {
                 return true;

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/soap12/SOAP12HeaderBlockImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/soap12/SOAP12HeaderBlockImpl.java?rev=592375&r1=592374&r2=592375&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/soap12/SOAP12HeaderBlockImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/soap12/SOAP12HeaderBlockImpl.java Tue Nov  6 03:19:28 2007
@@ -23,6 +23,7 @@
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMNamespace;
 import org.apache.axiom.om.OMXMLParserWrapper;
+import org.apache.axiom.soap.SOAP11Constants;
 import org.apache.axiom.soap.SOAPConstants;
 import org.apache.axiom.soap.SOAPFactory;
 import org.apache.axiom.soap.SOAPHeader;
@@ -89,7 +90,14 @@
     }
 
     public String getRole() {
-        return getAttributeValue(QNAME_ROLE);
+        // Get the property or attribute
+        String val;
+        if (this.hasOMDataSourceProperty(ROLE_PROPERTY)) {
+            val = this.getOMDataSourceProperty(ROLE_PROPERTY);
+        } else {
+            val = getAttributeValue(QNAME_ROLE);
+        }
+       return val;
     }
 
     public void setMustUnderstand(boolean mustUnderstand) {
@@ -117,9 +125,17 @@
     }
 
     public boolean getMustUnderstand() throws SOAPProcessingException {
+        // First, try getting the information from the property
+        // Fallback to getting the information from the attribute
         String mustUnderstand;
-        if ((mustUnderstand = getAttribute(ATTR_MUSTUNDERSTAND, SOAP_ENVELOPE_NAMESPACE_URI))
-                != null) {
+        if (this.hasOMDataSourceProperty(MUST_UNDERSTAND_PROPERTY)) {
+            mustUnderstand = this.getOMDataSourceProperty(this.MUST_UNDERSTAND_PROPERTY);
+        } else {
+            mustUnderstand = getAttribute(ATTR_MUSTUNDERSTAND, SOAP_ENVELOPE_NAMESPACE_URI);
+        }
+       
+        // Now parse the value
+        if (mustUnderstand != null) {
             if (SOAPConstants.ATTR_MUSTUNDERSTAND_TRUE.equals(mustUnderstand) ||
                     SOAPConstants.ATTR_MUSTUNDERSTAND_1.equals(mustUnderstand)) {
                 return true;
@@ -143,7 +159,15 @@
 
     public boolean getRelay() {
         boolean ret = false;
-        String val = getAttributeValue(QNAME_RELAY);
+        
+        // Get the property or attribute
+        String val;
+        if (this.hasOMDataSourceProperty(RELAY_PROPERTY)) {
+            val = this.getOMDataSourceProperty(RELAY_PROPERTY);
+        } else {
+            val = getAttributeValue(QNAME_RELAY);
+        }
+        
         if (val != null) {
             ret = "true".equalsIgnoreCase(val);
         }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/OMSourcedElementTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/OMSourcedElementTest.java?rev=592375&r1=592374&r2=592375&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/OMSourcedElementTest.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/OMSourcedElementTest.java Tue Nov  6 03:19:28 2007
@@ -22,6 +22,9 @@
 import org.apache.axiom.soap.SOAPBody;
 import org.apache.axiom.soap.SOAPEnvelope;
 import org.apache.axiom.soap.SOAPFactory;
+import org.apache.axiom.soap.SOAPHeader;
+import org.apache.axiom.soap.SOAPHeaderBlock;
+
 import javax.xml.stream.XMLStreamReader;
 
 import java.io.ByteArrayInputStream;
@@ -61,6 +64,7 @@
         super.setUp();
         soapEnvelope = soapFactory.createSOAPEnvelope();
         SOAPBody soapBody = soapFactory.createSOAPBody(soapEnvelope);
+        SOAPHeader soapHeader = soapFactory.createSOAPHeader(soapEnvelope);
         
         bads1 = new ByteArrayDataSource(payload1.getBytes(ENCODING), ENCODING);
         bads2 = new ByteArrayDataSource(payload2.getBytes(ENCODING), ENCODING);
@@ -113,6 +117,59 @@
         // of the OMSourcedElement.
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         soapBody.serialize(baos);
+        String output = baos.toString(ENCODING);
+        System.out.println(output);
+        assertTrue("The payload was not present in the output",
+                   output.indexOf(payload1) > 0);
+        assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
+        
+        // Test getting the raw bytes from the ByteArrayDataSource.
+        OMDataSourceExt ds = (OMDataSourceExt) child.getDataSource();
+        byte[] bytes = ds.getXMLBytes("UTF-16");  // Get the bytes as UTF-16 
+        String payload = new String(bytes, "utf-16");
+        assertTrue("The obtained bytes did not match the payload",
+                   payload1.equals(payload));
+        
+    }
+    
+    /**
+     * Tests functionality of ByteArrayDataSource
+     * @throws Exception
+     */
+    public void testHeader_ByteArrayDS() throws Exception {
+        SOAPHeader soapHeader = soapEnvelope.getHeader();
+        OMFactory factory = soapHeader.getOMFactory();
+        
+        // Set an empty MustUnderstand property on the data source
+        bads1.setProperty(SOAPHeaderBlock.MUST_UNDERSTAND_PROPERTY, null);
+        
+        OMSourcedElement omse = ((SOAPFactory)factory).createSOAPHeaderBlock(localName, ns, bads1);
+        soapHeader.addChild(omse);
+        OMNode firstChild = soapHeader.getFirstOMChild();
+        assertTrue("Expected OMSourcedElement child", firstChild instanceof SOAPHeaderBlock);
+        SOAPHeaderBlock child = (SOAPHeaderBlock) firstChild;
+        assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
+        assertTrue("OMSourcedElement should be backed by a ByteArrayDataSource",
+                   child.getDataSource() instanceof ByteArrayDataSource);
+        
+        // Make sure that getting the MustUnderstand property does not cause expansion.
+        assertTrue(!child.getMustUnderstand());
+        assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
+        assertTrue("OMSourcedElement should be backed by a ByteArrayDataSource",
+                   child.getDataSource() instanceof ByteArrayDataSource);
+        
+        // A ByteArrayDataSource does not consume the backing object when read.
+        // Thus getting the XMLStreamReader of the ByteArrayDataSource should not 
+        // cause expansion of the OMSourcedElement.
+        XMLStreamReader reader = child.getXMLStreamReader();
+        reader.next();
+        assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
+        
+        // Likewise, a ByteArrayDataSource does not consume the backing object when 
+        // written.  Thus serializing the OMSourcedElement should not cause the expansion
+        // of the OMSourcedElement.
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        soapHeader.serialize(baos);
         String output = baos.toString(ENCODING);
         System.out.println(output);
         assertTrue("The payload was not present in the output",



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: commons-dev-help@ws.apache.org