You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/03/02 09:24:42 UTC

[camel] branch camel-2.25.x updated: CAMEL-14633 (#3603)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-2.25.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-2.25.x by this push:
     new 8f74f63  CAMEL-14633 (#3603)
8f74f63 is described below

commit 8f74f63947938fee2125da7cc08367638dd98ee4
Author: Oliver Limberg <ok...@gmail.com>
AuthorDate: Mon Mar 2 10:24:30 2020 +0100

    CAMEL-14633 (#3603)
    
    * CAMEL-14633 - added tests to verify missing attachments after safe copy without headers
    
    The new two tests will make a safe copy of the exchange.
    in both tests, the in and the out message have a attachment but in only one
    of the tests will set an header on the messages before copying the exchange.
    The test without headers will fail.
    
    * CAMEL-14633 - fixed issue of missing attachments
    
    Moved logic to copy attachments outside of if-block to check for existence
    of headers
    
    * fixed Checkstyle errors
    
    * check for existence of attachments on in and out message before calling the copy method
---
 .../apache/camel/component/bean/MethodInfo.java    |  2 +-
 .../org/apache/camel/impl/DefaultExchange.java     | 10 +++--
 .../processor/aggregate/AggregateProcessor.java    |  4 +-
 .../java/org/apache/camel/runtimecatalog/Pair.java | 12 ++++--
 .../apache/camel/runtimecatalog/URISupport.java    |  6 +--
 .../runtimecatalog/UnsafeUriCharactersEncoder.java |  2 -
 .../src/main/java/org/apache/camel/util/Pair.java  | 12 ++++--
 .../java/org/apache/camel/util/URISupport.java     |  4 +-
 .../org/apache/camel/impl/DefaultExchangeTest.java | 48 ++++++++++++++++++++++
 9 files changed, 78 insertions(+), 22 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java b/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
index a06f011..8718bc2 100644
--- a/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
+++ b/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
@@ -59,8 +59,8 @@ import org.apache.camel.util.StringQuoteHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import static org.apache.camel.util.ObjectHelper.asString;
 import static org.apache.camel.util.ObjectHelper.asList;
+import static org.apache.camel.util.ObjectHelper.asString;
 import static org.apache.camel.util.ObjectHelper.invokeMethodSafe;
 
 /**
diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
index 61230f3..aed41fb 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
@@ -109,7 +109,9 @@ public final class DefaultExchange implements Exchange {
             exchange.getIn().setFault(getIn().isFault());
             if (getIn().hasHeaders()) {
                 exchange.getIn().setHeaders(safeCopyHeaders(getIn().getHeaders()));
-                // just copy the attachments here
+            }
+            // copy the attachments if there are any
+            if (getIn().hasAttachments()) {
                 exchange.getIn().copyAttachments(getIn());
             }
             if (hasOut()) {
@@ -118,8 +120,10 @@ public final class DefaultExchange implements Exchange {
                 if (getOut().hasHeaders()) {
                     exchange.getOut().setHeaders(safeCopyHeaders(getOut().getHeaders()));
                 }
-                // Just copy the attachments here
-                exchange.getOut().copyAttachments(getOut());
+                // copy the attachments if there are any
+                if (getOut().hasAttachments()) {
+                    exchange.getOut().copyAttachments(getOut());
+                }
             }
         } else {
             // old way of doing copy which is @deprecated
diff --git a/camel-core/src/main/java/org/apache/camel/processor/aggregate/AggregateProcessor.java b/camel-core/src/main/java/org/apache/camel/processor/aggregate/AggregateProcessor.java
index 41971cf..e1cd60a 100644
--- a/camel-core/src/main/java/org/apache/camel/processor/aggregate/AggregateProcessor.java
+++ b/camel-core/src/main/java/org/apache/camel/processor/aggregate/AggregateProcessor.java
@@ -393,13 +393,13 @@ public class AggregateProcessor extends ServiceSupport implements AsyncProcessor
     private Object removeFlagCompleteAllGroups(Exchange exchange) {
         Object removedHeader = exchange.getIn().removeHeader(Exchange.AGGREGATION_COMPLETE_ALL_GROUPS);
         Object removedProp = exchange.removeProperty(Exchange.AGGREGATION_COMPLETE_ALL_GROUPS);
-        return removedHeader == null ? removedProp: removedHeader;
+        return removedHeader == null ? removedProp : removedHeader;
     }
 
     private Boolean isCompleteAllGroups(Exchange exchange) {
         boolean retVal;
         retVal = exchange.getIn().getHeader(Exchange.AGGREGATION_COMPLETE_ALL_GROUPS, false, boolean.class);
-        if(!retVal) {
+        if (!retVal) {
             // according to doc it is a property but it is sometimes read as header
             // some test don't fail because they use the header expression which contains a fallback to properties
             retVal = exchange.getProperty(Exchange.AGGREGATION_COMPLETE_ALL_GROUPS, false, boolean.class);
diff --git a/camel-core/src/main/java/org/apache/camel/runtimecatalog/Pair.java b/camel-core/src/main/java/org/apache/camel/runtimecatalog/Pair.java
index 99d7b9c..13768b9 100644
--- a/camel-core/src/main/java/org/apache/camel/runtimecatalog/Pair.java
+++ b/camel-core/src/main/java/org/apache/camel/runtimecatalog/Pair.java
@@ -41,11 +41,15 @@ public class Pair<T> {
 
     @Override
     public boolean equals(Object o) {
-        if (this == o) return true;
-        if (o == null || getClass() != o.getClass()) return false;
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
         Pair<?> that = (Pair<?>) o;
-        return Objects.equals(left, that.left) &&
-                Objects.equals(right, that.right);
+        return Objects.equals(left, that.left)
+                && Objects.equals(right, that.right);
     }
 
     @Override
diff --git a/camel-core/src/main/java/org/apache/camel/runtimecatalog/URISupport.java b/camel-core/src/main/java/org/apache/camel/runtimecatalog/URISupport.java
index bc11ba2..73aa234 100644
--- a/camel-core/src/main/java/org/apache/camel/runtimecatalog/URISupport.java
+++ b/camel-core/src/main/java/org/apache/camel/runtimecatalog/URISupport.java
@@ -28,16 +28,14 @@ import java.util.List;
 import java.util.Map;
 import java.util.function.BiConsumer;
 
-import org.apache.camel.runtimecatalog.Pair;
-
 /**
  * Copied from org.apache.camel.util.URISupport
  */
 public final class URISupport {
 
     public static final String RAW_TOKEN_PREFIX = "RAW";
-    public static final char[] RAW_TOKEN_START = { '(', '{' };
-    public static final char[] RAW_TOKEN_END = { ')', '}' };
+    public static final char[] RAW_TOKEN_START = {'(', '{' };
+    public static final char[] RAW_TOKEN_END = {')', '}' };
 
     private static final String CHARSET = "UTF-8";
 
diff --git a/camel-core/src/main/java/org/apache/camel/runtimecatalog/UnsafeUriCharactersEncoder.java b/camel-core/src/main/java/org/apache/camel/runtimecatalog/UnsafeUriCharactersEncoder.java
index acb3838..74ff0af 100644
--- a/camel-core/src/main/java/org/apache/camel/runtimecatalog/UnsafeUriCharactersEncoder.java
+++ b/camel-core/src/main/java/org/apache/camel/runtimecatalog/UnsafeUriCharactersEncoder.java
@@ -20,8 +20,6 @@ import java.util.ArrayList;
 import java.util.BitSet;
 import java.util.List;
 
-import org.apache.camel.runtimecatalog.Pair;
-
 /**
  * Encoder for unsafe URI characters.
  * <p/>
diff --git a/camel-core/src/main/java/org/apache/camel/util/Pair.java b/camel-core/src/main/java/org/apache/camel/util/Pair.java
index 8f4e3b4..ee10a29 100644
--- a/camel-core/src/main/java/org/apache/camel/util/Pair.java
+++ b/camel-core/src/main/java/org/apache/camel/util/Pair.java
@@ -41,11 +41,15 @@ public class Pair<T> {
 
     @Override
     public boolean equals(Object o) {
-        if (this == o) return true;
-        if (o == null || getClass() != o.getClass()) return false;
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
         Pair<?> that = (Pair<?>) o;
-        return Objects.equals(left, that.left) &&
-                Objects.equals(right, that.right);
+        return Objects.equals(left, that.left)
+                && Objects.equals(right, that.right);
     }
 
     @Override
diff --git a/camel-core/src/main/java/org/apache/camel/util/URISupport.java b/camel-core/src/main/java/org/apache/camel/util/URISupport.java
index 14c1b25..150088d 100644
--- a/camel-core/src/main/java/org/apache/camel/util/URISupport.java
+++ b/camel-core/src/main/java/org/apache/camel/util/URISupport.java
@@ -35,8 +35,8 @@ import java.util.regex.Pattern;
 public final class URISupport {
 
     public static final String RAW_TOKEN_PREFIX = "RAW";
-    public static final char[] RAW_TOKEN_START = { '(', '{' };
-    public static final char[] RAW_TOKEN_END = { ')', '}' };
+    public static final char[] RAW_TOKEN_START = {'(', '{' };
+    public static final char[] RAW_TOKEN_END = {')', '}' };
 
     // Match any key-value pair in the URI query string whose key contains
     // "passphrase" or "password" or secret key (case-insensitive).
diff --git a/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java b/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java
index 4089c85..e231743 100644
--- a/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java
+++ b/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java
@@ -19,6 +19,8 @@ package org.apache.camel.impl;
 import java.io.IOException;
 import java.net.ConnectException;
 
+import javax.activation.DataHandler;
+
 import org.apache.camel.Exchange;
 import org.apache.camel.ExchangeTestSupport;
 import org.apache.camel.InvalidPayloadException;
@@ -249,6 +251,52 @@ public class DefaultExchangeTest extends ExchangeTestSupport {
     }
 
     @Test
+    public void testSafeCopyWithAttachments() {
+        DefaultExchange sourceExchange = new DefaultExchange(context);
+        MyMessage sourceIn = new MyMessage();
+        sourceIn.setCamelContext(context);
+        sourceIn.setHeader("test-header-in", "test-header-in-value");
+        final String attachmentIdIn = "test-attachment-in";
+        sourceIn.addAttachment(attachmentIdIn, new DataHandler("test-content", "text/plain"));
+        sourceExchange.setIn(sourceIn);
+        MyMessage sourceOut = new MyMessage();
+        sourceOut.setCamelContext(context);
+        sourceOut.setHeader("test-header-out", "test-header-out-value");
+        final String attachmentIdOut = "test-attachment-out";
+        sourceOut.addAttachment(attachmentIdOut, new DataHandler("test-content", "text/plain"));
+        sourceExchange.setOut(sourceOut);
+        Exchange destExchange = sourceExchange.copy(true);
+        Message destIn = destExchange.getIn();
+        Message destOut = destExchange.getOut();
+
+        assertEquals("Dest message in attachments should equal source message in attachments",
+                sourceIn.getAttachment(attachmentIdIn), destIn.getAttachment(attachmentIdIn));
+        assertEquals("Dest message out attachments should equal source message out attachments",
+                sourceOut.getAttachment(attachmentIdOut), destOut.getAttachment(attachmentIdOut));
+    }
+
+    @Test
+    public void testSafeCopyWithAttachmentsNoHeaders() {
+        DefaultExchange sourceExchange = new DefaultExchange(context);
+        MyMessage sourceIn = new MyMessage();
+        final String attachmentIdIn = "test-attachment-in";
+        sourceIn.addAttachment(attachmentIdIn, new DataHandler("test-content", "text/plain"));
+        sourceExchange.setIn(sourceIn);
+        MyMessage sourceOut = new MyMessage();
+        final String attachmentIdOut = "test-attachment-out";
+        sourceOut.addAttachment(attachmentIdOut, new DataHandler("test-content", "text/plain"));
+        sourceExchange.setOut(sourceOut);
+        Exchange destExchange = sourceExchange.copy(true);
+        Message destIn = destExchange.getIn();
+        Message destOut = destExchange.getOut();
+
+        assertEquals("Dest message in attachments should equal source message in attachments",
+                sourceIn.getAttachment(attachmentIdIn), destIn.getAttachment(attachmentIdIn));
+        assertEquals("Dest message out attachments should equal source message out attachments",
+                sourceOut.getAttachment(attachmentIdOut), destOut.getAttachment(attachmentIdOut));
+    }
+
+    @Test
     public void testFaultCopy() {
         testFaultCopy(false);
     }