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 di...@apache.org on 2005/07/14 16:37:25 UTC

svn commit: r219042 - in /webservices/axis/trunk/java/modules/xml: src/org/apache/axis2/om/impl/ src/org/apache/axis2/om/impl/llom/ test/org/apache/axis2/om/

Author: dims
Date: Thu Jul 14 07:37:23 2005
New Revision: 219042

URL: http://svn.apache.org/viewcvs?rev=219042&view=rev
Log:
Hook up the content-id for the attachments properly.


Modified:
    webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/MIMEOutputUtils.java
    webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/OMOutputImpl.java
    webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/OMTextImpl.java
    webservices/axis/trunk/java/modules/xml/test/org/apache/axis2/om/MIMEOutputUtilsTest.java

Modified: webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/MIMEOutputUtils.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/MIMEOutputUtils.java?rev=219042&r1=219041&r2=219042&view=diff
==============================================================================
--- webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/MIMEOutputUtils.java (original)
+++ webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/MIMEOutputUtils.java Thu Jul 14 07:37:23 2005
@@ -26,16 +26,20 @@
 import java.io.OutputStream;
 import java.util.Iterator;
 import java.util.LinkedList;
+import java.util.Random;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 
 public class MIMEOutputUtils {
 
     private static byte[] CRLF = {13, 10};
-
-    static String SOAP_PART_CONTENT_ID = "<http://apache.org/soappart>";
+    private static Random myRand = null;
 
     public static void complete(OutputStream outStream,
                                 OutputStream bufferedSoapOutStream, LinkedList binaryNodeList,
-                                String boundary) {
+                                String boundary, String contentId) {
         try {
             startWritingMime(outStream, boundary);
 
@@ -50,7 +54,7 @@
             rootMimeBodyPart.addHeader("Content-Type",
                     partContentType.toString());
             rootMimeBodyPart.addHeader("Content-Transfer-Encoding", "8bit");
-            rootMimeBodyPart.addHeader("Content-ID", SOAP_PART_CONTENT_ID);
+            rootMimeBodyPart.addHeader("Content-ID", contentId);
 
             writeBodyPart(outStream, rootMimeBodyPart, boundary);
 
@@ -73,7 +77,7 @@
         MimeBodyPart mimeBodyPart = new MimeBodyPart();
         mimeBodyPart.setDataHandler(node.getDataHandler());
         mimeBodyPart.addHeader("Content-Transfer-Encoding", "binary");
-        mimeBodyPart.addHeader("Content-ID", "<" + node.getContentID() + ">");
+        mimeBodyPart.addHeader("Content-ID", node.getContentID());
         return mimeBodyPart;
 
     }
@@ -82,7 +86,7 @@
      * @throws IOException This will write the boundary to output Stream
      */
     public static void writeMimeBoundary(OutputStream outStream,
-                                            String boundary) throws IOException {
+                                         String boundary) throws IOException {
         outStream.write(new byte[]{45, 45});
         outStream.write(boundary.getBytes());
     }
@@ -91,7 +95,7 @@
      * @throws IOException This will write the boundary with CRLF
      */
     public static void startWritingMime(OutputStream outStream,
-                                           String boundary)
+                                        String boundary)
             throws IOException {
         writeMimeBoundary(outStream, boundary);
         //outStream.write(CRLF);
@@ -106,7 +110,7 @@
      * @throws MessagingException
      */
     public static void writeBodyPart(OutputStream outStream,
-                                        MimeBodyPart part, String boundary) throws IOException,
+                                     MimeBodyPart part, String boundary) throws IOException,
             MessagingException {
         outStream.write(CRLF);
         part.writeTo(outStream);
@@ -122,13 +126,12 @@
         outStream.write(new byte[]{45, 45});
     }
 
-    public static String getContentTypeForMime(String boundary) {
+    public static String getContentTypeForMime(String boundary, String contentId) {
         ContentType contentType = new ContentType();
         contentType.setPrimaryType("multipart");
         contentType.setSubType("related");
         contentType.setParameter("boundary", boundary);
-        contentType.setParameter("start",
-                MIMEOutputUtils.SOAP_PART_CONTENT_ID);
+        contentType.setParameter("start", contentId);
         contentType.setParameter("type", "application/xop+xml");
         //TODO theres something called action that can be set with
         // following. May be SOAPAction. Better check.
@@ -136,4 +139,45 @@
         return contentType.toString();
     }
 
+    /**
+     * MD5 a random string with localhost/date etc will return 128 bits
+     * construct a string of 18 characters from those bits.
+     * @return string
+     */
+    public static String getRandomStringOf18Characters() {
+        if (myRand == null) {
+            myRand = new Random();
+        }
+        long rand = myRand.nextLong();
+        String sid;
+        try {
+            sid = InetAddress.getLocalHost().toString();
+        } catch (UnknownHostException e) {
+            sid = Thread.currentThread().getName();
+        }
+        long time = System.currentTimeMillis();
+        StringBuffer sb = new StringBuffer();
+        sb.append(sid);
+        sb.append(":");
+        sb.append(Long.toString(time));
+        sb.append(":");
+        sb.append(Long.toString(rand));
+        MessageDigest md5 = null;
+        try {
+            md5 = MessageDigest.getInstance("MD5");
+        } catch (NoSuchAlgorithmException e) {
+            System.out.println("Error: " + e);
+        }
+        md5.update(sb.toString().getBytes());
+        byte[] array = md5.digest();
+        StringBuffer sb2 = new StringBuffer();
+        for (int j = 0; j < array.length; ++j) {
+            int b = array[j] & 0xFF;
+            sb2.append(Integer.toHexString(b));
+        }
+        int begin = myRand.nextInt();
+        if(begin < 0) begin = begin * -1;
+        begin = begin % 8;
+        return new String("--" + sb2.toString().substring(begin, begin + 18)).toUpperCase();
+    }
 }

Modified: webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/OMOutputImpl.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/OMOutputImpl.java?rev=219042&r1=219041&r2=219042&view=diff
==============================================================================
--- webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/OMOutputImpl.java (original)
+++ webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/OMOutputImpl.java Thu Jul 14 07:37:23 2005
@@ -42,6 +42,8 @@
     private LinkedList binaryNodeList;
     private ByteArrayOutputStream bufferedSoapOutStream;
     private String mimeBoundary = null;
+    private String rootContentId = null;
+    private int nextid = 0;
 
     public OMOutputImpl() {
     }
@@ -76,7 +78,7 @@
         xmlWriter.flush();
         if (doOptimize) {
             MIMEOutputUtils.complete(outStream, bufferedSoapOutStream,
-                    binaryNodeList, getMimeBoundary());
+                    binaryNodeList, getMimeBoundary(), getRootContentId());
         }
     }
 
