cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ff...@apache.org
Subject svn commit: r532651 - in /incubator/cxf/trunk/rt: bindings/soap/src/main/java/org/apache/cxf/binding/soap/saaj/ core/src/main/java/org/apache/cxf/attachment/ core/src/test/java/org/apache/cxf/attachment/
Date Thu, 26 Apr 2007 08:21:06 GMT
Author: ffang
Date: Thu Apr 26 01:21:05 2007
New Revision: 532651

URL: http://svn.apache.org/viewvc?view=rev&rev=532651
Log:
[CXF-605] extract boundary from message body in AttachmentDeserializer
 

Modified:
    incubator/cxf/trunk/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/saaj/SAAJOutInterceptor.java
    incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentDeserializer.java
    incubator/cxf/trunk/rt/core/src/test/java/org/apache/cxf/attachment/AttachmentDeserializerTest.java

Modified: incubator/cxf/trunk/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/saaj/SAAJOutInterceptor.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/saaj/SAAJOutInterceptor.java?view=diff&rev=532651&r1=532650&r2=532651
==============================================================================
--- incubator/cxf/trunk/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/saaj/SAAJOutInterceptor.java
(original)
+++ incubator/cxf/trunk/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/saaj/SAAJOutInterceptor.java
Thu Apr 26 01:21:05 2007
@@ -30,6 +30,8 @@
 import javax.xml.soap.SOAPMessage;
 import javax.xml.soap.SOAPPart;
 import javax.xml.stream.XMLStreamWriter;
+import com.sun.xml.messaging.saaj.packaging.mime.MessagingException;
+
 
 import org.apache.cxf.binding.soap.SoapFault;
 import org.apache.cxf.binding.soap.SoapMessage;
@@ -41,6 +43,8 @@
 import org.apache.cxf.phase.Phase;
 import org.apache.cxf.staxutils.W3CDOMStreamWriter;
 
