From ba4a9ed841a9b16c9cdf2566bafab2fccbc0cd35 Mon Sep 17 00:00:00 2001 From: "Timothy A. Bish" Date: Tue, 12 Jun 2012 21:09:37 +0000 Subject: [PATCH] fix for: https://issues.apache.org/jira/browse/AMQ-3828 git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1349527 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/activemq/util/URISupport.java | 52 +++++++-- .../apache/activemq/util/URISupportTest.java | 107 ++++++++++-------- 2 files changed, 101 insertions(+), 58 deletions(-) diff --git a/activemq-core/src/main/java/org/apache/activemq/util/URISupport.java b/activemq-core/src/main/java/org/apache/activemq/util/URISupport.java index e969fc0db5..1d69753e32 100755 --- a/activemq-core/src/main/java/org/apache/activemq/util/URISupport.java +++ b/activemq-core/src/main/java/org/apache/activemq/util/URISupport.java @@ -219,6 +219,37 @@ public class URISupport { } } + public static int indexOfParenthesisMatch(String str, int first) throws URISyntaxException { + int index = -1; + + if (first < 0 || first > str.length()) { + throw new IllegalArgumentException("Invalid position for first parenthesis: " + first); + } + + if (str.charAt(first) != '(') { + throw new IllegalArgumentException("character at indicated position is not a parenthesis"); + } + + int depth = 1; + char[] array = str.toCharArray(); + for (index = first + 1; index < array.length; ++index) { + char current = array[index]; + if (current == '(') { + depth++; + } else if (current == ')') { + if (--depth == 0) { + break; + } + } + } + + if (depth != 0) { + throw new URISyntaxException(str, "URI did not contain a matching parenthesis."); + } + + return index; + } + /** * @param uri * @param rc @@ -234,16 +265,19 @@ public class URISupport { } int p; - int intialParen = ssp.indexOf("("); - if (intialParen == 0) { - rc.host = ssp.substring(0, intialParen); + int initialParen = ssp.indexOf("("); + if (initialParen == 0) { + + rc.host = ssp.substring(0, initialParen); p = rc.host.indexOf("/"); + if (p >= 0) { rc.path = rc.host.substring(p); rc.host = rc.host.substring(0, p); } - p = ssp.lastIndexOf(")"); - componentString = ssp.substring(intialParen + 1, p); + + p = indexOfParenthesisMatch(ssp, initialParen); + componentString = ssp.substring(initialParen + 1, p); params = ssp.substring(p + 1).trim(); } else { @@ -364,6 +398,7 @@ public class URISupport { .getPath(), bindAddr.getQuery(), bindAddr.getFragment()); } + public static boolean checkParenthesis(String str) { boolean result = true; if (str != null) { @@ -384,11 +419,4 @@ public class URISupport { } return result; } - - public int indexOfParenthesisMatch(String str) { - int result = -1; - - return result; - } - } diff --git a/activemq-core/src/test/java/org/apache/activemq/util/URISupportTest.java b/activemq-core/src/test/java/org/apache/activemq/util/URISupportTest.java index 3a98c1fdef..9102c23a4e 100644 --- a/activemq-core/src/test/java/org/apache/activemq/util/URISupportTest.java +++ b/activemq-core/src/test/java/org/apache/activemq/util/URISupportTest.java @@ -22,22 +22,19 @@ import java.util.HashMap; import java.util.Map; import junit.framework.TestCase; + import org.apache.activemq.util.URISupport.CompositeData; -/** - * - * - */ public class URISupportTest extends TestCase { - + public void testEmptyCompositePath() throws Exception { CompositeData data = URISupport.parseComposite(new URI("broker:()/localhost?persistent=false")); - assertEquals(0, data.getComponents().length); + assertEquals(0, data.getComponents().length); } - + public void testCompositePath() throws Exception { CompositeData data = URISupport.parseComposite(new URI("test:(path)/path")); - assertEquals("path", data.getPath()); + assertEquals("path", data.getPath()); data = URISupport.parseComposite(new URI("test:path")); assertNull(data.getPath()); } @@ -48,61 +45,79 @@ public class URISupportTest extends TestCase { } public void testComposite() throws Exception { - CompositeData data = URISupport.parseComposite(new URI("test:(part1://host,part2://(sub1://part,sube2:part))")); + URI uri = new URI("test:(part1://host,part2://(sub1://part,sube2:part))"); + CompositeData data = URISupport.parseComposite(uri); assertEquals(2, data.getComponents().length); } - + public void testEmptyCompositeWithParenthesisInParam() throws Exception { + URI uri = new URI("failover://()?updateURIsURL=file:/C:/Dir(1)/a.csv"); + CompositeData data = URISupport.parseComposite(uri); + assertEquals(0, data.getComponents().length); + assertEquals(1, data.getParameters().size()); + assertTrue(data.getParameters().containsKey("updateURIsURL")); + assertEquals("file:/C:/Dir(1)/a.csv", data.getParameters().get("updateURIsURL")); + } + + public void testCompositeWithParenthesisInParam() throws Exception { + URI uri = new URI("failover://(test)?updateURIsURL=file:/C:/Dir(1)/a.csv"); + CompositeData data = URISupport.parseComposite(uri); + assertEquals(1, data.getComponents().length); + assertEquals(1, data.getParameters().size()); + assertTrue(data.getParameters().containsKey("updateURIsURL")); + assertEquals("file:/C:/Dir(1)/a.csv", data.getParameters().get("updateURIsURL")); + } + public void testCompositeWithComponentParam() throws Exception { CompositeData data = URISupport.parseComposite(new URI("test:(part1://host?part1=true)?outside=true")); assertEquals(1, data.getComponents().length); assertEquals(1, data.getParameters().size()); - Map part1Params = URISupport.parseParameters(data.getComponents()[0]); + Map part1Params = URISupport.parseParameters(data.getComponents()[0]); assertEquals(1, part1Params.size()); assertTrue(part1Params.containsKey("part1")); } - + public void testParsingURI() throws Exception { URI source = new URI("tcp://localhost:61626/foo/bar?cheese=Edam&x=123"); - - Map map = URISupport.parseParameters(source); - + + Map map = URISupport.parseParameters(source); + assertEquals("Size: " + map, 2, map.size()); assertMapKey(map, "cheese", "Edam"); assertMapKey(map, "x", "123"); - + URI result = URISupport.removeQuery(source); - + assertEquals("result", new URI("tcp://localhost:61626/foo/bar"), result); } - - protected void assertMapKey(Map map, String key, Object expected) { + + protected void assertMapKey(Map map, String key, Object expected) { assertEquals("Map key: " + key, map.get(key), expected); } - + public void testParsingCompositeURI() throws URISyntaxException { CompositeData data = URISupport.parseComposite(new URI("broker://(tcp://localhost:61616)?name=foo")); assertEquals("one component", 1, data.getComponents().length); assertEquals("Size: " + data.getParameters(), 1, data.getParameters().size()); } - + public void testCheckParenthesis() throws Exception { String str = "fred:(((ddd))"; assertFalse(URISupport.checkParenthesis(str)); str += ")"; assertTrue(URISupport.checkParenthesis(str)); } - + public void testCreateWithQuery() throws Exception { URI source = new URI("vm://localhost"); URI dest = URISupport.createURIWithQuery(source, "network=true&one=two"); - + assertEquals("correct param count", 2, URISupport.parseParameters(dest).size()); assertEquals("same uri, host", source.getHost(), dest.getHost()); assertEquals("same uri, scheme", source.getScheme(), dest.getScheme()); assertFalse("same uri, ssp", dest.getQuery().equals(source.getQuery())); } - + public void testParsingParams() throws Exception { URI uri = new URI("static:(http://localhost:61617?proxyHost=jo&proxyPort=90)?proxyHost=localhost&proxyPort=80"); Mapparameters = URISupport.parseParameters(uri); @@ -113,27 +128,27 @@ public class URISupportTest extends TestCase { uri = new URI("http://0.0.0.0:61616"); parameters = URISupport.parseParameters(uri); } - + public void testCompositeCreateURIWithQuery() throws Exception { - String queryString = "query=value"; - URI originalURI = new URI("outerscheme:(innerscheme:innerssp)"); - URI querylessURI = originalURI; - assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null)); - assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, "")); - assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString)); - originalURI = new URI("outerscheme:(innerscheme:innerssp)?outerquery=0"); - assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null)); - assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, "")); - assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString)); - originalURI = new URI("outerscheme:(innerscheme:innerssp?innerquery=0)"); - querylessURI = originalURI; - assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null)); - assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, "")); - assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString)); - originalURI = new URI("outerscheme:(innerscheme:innerssp?innerquery=0)?outerquery=0"); - assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null)); - assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, "")); - assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString)); + String queryString = "query=value"; + URI originalURI = new URI("outerscheme:(innerscheme:innerssp)"); + URI querylessURI = originalURI; + assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null)); + assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, "")); + assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString)); + originalURI = new URI("outerscheme:(innerscheme:innerssp)?outerquery=0"); + assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null)); + assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, "")); + assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString)); + originalURI = new URI("outerscheme:(innerscheme:innerssp?innerquery=0)"); + querylessURI = originalURI; + assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null)); + assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, "")); + assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString)); + originalURI = new URI("outerscheme:(innerscheme:innerssp?innerquery=0)?outerquery=0"); + assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null)); + assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, "")); + assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString)); } public void testApplyParameters() throws Exception { @@ -157,10 +172,10 @@ public class URISupportTest extends TestCase { uri = URISupport.applyParameters(uri, parameters, "t."); verifyParams(URISupport.parseParameters(uri)); } - + private void verifyParams(Map parameters) { assertEquals(parameters.get("proxyHost"), "localhost"); assertEquals(parameters.get("proxyPort"), "80"); } - + }