geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From xuhaih...@apache.org
Subject svn commit: r1135361 - /geronimo/bundles/trunk/axis2-saaj/src/main/java/org/apache/axis2/saaj/SOAPMessageImpl.java
Date Tue, 14 Jun 2011 02:14:44 GMT
Author: xuhaihong
Date: Tue Jun 14 02:14:43 2011
New Revision: 1135361

URL: http://svn.apache.org/viewvc?rev=1135361&view=rev
Log:
Update the patch, Fix AXIS2-5067 content-type header is not generated with SAAJ API from Geronimo
side

Modified:
    geronimo/bundles/trunk/axis2-saaj/src/main/java/org/apache/axis2/saaj/SOAPMessageImpl.java

Modified: geronimo/bundles/trunk/axis2-saaj/src/main/java/org/apache/axis2/saaj/SOAPMessageImpl.java
URL: http://svn.apache.org/viewvc/geronimo/bundles/trunk/axis2-saaj/src/main/java/org/apache/axis2/saaj/SOAPMessageImpl.java?rev=1135361&r1=1135360&r2=1135361&view=diff
==============================================================================
--- geronimo/bundles/trunk/axis2-saaj/src/main/java/org/apache/axis2/saaj/SOAPMessageImpl.java
(original)
+++ geronimo/bundles/trunk/axis2-saaj/src/main/java/org/apache/axis2/saaj/SOAPMessageImpl.java
Tue Jun 14 02:14:43 2011
@@ -19,14 +19,16 @@
 
 package org.apache.axis2.saaj;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Hashtable;
-import java.util.Iterator;
-import java.util.Map;
+import org.apache.axiom.attachments.Attachments;
+import org.apache.axiom.om.OMException;
+import org.apache.axiom.om.OMOutputFormat;
+import org.apache.axiom.om.impl.OMMultipartWriter;
+import org.apache.axiom.soap.SOAPEnvelope;
+import org.apache.axiom.soap.impl.dom.soap11.SOAP11Factory;
+import org.apache.axiom.soap.impl.dom.soap12.SOAP12Factory;
+import org.apache.axiom.util.UIDGenerator;
+import org.apache.axis2.saaj.util.SAAJUtil;
+import org.apache.axis2.transport.http.HTTPConstants;
 
 import javax.mail.internet.ContentType;
 import javax.mail.internet.ParseException;
@@ -40,16 +42,14 @@ import javax.xml.soap.SOAPHeader;
 import javax.xml.soap.SOAPMessage;
 import javax.xml.soap.SOAPPart;
 
