axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jayachan...@apache.org
Subject svn commit: r267222 - in /webservices/axis2/trunk/java/modules/saaj: src/org/apache/axis2/saaj/SOAPConnectionImpl.java test-resources/Echo.aar test/org/apache/axis2/saaj/JUnitTestAttachmentIntegration.java test/org/apache/axis2/saaj/Readme.txt
Date Fri, 02 Sep 2005 15:30:19 GMT
Author: jayachandra
Date: Fri Sep  2 08:30:02 2005
New Revision: 267222

URL: http://svn.apache.org/viewcvs?rev=267222&view=rev
Log:
Adding a round trip integration test case for attachments support provided in SAAJ module.
A readme file is associated as to how this special test case should be run.

modified   src/org/apache/axis2/saaj/SOAPConnectionImpl.java
added      test-resources/Echo.aar
added      test/org/apache/axis2/saaj/JUnitTestAttachmentIntegration.java
added      test/org/apache/axis2/saaj/Readme.txt

Added:
    webservices/axis2/trunk/java/modules/saaj/test-resources/Echo.aar   (with props)
    webservices/axis2/trunk/java/modules/saaj/test/org/apache/axis2/saaj/JUnitTestAttachmentIntegration.java
    webservices/axis2/trunk/java/modules/saaj/test/org/apache/axis2/saaj/Readme.txt
Modified:
    webservices/axis2/trunk/java/modules/saaj/src/org/apache/axis2/saaj/SOAPConnectionImpl.java

Modified: webservices/axis2/trunk/java/modules/saaj/src/org/apache/axis2/saaj/SOAPConnectionImpl.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/saaj/src/org/apache/axis2/saaj/SOAPConnectionImpl.java?rev=267222&r1=267221&r2=267222&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
Fri Sep  2 08:30:02 2005
@@ -15,6 +15,8 @@
  */
 package org.apache.axis2.saaj;
 
+import org.w3c.dom.Node;
+
 import org.apache.axis2.AxisFault;
 import org.apache.axis2.Constants;
 import org.apache.axis2.addressing.EndpointReference;
@@ -23,10 +25,13 @@
 import org.apache.axis2.om.OMAttribute;
 import org.apache.axis2.om.OMNode;
 import org.apache.axis2.om.OMText;
+import org.apache.axis2.util.SessionUtils;
 
+import javax.activation.DataHandler;
 import javax.xml.soap.SOAPConnection;
 import javax.xml.soap.SOAPException;
 import javax.xml.soap.SOAPMessage;
+import javax.xml.soap.SOAPEnvelope;
 import javax.xml.soap.AttachmentPart;
 import javax.xml.namespace.QName;
 import java.net.MalformedURLException;
@@ -59,14 +64,19 @@
             call.set(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
             call.setTransportInfo(Constants.TRANSPORT_HTTP,
                     Constants.TRANSPORT_HTTP,
-                    true);
+                    false);
             call.setTo(
                     new EndpointReference(url.toString()));
             String axisOp = request.getSOAPBody().getFirstChild().getNodeName();
-            org.apache.axis2.soap.SOAPEnvelope responseEnv = (org.apache.axis2.soap.SOAPEnvelope)
call.invokeBlocking(
-                    axisOp, envelope);
+            NodeImpl bodyContentNode = (NodeImpl)request.getSOAPBody().getFirstChild();
+            OMElement bodyContent = (OMElement)bodyContentNode.getOMNode();
+            OMElement result = call.invokeBlocking(axisOp, bodyContent);
+            org.apache.axis2.soap.SOAPEnvelope responseEnv = (org.apache.axis2.soap.SOAPEnvelope)
((OMElement)result.getParent()).getParent(); 
             SOAPEnvelopeImpl response = new SOAPEnvelopeImpl(responseEnv);
