diff --git a/activemq-client/src/main/java/org/apache/activemq/ActiveMQMessageConsumer.java b/activemq-client/src/main/java/org/apache/activemq/ActiveMQMessageConsumer.java index d7188b5151a..2389f9760c5 100644 --- a/activemq-client/src/main/java/org/apache/activemq/ActiveMQMessageConsumer.java +++ b/activemq-client/src/main/java/org/apache/activemq/ActiveMQMessageConsumer.java @@ -616,7 +616,8 @@ private ActiveMQMessage createActiveMQMessage(final MessageDispatch md) throws J if (transformer != null) { Message transformedMessage = transformer.consumerTransform(session, this, m); if (transformedMessage != null) { - m = ActiveMQMessageTransformation.transformMessage(transformedMessage, session.connection); + // a message on its way to the application keeps its provider-set properties + m = ActiveMQMessageTransformation.transformMessage(transformedMessage, session.connection, true); } } if (session.isClientAcknowledge()) { diff --git a/activemq-client/src/main/java/org/apache/activemq/ActiveMQMessageTransformation.java b/activemq-client/src/main/java/org/apache/activemq/ActiveMQMessageTransformation.java index e3097619416..8ce761e3ae0 100644 --- a/activemq-client/src/main/java/org/apache/activemq/ActiveMQMessageTransformation.java +++ b/activemq-client/src/main/java/org/apache/activemq/ActiveMQMessageTransformation.java @@ -78,6 +78,27 @@ public static ActiveMQDestination transformDestination(Destination destination) */ public static ActiveMQMessage transformMessage(Message message, ActiveMQConnection connection) throws JMSException { + return transformMessage(message, connection, false); + } + + /** + * Creates a fast shallow copy of the current ActiveMQMessage or creates a + * whole new message instance from an available JMS message from another + * provider. + * + * @param message - Message to be converted into ActiveMQ's implementation. + * @param connection + * @param strictComplianceOverride true copies the properties in + * {@link ActiveMQMessage#JMSX_PROVIDER_PROPERTIES} even + * when the connection has strict compliance on, for + * example when bridging from another provider. false + * leaves them out on a strict connection. + * @return ActiveMQMessage - ActiveMQ's implementation object of the + * message. + * @throws JMSException if an error occurs + */ + public static ActiveMQMessage transformMessage(Message message, ActiveMQConnection connection, + final boolean strictComplianceOverride) throws JMSException { if (message instanceof ActiveMQMessage) { return (ActiveMQMessage)message; @@ -161,7 +182,7 @@ public static ActiveMQMessage transformMessage(Message message, ActiveMQConnecti activeMessage.setConnection(connection); } - copyProperties(message, activeMessage); + copyProperties(message, activeMessage, strictComplianceOverride); return activeMessage; } @@ -176,6 +197,24 @@ public static ActiveMQMessage transformMessage(Message message, ActiveMQConnecti * @throws JMSException */ public static void copyProperties(Message fromMessage, Message toMessage) throws JMSException { + copyProperties(fromMessage, toMessage, false); + } + + /** + * Copies the standard JMS and user defined properties from the givem + * message to the specified message + * + * @param fromMessage the message to take the properties from + * @param toMessage the message to add the properties to + * @param strictComplianceOverride true copies the properties in + * {@link ActiveMQMessage#JMSX_PROVIDER_PROPERTIES} even + * when the connection of the target message has strict + * compliance on. false leaves them out when it does. + * @throws JMSException + */ + public static void copyProperties(Message fromMessage, Message toMessage, final boolean strictComplianceOverride) + throws JMSException { + final boolean skipProviderProperties = !strictComplianceOverride && isStrictCompliance(toMessage); toMessage.setJMSMessageID(fromMessage.getJMSMessageID()); toMessage.setJMSCorrelationID(fromMessage.getJMSCorrelationID()); toMessage.setJMSReplyTo(transformDestination(fromMessage.getJMSReplyTo())); @@ -192,9 +231,26 @@ public static void copyProperties(Message fromMessage, Message toMessage) throws while (propertyNames.hasMoreElements()) { String name = propertyNames.nextElement().toString(); + boolean providerProperty = ActiveMQMessage.JMSX_PROVIDER_PROPERTIES.contains(name); + if (providerProperty && skipProviderProperties) { + continue; + } Object obj = fromMessage.getObjectProperty(name); - toMessage.setObjectProperty(name, obj); + if (providerProperty && toMessage instanceof ActiveMQMessage) { + // with the override a strict connection must not refuse the provider-set value + ((ActiveMQMessage) toMessage).setObjectProperty(name, obj, true, false); + } else { + toMessage.setObjectProperty(name, obj); + } + } + } + + private static boolean isStrictCompliance(Message message) { + if (message instanceof ActiveMQMessage) { + ActiveMQConnection connection = ((ActiveMQMessage) message).getConnection(); + return connection != null && connection.isStrictCompliance(); } + return false; } private static long getFromMessageDeliveryTime(Message fromMessage) throws JMSException { diff --git a/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQMessage.java b/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQMessage.java index 3a02d074c66..53a41358625 100644 --- a/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQMessage.java +++ b/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQMessage.java @@ -18,10 +18,15 @@ import java.io.IOException; import java.io.UnsupportedEncodingException; + +import java.util.Arrays; +import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.Vector; import jakarta.jms.DeliveryMode; @@ -51,6 +56,17 @@ public class ActiveMQMessage extends Message implements org.apache.activemq.Mess private static final Map JMS_PROPERTY_SETERS = new HashMap(); + public static final Set JMSX_PROVIDER_PROPERTIES = + Collections.unmodifiableSet(new HashSet(Arrays.asList( + "JMSXAppID", + "JMSXConsumerTXID", + "JMSXDeliveryCount", + "JMSXProducerTXID", + "JMSXRcvTimestamp", + "JMSXState", + "JMSXUserID" + ))); + protected transient Callback acknowledgeCallback; @Override @@ -487,6 +503,16 @@ public void setObjectProperty(String name, Object value) throws JMSException { } public void setObjectProperty(String name, Object value, boolean checkReadOnly) throws JMSException { + setObjectProperty(name, value, checkReadOnly, true); + } + + /** + * @param checkProviderProperties false sets a property in + * {@link #JMSX_PROVIDER_PROPERTIES} even when the connection has + * strict compliance on, for code that carries a message over from + * another provider and has to keep the values that provider set + */ + public void setObjectProperty(String name, Object value, boolean checkReadOnly, boolean checkProviderProperties) throws JMSException { if (checkReadOnly) { checkReadOnlyProperties(); @@ -501,6 +527,15 @@ public void setObjectProperty(String name, Object value, boolean checkReadOnly) checkValidObject(value); value = convertScheduled(name, value); + + // Strict Compliance Check For Provider-Set JMSX Properties + ActiveMQConnection conn = getConnection(); + if (checkProviderProperties && conn != null && conn.isStrictCompliance()) { + if (JMSX_PROVIDER_PROPERTIES.contains(name)) { + throw new JMSException("Provider set JMSX property '" + name + "' cannot be set by a client under strict compliance."); + } + } + PropertySetter setter = JMS_PROPERTY_SETERS.get(name); if (setter != null && value != null) { diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/StrictComplianceForeignMessageTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/StrictComplianceForeignMessageTest.java new file mode 100644 index 00000000000..72c3c64ae1f --- /dev/null +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/StrictComplianceForeignMessageTest.java @@ -0,0 +1,227 @@ +/** + * 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.activemq; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.lang.reflect.Proxy; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import jakarta.jms.Message; +import jakarta.jms.MessageConsumer; +import jakarta.jms.MessageProducer; +import jakarta.jms.Session; + +import org.apache.activemq.broker.BrokerService; +import org.apache.activemq.command.ActiveMQMessage; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * A message from another provider lists the JMSX properties that provider + * set. A strict connection refuses those when a client sets them, so the + * conversion to an ActiveMQ message has to leave them out instead of failing. + */ +public class StrictComplianceForeignMessageTest { + + private static final String QUEUE_NAME = "test.strict.foreign"; + private static final List STORED_PROVIDER_PROPERTIES = + List.of("JMSXRcvTimestamp", "JMSXState", "JMSXProducerTXID", "JMSXConsumerTXID"); + + private BrokerService broker; + private String connectionUri; + + @Before + public void setUp() throws Exception { + broker = new BrokerService(); + broker.setPersistent(false); + broker.setUseJmx(false); + broker.addConnector("vm://localhost"); + broker.start(); + broker.waitUntilStarted(); + connectionUri = broker.getTransportConnectors().get(0).getPublishableConnectString(); + } + + @After + public void tearDown() throws Exception { + if (broker != null) { + broker.stop(); + broker.waitUntilStopped(); + } + } + + @Test + public void testStrictConnectionSendsForeignMessage() throws Exception { + try (var connection = createConnection(true)) { + connection.start(); + final var session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + final var queue = session.createQueue(QUEUE_NAME); + final var consumer = session.createConsumer(queue); + + session.createProducer(queue).send(foreignMessage()); + + final var received = consumer.receive(5000); + assertNotNull(received); + assertEquals("red", received.getStringProperty("color")); + assertEquals("group-a", received.getStringProperty("JMSXGroupID")); + assertEquals("the other provider's delivery count is not carried over", + 1, received.getIntProperty("JMSXDeliveryCount")); + assertFalse(received.propertyExists("JMSXState")); + } + } + + // on the way in the values come from a provider, so a strict connection keeps them + @Test + public void testStrictConnectionKeepsProviderPropertiesFromConsumerTransformer() throws Exception { + try (var connection = createConnection(true)) { + connection.start(); + final var session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + final var queue = session.createQueue(QUEUE_NAME); + final var consumer = (ActiveMQMessageConsumer) session.createConsumer(queue); + consumer.setTransformer(new MessageTransformer() { + @Override + public Message producerTransform(Session session, MessageProducer producer, Message message) { + return message; + } + + @Override + public Message consumerTransform(Session session, MessageConsumer consumer, Message message) { + return foreignMessage(); + } + }); + + session.createProducer(queue).send(session.createMessage()); + + final var received = (ActiveMQMessage) consumer.receive(5000); + assertNotNull(received); + assertProviderPropertiesCopied(received); + } + } + + @Test + public void testTransformFollowsConnection() throws Exception { + try (var strict = createConnection(true); var lenient = createConnection(false)) { + assertProviderPropertiesSkipped(ActiveMQMessageTransformation.transformMessage(foreignMessage(), strict)); + assertProviderPropertiesCopied(ActiveMQMessageTransformation.transformMessage(foreignMessage(), lenient)); + } + } + + @Test + public void testTransformOverrideCopiesProviderPropertiesOnStrictConnection() throws Exception { + try (var strict = createConnection(true); var lenient = createConnection(false)) { + assertProviderPropertiesCopied( + ActiveMQMessageTransformation.transformMessage(foreignMessage(), strict, true)); + assertProviderPropertiesSkipped( + ActiveMQMessageTransformation.transformMessage(foreignMessage(), strict, false)); + + // the override has nothing to do on a connection that is not strict + assertProviderPropertiesCopied( + ActiveMQMessageTransformation.transformMessage(foreignMessage(), lenient, true)); + assertProviderPropertiesCopied( + ActiveMQMessageTransformation.transformMessage(foreignMessage(), lenient, false)); + } + } + + @Test + public void testTransformWithoutConnectionCopiesProviderProperties() throws Exception { + assertProviderPropertiesCopied(ActiveMQMessageTransformation.transformMessage(foreignMessage(), null)); + } + + @Test + public void testCopyPropertiesFollowsConnectionOfTarget() throws Exception { + try (var strict = createConnection(true)) { + final var followsConnection = new ActiveMQMessage(); + followsConnection.setConnection(strict); + ActiveMQMessageTransformation.copyProperties(foreignMessage(), followsConnection); + assertProviderPropertiesSkipped(followsConnection); + + final var overridden = new ActiveMQMessage(); + overridden.setConnection(strict); + ActiveMQMessageTransformation.copyProperties(foreignMessage(), overridden, true); + assertProviderPropertiesCopied(overridden); + } + + final var noConnection = new ActiveMQMessage(); + ActiveMQMessageTransformation.copyProperties(foreignMessage(), noConnection); + assertProviderPropertiesCopied(noConnection); + } + + private ActiveMQConnection createConnection(final boolean strictCompliance) throws Exception { + final var factory = new ActiveMQConnectionFactory(connectionUri); + factory.setStrictCompliance(strictCompliance); + return (ActiveMQConnection) factory.createConnection(); + } + + private static void assertProviderPropertiesCopied(final ActiveMQMessage message) throws Exception { + assertEquals("red", message.getStringProperty("color")); + assertEquals("JMSXDeliveryCount 3 is a redelivery counter of 2", 2, message.getRedeliveryCounter()); + for (final var name : STORED_PROVIDER_PROPERTIES) { + assertTrue(name, message.getProperties().containsKey(name)); + } + } + + private static void assertProviderPropertiesSkipped(final ActiveMQMessage message) throws Exception { + assertEquals("red", message.getStringProperty("color")); + assertEquals("group-a", message.getStringProperty("JMSXGroupID")); + assertEquals(0, message.getRedeliveryCounter()); + for (final var name : STORED_PROVIDER_PROPERTIES) { + assertFalse(name, message.getProperties().containsKey(name)); + } + } + + // a message that is not an ActiveMQ message and lists every provider-set JMSX property + private static Message foreignMessage() { + final Map properties = new LinkedHashMap<>(); + properties.put("color", "red"); + properties.put("JMSXGroupID", "group-a"); + properties.put("JMSXDeliveryCount", 3); + properties.put("JMSXRcvTimestamp", 1000L); + properties.put("JMSXState", 1); + properties.put("JMSXProducerTXID", "producer-tx"); + properties.put("JMSXConsumerTXID", "consumer-tx"); + + return (Message) Proxy.newProxyInstance(Message.class.getClassLoader(), new Class[] {Message.class}, + (proxy, method, args) -> { + switch (method.getName()) { + case "getPropertyNames": + return Collections.enumeration(properties.keySet()); + case "getObjectProperty": + return properties.get(args[0]); + default: + return defaultValue(method.getReturnType()); + } + }); + } + + private static Object defaultValue(final Class type) { + if (type == boolean.class) { + return false; + } else if (type == int.class) { + return 0; + } else if (type == long.class) { + return 0L; + } + return null; + } +} diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/StrictComplianceProviderJMSXPropertyTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/StrictComplianceProviderJMSXPropertyTest.java new file mode 100644 index 00000000000..81ab196f65f --- /dev/null +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/StrictComplianceProviderJMSXPropertyTest.java @@ -0,0 +1,140 @@ +/** + * 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.activemq; + +import jakarta.jms.Connection; +import jakarta.jms.JMSException; +import jakarta.jms.Message; +import jakarta.jms.Session; +import org.apache.activemq.ActiveMQConnectionFactory; +import org.apache.activemq.broker.BrokerService; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +public class StrictComplianceProviderJMSXPropertyTest { + + private BrokerService broker; + private String connectionUri; + + @Before + public void setUp() throws Exception { + broker = new BrokerService(); + broker.setPersistent(false); + broker.setUseJmx(false); + broker.addConnector("vm://localhost"); + broker.start(); + broker.waitUntilStarted(); + connectionUri = broker.getTransportConnectors().get(0).getPublishableConnectString(); + } + + @After + public void tearDown() throws Exception { + if (broker != null) { + broker.stop(); + broker.waitUntilStopped(); + } + } + + @Test + public void testLegacyModeAllowsProviderSetJMSXProperties() throws Exception { + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(connectionUri); + + // Legacy mode (default) + factory.setStrictCompliance(false); + + try (Connection connection = factory.createConnection(); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)) { + + connection.start(); + Message message = session.createMessage(); + + try { + // ActiveMQ allows clients to overwrite the delivery count + message.setIntProperty("JMSXDeliveryCount", 5); + assertEquals(5, message.getIntProperty("JMSXDeliveryCount")); + // PASS: Should not throw an exception in legacy mode + } catch (Exception e) { + fail("Legacy mode should not restrict setting provider-only JMSX properties"); + } + } + } + + @Test + public void testStrictModeRejectsProviderSetJMSXProperties() throws Exception { + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(connectionUri); + + // Enforce Jakarta 3.1 compliance + factory.setStrictCompliance(true); + + try (Connection connection = factory.createConnection(); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)) { + + connection.start(); + Message message = session.createMessage(); + + String[] providerOnlyProperties = { + "JMSXAppID", + "JMSXConsumerTXID", + "JMSXDeliveryCount", + "JMSXProducerTXID", + "JMSXRcvTimestamp", + "JMSXState", + "JMSXUserID" + }; + + for (String property : providerOnlyProperties) { + try { + // Attempting to set these should fail + message.setObjectProperty(property, 1); + fail("Strict mode must reject client attempt to set provider-only property: " + property); + } catch (JMSException e) { + assertEquals("exception type for " + property, JMSException.class, e.getClass()); + } + } + } + } + + @Test + public void testStrictModeAllowsClientSetJMSXProperties() throws Exception { + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(connectionUri); + + // Enforce Jakarta 3.1 compliance + factory.setStrictCompliance(true); + + try (Connection connection = factory.createConnection(); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)) { + + connection.start(); + Message message = session.createMessage(); + + try { + // The spec explicitly allows clients to set these specific JMSX properties + message.setStringProperty("JMSXGroupID", "Group-A"); + message.setIntProperty("JMSXGroupSeq", 1); + + assertEquals("Group-A", message.getStringProperty("JMSXGroupID")); + assertEquals(1, message.getIntProperty("JMSXGroupSeq")); + // PASS: Valid client-set properties succeeded + } catch (JMSException e) { + fail("Strict mode incorrectly rejected a valid client-set JMSX property"); + } + } + } +} \ No newline at end of file