@@ -85,7 +87,7 @@
     }
 
     public String getOptimizedContentType() {
-        return org.apache.axis2.om.impl.MIMEOutputUtils.getContentTypeForMime(getMimeBoundary());
+        return MIMEOutputUtils.getContentTypeForMime(getMimeBoundary(),getRootContentId());
     }
 
     public void writeOptimized(OMText node) {
@@ -102,52 +104,19 @@
 
     public String getMimeBoundary() {
         if(mimeBoundary != null) {
-            mimeBoundary = "--MIMEBoundary" + getRandomStringOf18Characters();
+            mimeBoundary = "--MIMEBoundary" + MIMEOutputUtils.getRandomStringOf18Characters();
         }
         return mimeBoundary;
     }
 
-    private static Random myRand = null;
-
-    /**
-     * MD5 a random string with localhost/date etc will return 128 bits
-     * construct a string of 18 characters from those bits.
-     * @return string
-     */
-    private static String getRandomStringOf18Characters() {
-        if (myRand == null) {
-            myRand = new Random();
-        }
-        long rand = myRand.nextLong();
-        String sid;
-        try {
-            sid = InetAddress.getLocalHost().toString();
-        } catch (UnknownHostException e) {
-            sid = Thread.currentThread().getName();
-        }
-        long time = System.currentTimeMillis();
-        StringBuffer sb = new StringBuffer();
-        sb.append(sid);
-        sb.append(":");
-        sb.append(Long.toString(time));
-        sb.append(":");
-        sb.append(Long.toString(rand));
-        MessageDigest md5 = null;
-        try {
-            md5 = MessageDigest.getInstance("MD5");
-        } catch (NoSuchAlgorithmException e) {
-            System.out.println("Error: " + e);
+    public String getRootContentId() {
+        if(rootContentId != null) {
+            rootContentId = "cid:0." + MIMEOutputUtils.getRandomStringOf18Characters() + "@apache.org";
         }
-        md5.update(sb.toString().getBytes());
-        byte[] array = md5.digest();
-        StringBuffer sb2 = new StringBuffer();
-        for (int j = 0; j < array.length; ++j) {
-            int b = array[j] & 0xFF;
-            sb2.append(Integer.toHexString(b));
-        }
-        int begin = myRand.nextInt();
-        if(begin < 0) begin = begin * -1;
-        begin = begin % 8;
-        return new String("--" + sb2.toString().substring(begin, begin + 18)).toUpperCase();
+        return rootContentId;
+    }
+
+    public String getNextContentId() {
+        return "cid:" + nextid + "." + MIMEOutputUtils.getRandomStringOf18Characters() + "@apache.org";
     }
 }

Modified: webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/OMTextImpl.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/OMTextImpl.java?rev=219042&r1=219041&r2=219042&view=diff
==============================================================================
--- webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/OMTextImpl.java (original)
+++ webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/OMTextImpl.java Thu Jul 14 07:37:23 2005
@@ -25,6 +25,7 @@
 import org.apache.axis2.om.OMNamespace;
 import org.apache.axis2.om.OMNode;
 import org.apache.axis2.om.impl.OMOutputImpl;
+import org.apache.axis2.om.impl.MIMEOutputUtils;
 import org.apache.axis2.om.OMText;
 import org.apache.axis2.om.OMXMLParserWrapper;
 import org.apache.axis2.om.impl.llom.mtom.MTOMStAXSOAPModelBuilder;
@@ -109,9 +110,6 @@
         this(parent, s);
         this.mimeType = mimeType;
         this.optimize = optimize;
-        if (this.contentID == null && optimize) {
-            createContentID();
-        }
         done = true;
         this.nodeType = TEXT_NODE;
     }
@@ -131,9 +129,6 @@
         this.dataHandler = dataHandler;
         this.isBinary = true;
         this.optimize = optimize;
-        if (this.contentID == null && optimize) {
-            createContentID();
-        }
         done = true;
         this.nodeType = TEXT_NODE;
     }