-            return new SOAPMessageImpl(response);
+            
+            SOAPMessageImpl sMsg = new SOAPMessageImpl(response);
+            extractAttachmentNodes(responseEnv, sMsg);
+            return sMsg;
 
         } catch (MalformedURLException mue) {
             throw new SOAPException(mue);
@@ -99,27 +109,6 @@
     		String hrefContentId = validateHref(hrefAttr);
     		
     		if (hrefContentId!=null) {//This is an element referencing an attachment!
-    			/*
-    			//Get a handle to this element's parent and next sibling for later use.
-    			OMElement parent = (OMElement)child.getParent();
-    			OMNode nextSibling = child.getNextSibling();
-    			OMNode prevSibling = child.getPreviousSibling();
-    			
-    			OMText omText = getOMTextForReferencedAttachment(hrefContentId, soapMsg);
-    			
-    			child.build();
-    			child.detach();
-    			//We should now detach the element which referenced the attachment
-    			//and in its place put an OMText node created out of the attachment's
-    			//data handler, of course, preserving the order of attachments
-    			if(nextSibling!=null) {
-    				nextSibling.insertSiblingBefore(omText); //preserving the order of attachments
-    			} else if (prevSibling!=null) {
-    				prevSibling.insertSiblingAfter(omText);
-    			} else {//only child for its parent, so needn't bother about order
-    				parent.addChild(omText);
-    			}
-    			*/
     			OMText omText = getOMTextForReferencedAttachment(hrefContentId, soapMsg);
     			child.build();
     			child.removeAttribute(hrefAttr);
@@ -132,6 +121,39 @@
     	}
     }
     
