From commits-return-50463-archive-asf-public=cust-asf.ponee.io@activemq.apache.org Wed Feb 7 16:27:40 2018 Return-Path: X-Original-To: archive-asf-public@eu.ponee.io Delivered-To: archive-asf-public@eu.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by mx-eu-01.ponee.io (Postfix) with ESMTP id 05730180676 for ; Wed, 7 Feb 2018 16:27:40 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id E93A2160C5E; Wed, 7 Feb 2018 15:27:39 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id D8E43160C3C for ; Wed, 7 Feb 2018 16:27:38 +0100 (CET) Received: (qmail 31581 invoked by uid 500); 7 Feb 2018 15:27:38 -0000 Mailing-List: contact commits-help@activemq.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@activemq.apache.org Delivered-To: mailing list commits@activemq.apache.org Received: (qmail 31572 invoked by uid 99); 7 Feb 2018 15:27:38 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 07 Feb 2018 15:27:38 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id EEC62DFC25; Wed, 7 Feb 2018 15:27:37 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: michaelpearce@apache.org To: commits@activemq.apache.org Date: Wed, 07 Feb 2018 15:27:38 -0000 Message-Id: <42c82c29533b4d97bc5dfb84c2d85a8b@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [2/5] activemq-artemis git commit: ARTEMIS-853 Support for exclusive consumers ARTEMIS-853 Support for exclusive consumers Rationalise and re-use URISupport. Project: http://git-wip-us.apache.org/repos/asf/activemq-artemis/repo Commit: http://git-wip-us.apache.org/repos/asf/activemq-artemis/commit/38c45c92 Tree: http://git-wip-us.apache.org/repos/asf/activemq-artemis/tree/38c45c92 Diff: http://git-wip-us.apache.org/repos/asf/activemq-artemis/diff/38c45c92 Branch: refs/heads/master Commit: 38c45c92142cd6a3b9f49f2d436d7a0665309c00 Parents: dc41f3c Author: Michael André Pearce Authored: Fri Feb 2 18:19:40 2018 +0000 Committer: Michael Andre Pearce Committed: Wed Feb 7 15:27:29 2018 +0000 ---------------------------------------------------------------------- .../artemis/api/core/ParameterisedAddress.java | 55 +++++++------------- .../activemq/artemis/utils/uri/URISupport.java | 32 ++++++++---- .../jms/client/ActiveMQParameterTest.java | 17 ++++++ 3 files changed, 58 insertions(+), 46 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/38c45c92/artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/ParameterisedAddress.java ---------------------------------------------------------------------- diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/ParameterisedAddress.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/ParameterisedAddress.java index bbc3c4d..6a6d45c 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/ParameterisedAddress.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/ParameterisedAddress.java @@ -16,46 +16,32 @@ */ package org.apache.activemq.artemis.api.core; +import static org.apache.activemq.artemis.utils.uri.URISupport.appendParameters; +import static org.apache.activemq.artemis.utils.uri.URISupport.parseQuery; + +import java.net.URISyntaxException; import java.util.Map; +import org.apache.activemq.artemis.utils.uri.URISupport; + public class ParameterisedAddress { - public static SimpleString toParameterisedAddress(SimpleString address, Map parameters) { - if (parameters != null && parameters.size() > 0) { + public static SimpleString toParameterisedAddress(SimpleString address, Map parameters) throws URISyntaxException { + if (parameters != null && !parameters.isEmpty()) { return SimpleString.toSimpleString(toParameterisedAddress(address.toString(), parameters)); } else { return address; } } - public static String toParameterisedAddress(String address, Map parameters) { - if (parameters != null && parameters.size() > 0) { - StringBuilder stringBuilder = new StringBuilder(address).append(PARAMETER_MARKER); - return toParameterString(stringBuilder, parameters).toString(); + public static String toParameterisedAddress(String address, Map parameters) throws URISyntaxException { + if (parameters != null && !parameters.isEmpty()) { + return appendParameters(new StringBuilder(address), parameters).toString(); } else { return address; } } - private static StringBuilder toParameterString(StringBuilder stringBuilder, Map parameters) { - boolean first = true; - for (Map.Entry entry : parameters.entrySet()) { - if (first) { - first = false; - } else { - stringBuilder.append(PARAMETER_SEPERATOR); - } - stringBuilder.append(entry.getKey()).append(PARAMETER_KEY_VALUE_SEPERATOR).append(entry.getValue()); - } - return stringBuilder; - } - - public static char PARAMETER_SEPERATOR = '&'; - public static char PARAMETER_KEY_VALUE_SEPERATOR = '='; - public static char PARAMETER_MARKER = '?'; - public static String PARAMETER_SEPERATOR_STRING = Character.toString(PARAMETER_SEPERATOR); - public static String PARAMETER_KEY_VALUE_SEPERATOR_STRING = Character.toString(PARAMETER_KEY_VALUE_SEPERATOR); - public static String PARAMETER_MARKER_STRING = Character.toString(PARAMETER_MARKER); private final SimpleString address; private final QueueAttributes queueAttributes; @@ -81,22 +67,17 @@ public class ParameterisedAddress { } public ParameterisedAddress(String address) { - int index = address.indexOf(PARAMETER_MARKER); + int index = address.indexOf('?'); if (index == -1) { this.address = SimpleString.toSimpleString(address); this.queueAttributes = null; } else { this.address = SimpleString.toSimpleString(address.substring(0, index)); - String parametersString = address.substring(index + 1, address.length()); - String[] parameterPairs = parametersString.split(PARAMETER_SEPERATOR_STRING); QueueAttributes queueAttributes = new QueueAttributes(); - for (String param : parameterPairs) { - String[] keyValue = param.split(PARAMETER_KEY_VALUE_SEPERATOR_STRING); - if (keyValue.length != 2) { - throw new IllegalArgumentException("Malformed parameter section " + param); - } else { - queueAttributes.set(keyValue[0], keyValue[1]); - } + try { + parseQuery(address).forEach(queueAttributes::set); + } catch (URISyntaxException use) { + throw new IllegalArgumentException("Malformed parameters in address " + address); } this.queueAttributes = queueAttributes; } @@ -107,11 +88,11 @@ public class ParameterisedAddress { } public static boolean isParameterised(String address) { - return address.contains(PARAMETER_MARKER_STRING); + return URISupport.containsQuery(address); } public static boolean isParameterised(SimpleString address) { - return address.contains(PARAMETER_MARKER); + return URISupport.containsQuery(address); } } http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/38c45c92/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISupport.java ---------------------------------------------------------------------- diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISupport.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISupport.java index e26993d..7530759 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISupport.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISupport.java @@ -27,6 +27,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.apache.activemq.artemis.api.core.SimpleString; + /** * Utility class that provides methods for parsing URI's * @@ -75,7 +77,7 @@ public class URISupport { } public URI toURI() throws URISyntaxException { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); if (scheme != null) { sb.append(scheme); sb.append(':'); @@ -98,18 +100,23 @@ public class URISupport { sb.append('/'); sb.append(path); } - if (!parameters.isEmpty()) { - sb.append("?"); - sb.append(createQueryString(parameters)); - } + appendParameters(sb, parameters); if (fragment != null) { - sb.append("#"); + sb.append('#'); sb.append(fragment); } return new URI(sb.toString()); } } + public static StringBuilder appendParameters(StringBuilder sb, Map parameters) throws URISyntaxException { + if (!parameters.isEmpty()) { + sb.append('?'); + sb.append(createQueryString(parameters)); + } + return sb; + } + /** * Give a URI break off any URI options and store them in a Key / Value Mapping. * @@ -122,8 +129,7 @@ public class URISupport { uri = uri.substring(uri.lastIndexOf("?") + 1); // get only the relevant part of the query Map rc = new HashMap<>(); if (uri != null && !uri.isEmpty()) { - parseParameters(rc, uri.split("&")); - parseParameters(rc, uri.split(";")); + parseParameters(rc, uri.split("[&;]")); } return rc; } catch (UnsupportedEncodingException e) { @@ -131,6 +137,14 @@ public class URISupport { } } + public static boolean containsQuery(String uri) { + return uri.contains("?"); + } + + public static boolean containsQuery(SimpleString uri) { + return uri.contains('?'); + } + private static void parseParameters(Map rc, String[] parameters) throws UnsupportedEncodingException { for (String parameter : parameters) { @@ -198,7 +212,7 @@ public class URISupport { Map queryParameters, String optionPrefix) throws URISyntaxException { if (queryParameters != null && !queryParameters.isEmpty()) { - StringBuffer newQuery = uri.getRawQuery() != null ? new StringBuffer(uri.getRawQuery()) : new StringBuffer(); + StringBuilder newQuery = uri.getRawQuery() != null ? new StringBuilder(uri.getRawQuery()) : new StringBuilder(); for (Map.Entry param : queryParameters.entrySet()) { if (param.getKey().startsWith(optionPrefix)) { if (newQuery.length() != 0) { http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/38c45c92/artemis-jms-client/src/test/java/org/apache/activemq/artemis/jms/client/ActiveMQParameterTest.java ---------------------------------------------------------------------- diff --git a/artemis-jms-client/src/test/java/org/apache/activemq/artemis/jms/client/ActiveMQParameterTest.java b/artemis-jms-client/src/test/java/org/apache/activemq/artemis/jms/client/ActiveMQParameterTest.java index cf42fe3..1cb179d 100644 --- a/artemis-jms-client/src/test/java/org/apache/activemq/artemis/jms/client/ActiveMQParameterTest.java +++ b/artemis-jms-client/src/test/java/org/apache/activemq/artemis/jms/client/ActiveMQParameterTest.java @@ -18,6 +18,7 @@ package org.apache.activemq.artemis.jms.client; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import org.junit.Test; @@ -42,5 +43,21 @@ public class ActiveMQParameterTest { activeMQDestination = new ActiveMQQueue("jms.queue.foo?last-value=false"); assertFalse(activeMQDestination.getQueueAttributes().getLastValue()); + + } + + @Test + public void testMultipleQueueParameters() { + ActiveMQDestination activeMQDestination = new ActiveMQQueue("jms.queue.foo?last-value=true&exclusive=true"); + assertEquals("jms.queue.foo", activeMQDestination.getAddress()); + assertTrue(activeMQDestination.getQueueAttributes().getLastValue()); + assertTrue(activeMQDestination.getQueueAttributes().getExclusive()); + } + + @Test + public void testNoQueueParameters() { + ActiveMQDestination activeMQDestination = new ActiveMQQueue("jms.queue.foo"); + assertEquals("jms.queue.foo", activeMQDestination.getAddress()); + assertNull(activeMQDestination.getQueueAttributes()); } }