servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ge...@apache.org
Subject svn commit: r784256 - in /servicemix/components/engines/servicemix-camel/trunk/src: main/java/org/apache/servicemix/camel/JbiMessage.java test/java/org/apache/servicemix/camel/JbiMessageTest.java
Date Fri, 12 Jun 2009 20:03:13 GMT
Author: gertv
Date: Fri Jun 12 20:03:13 2009
New Revision: 784256

URL: http://svn.apache.org/viewvc?rev=784256&view=rev
Log:
SMCOMP-569: Allow settings headers/attachments through the Map

Modified:
    servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiMessage.java
    servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiMessageTest.java

Modified: servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiMessage.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiMessage.java?rev=784256&r1=784255&r2=784256&view=diff
==============================================================================
--- servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiMessage.java
(original)
+++ servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiMessage.java
Fri Jun 12 20:03:13 2009
@@ -16,6 +16,9 @@
  */
 package org.apache.servicemix.camel;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import javax.activation.DataHandler;
 import javax.jbi.messaging.MessagingException;
 import javax.jbi.messaging.NormalizedMessage;
@@ -36,7 +39,8 @@
     }
 
     public JbiMessage(NormalizedMessage normalizedMessage) {
-        this.normalizedMessage = normalizedMessage;
+        super();
+        setNormalizedMessage(normalizedMessage);
     }
 
     @Override
@@ -62,8 +66,13 @@
         return normalizedMessage;
     }
 
-    protected void setNormalizedMessage(NormalizedMessage normalizedMessage) {
+    protected final void setNormalizedMessage(NormalizedMessage normalizedMessage) {
         this.normalizedMessage = normalizedMessage;
+        // copy all properties into the Camel Message
+        if (normalizedMessage != null) {
+            getHeaders().putAll(getHeaders(normalizedMessage));
+            getAttachments().putAll(getAttachments(normalizedMessage));
+        }
     }
 
     @Override
@@ -77,13 +86,18 @@
         }
         return answer;
     }
-
+    
     @Override
-    public void setHeader(String name , Object value) {
-        if (normalizedMessage != null) {
-            normalizedMessage.setProperty(name, value);
-        }
-        super.setHeader(name, value);
+    protected Map<String, Object> createHeaders() {
+        return new HashMap<String, Object>() {
+            @Override
+            public Object put(String key, Object value) {
+                if (normalizedMessage != null) {
+                    normalizedMessage.setProperty(key, value);
+                }
+                return super.put(key, value);
+            }
+        };
     }
 
     @Override
@@ -97,17 +111,22 @@
         }
         return answer;
     }
-
+    
     @Override
-    public void addAttachment(String id, DataHandler content) {
-        if (normalizedMessage != null) {
-            try {
-                normalizedMessage.addAttachment(id, content);
-            } catch (MessagingException e) {
-                throw new JbiException(e);
+    protected Map<String, DataHandler> createAttachments() {
+        return new HashMap<String, DataHandler>() {
+            @Override
+            public DataHandler put(String key, DataHandler value) {
+                if (normalizedMessage != null) {
+                    try {
+                        normalizedMessage.addAttachment(key, value);
+                    } catch (MessagingException e) {
+                        throw new JbiException(e);
+                    }
+                }
+                return super.put(key, value);
             }
-        }
-        super.addAttachment(id, content);
+        };
     }
 
     @Override
@@ -146,4 +165,20 @@
         return String.format("NormalizedMessage@%s(%s)", 
                              Integer.toHexString(message.hashCode()), message.getContent());
     }
+    
+    private Map<? extends String, ? extends Object> getHeaders(NormalizedMessage message)
{
+        Map<String, Object> headers = new HashMap<String, Object>();
+        for (Object key : message.getPropertyNames()) {
+            headers.put(key.toString(), message.getProperty(key.toString()));
+        }
+        return headers;
+    }
+    
+    private Map<? extends String, ? extends DataHandler> getAttachments(NormalizedMessage
message) {
+        Map<String, DataHandler> attachments = new HashMap<String, DataHandler>();
+        for (Object name : message.getAttachmentNames()) {
+            attachments.put(name.toString(), message.getAttachment(name.toString()));
+        }
+        return attachments;
+    }
 }

Modified: servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiMessageTest.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiMessageTest.java?rev=784256&r1=784255&r2=784256&view=diff
==============================================================================
--- servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiMessageTest.java
(original)
+++ servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiMessageTest.java
Fri Jun 12 20:03:13 2009
@@ -55,6 +55,25 @@
         // and they should also be on the Camel Message itself
         camelMessage.setNormalizedMessage(null);
         assertSame(ANOTHER_ATTACHMENT, camelMessage.getAttachment(ANOTHER_ATTACHMENT_ID));
+        assertSame(ATTACHMENT, camelMessage.getAttachment(ATTACHMENT_ID));
+    }
+    
+    public void testSetAttachmentsThroughMap() throws Exception {
+        NormalizedMessage jbiMessage = new MockNormalizedMessage();
+        jbiMessage.addAttachment(ATTACHMENT_ID, ATTACHMENT);
+        
+        // make sure the Camel Message also has the attachment
+        JbiMessage camelMessage = new JbiMessage(jbiMessage);
+        assertSame(ATTACHMENT, camelMessage.getAttachment(ATTACHMENT_ID));
+        
+        // and ensure that attachments are propagated back to the underlying NormalizedMessage
+        camelMessage.getAttachments().put(ANOTHER_ATTACHMENT_ID, ANOTHER_ATTACHMENT);
+        assertSame(ANOTHER_ATTACHMENT, jbiMessage.getAttachment(ANOTHER_ATTACHMENT_ID));
+        
+        // and they should also be on the Camel Message itself
+        camelMessage.setNormalizedMessage(null);
+        assertSame(ANOTHER_ATTACHMENT, camelMessage.getAttachment(ANOTHER_ATTACHMENT_ID));
+        assertSame(ATTACHMENT, camelMessage.getAttachment(ATTACHMENT_ID));
     }
     
     public void testHeadersWithNormalizedMessage() throws Exception {
@@ -72,8 +91,27 @@
         // and they should also be on the Camel Message itself
         camelMessage.setNormalizedMessage(null);
         assertSame(ANOTHER_VALUE, camelMessage.getHeader(ANOTHER_HEADER));
+        assertSame(VALUE, camelMessage.getHeader(HEADER));
     }    
     
+    public void testSetHeaderThroughMap() throws Exception {
+        NormalizedMessage jbiMessage = new MockNormalizedMessage();
+        jbiMessage.setProperty(HEADER, VALUE);
+        
+        // make sure the Camel Message also has the header
+        JbiMessage camelMessage = new JbiMessage(jbiMessage);
+        assertSame(VALUE, camelMessage.getHeader(HEADER));
+        
+        // and ensure the headers are propagated back to the underlying NormalizedMessage
+        camelMessage.getHeaders().put(ANOTHER_HEADER, ANOTHER_VALUE);
+        assertSame(ANOTHER_VALUE, jbiMessage.getProperty(ANOTHER_HEADER));
+        
+        // and they should also be on the Camel Message itself
+        camelMessage.setNormalizedMessage(null);
+        assertSame(ANOTHER_VALUE, camelMessage.getHeader(ANOTHER_HEADER));
+        assertSame(VALUE, camelMessage.getHeader(HEADER));        
+    }
+        
     public void testCopyMessage() throws Exception {
         JbiMessage message = new JbiMessage();
         Message copy = message.copy();



Mime
View raw message