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 2016/01/17 10:17:38 UTC

svn commit: r1725082 - in /axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2: datasource/jaxb/AbstractJAXBAttachmentMarshaller.java datasource/jaxb/JAXBAttachmentMarshaller.java jaxws/message/attachments/JAXBAttachmentMarshaller.java

Author: veithen
Date: Sun Jan 17 09:17:37 2016
New Revision: 1725082

URL: http://svn.apache.org/viewvc?rev=1725082&view=rev
Log:
Use OO design best practices.

Added:
    axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/AbstractJAXBAttachmentMarshaller.java
      - copied, changed from r1725013, axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/JAXBAttachmentMarshaller.java
Modified:
    axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/JAXBAttachmentMarshaller.java
    axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/attachments/JAXBAttachmentMarshaller.java

Copied: axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/AbstractJAXBAttachmentMarshaller.java (from r1725013, axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/JAXBAttachmentMarshaller.java)
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/AbstractJAXBAttachmentMarshaller.java?p2=axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/AbstractJAXBAttachmentMarshaller.java&p1=axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/JAXBAttachmentMarshaller.java&r1=1725013&r2=1725082&rev=1725082&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/JAXBAttachmentMarshaller.java (original)
+++ axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/AbstractJAXBAttachmentMarshaller.java Sun Jan 17 09:17:37 2016
@@ -23,7 +23,6 @@ import org.apache.axiom.om.OMException;
 import org.apache.axiom.om.impl.MTOMXMLStreamWriter;
 import org.apache.axiom.util.UIDGenerator;
 import org.apache.axis2.Constants;
-import org.apache.axis2.Constants.Configuration;
 import org.apache.axis2.context.MessageContext;
 import org.apache.axis2.java.security.AccessController;
 import org.apache.axis2.transport.http.HTTPConstants;
@@ -40,16 +39,12 @@ import javax.xml.stream.XMLStreamWriter;
 
 import java.security.PrivilegedAction;
 