@@ -200,9 +195,6 @@
 
     public void setOptimize(boolean value) {
         this.optimize = value;
-        if (this.contentID == null && value) {
-            getContentID();
-        }
     }
 
     /**
@@ -227,6 +219,9 @@
             }
             return new DataHandler(dataSource);
         } else {
+            if (contentID == null){
+                throw new RuntimeException("ContentID is null");
+            }
             if (dataHandler == null) {
                 dataHandler = ((MTOMStAXSOAPModelBuilder) builder)
                         .getDataHandler(contentID);
@@ -258,7 +253,10 @@
     }
 
     public String getContentID() {
-        return this.contentID;
+        if(contentID == null) {
+            contentID = "cid:" + MIMEOutputUtils.getRandomStringOf18Characters() + "@apache.org";
+        }
+        return contentID;
     }
 
     public boolean isComplete() {
@@ -270,12 +268,14 @@
             serializeWithCache(omOutput);
         } else {
             if (omOutput.isOptimized()) {
+                if(contentID == null){
+                    contentID = omOutput.getNextContentId();
+                }
                 // send binary as MTOM optimised
                 this.attribute =
                         new OMAttributeImpl("href",
                                 new OMNamespaceImpl("", ""),
-                                "cid:"
-                                        + this.contentID.trim());
+                                contentID);
                 this.serializeStartpart(omOutput);
                 omOutput.writeOptimized(this);
                 omOutput.getXmlStreamWriter().writeEndElement();
@@ -296,13 +296,6 @@
                 }
             }
         }
-    }
-
-    private void createContentID() {
-        // We can use a UUID, taken using Apache commons id project.
-        // TODO change to UUID
-        this.contentID = "2" + String.valueOf(rnd.nextLong()) +
-                "@schemas.xmlsoap.org";
     }
 
     /*

Modified: webservices/axis/trunk/java/modules/xml/test/org/apache/axis2/om/MIMEOutputUtilsTest.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/xml/test/org/apache/axis2/om/MIMEOutputUtilsTest.java?rev=219042&r1=219041&r2=219042&view=diff
==============================================================================
--- webservices/axis/trunk/java/modules/xml/test/org/apache/axis2/om/MIMEOutputUtilsTest.java (original)
+++ webservices/axis/trunk/java/modules/xml/test/org/apache/axis2/om/MIMEOutputUtilsTest.java Thu Jul 14 07:37:23 2005
@@ -45,7 +45,7 @@
         ByteArrayOutputStream outStream;
         String boundary = "----TemporaryBoundary";
 
-        String contentType = org.apache.axis2.om.impl.MIMEOutputUtils.getContentTypeForMime(boundary);
+        String contentType = org.apache.axis2.om.impl.MIMEOutputUtils.getContentTypeForMime(boundary, "cid:0.632569289925808400@example.org");
         DataHandler dataHandler;
         dataHandler = new DataHandler(new ByteArrayDataSource(byteArray));
         OMText textData = factory.createText(dataHandler, true);