camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r674786 - in /activemq/camel/trunk/components/camel-jms/src: main/java/org/apache/camel/component/jms/ test/java/org/apache/camel/component/jms/issues/
Date Tue, 08 Jul 2008 12:06:07 GMT
Author: davsclaus
Date: Tue Jul  8 05:06:07 2008
New Revision: 674786

URL: http://svn.apache.org/viewvc?rev=674786&view=rev
Log:
CAMEL-685: Ignoring non valid JMS header values. Only primitive, String etc. types is allowed.
Any custom object will be ignored.

Added:
    activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/DummyOrder.java
  (with props)
    activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsHeaderAsObjectTest.java
  (with props)
Modified:
    activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java

Modified: activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java?rev=674786&r1=674785&r2=674786&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java
(original)
+++ activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java
Tue Jul  8 05:06:07 2008
@@ -23,7 +23,9 @@
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
-
+import java.util.Date;
+import java.math.BigDecimal;
+import java.math.BigInteger;
 import javax.jms.BytesMessage;
 import javax.jms.Destination;
 import javax.jms.JMSException;
@@ -64,6 +66,7 @@
     public JmsBinding(JmsEndpoint endpoint) {
         this.endpoint = endpoint;
     }
+
     /**
      * Extracts the body from the JMS message
      *
@@ -158,17 +161,18 @@
         }
     }
 
-    public void appendJmsProperty(Message jmsMessage, Exchange exchange, org.apache.camel.Message
in, String headerName, Object headerValue) throws JMSException {
+    public void appendJmsProperty(Message jmsMessage, Exchange exchange, org.apache.camel.Message
in,
+                                  String headerName, Object headerValue) throws JMSException
{
         if (headerName.startsWith("JMS") && !headerName.startsWith("JMSX")) {
             if (headerName.equals("JMSCorrelationID")) {
                 jmsMessage.setJMSCorrelationID(ExchangeHelper.convertToType(exchange, String.class,
-                                                                            headerValue));
+                    headerValue));
             } else if (headerName.equals("JMSCorrelationID")) {
                 jmsMessage.setJMSCorrelationID(ExchangeHelper.convertToType(exchange, String.class,
-                                                                            headerValue));
+                    headerValue));
             } else if (headerName.equals("JMSReplyTo") && headerValue != null) {
                 jmsMessage.setJMSReplyTo(ExchangeHelper.convertToType(exchange, Destination.class,
-                                                                      headerValue));
+                    headerValue));
             } else if (headerName.equals("JMSType")) {
                 jmsMessage.setJMSType(ExchangeHelper.convertToType(exchange, String.class,
headerValue));
             } else if (LOG.isDebugEnabled()) {
@@ -182,8 +186,54 @@
         } else if (shouldOutputHeader(in, headerName, headerValue)) {
             // must encode to safe JMS header name before setting property on jmsMessage
             String key = encodeToSafeJmsHeaderName(headerName);
-            jmsMessage.setObjectProperty(key, headerValue);
+
+            // only primitive headers and strings is allowed as properties
+            // see message properties: http://java.sun.com/j2ee/1.4/docs/api/javax/jms/Message.html
+            Object value = getValidJMSHeaderValue(headerName, headerValue);
+            if (value != null) {
+                jmsMessage.setObjectProperty(key, headerValue);
+            } else if (LOG.isDebugEnabled()) {
+                // okay the value is not a primitive or string so we can not sent it over
the wire
+                LOG.debug("Ignoring non primitive header: " + headerName + " of class: "
+                    + headerValue.getClass().getName() + " with value: " + headerValue);
+            }
+        }
+    }
+
+    /**
+     * Strategy to test if the given header is valid according to the JMS spec to be set
as a property
+     * on the JMS message.
+     * <p/>
+     * This default implementation will allow:
+     * <ul>
+     *   <li>any primitives and their counter Objects (Integer, Double etc.)</li>
+     *   <li>String and any other litterals, Character, CharSequence</li>
+     *   <li>BigDecimal and BigInteger</li>
+     *   <li>java.util.Date</li>
+     * </ul>
+     *
+     * @param headerName   the header name
+     * @param headerValue  the header value
+     * @return  the value to use, <tt>null</tt> to ignore this header
+     */
+    protected Object getValidJMSHeaderValue(String headerName, Object headerValue) {
+        if (headerValue.getClass().isPrimitive()) {
+            return headerValue;
+        } else if (headerValue instanceof String) {
+            return headerValue;
+        } else if (headerValue instanceof Number) {
+            return headerValue;
+        } else if (headerValue instanceof Character) {
+            return headerValue.toString();
+        } else if (headerValue instanceof BigDecimal || headerValue instanceof BigInteger)
{
+            return headerValue.toString();
+        } else if (headerValue instanceof CharSequence) {
+            return headerValue.toString();
+        } else if (headerValue instanceof Date) {
+            return headerValue.toString();
         }
+
+        return null;
     }
 
     protected Message createJmsMessage(Object body, Session session, CamelContext context)
