You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-commits@axis.apache.org by ve...@apache.org on 2012/04/30 18:16:30 UTC

svn commit: r1332277 - in /axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/builder: MIMEAwareBuilder.java MIMEBuilder.java MTOMBuilder.java SOAPBuilder.java

Author: veithen
Date: Mon Apr 30 16:16:30 2012
New Revision: 1332277

URL: http://svn.apache.org/viewvc?rev=1332277&view=rev
Log:
Made the design of the message builders responsible for building SwA and MTOM messages a bit more object oriented by introducing a MIMEAwareBuilder extension interface. MIMEBuilder now essentially dispatches to an instance of that interface.

This allows us to use the proper Axiom API to build MTOM messages. It will also make it easier to implement AXIS2-5308.

Added:
    axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/builder/MIMEAwareBuilder.java   (with props)
Modified:
    axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/builder/MIMEBuilder.java
    axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/builder/MTOMBuilder.java
    axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/builder/SOAPBuilder.java

Added: axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/builder/MIMEAwareBuilder.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/builder/MIMEAwareBuilder.java?rev=1332277&view=auto
==============================================================================
--- axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/builder/MIMEAwareBuilder.java (added)
+++ axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/builder/MIMEAwareBuilder.java Mon Apr 30 16:16:30 2012
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axis2.builder;
+
+import org.apache.axiom.attachments.Attachments;
+import org.apache.axiom.om.OMElement;
+import org.apache.axis2.AxisFault;
+import org.apache.axis2.context.MessageContext;
+
+/**
+ * Extension interface for {@link Builder} implementations that can build a message from a MIME
+ * multipart message. This interface should be implemented by message builders associated with MIME
+ * types that can appear as the root part of a multipart message. This is the case for SwA and MTOM.
+ * <p>
+ * The {@link #processMIMEMessage(Attachments, String, MessageContext)} method is called by
+ * {@link MIMEBuilder} (which is associated by default with the <tt>multipart/related</tt> MIME
+ * type) after identifying the target builder based on the content type of the root part.
+ */
+public interface MIMEAwareBuilder extends Builder {
+    /**
+     * Process a MIME multipart message and initialize the message context.
+     * 
+     * @param attachments
+     *            the MIME message
+     * @param contentType
+     *            the content type of the root part, as specified by the <tt>type</tt> parameter in
+     *            the content type of the MIME message
+     * @param messageContext
+     *            the message context
+     * @return the SOAP infoset for the given message (which is typically a representation of the
+     *         root part of the MIME message)
+     * @throws AxisFault
+     */
+    OMElement processMIMEMessage(Attachments attachments, String contentType,
+            MessageContext messageContext) throws AxisFault;
+}

Propchange: axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/builder/MIMEAwareBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/builder/MIMEBuilder.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/builder/MIMEBuilder.java?rev=1332277&r1=1332276&r2=1332277&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/builder/MIMEBuilder.java (original)
+++ axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/builder/MIMEBuilder.java Mon Apr 30 16:16:30 2012
@@ -22,7 +22,6 @@ package org.apache.axis2.builder;
 import org.apache.axiom.attachments.Attachments;
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMException;
-import org.apache.axiom.om.impl.MTOMConstants;
 import org.apache.axis2.AxisFault;
 import org.apache.axis2.Constants;
 import org.apache.axis2.context.MessageContext;
@@ -38,22 +37,7 @@ public class MIMEBuilder implements Buil
             throws AxisFault {
         Attachments attachments =
                 BuilderUtil.createAttachmentsMap(msgContext, inputStream, contentType);
-        String charSetEncoding =
-                BuilderUtil.getCharSetEncoding(attachments.getRootPartContentType());
 
-        if ((charSetEncoding == null)
-                || "null".equalsIgnoreCase(charSetEncoding)) {
-            charSetEncoding = MessageContext.UTF_8;
-        }
-        msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING,
-                               charSetEncoding);
-
-        // Setting the Attachments map to new SwA API
-        msgContext.setAttachmentMap(attachments);
-        // We set the following for all the MIME messages.. Will be overridden
-        // by subsequent builders(eg:MTOMBuilder) if needed..
-        msgContext.setDoingSwA(true);
-        
         ContentType ct;
         try {
             ct = new ContentType(contentType);
@@ -65,13 +49,27 @@ public class MIMEBuilder implements Buil
         String type = ct.getParameter("type");
         Builder builder =
                 BuilderUtil.getBuilderFromSelector(type, msgContext);
-        if(MTOMConstants.MTOM_TYPE.equals(type)){
-            String startInfo = ct.getParameter("start-info");
-            if(startInfo != null){
-                type = startInfo;
+        
+        if (builder instanceof MIMEAwareBuilder) {
+            return ((MIMEAwareBuilder)builder).processMIMEMessage(attachments, type, msgContext);
+        } else {
+            // For compatibility with older code
+            
+            String charSetEncoding =
+                    BuilderUtil.getCharSetEncoding(attachments.getRootPartContentType());
+
+            if ((charSetEncoding == null)
+                    || "null".equalsIgnoreCase(charSetEncoding)) {
+                charSetEncoding = MessageContext.UTF_8;
             }
+            msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING,
+                                   charSetEncoding);
+
+            // Setting the Attachments map to new SwA API
+            msgContext.setAttachmentMap(attachments);
+            
+            return builder.processDocument(attachments.getRootPartInputStream(false),
+                    type, msgContext);
         }
-        return builder.processDocument(attachments.getRootPartInputStream(false),
-                type, msgContext);
     }
 }

