You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by cl...@apache.org on 2019/07/17 15:35:50 UTC

[activemq-artemis] 01/03: ARTEMIS-2409 Convert HornetQ field names in consumer/queue selector strings

This is an automated email from the ASF dual-hosted git repository.

clebertsuconic pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/activemq-artemis.git

commit 1c45d1758db0fa3543d6ee07359a6fdd1fbb7ded
Author: Tomas Hofman <th...@redhat.com>
AuthorDate: Wed Jul 3 12:30:44 2019 +0200

    ARTEMIS-2409 Convert HornetQ field names in consumer/queue selector strings
---
 .../activemq/artemis/utils/SelectorTranslator.java | 15 ++++
 .../hornetq/HornetQProtocolManagerFactory.java     |  1 +
 .../hornetq/HQFilterConversionInterceptor.java     | 62 ++++++++++++++
 .../protocols/hornetq/HornetQProtocolTest.java     | 94 ++++++++++++++++++++++
 .../unit/jms/client/SelectorTranslatorTest.java    | 31 +++++++
 5 files changed, 203 insertions(+)

diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/SelectorTranslator.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/SelectorTranslator.java
index dd391a9..d006783 100644
--- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/SelectorTranslator.java
+++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/SelectorTranslator.java
@@ -54,6 +54,21 @@ public class SelectorTranslator {
 
    }
 
