cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a.@apache.org
Subject cxf git commit: [CXF-6431] Attachment serialization does not conform to the relevant specs
Date Mon, 01 Jun 2015 20:13:27 GMT
Repository: cxf
Updated Branches:
  refs/heads/master fb4793ce6 -> 9bbf1acd7


[CXF-6431] Attachment serialization does not conform to the relevant specs


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/9bbf1acd
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/9bbf1acd
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/9bbf1acd

Branch: refs/heads/master
Commit: 9bbf1acd7291416f9afcd312e7dc302e9031fa25
Parents: fb4793c
Author: Akitoshi Yoshida <ay@apache.org>
Authored: Mon Jun 1 21:46:43 2015 +0200
Committer: Akitoshi Yoshida <ay@apache.org>
Committed: Mon Jun 1 21:46:56 2015 +0200

----------------------------------------------------------------------
 .../cxf/attachment/AttachmentSerializer.java    | 56 ++++++++++---------
 .../attachment/AttachmentSerializerTest.java    | 59 ++++++++++++++++----
 2 files changed, 79 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/9bbf1acd/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java b/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java
index 24b4766..62f2bb6 100644
--- a/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java
+++ b/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java
@@ -72,26 +72,19 @@ public class AttachmentSerializer {
         bodyBoundary = AttachmentUtil.getUniqueBoundaryValue();
 
         String bodyCt = (String) message.get(Message.CONTENT_TYPE);
-        
-        // The bodyCt string is used enclosed within "", so if it contains the character
", it
-        // should be adjusted, like in the following case:
-        //   application/soap+xml; action="urn:ihe:iti:2007:RetrieveDocumentSet"
-        // The attribute action is added in SoapActionOutInterceptor, when SOAP 1.2 is used
-        // The string has to be changed in:
-        //   application/soap+xml"; action="urn:ihe:iti:2007:RetrieveDocumentSet
-        // so when it is enclosed within "", the result must be:
-        //   "application/soap+xml"; action="urn:ihe:iti:2007:RetrieveDocumentSet"
-        // instead of 
-        //   "application/soap+xml; action="urn:ihe:iti:2007:RetrieveDocumentSet""
-        // that is wrong because when used it produces:
-        //   type="application/soap+xml; action="urn:ihe:iti:2007:RetrieveDocumentSet""
-        if ((bodyCt.indexOf('"') != -1) && (bodyCt.indexOf(';') != -1)) {
+        String bodyCtParams = null;
+        // split the bodyCt to its head that is the type and its properties so that we
+        // can insert the values at the right places based on the soap version and the mtom
option
+        // bodyCt will be of the form
+        // soap11 -> text/xml
+        // soap12 -> application/soap+xml; action="urn:ihe:iti:2007:RetrieveDocumentSet"
+        if (bodyCt.indexOf(';') != -1) {
             int pos = bodyCt.indexOf(';');
-            StringBuilder st = new StringBuilder(bodyCt.substring(0 , pos));
-            st.append("\"").append(bodyCt.substring(pos, bodyCt.length() - 1));
-            bodyCt = st.toString();
-        }        
-        
+            // get everything from the semi-colon
+            bodyCtParams = bodyCt.substring(pos);
+            // keep the type/subtype part in bodyCt
+            bodyCt = bodyCt.substring(0, pos);
+        }
         // Set transport mime type
         String requestMimeType = multipartType == null ? DEFAULT_MULTIPART_TYPE : multipartType;
         
@@ -131,10 +124,14 @@ public class AttachmentSerializer {
         
         // start-info is a required parameter for XOP/MTOM, may be needed for
         // other WS cases but is redundant in simpler multipart/related cases
+        // the parameters need to be included within the start-info's value in the escaped
form
         if (writeOptionalTypeParameters || xop) {
             ct.append("; start-info=\"")
-                .append(bodyCt)
-                .append("\"");
+                .append(bodyCt);
+            if (bodyCtParams != null) {
+                ct.append(escapeQuotes(bodyCtParams));                
+            }
+            ct.append("\"");
         }
         
         
@@ -154,11 +151,14 @@ public class AttachmentSerializer {
         StringBuilder mimeBodyCt = new StringBuilder();
         String bodyType = getHeaderValue("Content-Type", null);
         if (bodyType == null) {
-            mimeBodyCt.append(xop ? "application/xop+xml; charset=" : "text/xml; charset=")
-                .append(encoding)
-                .append("; type=\"")
-                .append(bodyCt)
-                .append("\"");
+            mimeBodyCt.append(xop ? "application/xop+xml" : bodyCt)
+                .append("; charset=").append(encoding);
+            if (xop) {
+                mimeBodyCt.append("; type=\"").append(bodyCt).append("\"");
+            }
+            if (bodyCtParams != null) {
+                mimeBodyCt.append(bodyCtParams);
+            }
         } else {
             mimeBodyCt.append(bodyType);
         }
@@ -167,6 +167,10 @@ public class AttachmentSerializer {
         out.write(writer.getBuffer().toString().getBytes(encoding));
     }
 
+    private static String escapeQuotes(String s) {
+        return s.indexOf('"') != 0 ? s.replace("\"", "\\\"") : s;    
+    }
+
     private String getHeaderValue(String name, String defaultValue) {
         List<String> value = rootHeaders.get(name);
         if (value == null || value.isEmpty()) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/9bbf1acd/core/src/test/java/org/apache/cxf/attachment/AttachmentSerializerTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/cxf/attachment/AttachmentSerializerTest.java b/core/src/test/java/org/apache/cxf/attachment/AttachmentSerializerTest.java
index 4ddfb19..7645929 100644
--- a/core/src/test/java/org/apache/cxf/attachment/AttachmentSerializerTest.java
+++ b/core/src/test/java/org/apache/cxf/attachment/AttachmentSerializerTest.java
@@ -44,16 +44,26 @@ import org.junit.Test;
 public class AttachmentSerializerTest extends Assert {
     
     @Test
-    public void testMessageWriteXopOn() throws Exception {
-        doTestMessageWrite(true);
+    public void testMessageWriteXopOn1() throws Exception {
+        doTestMessageWrite(true, "text/xml");
     }
     
     @Test
-    public void testMessageWriteXopOff() throws Exception {
-        doTestMessageWrite(false);
+    public void testMessageWriteXopOn2() throws Exception {
+        doTestMessageWrite(true, "application/soap+xml; action=\"urn:foo\"");
     }
     
-    private void doTestMessageWrite(boolean xop) throws Exception {
+    @Test
+    public void testMessageWriteXopOff1() throws Exception {
+        doTestMessageWrite(false, "text/xml");
+    }
+    
+    @Test
+    public void testMessageWriteXopOff2() throws Exception {
+        doTestMessageWrite(false, "application/soap+xml; action=\"urn:foo\"");
+    }
+    
+    private void doTestMessageWrite(boolean xop, String soapContentType) throws Exception
{
         MessageImpl msg = new MessageImpl();
         
         Collection<Attachment> atts = new ArrayList<Attachment>();
@@ -68,8 +78,17 @@ public class AttachmentSerializerTest extends Assert {
         msg.setAttachments(atts);
         
         // Set the SOAP content type
-        msg.put(Message.CONTENT_TYPE, "application/soap+xml");
-        
+        msg.put(Message.CONTENT_TYPE, soapContentType);
+        String soapCtType = null;
+        String soapCtParams = null;
+        int p = soapContentType.indexOf(';');
+        if (p != -1) {
+            soapCtParams = soapContentType.substring(p);
+            soapCtType = soapContentType.substring(0, p);
+        } else {
+            soapCtParams = "";
+            soapCtType = soapContentType;
+        }
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         msg.setContent(OutputStream.class, out);
         
@@ -81,10 +100,22 @@ public class AttachmentSerializerTest extends Assert {
         
         serializer.writeProlog();
 
+        // we expect
+        // - the package header must have type multipart/related
+        // - the start-info property must be present for mtom but otherwise optional
+        // - the action property should not appear directly
+        // - the type property must be application/xop+xml for mtom but otherwise text/xml
or application/soap+xml
         String ct = (String) msg.get(Message.CONTENT_TYPE);
+        System.out.println("##teset ct=" + ct);
         assertTrue(ct.indexOf("multipart/related;") == 0);
         assertTrue(ct.indexOf("start=\"<root.message@cxf.apache.org>\"") > -1);
-        assertTrue(ct.indexOf("start-info=\"application/soap+xml\"") > -1);
+        assertTrue(ct.indexOf("start-info=\"" + escapeQuotes(soapContentType) + "\"") >
-1);
+        assertTrue(ct.indexOf("action=\"") == -1);
+        if (xop) {
+            assertTrue(ct.indexOf("type=\"application/xop+xml\"") > -1);
+        } else {
+            assertTrue(ct.indexOf("type=\"" + soapCtType + "\"") > -1);
+        }
         out.write("<soap:Body/>".getBytes());
         
         serializer.writeAttachments();
@@ -101,11 +132,15 @@ public class AttachmentSerializerTest extends Assert {
         MimeMultipart multipart = (MimeMultipart) inMsg.getContent();
         
         MimeBodyPart part = (MimeBodyPart) multipart.getBodyPart(0);
+        // we expect
+        // - the envelope header must have type application/xop+xml for mtom but otherwise
t
+        // - the start-info property must be present for mtom but otherwise text/xml or application/soap+xml
+        // - the action must appear if it was present in the original message
         if (xop) {
-            assertEquals("application/xop+xml; charset=UTF-8; type=\"application/soap+xml\"",

+            assertEquals("application/xop+xml; charset=UTF-8; type=\"" + soapCtType + "\""
+ soapCtParams, 
                          part.getHeader("Content-Type")[0]);
         } else {
-            assertEquals("text/xml; charset=UTF-8; type=\"application/soap+xml\"", 
+            assertEquals(soapCtType + "; charset=UTF-8" + soapCtParams,  
                          part.getHeader("Content-Type")[0]);
         }
         
@@ -192,4 +227,8 @@ public class AttachmentSerializerTest extends Assert {
         assertEquals("<test.xml>", part2.getHeader("Content-ID")[0]);
         
     }
+
+    private static String escapeQuotes(String s) {
+        return s.indexOf('"') != 0 ? s.replace("\"", "\\\"") : s;    
+    }
 }


Mime
View raw message