Modified: axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/builder/MTOMBuilder.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/builder/MTOMBuilder.java?rev=1332277&r1=1332276&r2=1332277&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/builder/MTOMBuilder.java (original)
+++ axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/builder/MTOMBuilder.java Mon Apr 30 16:16:30 2012
@@ -20,48 +20,52 @@
 package org.apache.axis2.builder;
 
 import org.apache.axiom.attachments.Attachments;
+import org.apache.axiom.om.OMDocument;
 import org.apache.axiom.om.OMElement;
-import org.apache.axiom.om.impl.builder.StAXBuilder;
-import org.apache.axiom.om.util.StAXParserConfiguration;
-import org.apache.axiom.om.util.StAXUtils;
+import org.apache.axiom.om.OMException;
+import org.apache.axiom.om.OMXMLBuilderFactory;
 import org.apache.axiom.soap.SOAPEnvelope;
-import org.apache.axiom.soap.impl.builder.MTOMStAXSOAPModelBuilder;
+import org.apache.axiom.soap.SOAPModelBuilder;
 import org.apache.axis2.AxisFault;
 import org.apache.axis2.Constants;
 import org.apache.axis2.context.MessageContext;
 
-import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.XMLStreamReader;
-import java.io.IOException;
 import java.io.InputStream;
 
-public class MTOMBuilder implements Builder {
+public class MTOMBuilder implements MIMEAwareBuilder {
 
     public OMElement processDocument(InputStream inputStream, String contentType,
                                      MessageContext messageContext)
             throws AxisFault {
-        XMLStreamReader streamReader;
+        throw new AxisFault("A message with content type application/xop+xml can only appear in a MIME multipart message");
+    }
+    
+    public OMElement processMIMEMessage(Attachments attachments, String contentType,
+            MessageContext messageContext) throws AxisFault {
         try {
-            Attachments attachments = messageContext.getAttachmentMap();
-            String charSetEncoding = (String) messageContext
-            .getProperty(Constants.Configuration.CHARACTER_SET_ENCODING);
+            // TODO: this will be changed later (see AXIS2-5308)
+            messageContext.setAttachmentMap(attachments);
+            
+            SOAPModelBuilder builder = OMXMLBuilderFactory.createSOAPModelBuilder(attachments);
+            OMDocument document = builder.getDocument();
+            String charsetEncoding = document.getCharsetEncoding();
+            if (charsetEncoding == null) {
+                charsetEncoding = MessageContext.UTF_8;
+            }
+            messageContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING,
+                                       charsetEncoding);
+            
+            SOAPEnvelope envelope = (SOAPEnvelope)document.getOMDocumentElement();
+            
+            // TODO: Axiom should take care of this, but we need to validate that
+//            BuilderUtil
+//                    .validateSOAPVersion(BuilderUtil.getEnvelopeNamespace(contentType), envelope);
+//            BuilderUtil.validateCharSetEncoding(charSetEncoding, builder.getDocument()
+//                    .getCharsetEncoding(), envelope.getNamespace().getNamespaceURI());
             
-            // Get the XMLStreamReader for this input stream
-            streamReader = StAXUtils.createXMLStreamReader(StAXParserConfiguration.SOAP, inputStream, charSetEncoding);        
-            StAXBuilder builder = new MTOMStAXSOAPModelBuilder(streamReader,
-                    attachments);
-            SOAPEnvelope envelope = (SOAPEnvelope) builder.getDocumentElement();
-            BuilderUtil
-                    .validateSOAPVersion(BuilderUtil.getEnvelopeNamespace(contentType), envelope);
-            BuilderUtil.validateCharSetEncoding(charSetEncoding, builder.getDocument()
-                    .getCharsetEncoding(), envelope.getNamespace().getNamespaceURI());
-            //Overriding the earlier setting by MIMEBuilder
-            messageContext.setDoingSwA(false);
             messageContext.setDoingMTOM(true);
             return envelope;
-        } catch (IOException e) {
-            throw AxisFault.makeFault(e);
-        } catch (XMLStreamException e) {
+        } catch (OMException e) {
             throw AxisFault.makeFault(e);
         }
     }

Modified: axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/builder/SOAPBuilder.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/builder/SOAPBuilder.java?rev=1332277&r1=1332276&r2=1332277&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/builder/SOAPBuilder.java (original)
+++ axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/builder/SOAPBuilder.java Mon Apr 30 16:16:30 2012
@@ -19,6 +19,7 @@
 
 package org.apache.axis2.builder;
 
+import org.apache.axiom.attachments.Attachments;
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMXMLBuilderFactory;
 import org.apache.axiom.om.OMXMLParserWrapper;
@@ -31,7 +32,7 @@ import org.apache.axis2.context.MessageC
 import java.io.IOException;
 import java.io.InputStream;
 
-public class SOAPBuilder implements Builder {
+public class SOAPBuilder implements MIMEAwareBuilder {
 
     public OMElement processDocument(InputStream inputStream, String contentType,
                                      MessageContext messageContext) throws AxisFault {
@@ -59,4 +60,21 @@ public class SOAPBuilder implements Buil
             throw AxisFault.makeFault(e);
         }
     }
+
+    public OMElement processMIMEMessage(Attachments attachments, String contentType,
+            MessageContext messageContext) throws AxisFault {
+        messageContext.setAttachmentMap(attachments);
+        
+        String charSetEncoding =
+                BuilderUtil.getCharSetEncoding(attachments.getRootPartContentType());
+        if (charSetEncoding == null) {
+            charSetEncoding = MessageContext.UTF_8;
+        }
+        messageContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING,
+                                   charSetEncoding);
+        
+        messageContext.setDoingSwA(true);
+        
+        return processDocument(attachments.getRootPartInputStream(false), contentType, messageContext);
+    }
 }