camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1358701 - in /camel/trunk/components/camel-jms/src: main/java/org/apache/camel/component/jms/ test/java/org/apache/camel/component/jms/
Date Sun, 08 Jul 2012 08:02:08 GMT
Author: davsclaus
Date: Sun Jul  8 08:02:07 2012
New Revision: 1358701

URL: http://svn.apache.org/viewvc?rev=1358701&view=rev
Log:
CAMEL-5429: Added allowNullBody option to jms component. Fixed to allow sending null body
with ObjectMessage type.

Added:
    camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsAllowNullBodyTest.java
Modified:
    camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java
    camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsComponent.java
    camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsConfiguration.java
    camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsEndpoint.java

Modified: camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java?rev=1358701&r1=1358700&r2=1358701&view=diff
==============================================================================
--- camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java
(original)
+++ camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java
Sun Jul  8 08:02:07 2012
@@ -457,6 +457,9 @@ public class JmsBinding {
 
         // create the JmsMessage based on the type
         if (type != null) {
+            if (body == null && (endpoint != null && !endpoint.getConfiguration().isAllowNullBody()))
{
+                throw new JMSException("Cannot send message as message body is null, and
option allowNullBody is false.");
+            }
             LOG.trace("Using JmsMessageType: {}", type);
             Message answer = createJmsMessageForType(exchange, body, headers, session, context,
type);
             // ensure default delivery mode is used by default
@@ -464,6 +467,11 @@ public class JmsBinding {
             return answer;
         }
 
+        // check for null body
+        if (body == null && (endpoint != null && !endpoint.getConfiguration().isAllowNullBody()))
{
+            throw new JMSException("Cannot send message as message body is null, and option
allowNullBody is false.");
+        }
+
         // warn if the body could not be mapped
         if (body != null && LOG.isWarnEnabled()) {
             LOG.warn("Cannot determine specific JmsMessage type to use from body class."
@@ -497,8 +505,8 @@ public class JmsBinding {
             type = Map;
         } else if (body instanceof Serializable) {
             type = Object;            
-        } else if (exchange.getContext().getTypeConverter().convertTo(File.class, body) !=
null 
-                || exchange.getContext().getTypeConverter().convertTo(InputStream.class,
body) != null) {
+        } else if (exchange.getContext().getTypeConverter().tryConvertTo(File.class, body)
!= null
+                || exchange.getContext().getTypeConverter().tryConvertTo(InputStream.class,
body) != null) {
             type = Bytes;
         }
         return type;
@@ -514,33 +522,42 @@ public class JmsBinding {
         switch (type) {
         case Text: {
             TextMessage message = session.createTextMessage();
-            String payload = context.getTypeConverter().convertTo(String.class, exchange,
body);
-            message.setText(payload);
+            if (body != null) {
+                String payload = context.getTypeConverter().convertTo(String.class, exchange,
body);
+                message.setText(payload);
+            }
             return message;
         }
         case Bytes: {
             BytesMessage message = session.createBytesMessage();
-            byte[] payload = context.getTypeConverter().convertTo(byte[].class, exchange,
body);
-            message.writeBytes(payload);
+            if (body != null) {
+                byte[] payload = context.getTypeConverter().convertTo(byte[].class, exchange,
body);
+                message.writeBytes(payload);
+            }
             return message;
         }
         case Map: {
             MapMessage message = session.createMapMessage();
-            Map<?, ?> payload = context.getTypeConverter().convertTo(Map.class, exchange,
body);
-            populateMapMessage(message, payload, context);
+            if (body != null) {
+                Map<?, ?> payload = context.getTypeConverter().convertTo(Map.class,
exchange, body);
+                populateMapMessage(message, payload, context);
+            }
             return message;
         }
         case Object:
-            Serializable payload;
-            try {
-                payload = context.getTypeConverter().mandatoryConvertTo(Serializable.class,
exchange, body);
-            } catch (NoTypeConversionAvailableException e) {
-                // cannot convert to serializable then thrown an exception to avoid sending
a null message
-                JMSException cause = new MessageFormatException(e.getMessage());
-                cause.initCause(e);
-                throw cause;
+            ObjectMessage message = session.createObjectMessage();
+            if (body != null) {
+                try {
+                    Serializable payload = context.getTypeConverter().mandatoryConvertTo(Serializable.class,
exchange, body);
+                    message.setObject(payload);
+                } catch (NoTypeConversionAvailableException e) {
+                    // cannot convert to serializable then thrown an exception to avoid sending
a null message
+                    JMSException cause = new MessageFormatException(e.getMessage());
+                    cause.initCause(e);
+                    throw cause;
+                }
             }
-            return session.createObjectMessage(payload);
+            return message;
         default:
             break;
         }

Modified: camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsComponent.java?rev=1358701&r1=1358700&r2=1358701&view=diff
==============================================================================
--- camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsComponent.java
(original)
+++ camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsComponent.java
Sun Jul  8 08:02:07 2012
@@ -360,28 +360,20 @@ public class JmsComponent extends Defaul
         getConfiguration().setDestinationResolver(destinationResolver);
     }
 
-    public ReplyToType getReplyToType() {
-        return getConfiguration().getReplyToType();
-    }
-
     public void setReplyToType(ReplyToType replyToType) {
         getConfiguration().setReplyToType(replyToType);
     }
 
-    public boolean isPreserveMessageQos() {
-        return getConfiguration().isPreserveMessageQos();
-    }
-
     public void setPreserveMessageQos(boolean preserveMessageQos) {
         getConfiguration().setPreserveMessageQos(preserveMessageQos);
     }
 
     public void setAsyncConsumer(boolean asyncConsumer) {
-        configuration.setAsyncConsumer(asyncConsumer);
+        getConfiguration().setAsyncConsumer(asyncConsumer);
     }
 
-    public boolean isAsyncConsumer() {
-        return configuration.isAsyncConsumer();
+    public void setAllowNullBody(boolean allowNullBody) {
+        getConfiguration().setAllowNullBody(allowNullBody);
     }
 
     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
{

Modified: camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsConfiguration.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsConfiguration.java?rev=1358701&r1=1358700&r2=1358701&view=diff
==============================================================================
--- camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsConfiguration.java
(original)
+++ camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsConfiguration.java
Sun Jul  8 08:02:07 2012
@@ -134,6 +134,7 @@ public class JmsConfiguration implements
     private boolean asyncConsumer;
     // the cacheLevelName of reply manager
     private String replyToCacheLevelName;
+    private boolean allowNullBody = true;
 
     public JmsConfiguration() {
     }
@@ -1266,4 +1267,14 @@ public class JmsConfiguration implements
         return replyToCacheLevelName;
     }
 
+    public boolean isAllowNullBody() {
+        return allowNullBody;
+    }
+
+    /**
+     * Whether to allow sending with no doy (eg as null)
+     */
+    public void setAllowNullBody(boolean allowNullBody) {
+        this.allowNullBody = allowNullBody;
+    }
 }

Modified: camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsEndpoint.java?rev=1358701&r1=1358700&r2=1358701&view=diff
==============================================================================
--- camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsEndpoint.java
(original)
+++ camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsEndpoint.java
Sun Jul  8 08:02:07 2012
@@ -1032,6 +1032,16 @@ public class JmsEndpoint extends Default
     }
 
     @ManagedAttribute
+    public boolean isAllowNullBody() {
+        return configuration.isAllowNullBody();
+    }
+
+    @ManagedAttribute
+    public void setAllowNullBody(boolean allowNullBody) {
+        configuration.setAllowNullBody(allowNullBody);
+    }
+
+    @ManagedAttribute
     public String getReplyToType() {
         if (configuration.getReplyToType() != null) {
             return configuration.getReplyToType().name();

Added: camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsAllowNullBodyTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsAllowNullBodyTest.java?rev=1358701&view=auto
==============================================================================
--- camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsAllowNullBodyTest.java
(added)
+++ camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsAllowNullBodyTest.java
Sun Jul  8 08:02:07 2012
@@ -0,0 +1,96 @@
+/**
+ * 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.camel.component.jms;
+
+import javax.jms.ConnectionFactory;
+import javax.jms.JMSException;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+import static org.apache.camel.component.jms.JmsComponent.jmsComponentAutoAcknowledge;
+
+/**
+ *
+ */
+public class JmsAllowNullBodyTest extends CamelTestSupport {
+
+    @Test
+    public void testAllowNullBodyDefault() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+        getMockEndpoint("mock:result").message(0).body().isNull();
+        getMockEndpoint("mock:result").message(0).header("bar").isEqualTo(123);
+
+        // allow null body is default enabled
+        template.sendBodyAndHeader("activemq:queue:foo", null, "bar", 123);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testAllowNullBody() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+        getMockEndpoint("mock:result").message(0).body().isNull();
+        getMockEndpoint("mock:result").message(0).header("bar").isEqualTo(123);
+
+        template.sendBodyAndHeader("activemq:queue:foo?allowNullBody=true", null, "bar",
123);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testAllowNullTextBody() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+        getMockEndpoint("mock:result").message(0).body().isNull();
+        getMockEndpoint("mock:result").message(0).header("bar").isEqualTo(123);
+
+        template.sendBodyAndHeader("activemq:queue:foo?allowNullBody=true&jmsMessageType=Text",
null, "bar", 123);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testNoAllowNullBody() throws Exception {
+        try {
+            template.sendBodyAndHeader("activemq:queue:foo?allowNullBody=false", null, "bar",
123);
+            fail("Should have thrown exception");
+        } catch (CamelExecutionException e) {
+            JMSException cause = assertIsInstanceOf(JMSException.class, e.getCause().getCause());
+            assertEquals("Cannot send message as message body is null, and option allowNullBody
is false.", cause.getMessage());
+        }
+    }
+
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext camelContext = super.createCamelContext();
+        ConnectionFactory connectionFactory = CamelJmsTestHelper.createConnectionFactory();
+        camelContext.addComponent("activemq", jmsComponentAutoAcknowledge(connectionFactory));
+        return camelContext;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("activemq:queue:foo").to("mock:result");
+            }
+        };
+    }
+}



Mime
View raw message