-/**
- * An implementation of the JAXB AttachmentMarshaller that is used to handle binary data from JAXB
- * and create populate the appropriate constructs on the MessageContext
- */
-public class JAXBAttachmentMarshaller extends AttachmentMarshaller {
+public abstract class AbstractJAXBAttachmentMarshaller extends AttachmentMarshaller {
 
-    private static final Log log = LogFactory.getLog(JAXBAttachmentMarshaller.class);
+    private static final Log log = LogFactory.getLog(AbstractJAXBAttachmentMarshaller.class);
 
-    private MessageContext msgContext;
-    private XMLStreamWriter writer;
+    protected final MessageContext msgContext;
+    private final XMLStreamWriter writer;
     private static final String APPLICATION_OCTET = "application/octet-stream";
     
     /**
@@ -57,7 +52,7 @@ public class JAXBAttachmentMarshaller ex
      * @param msgContext
      * @param writer
      */
-    public JAXBAttachmentMarshaller(MessageContext msgContext, XMLStreamWriter writer) {
+    public AbstractJAXBAttachmentMarshaller(MessageContext msgContext, XMLStreamWriter writer) {
         this.msgContext = msgContext;
         this.writer = writer;
     }
@@ -92,7 +87,7 @@ public class JAXBAttachmentMarshaller ex
     /* (non-Javadoc)
      * @see javax.xml.bind.attachment.AttachmentMarshaller#addMtomAttachment(byte[], int, int, java.lang.String, java.lang.String, java.lang.String)
      */
-    public String addMtomAttachment(byte[] data, int offset, int length,
+    public final String addMtomAttachment(byte[] data, int offset, int length,
                                     String mimeType, String namespace, String localPart) {
 
         if (offset != 0 || length != data.length) {
@@ -160,7 +155,7 @@ public class JAXBAttachmentMarshaller ex
     /* (non-Javadoc)
      * @see javax.xml.bind.attachment.AttachmentMarshaller#addMtomAttachment(javax.activation.DataHandler, java.lang.String, java.lang.String)
      */
-    public String addMtomAttachment(DataHandler data, String namespace, String localPart) {
+    public final String addMtomAttachment(DataHandler data, String namespace, String localPart) {
         if (log.isDebugEnabled()){ 
             log.debug("Adding MTOM/XOP datahandler attachment for element: " + 
                       "{" + namespace + "}" + localPart);
@@ -173,7 +168,7 @@ public class JAXBAttachmentMarshaller ex
     /* (non-Javadoc)
      * @see javax.xml.bind.attachment.AttachmentMarshaller#addSwaRefAttachment(javax.activation.DataHandler)
      */
-    public String addSwaRefAttachment(DataHandler data) {
+    public final String addSwaRefAttachment(DataHandler data) {
         if (log.isDebugEnabled()){ 
             log.debug("Adding SWAREF attachment");
         }
@@ -235,39 +230,17 @@ public class JAXBAttachmentMarshaller ex
     /**
      * @return if MTOM enabled calculated from the context information
      */
-    public boolean isMTOMEnabled() {
-        if (msgContext == null) {
-            return false;
-        } else {
-            String value = (String) msgContext.getProperty(Configuration.ENABLE_MTOM);
-            return ("true".equalsIgnoreCase(value));
-        }  
-    }
+    protected abstract boolean isMTOMEnabled();
     
     /**
      * Indicate on the context that SWA attachments are encountered
      */
-    public void setDoingSWA() {
-        if (msgContext != null) {
-            msgContext.setDoingSwA(true);
-            msgContext.setProperty(Configuration.ENABLE_SWA, "true");
-        }
-    }
+    protected abstract void setDoingSWA();
     
     /**
      * Inform the context that an Attachment (DataHandler, CID) is written.
      * @param dh
      * @param cid
      */
-    public void addDataHandler(DataHandler dh, String cid) {
-        if (msgContext != null) {
-            msgContext.addAttachment(cid, dh);
-        } else {
-            if (log.isDebugEnabled()) {
-                log.debug("The msgContext is null.  The attachment is not stored");
-                log.debug("   content id=" + cid);
-                log.debug("   dataHandler  =" + dh);
-            }
-        }
-    }
+    protected abstract void addDataHandler(DataHandler dh, String cid);
 }

Modified: axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/JAXBAttachmentMarshaller.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/JAXBAttachmentMarshaller.java?rev=1725082&r1=1725081&r2=1725082&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/JAXBAttachmentMarshaller.java (original)
+++ axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/JAXBAttachmentMarshaller.java Sun Jan 17 09:17:37 2016
@@ -19,223 +19,35 @@
 
 package org.apache.axis2.datasource.jaxb;
 
-import org.apache.axiom.om.OMException;
-import org.apache.axiom.om.impl.MTOMXMLStreamWriter;
-import org.apache.axiom.util.UIDGenerator;
-import org.apache.axis2.Constants;
 import org.apache.axis2.Constants.Configuration;
 import org.apache.axis2.context.MessageContext;
-import org.apache.axis2.java.security.AccessController;
-import org.apache.axis2.transport.http.HTTPConstants;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
 import javax.activation.DataHandler;
-import javax.mail.MessagingException;
-import javax.mail.internet.InternetHeaders;
-import javax.mail.internet.MimeBodyPart;
-import javax.mail.internet.MimePartDataSource;
-import javax.xml.bind.attachment.AttachmentMarshaller;
 import javax.xml.stream.XMLStreamWriter;
 
-import java.security.PrivilegedAction;
-
 /**
  * An implementation of the JAXB AttachmentMarshaller that is used to handle binary data from JAXB
  * and create populate the appropriate constructs on the MessageContext
  */
-public class JAXBAttachmentMarshaller extends AttachmentMarshaller {
+public final class JAXBAttachmentMarshaller extends AbstractJAXBAttachmentMarshaller {
 
     private static final Log log = LogFactory.getLog(JAXBAttachmentMarshaller.class);
 
-    private MessageContext msgContext;
-    private XMLStreamWriter writer;
-    private static final String APPLICATION_OCTET = "application/octet-stream";
-    
     /**
      * Construct the JAXBAttachmentMarshaller that has access to the MessageContext
      * @param msgContext
      * @param writer
      */
     public JAXBAttachmentMarshaller(MessageContext msgContext, XMLStreamWriter writer) {
-        this.msgContext = msgContext;
-        this.writer = writer;
-    }
-
-    /**
-     * Override isXOPPackaget to calculate the isXOPPackage setting
-     */
-    public boolean isXOPPackage() {
-        boolean value = false;
-        
-        // For outbound messages, only trigger MTOM if
-        // the message is mtom enabled.
-        value = isMTOMEnabled();
-        
-        // If the writer is not an MTOM XMLStreamWriter then we don't have
-        // any place to store the attachment
-        if (!(writer instanceof MTOMXMLStreamWriter)) {
-            if (log.isDebugEnabled()) {
-                log.debug("The writer is not enabled for MTOM.  " +
-                                "MTOM values will not be optimized");
-            }
-            value = false;
-        }
-    
-        if (log.isDebugEnabled()){ 
-            log.debug("isXOPPackage returns " + value);
-        }
-        return value;        
-    }
-
-    
-    /* (non-Javadoc)
-     * @see javax.xml.bind.attachment.AttachmentMarshaller#addMtomAttachment(byte[], int, int, java.lang.String, java.lang.String, java.lang.String)
-     */
-    public String addMtomAttachment(byte[] data, int offset, int length,
-                                    String mimeType, String namespace, String localPart) {
-
-        if (offset != 0 || length != data.length) {
-            int len = length - offset;
-            byte[] newData = new byte[len];
-            System.arraycopy(data, offset, newData, 0, len);
-            data = newData;
-        }
-        
-        if (mimeType == null || mimeType.length() == 0) {
-            mimeType = APPLICATION_OCTET;
-        }
-        
-        if (log.isDebugEnabled()){ 
-            log.debug("Adding MTOM/XOP byte array attachment for element: " + 
-                      "{" + namespace + "}" + localPart);
-        }
-        
-        String cid = null;
-        
-        try {
-            // Create MIME Body Part
-            final InternetHeaders ih = new InternetHeaders();
-            final byte[] dataArray = data; 
-            ih.setHeader(HTTPConstants.HEADER_CONTENT_TYPE, mimeType);
-            final MimeBodyPart mbp = (MimeBodyPart) AccessController.doPrivileged(new PrivilegedAction() {
-                public Object run() {
-                    try {
-                        return new MimeBodyPart(ih, dataArray);
-                    } catch (MessagingException e) {
-                        throw new OMException(e);
-                    }
-                }});
-
-            //Create a data source for the MIME Body Part
-            MimePartDataSource mpds = (MimePartDataSource) AccessController.doPrivileged(new PrivilegedAction() {
-                public Object run() {
-                    return new MimePartDataSource(mbp);
-                }});
-            long dataLength =data.length;
-            Integer value = null;
-            if (msgContext != null) {
-                value = (Integer) msgContext.getProperty(Constants.Configuration.MTOM_THRESHOLD);
-            } else if (log.isDebugEnabled()) {
-                log.debug("The msgContext is null so the MTOM threshold value can not be determined; it will default to 0.");
-            }
-
-            int optimizedThreshold = (value != null) ? value.intValue() : 0;
-
-            if(optimizedThreshold==0 || dataLength > optimizedThreshold){
-                DataHandler dataHandler = new DataHandler(mpds);
-                cid = addDataHandler(dataHandler, false);
-            }
-
-            // Add the content id to the mime body part
-            mbp.setHeader(HTTPConstants.HEADER_CONTENT_ID, cid);
-        } catch (MessagingException e) {
-            throw new OMException(e);
-        }
-
-        return cid == null ? null : "cid:" + cid;
-    }
-    
-    
-    /* (non-Javadoc)
-     * @see javax.xml.bind.attachment.AttachmentMarshaller#addMtomAttachment(javax.activation.DataHandler, java.lang.String, java.lang.String)
-     */
-    public String addMtomAttachment(DataHandler data, String namespace, String localPart) {
-        if (log.isDebugEnabled()){ 
-            log.debug("Adding MTOM/XOP datahandler attachment for element: " + 
-                      "{" + namespace + "}" + localPart);
-        }
-        String cid = addDataHandler(data, false);
-        return cid == null ? null : "cid:" + cid;
-    }
-    
-    
-    /* (non-Javadoc)
-     * @see javax.xml.bind.attachment.AttachmentMarshaller#addSwaRefAttachment(javax.activation.DataHandler)
-     */
-    public String addSwaRefAttachment(DataHandler data) {
-        if (log.isDebugEnabled()){ 
-            log.debug("Adding SWAREF attachment");
-        }
-        
-        String cid = addDataHandler(data, true);
-        setDoingSWA();
-        return "cid:" + cid;
-    }
-    
-    /**
-     * Add the DataHandler to the writer and context
-     * @param dh
-     * @return
-     */
-    private String addDataHandler(DataHandler dh, boolean isSWA) {
-        String cid = null;
-        
-        // If this is an MTOMXMLStreamWriter then inform the writer 
-        // that it must write out this attachment (I guess we should do this
-        // even if the attachment is SWAREF ?)
-        if (isSWA) {
-            if (log.isDebugEnabled()){ 
-                log.debug("adding DataHandler for SWA");
-            }
-            // If old SWA attachments, get the ID and add the attachment to message
-            cid = UIDGenerator.generateContentId();
-            addDataHandler(dh, cid);   
-        } else {
-            if (log.isDebugEnabled()){ 
-                log.debug("adding DataHandler for MTOM");
-            }
-            if (writer instanceof MTOMXMLStreamWriter) {
-                cid = ((MTOMXMLStreamWriter)writer).prepareDataHandler(dh);
-                if (cid != null) {
-                    if (log.isDebugEnabled()){ 
-                        log.debug("The MTOM attachment is written as an attachment part.");
-                    }
-                    // Remember the attachment on the message.
-                    addDataHandler(dh, cid);
-                } else {
-                    if (log.isDebugEnabled()){ 
-                        log.debug("The MTOM attachment is inlined.");
-                    }
-                }
-            } else {
-                if (log.isDebugEnabled()){ 
-                    log.debug("writer is not MTOM capable.  The attachment will be inlined.");
-                }
-            }
-        }
-        
-        if (log.isDebugEnabled()){ 
-            log.debug("   content id=" + cid);
-            log.debug("   dataHandler  =" + dh);
-        }
-        return cid;
+        super(msgContext, writer);
     }
 
     /**
      * @return if MTOM enabled calculated from the context information
      */
-    public boolean isMTOMEnabled() {
+    protected boolean isMTOMEnabled() {
         if (msgContext == null) {
             return false;
         } else {
@@ -247,7 +59,7 @@ public class JAXBAttachmentMarshaller ex
     /**
      * Indicate on the context that SWA attachments are encountered
      */
-    public void setDoingSWA() {
+    protected void setDoingSWA() {
         if (msgContext != null) {
             msgContext.setDoingSwA(true);
             msgContext.setProperty(Configuration.ENABLE_SWA, "true");
@@ -259,7 +71,7 @@ public class JAXBAttachmentMarshaller ex
      * @param dh
      * @param cid
      */
-    public void addDataHandler(DataHandler dh, String cid) {
+    protected void addDataHandler(DataHandler dh, String cid) {
         if (msgContext != null) {
             msgContext.addAttachment(cid, dh);
         } else {

Modified: axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/attachments/JAXBAttachmentMarshaller.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/attachments/JAXBAttachmentMarshaller.java?rev=1725082&r1=1725081&r2=1725082&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/attachments/JAXBAttachmentMarshaller.java (original)
+++ axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/attachments/JAXBAttachmentMarshaller.java Sun Jan 17 09:17:37 2016
@@ -20,6 +20,7 @@
 package org.apache.axis2.jaxws.message.attachments;
 
 import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.datasource.jaxb.AbstractJAXBAttachmentMarshaller;
 import org.apache.axis2.jaxws.message.Message;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -31,7 +32,7 @@ import javax.xml.stream.XMLStreamWriter;
  * An implementation of the JAXB AttachmentMarshaller that is used to handle binary data from JAXB
  * and create populate the appropriate constructs within the JAX-WS Message Model.
  */
-public class JAXBAttachmentMarshaller extends org.apache.axis2.datasource.jaxb.JAXBAttachmentMarshaller {
+public final class JAXBAttachmentMarshaller extends AbstractJAXBAttachmentMarshaller {
     
     private static final Log log = LogFactory.getLog(JAXBAttachmentMarshaller.class);
     private Message message;
@@ -58,7 +59,7 @@ public class JAXBAttachmentMarshaller ex
     /**
      * @return if MTOM enabled calculated from the context information
      */
-    public boolean isMTOMEnabled() {
+    protected boolean isMTOMEnabled() {
         if (message == null) {
             return false;
         } else {
@@ -69,7 +70,7 @@ public class JAXBAttachmentMarshaller ex
     /**
      * Indicate on the context that SWA attachments are encountered
      */
-    public void setDoingSWA() {
+    protected void setDoingSWA() {
         if (message != null) {
             message.setDoingSWA(true);
         }
@@ -80,7 +81,7 @@ public class JAXBAttachmentMarshaller ex
      * @param dh
      * @param cid
      */
-    public void addDataHandler(DataHandler dh, String cid) {
+    protected void addDataHandler(DataHandler dh, String cid) {
         if (message != null) {
             message.addDataHandler(dh, cid);
         } else {