+   public static String convertHQToActiveMQFilterString(final String hqFilterString) {
+      if (hqFilterString == null) {
+         return null;
+      }
+
+      String filterString = SelectorTranslator.parse(hqFilterString, "HQDurable", "AMQDurable");
+      filterString = SelectorTranslator.parse(filterString, "HQPriority", "AMQPriority");
+      filterString = SelectorTranslator.parse(filterString, "HQTimestamp", "AMQTimestamp");
+      filterString = SelectorTranslator.parse(filterString, "HQUserID", "AMQUserID");
+      filterString = SelectorTranslator.parse(filterString, "HQExpiration", "AMQExpiration");
+
+      return filterString;
+
+   }
+
    private static String parse(final String input, final String match, final String replace) {
       final char quote = '\'';
 
diff --git a/artemis-protocols/artemis-hornetq-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/hornetq/HornetQProtocolManagerFactory.java b/artemis-protocols/artemis-hornetq-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/hornetq/HornetQProtocolManagerFactory.java
index de378c5..cb8cc16 100644
--- a/artemis-protocols/artemis-hornetq-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/hornetq/HornetQProtocolManagerFactory.java
+++ b/artemis-protocols/artemis-hornetq-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/hornetq/HornetQProtocolManagerFactory.java
@@ -47,6 +47,7 @@ public class HornetQProtocolManagerFactory extends CoreProtocolManagerFactory {
       List<Interceptor> hqOutgoing = filterInterceptors(outgoingInterceptors);
 
       hqIncoming.add(new HQPropertiesConversionInterceptor(true));
+      hqIncoming.add(new HQFilterConversionInterceptor());
       hqOutgoing.add(new HQPropertiesConversionInterceptor(false));
 
       stripPasswordParameters(parameters);
diff --git a/artemis-protocols/artemis-hqclient-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/hornetq/HQFilterConversionInterceptor.java b/artemis-protocols/artemis-hqclient-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/hornetq/HQFilterConversionInterceptor.java
new file mode 100644
index 0000000..e1a5257
--- /dev/null
+++ b/artemis-protocols/artemis-hqclient-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/hornetq/HQFilterConversionInterceptor.java
@@ -0,0 +1,62 @@
+/*
+ * 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.artemis.core.protocol.hornetq;
+
+import org.apache.activemq.artemis.api.core.Interceptor;
+import org.apache.activemq.artemis.api.core.SimpleString;
+import org.apache.activemq.artemis.core.protocol.core.Packet;
+import org.apache.activemq.artemis.core.protocol.core.impl.PacketImpl;
+import org.apache.activemq.artemis.core.protocol.core.impl.wireformat.CreateQueueMessage;
+import org.apache.activemq.artemis.core.protocol.core.impl.wireformat.CreateSharedQueueMessage;
+import org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionCreateConsumerMessage;
+import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;
+import org.apache.activemq.artemis.utils.SelectorTranslator;
+
+public class HQFilterConversionInterceptor implements Interceptor {
+   @Override
+   public boolean intercept(Packet packet, RemotingConnection connection) {
+      if (packet.getType() == PacketImpl.SESS_CREATECONSUMER) {
+         handleMessage((SessionCreateConsumerMessage) packet);
+      } else if (packet.getType() == PacketImpl.CREATE_QUEUE || packet.getType() == PacketImpl.CREATE_QUEUE_V2) {
+         handleMessage((CreateQueueMessage) packet);
+      } else if (packet.getType() == PacketImpl.CREATE_SHARED_QUEUE || packet.getType() == PacketImpl.CREATE_SHARED_QUEUE_V2) {
+         handleMessage((CreateSharedQueueMessage) packet);
+      }
+      return true;
+   }
+
+   private void handleMessage(SessionCreateConsumerMessage message) {
+      message.setFilterString(replaceFilterString(message.getFilterString()));
+   }
+
+   private void handleMessage(CreateQueueMessage message) {
+      message.setFilterString(replaceFilterString(message.getFilterString()));
+   }
+
+   private void handleMessage(CreateSharedQueueMessage message) {
+      message.setFilterString(replaceFilterString(message.getFilterString()));
+   }
+
+   private SimpleString replaceFilterString(SimpleString filterString) {
+      if (filterString == null) {
+         return null;
+      }
+      return SimpleString.toSimpleString(
+            SelectorTranslator.convertHQToActiveMQFilterString(filterString.toString()));
+   }
+}
diff --git a/tests/extra-tests/src/test/java/org/apache/activemq/artemis/tests/extras/protocols/hornetq/HornetQProtocolTest.java b/tests/extra-tests/src/test/java/org/apache/activemq/artemis/tests/extras/protocols/hornetq/HornetQProtocolTest.java
index 920b77a..b498d34 100644
--- a/tests/extra-tests/src/test/java/org/apache/activemq/artemis/tests/extras/protocols/hornetq/HornetQProtocolTest.java
+++ b/tests/extra-tests/src/test/java/org/apache/activemq/artemis/tests/extras/protocols/hornetq/HornetQProtocolTest.java
@@ -32,7 +32,9 @@ import org.apache.activemq.artemis.api.core.client.ServerLocator;
 import org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnectorFactory;
 import org.apache.activemq.artemis.core.server.ActiveMQServer;
 import org.apache.activemq.artemis.tests.util.ActiveMQTestBase;
+import org.hornetq.api.core.HornetQException;
 import org.hornetq.api.core.client.HornetQClient;
+import org.hornetq.utils.UUIDGenerator;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
@@ -104,6 +106,45 @@ public class HornetQProtocolTest extends ActiveMQTestBase {
       hqSession.close();
    }
 
+
+   @Test
+   public void testCreateConsumerHQSelectorIsTransformed() throws Exception {
+      try (org.hornetq.api.core.client.ClientSession hqSession = createHQClientSession()) {
+
+         // Create Queue
+         String queueName = "test.hq.queue";
+         hqSession.createQueue(queueName, queueName, true);
+
+         hqSession.start();
+
+         // Send message with UserID set
+         org.hornetq.utils.UUID userID = UUIDGenerator.getInstance().generateUUID();
+         try (org.hornetq.api.core.client.ClientProducer hqProducer = hqSession.createProducer(queueName)) {
+            org.hornetq.api.core.client.ClientMessage message = createHQTestMessage(hqSession);
+            message.setUserID(userID);
+            hqProducer.send(message);
+         }
+
+         // Verify that selector using AMQ field works
+         verifyConsumerWithSelector(hqSession, queueName,
+                 String.format("AMQUserID = 'ID:%s'", userID.toString()), userID);
+
+         // Verify that selector using HornetQ field works
+         verifyConsumerWithSelector(hqSession, queueName,
+                 String.format("HQUserID = 'ID:%s'", userID.toString()), userID);
+      }
+   }
+
+   @Test
+   public void testCreateQueueHQFilterIsTransformed() throws Exception {
+      testQueueWithHQFilter(false);
+   }
+
+   @Test
+   public void testCreateTemporaryQueueHQFilterIsTransformed() throws Exception {
+      testQueueWithHQFilter(true);
+   }
+
    @Test
    public void testLargeMessagesOverHornetQClients() throws Exception {
       org.hornetq.api.core.client.ClientSession hqSession = createHQClientSession();
@@ -236,4 +277,57 @@ public class HornetQProtocolTest extends ActiveMQTestBase {
 
       return sf.createSession();
    }
+
+   private static void verifyConsumerWithSelector(org.hornetq.api.core.client.ClientSession hqSession, String queueName,
+                                                  String selector, org.hornetq.utils.UUID expectedUserID)
+           throws HornetQException {
+      try (org.hornetq.api.core.client.ClientConsumer hqConsumer =
+                   hqSession.createConsumer(queueName, selector, true)) {
+         org.hornetq.api.core.client.ClientMessage message = hqConsumer.receive(1000);
+
+         Assert.assertNotNull(message);
+         Assert.assertEquals(expectedUserID, message.getUserID());
+      }
+   }
+
+   private void testQueueWithHQFilter(boolean temporary) throws Exception {
+      try (org.hornetq.api.core.client.ClientSession hqSession = createHQClientSession()) {
+
+         org.hornetq.utils.UUID userID = UUIDGenerator.getInstance().generateUUID();
+
+         // Create queue with filter
+         String queueName = "test.hq.queue";
+         String filter = String.format("HQUserID = 'ID:%s'", userID.toString());
+         if (temporary) {
+            hqSession.createTemporaryQueue(queueName, queueName, filter);
+         } else {
+            hqSession.createQueue(queueName, queueName, filter, true);
+         }
+
+         hqSession.start();
+
+         // Send two messages with different UserIDs
+         try (org.hornetq.api.core.client.ClientProducer hqProducer = hqSession.createProducer(queueName)) {
+            org.hornetq.api.core.client.ClientMessage message = createHQTestMessage(hqSession);
+            message.setUserID(userID);
+            hqProducer.send(message);
+
+            message = createHQTestMessage(hqSession);
+            message.setUserID(UUIDGenerator.getInstance().generateUUID());
+            hqProducer.send(message);
+         }
+
+         // Only the message matching the queue filter should be present
+         try (org.hornetq.api.core.client.ClientConsumer hqConsumer =
+                      hqSession.createConsumer(queueName, true)) {
+            org.hornetq.api.core.client.ClientMessage message = hqConsumer.receiveImmediate();
+
+            Assert.assertNotNull(message);
+            Assert.assertEquals(userID, message.getUserID());
+
+            message = hqConsumer.receiveImmediate();
+            Assert.assertNull(message);
+         }
+      }
+   }
 }
diff --git a/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/jms/client/SelectorTranslatorTest.java b/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/jms/client/SelectorTranslatorTest.java
index c2b14eb..cc8667b 100644
--- a/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/jms/client/SelectorTranslatorTest.java
+++ b/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/jms/client/SelectorTranslatorTest.java
@@ -204,6 +204,37 @@ public class SelectorTranslatorTest extends ActiveMQTestBase {
       checkNoSubstitute("JMSType");
    }
 
+   @Test
+   public void testConvertHQFilterString() {
+      String selector = "HQUserID = 'ID:AMQ-12435678'";
+
+      Assert.assertEquals("AMQUserID = 'ID:AMQ-12435678'", SelectorTranslator.convertHQToActiveMQFilterString(selector));
+
+      selector = "HQUserID = 'HQUserID'";
+
+      Assert.assertEquals("AMQUserID = 'HQUserID'", SelectorTranslator.convertHQToActiveMQFilterString(selector));
+
+      selector = "HQUserID = 'ID:AMQ-12435678'";
+
+      Assert.assertEquals("AMQUserID = 'ID:AMQ-12435678'", SelectorTranslator.convertHQToActiveMQFilterString(selector));
+
+      selector = "HQDurable='NON_DURABLE'";
+
+      Assert.assertEquals("AMQDurable='NON_DURABLE'", SelectorTranslator.convertHQToActiveMQFilterString(selector));
+
+      selector = "HQPriority=5";
+
+      Assert.assertEquals("AMQPriority=5", SelectorTranslator.convertHQToActiveMQFilterString(selector));
+
+      selector = "HQTimestamp=12345678";
+
+      Assert.assertEquals("AMQTimestamp=12345678", SelectorTranslator.convertHQToActiveMQFilterString(selector));
+
+      selector = "HQExpiration=12345678";
+
+      Assert.assertEquals("AMQExpiration=12345678", SelectorTranslator.convertHQToActiveMQFilterString(selector));
+   }
+
    // Private -------------------------------------------------------------------------------------
 
    private void checkNoSubstitute(final String fieldName) {