+    /**
+     * The method recursively looks for the binary text nodes and adds them as attachment
+     * to soapMessage at the same time removing them from soapEnv and putting appropriate
+     * contentId
+     * @param element
+     * @param soapMsg
+     */
+    private void extractAttachmentNodes(OMElement element, SOAPMessage soapMsg){
+    	Iterator childIter = element.getChildElements();
+    	while(childIter.hasNext()) {
+    		OMNode child = (OMNode)childIter.next();
+    		if(child instanceof OMText){
+    			OMText binaryNode = (OMText)child;
+    			DataHandler actualDH = binaryNode.getDataHandler();
+    			if(actualDH != null){
+    				AttachmentPart ap = soapMsg.createAttachmentPart(actualDH);
+    				String contentId = SessionUtils.generateSessionId();
+    				ap.setContentId(contentId);
+    				ap.setContentType(actualDH.getContentType());
+    				OMElement parent = (OMElement)child.getParent();
+    				OMAttribute attr = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMAttribute("href",
null,"cid:"+contentId);
+    				parent.addAttribute(attr);
+    				binaryNode.detach();
+    				soapMsg.addAttachmentPart(ap);
+    			}
+    		} else{
+				if(child instanceof OMElement) {
+					OMElement childElement = (OMElement)child;
+					extractAttachmentNodes(childElement, soapMsg);
+				}
+    		}
+    	}
+    }
     /**
      * This method checks the value of attribute and if it is a valid CID then
      * returns the contentID (with cid: prefix stripped off) or else returns null.

Added: webservices/axis2/trunk/java/modules/saaj/test-resources/Echo.aar
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/saaj/test-resources/Echo.aar?rev=267222&view=auto
==============================================================================
Binary file - no diff available.

Propchange: webservices/axis2/trunk/java/modules/saaj/test-resources/Echo.aar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: webservices/axis2/trunk/java/modules/saaj/test/org/apache/axis2/saaj/JUnitTestAttachmentIntegration.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/saaj/test/org/apache/axis2/saaj/JUnitTestAttachmentIntegration.java?rev=267222&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/saaj/test/org/apache/axis2/saaj/JUnitTestAttachmentIntegration.java
(added)
+++ webservices/axis2/trunk/java/modules/saaj/test/org/apache/axis2/saaj/JUnitTestAttachmentIntegration.java
Fri Sep  2 08:30:02 2005
@@ -0,0 +1,88 @@
+package org.apache.axis2.saaj;
+
+import junit.framework.TestCase;
+import org.apache.axis2.attachments.utils.ImageDataSource;
+
+import javax.activation.DataHandler;
+import javax.activation.FileDataSource;
+import javax.xml.soap.*;
+import java.awt.Image;
+import java.awt.Toolkit;
+import java.io.File;
+import java.util.Iterator;
+//import javax.activation.FileDataSource;
+
+public class JUnitTestAttachmentIntegration extends TestCase {
+	
+    public JUnitTestAttachmentIntegration(String name) {
+        super(name);
+    }
+    
+    public static void main(String args[]) throws Exception {
+    	JUnitTestAttachmentIntegration tester = new JUnitTestAttachmentIntegration("tester");
+        testSendReceive();
+    }
+
+	public static void testSendReceive() throws Exception{
+		MessageFactory mf = MessageFactory.newInstance();
+		SOAPMessage message = mf.createMessage();
+		
+		//create the SOAPPart
+		createSOAPPart(message);
+		
+		//Attach a text/plain object with the SOAP message
+		String sampleMessage = "Sample Message: Hello World!";
+		AttachmentPart textAttach = message.createAttachmentPart(sampleMessage,"text/plain");
+		textAttach.addMimeHeader("Content-Transfer-Encoding", "binary");
+		textAttach.setContentId("submitSampleText@apache.org");
+		message.addAttachmentPart(textAttach);
+		
+		//Attach a java.awt.Image object to the SOAP message
+        String jpgfilename = "./test-resources/axis.jpg";
+        File myfile = new File(jpgfilename);
+        FileDataSource fds = new FileDataSource(myfile);
+		DataHandler imageDH = new DataHandler(fds);
+		AttachmentPart jpegAttach = message.createAttachmentPart(imageDH);
+		jpegAttach.addMimeHeader("Content-Transfer-Encoding", "binary");
+		jpegAttach.setContentId("submitSampleImage@apache.org");
+		message.addAttachmentPart(jpegAttach);
+		
+		SOAPConnection sCon = SOAPConnectionFactory.newInstance().createConnection();
+		
+		SOAPMessage sMsg =  sCon.call(message,"http://localhost:8080/axis2/services/Echo");
+		int attachmentCount = sMsg.countAttachments();
+		assertTrue(attachmentCount == 2);
+
+		Iterator attachIter = sMsg.getAttachments();		
+		//Of the two attachments first should be of type text/plain and 
+		//second of content-type image/jpeg
+		
+		//Underlying MTOM is converting all contentTypes to application/octet-stream
+		//Thats something to be fixed, I guess. Till then commenting out
+		//these two asserts
+		/*
+		AttachmentPart ap1 = (AttachmentPart)attachIter.next();
+		assertTrue(ap1.getContentType().equals("text/plain"));
+		AttachmentPart ap2 = (AttachmentPart)attachIter.next();
+		assertTrue(ap2.getContentType().equals("image/jpeg"));
+		*/
+	}
+	
+	private static void createSOAPPart(SOAPMessage message) throws SOAPException {
+		SOAPPart sPart = message.getSOAPPart();
+		SOAPEnvelope env = sPart.getEnvelope();
+		SOAPBody body = env.getBody();
+		
+		Name ns = env.createName("echo","swa","http://fakeNamespace.org");
+		SOAPBodyElement sbe = body.addBodyElement(ns);
+		
+		Name ns2 = env.createName("text");
+		SOAPElement textReference = sbe.addChildElement(ns2);
+		Name hrefAttr = env.createName("href");
+		textReference.addAttribute(hrefAttr, "cid:submitSampleText@apache.org");
+		
+		Name ns3 = env.createName("image");
+		SOAPElement imageReference = sbe.addChildElement(ns3);
+		imageReference.addAttribute(hrefAttr, "cid:submitSampleImage@apache.org");
+	}
+}

Added: webservices/axis2/trunk/java/modules/saaj/test/org/apache/axis2/saaj/Readme.txt
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/saaj/test/org/apache/axis2/saaj/Readme.txt?rev=267222&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/saaj/test/org/apache/axis2/saaj/Readme.txt (added)
+++ webservices/axis2/trunk/java/modules/saaj/test/org/apache/axis2/saaj/Readme.txt Fri Sep
 2 08:30:02 2005
@@ -0,0 +1,16 @@
+The testcase JUnitTestAttachmentsIntegration.java is a round trip 
+integration test written to put to test the attachment support added 
+to SAAJ module. However this test can't be run as part of maven b'coz
+of following pre-requisites
+A web service to echo back attachments is expected to be up and running
+at "http://localhost:8080/axis2/services/Echo" with operation name as
+"echo". We don't want the maven to break because of this dependency.
+The name of the test case is hence modified not to match *Test.java pattern,
+so that maven wouldn't run it.
+
+However, for those who want to test this round trip test of attachments
+support, we are providing the Echo.aar service in the test-resources
+folder. Hot deploy this service on a servlet container at 8080 port and
+manually run this test case.
+
+****
\ No newline at end of file



Mime
View raw message