+
+
 /**
  * Sets up the outgoing chain to build a SAAJ tree instead of writing
  * directly to the output stream. First it will replace the XMLStreamWriter
@@ -89,8 +93,10 @@
             SOAPMessage soapMessage = message.getContent(SOAPMessage.class);
 
             if (soapMessage != null) {
+                
                 OutputStream os = message.getContent(OutputStream.class);
                 try {
+                    setMessageContent(message, soapMessage);
                     soapMessage.writeTo(os);
                     os.flush();
                 } catch (IOException e) {
@@ -99,8 +105,28 @@
                 } catch (SOAPException e) {
                     throw new SoapFault(new Message("SOAPEXCEPTION", BUNDLE), e, message.getVersion()
                         .getSender());
+                } catch (MessagingException e) {
+                    throw new SoapFault(new Message("SOAPEXCEPTION", BUNDLE), e, message.getVersion()
+                        .getSender());
+                }
+            }
+        }
+
+        private void setMessageContent(SoapMessage message, SOAPMessage soapMessage) 
+            throws MessagingException, SOAPException {
+            
+            if (soapMessage.getAttachments().hasNext()) {
+                StringBuffer sb = new StringBuffer();
+                for (String str : soapMessage.getMimeHeaders().getHeader("Content-Type"))
{
+                    sb.append(str);
+                }
+                String contentType = sb.toString();
+                if (contentType != null && contentType.length() > 0) {
+                    message.put(org.apache.cxf.message.Message.CONTENT_TYPE, contentType);
                 }
+                    
             }
+            
         }
 
     }

Modified: incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentDeserializer.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentDeserializer.java?view=diff&rev=532651&r1=532650&r2=532651
==============================================================================
--- incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentDeserializer.java
(original)
+++ incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentDeserializer.java
Thu Apr 26 01:21:05 2007
@@ -19,6 +19,8 @@
 
 package org.apache.cxf.attachment;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.PushbackInputStream;
@@ -32,6 +34,7 @@
 import javax.mail.internet.InternetHeaders;
 
 import org.apache.cxf.helpers.IOUtils;
+import org.apache.cxf.interceptor.Fault;
 import org.apache.cxf.message.Attachment;
 import org.apache.cxf.message.Message;
 
@@ -75,31 +78,21 @@
             throw new IllegalStateException("Content-Type can not be empty!");
         }
         
-        InputStream input = message.getContent(InputStream.class);
-        if (input == null) {
-            throw new IllegalStateException("An InputStream must be provided!");
-        }
+        
 
         if (contentType.toLowerCase().indexOf("multipart/related") != -1) {
-            int i = contentType.indexOf("boundary=\"");
-            int end;
-            int len;
-            if (i == -1) {
-                i = contentType.indexOf("boundary=");
-                end = contentType.indexOf(";", i + 9);
-                if (end == -1) {
-                    end = contentType.length();
-                }
-                len = 9;
-            } else {
-                end = contentType.indexOf("\"", i + 10);
-                len = 10;
-            }
-            if (i == -1 || end == -1) {
-                throw new IOException("Invalid content type: missing boundary! " + contentType);
+            boundary = findBoundry();
+            boundary = "--" + boundary;
+            
+            
+            InputStream input = message.getContent(InputStream.class);
+            if (input == null) {
+                throw new IllegalStateException("An InputStream must be provided!");
             }
-            boundary = "--" + contentType.substring(i + len, end);
-            stream = new PushbackInputStream(input, boundary.length());
+            stream = new PushbackInputStream(input, boundary.getBytes().length);
+            
+            
+            
             if (!readTillFirstBoundary(stream, boundary.getBytes())) {
                 throw new IOException("Couldn't find MIME boundary: " + boundary);
             }
@@ -116,6 +109,30 @@
         }
     }
 
+    private String findBoundry() {
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        try {
+            InputStream is = message.getContent(InputStream.class);
+            IOUtils.copy(is, bos);
+
+            is.close();
+            bos.close();
+            String msg = bos.toString();
+            message.setContent(InputStream.class, new ByteArrayInputStream(bos.toByteArray()));
+            if (msg.indexOf("----=_Part_") == -1) {
+                return null;
+            } else {
+                int begin = msg.indexOf("----=_Part_");
+                int end = msg.indexOf(".", begin) + 14;
+                return msg.substring(begin, end);
+            }
+
+        } catch (IOException e) {
+            throw new Fault(e);
+        }
+
+    }
+    
     public AttachmentImpl readNext() throws IOException {
         // Cache any mime parts that are currently being streamed
         cacheStreamedAttachments();

Modified: incubator/cxf/trunk/rt/core/src/test/java/org/apache/cxf/attachment/AttachmentDeserializerTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/core/src/test/java/org/apache/cxf/attachment/AttachmentDeserializerTest.java?view=diff&rev=532651&r1=532650&r2=532651
==============================================================================
--- incubator/cxf/trunk/rt/core/src/test/java/org/apache/cxf/attachment/AttachmentDeserializerTest.java
(original)
+++ incubator/cxf/trunk/rt/core/src/test/java/org/apache/cxf/attachment/AttachmentDeserializerTest.java
Thu Apr 26 01:21:05 2007
@@ -84,6 +84,52 @@
         String ct = "multipart/related; type=\"text/xml\"; "
             + "start=\"<86048FF3556694F7DA1918466DDF8143>\";    "
             + "boundary=\"----=_Part_0_14158819.1167275505862\"";
+
+        
+        MessageImpl msg = new MessageImpl();
+        msg.put(Message.CONTENT_TYPE, ct);
+        msg.setContent(InputStream.class, is);
+        
+        AttachmentDeserializer deserializer = new AttachmentDeserializer(msg);
+        deserializer.initializeAttachments();
+        
+        InputStream attBody = msg.getContent(InputStream.class);
+        assertTrue(attBody != is);
+        assertTrue(attBody instanceof DelegatingInputStream);
+        
+        Collection<Attachment> atts = msg.getAttachments();
+        assertNotNull(atts);
+        
+        Iterator<Attachment> itr = atts.iterator();
+        assertTrue(itr.hasNext());
+        
+        Attachment a = itr.next();
+        assertNotNull(a);
+        
+        InputStream attIs = a.getDataHandler().getInputStream();
+        
+        assertTrue(((DelegatingInputStream) attIs).getInputStream() instanceof MimeBodyPartInputStream);
+        assertTrue(((DelegatingInputStream) attBody).getInputStream() instanceof ByteArrayInputStream);
+        
+        // check the cached output stream
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        IOUtils.copy(attBody, out);
+        assertTrue(out.toString().startsWith("<?xml"));
+        
+        // try streaming a character off the wire
+        assertTrue(attIs.read() == 'f');
+        assertTrue(attIs.read() == 'o');
+        assertTrue(attIs.read() == 'o');
+        assertTrue(attIs.read() == 'b');
+        assertTrue(attIs.read() == 'a');
+        assertTrue(attIs.read() == 'r');
+        assertTrue(attIs.read() == -1);
+    }
+    
+    public void testDeserializerSwAWithoutBoundryInContentType() throws Exception {
+        InputStream is = getClass().getResourceAsStream("swadata");
+        String ct = "multipart/related; type=\"text/xml\"; ";
+
         
         MessageImpl msg = new MessageImpl();
         msg.put(Message.CONTENT_TYPE, ct);



Mime
View raw message