From b313209aa27b2edc5a51e3c68cc614bab69e4b40 Mon Sep 17 00:00:00 2001 From: Timothy Bish Date: Tue, 21 Apr 2015 17:45:45 -0400 Subject: [PATCH] https://issues.apache.org/jira/browse/AMQ-5711 Reject sender / receiver creation when the address is a temp destination prefixed value but the destination doesn't map down to a temp destination created using a dynamic link which means the orignal connection ID where the destination was created cannot be determined. --- .../transport/amqp/protocol/AmqpSession.java | 16 ++++- .../amqp/interop/AmqpTempDestinationTest.java | 63 +++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/activemq-amqp/src/main/java/org/apache/activemq/transport/amqp/protocol/AmqpSession.java b/activemq-amqp/src/main/java/org/apache/activemq/transport/amqp/protocol/AmqpSession.java index 6ca4d71c18..d9f0c0f07a 100644 --- a/activemq-amqp/src/main/java/org/apache/activemq/transport/amqp/protocol/AmqpSession.java +++ b/activemq-amqp/src/main/java/org/apache/activemq/transport/amqp/protocol/AmqpSession.java @@ -179,6 +179,12 @@ public class AmqpSession implements AmqpResource { }); } else if (targetNodeName != null && !targetNodeName.isEmpty()) { destination = createDestination(remoteTarget); + if (destination.isTemporary()) { + String connectionId = ((ActiveMQTempDestination) destination).getConnectionId(); + if (connectionId == null) { + throw new AmqpProtocolException(AmqpError.PRECONDITION_FAILED.toString(), "Not a broker created temp destination"); + } + } } receiver.setDestination(destination); @@ -276,6 +282,12 @@ public class AmqpSession implements AmqpResource { }); } else { destination = createDestination(source); + if (destination.isTemporary()) { + String connectionId = ((ActiveMQTempDestination) destination).getConnectionId(); + if (connectionId == null) { + throw new AmqpProtocolException(AmqpError.INVALID_FIELD.toString(), "Not a broker created temp destination"); + } + } } source.setFilter(supportedFilters.isEmpty() ? null : supportedFilters); @@ -359,11 +371,11 @@ public class AmqpSession implements AmqpResource { //----- Internal Implementation ------------------------------------------// - protected ConsumerId getNextConsumerId() { + private ConsumerId getNextConsumerId() { return new ConsumerId(sessionId, nextConsumerId++); } - protected ProducerId getNextProducerId() { + private ProducerId getNextProducerId() { return new ProducerId(sessionId, nextProducerId++); } } diff --git a/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/interop/AmqpTempDestinationTest.java b/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/interop/AmqpTempDestinationTest.java index 2df5141b71..ea686399e5 100644 --- a/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/interop/AmqpTempDestinationTest.java +++ b/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/interop/AmqpTempDestinationTest.java @@ -21,6 +21,7 @@ import static org.apache.activemq.transport.amqp.AmqpSupport.TEMP_QUEUE_CAPABILI import static org.apache.activemq.transport.amqp.AmqpSupport.TEMP_TOPIC_CAPABILITY; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; import java.util.HashMap; import java.util.Map; @@ -45,6 +46,68 @@ import org.junit.Test; */ public class AmqpTempDestinationTest extends AmqpClientTestSupport { + @Test(timeout = 60000) + public void testCannotCreateSenderWithNamedTempQueue() throws Exception { + doTestCannotCreateSenderWithNamedTempDestination(false); + } + + @Test(timeout = 60000) + public void testCannotCreateSenderWithNamedTempTopic() throws Exception { + doTestCannotCreateSenderWithNamedTempDestination(true); + } + + protected void doTestCannotCreateSenderWithNamedTempDestination(boolean topic) throws Exception { + + AmqpClient client = createAmqpClient(); + AmqpConnection connection = client.connect(); + AmqpSession session = connection.createSession(); + + String address = null; + if (topic) { + address = "temp-topic://" + getTestName(); + } else { + address = "temp-queue://" + getTestName(); + } + + try { + session.createSender(address); + fail("Should not be able to create sender to a temp destination that doesn't exist."); + } catch (Exception ex) { + LOG.info("Error creating sender: {}", ex.getMessage()); + } + } + + @Test(timeout = 60000) + public void testCanntCreateReceverWithNamedTempQueue() throws Exception { + doTestCannotCreateReceiverWithNamedTempDestination(false); + } + + @Test(timeout = 60000) + public void testCannotCreateReceiverWithNamedTempTopic() throws Exception { + doTestCannotCreateReceiverWithNamedTempDestination(true); + } + + protected void doTestCannotCreateReceiverWithNamedTempDestination(boolean topic) throws Exception { + + AmqpClient client = createAmqpClient(); + AmqpConnection connection = client.connect(); + AmqpSession session = connection.createSession(); + + String address = null; + if (topic) { + address = "temp-topic://" + getTestName(); + } else { + address = "temp-queue://" + getTestName(); + } + + try { + session.createReceiver(address); + fail("Should not be able to create sender to a temp destination that doesn't exist."); + } catch (Exception ex) { + LOG.info("Error creating sender: {}", ex.getMessage()); + } + } + @Test(timeout = 60000) public void testCreateDynamicSenderToTopic() throws Exception { doTestCreateDynamicSender(true);