[ 
https://issues.apache.org/activemq/browse/SM-1238?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=43977#action_43977
 ] 

Ron Gavlin commented on SM-1238:
--------------------------------

Here is an updated version of the patch that preserves the existing 
property/attachment copying behavior while allowing it to now be configurable.

Index: src/main/java/org/apache/servicemix/eip/patterns/Pipeline.java
===================================================================
--- src/main/java/org/apache/servicemix/eip/patterns/Pipeline.java      
(revision 674147)
+++ src/main/java/org/apache/servicemix/eip/patterns/Pipeline.java      
(working copy)
@@ -17,7 +17,11 @@
 package org.apache.servicemix.eip.patterns;
 
 import java.net.URI;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Set;
 
+import javax.activation.DataHandler;
 import javax.jbi.management.DeploymentException;
 import javax.jbi.messaging.ExchangeStatus;
 import javax.jbi.messaging.Fault;
@@ -24,6 +28,8 @@
 import javax.jbi.messaging.InOnly;
 import javax.jbi.messaging.InOut;
 import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.NormalizedMessage;
 import javax.jbi.messaging.RobustInOnly;
 import javax.wsdl.Definition;
 
@@ -31,6 +37,7 @@
 import org.apache.servicemix.eip.support.ExchangeTarget;
 import org.apache.servicemix.jbi.FaultException;
 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.apache.servicemix.jbi.messaging.PojoMarshaler;
 import org.apache.servicemix.jbi.util.MessageUtil;
 
 /**
@@ -90,6 +97,10 @@
      */
     private String correlationTarget;
 
+    private boolean copyProperties = false;
+    
+    private boolean copyAttachments = false;
+    
     /**
      * @return Returns the target.
      */
@@ -146,6 +157,22 @@
         this.transformer = transformer;
     }
 
+    public boolean isCopyProperties() {
+       return copyProperties;
+    }
+    
+    public void setCopyProperties(boolean copyProperties) {
+       this.copyProperties = copyProperties;
+    }
+    
+    public boolean isCopyAttachments() {
+       return copyAttachments;
+    }
+    
+    public void setCopyAttachments(boolean copyAttachments) {
+       this.copyAttachments = copyAttachments;
+    }
+    
     /* (non-Javadoc)
      * @see org.apache.servicemix.eip.EIPEndpoint#validate()
      */
@@ -380,6 +407,20 @@
             }
         // This is the answer from the transformer
         } else if (exchange.getMessage("out") != null) {
+               if (copyProperties || copyAttachments) {
+                       MessageExchange cme = (MessageExchange) 
store.load(consumerId);
+                       NormalizedMessage cmeInMsg = cme.getMessage("in");
+                       if (cmeInMsg != null) {
+                               NormalizedMessage tmeOutMsg = 
exchange.getMessage("out");
+                               if (copyProperties) {
+                                       copyProperties(cmeInMsg, tmeOutMsg);
+                               }
+                               if (copyAttachments) {
+                                       copyAttachments(cmeInMsg, tmeOutMsg);
+                               }
+                       }
+                       store.store(consumerId, cme);
+               }
             // Retrieve the consumer MEP
             URI mep = (URI) exchange.getProperty(CONSUMER_MEP);
             if (mep == null) {
@@ -452,5 +493,38 @@
         }
         return rc;
     }
-
+    
+    /**
+     * Copies properties from one message to another that do not already exist
+     * 
+     * @param from the message containing the properties
+     * @param to the destination message where the properties are set
+     */
+    private void copyProperties(NormalizedMessage from, NormalizedMessage to) {
+       for (String propertyName : (Set<String>) from.getPropertyNames()) {
+               // Do not copy existing properties or transient properties
+               if (to.getProperty(propertyName) == null && 
!PojoMarshaler.BODY.equals(propertyName)) {
+                       Object value = from.getProperty(propertyName);
+                       to.setProperty(propertyName, value);
+               }
+       }
+    }
+    
+    /**
+     * Copies attachments from one message to another that do not already exist
+     * 
+     * @param from the message with the attachments
+     * @param to the destination message where the attachments are to be added
+     * @throws MessagingException if an attachment could not be added
+     */
+    private void copyAttachments(NormalizedMessage from, NormalizedMessage to) 
throws MessagingException {
+       for (String attachmentName : (Set<String>) from.getAttachmentNames()) {
+               // Do not copy existing attachments
+               if (to.getAttachment(attachmentName) == null) {
+                       DataHandler value = from.getAttachment(attachmentName);
+                       to.addAttachment(attachmentName, value);
+               }
+       }
+    }
+    
 }


> EIP Pipeline should propagate message properties across asynchronous exchanges
> ------------------------------------------------------------------------------
>
>                 Key: SM-1238
>                 URL: https://issues.apache.org/activemq/browse/SM-1238
>             Project: ServiceMix
>          Issue Type: New Feature
>          Components: servicemix-eip
>    Affects Versions: 3.2.1
>            Reporter: Ron Gavlin
>         Attachments: Pipeline.java.patch
>
>
> EIP Pipeline output properties should be the same as input properties except 
> where modified by the transform in asynchronous exchanges. 
> Also, StreamSource faults should be localized before returning to the 
> exchange.
> I hope to submit a patch for this issue shortly.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to