@@ -271,7 +321,7 @@
                                          Object headerValue) {
         String key = encodeToSafeJmsHeaderName(headerName);
         return headerValue != null && !getIgnoreJmsHeaders().contains(headerName)
-               && ObjectHelper.isJavaIdentifier(key);
+            && ObjectHelper.isJavaIdentifier(key);
     }
 
     /**
@@ -282,8 +332,8 @@
      * <p/>
      * <b>Note</b>: Currently this encoder is simple as it only supports encoding
dots to underscores.
      *
-     * @param headerName  the header name
-     * @return  the key to use instead for storing properties and to be for lookup of the
same property
+     * @param headerName the header name
+     * @return the key to use instead for storing properties and to be for lookup of the
same property
      */
     public static String encodeToSafeJmsHeaderName(String headerName) {
         return headerName.replace(".", "_");
@@ -292,8 +342,8 @@
     /**
      * Decode operation for the {@link #encodeToSafeJmsHeaderName(String)}.
      *
-     * @param headerName  the header name
-     * @return  the original key
+     * @param headerName the header name
+     * @return the original key
      */
     public static String decodeFromSafeJmsHeaderName(String headerName) {
         return headerName.replace("_", ".");
@@ -308,7 +358,7 @@
 
         // added "JMSXRecvTimestamp" as a workaround for an Oracle bug/typo in AqjmsMessage
         String[] ignore = {"JMSXUserID", "JMSXAppID", "JMSXDeliveryCount", "JMSXProducerTXID",
-                           "JMSXConsumerTXID", "JMSXRcvTimestamp", "JMSXRecvTimestamp", "JMSXState"};
+            "JMSXConsumerTXID", "JMSXRcvTimestamp", "JMSXRecvTimestamp", "JMSXState"};
         set.addAll(Arrays.asList(ignore));
     }
 

Added: activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/DummyOrder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/DummyOrder.java?rev=674786&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/DummyOrder.java
(added)
+++ activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/DummyOrder.java
Tue Jul  8 05:06:07 2008
@@ -0,0 +1,76 @@
+package org.apache.camel.component.jms.issues;
+
+import java.io.Serializable;
+
+/**
+ * Model for unit test.
+ */
+public class DummyOrder implements Serializable {
+
+    private long orderId;
+    private long itemId;
+    private int quantity;
+
+    public long getOrderId() {
+        return orderId;
+    }
+
+    public void setOrderId(long orderId) {
+        this.orderId = orderId;
+    }
+
+    public long getItemId() {
+        return itemId;
+    }
+
+    public void setItemId(long itemId) {
+        this.itemId = itemId;
+    }
+
+    public int getQuantity() {
+        return quantity;
+    }
+
+    public void setQuantity(int quantity) {
+        this.quantity = quantity;
+    }
+
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+
+        DummyOrder that = (DummyOrder)o;
+
+        if (itemId != that.itemId) {
+            return false;
+        }
+        if (orderId != that.orderId) {
+            return false;
+        }
+        if (quantity != that.quantity) {
+            return false;
+        }
+
+        return true;
+    }
+
+    public int hashCode() {
+        int result;
+        result = (int)(orderId ^ (orderId >>> 32));
+        result = 31 * result + (int)(itemId ^ (itemId >>> 32));
+        result = 31 * result + quantity;
+        return result;
+    }
+
+    public String toString() {
+        return "DummyOrder{" +
+            "orderId=" + orderId +
+            ", itemId=" + itemId +
+            ", quantity=" + quantity +
+            '}';
+    }
+}

Propchange: activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/DummyOrder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/DummyOrder.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsHeaderAsObjectTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsHeaderAsObjectTest.java?rev=674786&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsHeaderAsObjectTest.java
(added)
+++ activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsHeaderAsObjectTest.java
Tue Jul  8 05:06:07 2008
@@ -0,0 +1,63 @@
+package org.apache.camel.component.jms.issues;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.apache.activemq.camel.component.ActiveMQComponent.activeMQComponent;
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ *
+ */
+public class JmsHeaderAsObjectTest extends ContextTestSupport {
+
+    public void testSendHeaderAsPrimitiveOnly() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello World");
+        mock.message(0).header("foo").isEqualTo("bar");
+        mock.message(0).header("number").isEqualTo(23);
+
+        Map headers = new HashMap();
+        headers.put("foo", "bar");
+        headers.put("number", 23);
+        template.sendBodyAndHeaders("activemq:in", "Hello World", headers);
+
+        mock.assertIsSatisfied();
+    }
+
+    public void testSendHeaderAsObject() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello World");
+        mock.message(0).header("foo").isEqualTo("bar");
+        mock.message(0).header("order").isNull();
+
+        DummyOrder order = new DummyOrder();
+        order.setItemId(4444);
+        order.setOrderId(333);
+        order.setQuantity(2);
+
+        Map headers = new HashMap();
+        headers.put("foo", "bar");
+        headers.put("order", order);
+        template.sendBodyAndHeaders("activemq:in", "Hello World", headers);
+
+        mock.assertIsSatisfied();
+    }
+
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext camelContext = super.createCamelContext();
+        camelContext.addComponent("activemq", activeMQComponent("vm://localhost?broker.persistent=false&broker.useJmx=false"));
+        return camelContext;
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from("activemq:in").to("mock:result");
+            }
+        };
+    }
+}

Propchange: activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsHeaderAsObjectTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsHeaderAsObjectTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message