-import org.apache.axiom.attachments.Attachments;
-import org.apache.axiom.om.OMException;
-import org.apache.axiom.om.OMOutputFormat;
-import org.apache.axiom.om.impl.OMMultipartWriter;
-import org.apache.axiom.soap.SOAPEnvelope;
-import org.apache.axiom.soap.impl.dom.soap11.SOAP11Factory;
-import org.apache.axiom.soap.impl.dom.soap12.SOAP12Factory;
-import org.apache.axiom.util.UIDGenerator;
-import org.apache.axis2.saaj.util.SAAJUtil;
-import org.apache.axis2.transport.http.HTTPConstants;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.Map;
 
 public class SOAPMessageImpl extends SOAPMessage {
 
@@ -169,6 +169,7 @@ public class SOAPMessageImpl extends SOA
      */
     public void removeAllAttachments() {
         attachmentParts.clear();
+        saveRequired = true;
     }
 
     /**
@@ -229,6 +230,7 @@ public class SOAPMessageImpl extends SOA
         if (attachmentPart != null) {
             attachmentParts.add(attachmentPart);
             mimeHeaders.setHeader(HTTPConstants.CONTENT_TYPE, "multipart/related");
+            saveRequired = true;
         }
     }
 
@@ -272,19 +274,23 @@ public class SOAPMessageImpl extends SOA
      * @throws SOAPException if there was a problem saving changes to this message.
      */
     public void saveChanges() throws SOAPException {
-        if (!saveRequired) {
-            return;
-        }        
         try {
-            String[] contentTypes = mimeHeaders.getHeader(HTTPConstants.CONTENT_TYPE);
+            String contentTypeValue = getSingleHeaderValue(HTTPConstants.CONTENT_TYPE);
             ContentType contentType = null;
-            if (contentTypes == null || contentTypes.length == 0) {
-                if(attachmentParts.size() > 0) {
-                    contentType = new ContentType(HTTPConstants.HEADER_ACCEPT_MULTIPART_RELATED);
-                }                
-            } else {
-                contentType = new ContentType(contentTypes[0]);
+            if (isEmptyString(contentTypeValue)) {
+                if (attachmentParts.size() > 0) {
+                    contentTypeValue = HTTPConstants.HEADER_ACCEPT_MULTIPART_RELATED;
+                } else {
+                    contentTypeValue = getBaseType();
+                }
+            }
+            contentType = new ContentType(contentTypeValue);
+            
+            //Use configures the baseType with multipart/related while no attachment exists
or all the attachments are removed
+            if(contentType.getBaseType().equals(HTTPConstants.HEADER_ACCEPT_MULTIPART_RELATED)
&& attachmentParts.size() == 0) {
+                contentType = new ContentType(getBaseType());
             }
+           
             //If it is of multipart/related, initialize those required values in the content-type,
including boundary etc.
             if (contentType.getBaseType().equals(HTTPConstants.HEADER_ACCEPT_MULTIPART_RELATED))
{
                 
@@ -293,17 +299,14 @@ public class SOAPMessageImpl extends SOA
                 if (isEmptyString(boundaryParam)) {
                     contentType.setParameter("boundary", UIDGenerator.generateMimeBoundary());
                 }
-                
-                //Configure start conent id
-                String rootContentId = contentType.getParameter("start");
-                if (isEmptyString(rootContentId)) {
-                    String soapPartContentId = soapPart.getContentId();
-                    if (isEmptyString(soapPartContentId)) {
-                        rootContentId = "<" + UIDGenerator.generateContentId() + ">";
-                        soapPart.setContentId(rootContentId);
-                    }
-                    contentType.setParameter("start", rootContentId);
+
+                //Configure start content id, always get it from soapPart in case it is changed
+                String soapPartContentId = soapPart.getContentId();
+                if (isEmptyString(soapPartContentId)) {
+                    soapPartContentId = "<" + UIDGenerator.generateContentId() + ">";
+                    soapPart.setContentId(soapPartContentId);
                 }
+                contentType.setParameter("start", soapPartContentId);
                 
                 //Configure contentId for each attachments
                 for(AttachmentPart attachmentPart : attachmentParts) {
@@ -311,22 +314,22 @@ public class SOAPMessageImpl extends SOA
                         attachmentPart.setContentId("<" + UIDGenerator.generateContentId()
+ ">");
                     }
                 }
-            }
-
-           //Configure type
-            if (isEmptyString(contentType.getParameter("type"))) {
-                boolean isSOAP12 = ((SOAPEnvelopeImpl) soapPart.getEnvelope()).getOMFactory()
instanceof SOAP12Factory;
-                contentType.setParameter("type", isSOAP12 ? HTTPConstants.MEDIA_TYPE_APPLICATION_SOAP_XML
: HTTPConstants.MEDIA_TYPE_TEXT_XML);
-            }
-            
-            //Configure encoding
-            String encoding = contentType.getParameter("charset");
-            if (isEmptyString(encoding)) {
-                encoding = (String)getProperty(CHARACTER_SET_ENCODING); 
-                if(isEmptyString(encoding)) {
-                    encoding = "UTF-8";
-                }
-                contentType.setParameter("charset", encoding);
+                
+                //Configure type                
+                contentType.setParameter("type", getBaseType());
+                
+                //Configure charset
+                String soapPartContentTypeValue = getSingleHeaderValue(soapPart.getMimeHeader(HTTPConstants.CONTENT_TYPE));
+                ContentType soapPartContentType = null;
+                if (isEmptyString(soapPartContentTypeValue)) {
+                    soapPartContentType = new ContentType(soapPartContentTypeValue);
+                } else {
+                    soapPartContentType = new ContentType(getBaseType());
+                }                
+                setCharsetParameter(soapPartContentType);
+            } else {
+                //Configure charset
+                setCharsetParameter(contentType);
             }
             
             mimeHeaders.setHeader(HTTPConstants.CONTENT_TYPE, contentType.toString());
@@ -335,7 +338,6 @@ public class SOAPMessageImpl extends SOA
         }
 
         saveRequired = false;
-        // TODO not sure of the implementation
     }
 
     public void setSaveRequired() {
@@ -366,11 +368,7 @@ public class SOAPMessageImpl extends SOA
      * @throws IOException   if an I/O error occurs
      */
     public void writeTo(OutputStream out) throws SOAPException, IOException {
-        try {
-            //In case the user remove the content-type header after invoking the saveChanges()
method
-            if(!saveRequired && getSingleHeaderValue(HTTPConstants.CONTENT_TYPE)
== null) {
-                saveRequired = true;
-            }
+        try {           
             saveChanges();
             OMOutputFormat format = new OMOutputFormat();
             String enc = (String)getProperty(CHARACTER_SET_ENCODING);
@@ -379,21 +377,36 @@ public class SOAPMessageImpl extends SOA
             if (writeXmlDecl == null || writeXmlDecl.equals("false")) {
                 //SAAJ default case doesn't send XML decl
                 format.setIgnoreXMLDeclaration(true);
-            }                       
+            }
             
             SOAPEnvelope envelope = ((SOAPEnvelopeImpl) soapPart.getEnvelope()).getOMEnvelope();
             if (attachmentParts.isEmpty()) {
                 envelope.serialize(out, format);
             } else {
-                ContentType contentType = new ContentType(getSingleHeaderValue(HTTPConstants.CONTENT_TYPE));
               
-                format.setMimeBoundary(contentType.getParameter("boundary"));
-                String rootContentId = contentType.getParameter("start");
+                ContentType contentType = new ContentType(getSingleHeaderValue(HTTPConstants.CONTENT_TYPE));
+                String boundary = contentType.getParameter("boundary");
+                if(isEmptyString(boundary)) {
+                    boundary = UIDGenerator.generateMimeBoundary();
+                    contentType.setParameter("boundary", boundary);
+                }
+                format.setMimeBoundary(boundary);
+
+                String rootContentId = soapPart.getContentId();
+                if(isEmptyString(rootContentId)) {
+                    rootContentId = "<" + UIDGenerator.generateContentId() + ">";
+                    soapPart.setContentId(rootContentId);
+                }                
+                contentType.setParameter("start", rootContentId);
                 if ((rootContentId.indexOf("<") > -1) & (rootContentId.indexOf(">")
> -1)) {
                     rootContentId = rootContentId.substring(1, (rootContentId.length() -
1));
                 }
-                format.setRootContentId(rootContentId);               
+                format.setRootContentId(rootContentId);
 
                 format.setSOAP11(((SOAPEnvelopeImpl) soapPart.getEnvelope()).getOMFactory()
instanceof SOAP11Factory);
+                
+                //Double save the content-type in case anything is updated
+                mimeHeaders.setHeader(HTTPConstants.CONTENT_TYPE, contentType.toString());
+
                 OMMultipartWriter mpw = new OMMultipartWriter(out, format);
                 OutputStream rootPartOutputStream = mpw.writeRootPart();
                 envelope.serialize(rootPartOutputStream);
@@ -403,6 +416,8 @@ public class SOAPMessageImpl extends SOA
                 }
                 mpw.complete();
             }
+
+            saveRequired = true;
         } catch (Exception e) {
             throw new SOAPException(e);
         }
@@ -530,6 +545,7 @@ public class SOAPMessageImpl extends SOA
         }
         attachmentParts.clear();
         this.attachmentParts = newAttachmentParts;
+        saveRequired = true;
     }
 
     /**
@@ -588,6 +604,10 @@ public class SOAPMessageImpl extends SOA
         return value == null || value.length() == 0;
     }
     
+    private String getSingleHeaderValue(String[] values) {
+        return values != null && values.length > 0 ? values[0] : null;
+    }
+
     private String getSingleHeaderValue(String name) {
         String[] values = mimeHeaders.getHeader(name);
         if (values == null || values.length == 0) {
@@ -596,4 +616,28 @@ public class SOAPMessageImpl extends SOA
             return values[0];
         }
     }
+    
+    private String getBaseType() throws SOAPException {
+        boolean isSOAP12 = ((SOAPEnvelopeImpl) soapPart.getEnvelope()).getOMFactory() instanceof
SOAP12Factory;
+        return isSOAP12 ? HTTPConstants.MEDIA_TYPE_APPLICATION_SOAP_XML : HTTPConstants.MEDIA_TYPE_TEXT_XML;
+    }
+    
+    /**
+     * If the charset is configured by CHARACTER_SET_ENCODING, set it in the contentPart
always. 
+     * If it has already been configured in the contentType, leave it there.
+     * UTF-8 is used as the default value. 
+     * @param contentType
+     * @throws SOAPException
+     */
+    private void setCharsetParameter(ContentType contentType) throws SOAPException{
+        String charset = (String)getProperty(CHARACTER_SET_ENCODING); 
+        if (!isEmptyString(charset)) {
+            contentType.setParameter("charset", charset);
+        } else {
+            charset = contentType.getParameter("charset");
+            if(isEmptyString(charset)) {
+                contentType.setParameter("charset", "UTF-8");
+            }
+        }
+    }
 }



Mime
View raw message