AMQ-2505: Sanitize hostname in Id generator to only include ascii chars as otherwise this can cause problems in openwire protocol.
Project: http://git-wip-us.apache.org/repos/asf/activemq/repo Commit: http://git-wip-us.apache.org/repos/asf/activemq/commit/1c392d12 Tree: http://git-wip-us.apache.org/repos/asf/activemq/tree/1c392d12 Diff: http://git-wip-us.apache.org/repos/asf/activemq/diff/1c392d12 Branch: refs/heads/activemq-5.9 Commit: 1c392d12ac7deaab33812fda38b536e08493665e Parents: 3e6c0dc Author: Claus Ibsen <[email protected]> Authored: Wed Nov 13 16:37:49 2013 +0100 Committer: Hadrian Zbarcea <[email protected]> Committed: Wed Mar 12 10:28:51 2014 -0400 ---------------------------------------------------------------------- .../org/apache/activemq/util/IdGenerator.java | 33 +++++++++++++++----- .../apache/activemq/util/IdGeneratorTest.java | 28 +++++++++++++++++ 2 files changed, 54 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/activemq/blob/1c392d12/activemq-client/src/main/java/org/apache/activemq/util/IdGenerator.java ---------------------------------------------------------------------- diff --git a/activemq-client/src/main/java/org/apache/activemq/util/IdGenerator.java b/activemq-client/src/main/java/org/apache/activemq/util/IdGenerator.java index e5a5ac5..844847a 100755 --- a/activemq-client/src/main/java/org/apache/activemq/util/IdGenerator.java +++ b/activemq-client/src/main/java/org/apache/activemq/util/IdGenerator.java @@ -26,7 +26,6 @@ import org.slf4j.LoggerFactory; /** * Generator for Globally unique Strings. */ - public class IdGenerator { private static final Logger LOG = LoggerFactory.getLogger(IdGenerator.class); @@ -85,6 +84,8 @@ public class IdGenerator { if (hostName == null) { hostName = "localhost"; } + hostName = sanitizeHostName(hostName); + if (stub.length() == 0) { stub = "-1-" + System.currentTimeMillis() + "-"; } @@ -107,22 +108,19 @@ public class IdGenerator { /** * As we have to find the hostname as a side-affect of generating a unique - * stub, we allow it's easy retrevial here + * stub, we allow it's easy retrieval here * * @return the local host name */ - public static String getHostName() { return hostName; } - /** - * Generate a unqiue id + * Generate a unique id * * @return a unique id */ - public synchronized String generateId() { StringBuilder sb = new StringBuilder(length); sb.append(seed); @@ -130,6 +128,28 @@ public class IdGenerator { return sb.toString(); } + public static String sanitizeHostName(String hostName) { + boolean changed = false; + + StringBuilder sb = new StringBuilder(); + for (char ch : hostName.toCharArray()) { + // only include ASCII chars + if (ch < 127) { + sb.append(ch); + } else { + changed = true; + } + } + + if (changed) { + String newHost = sb.toString(); + LOG.info("Sanitized hostname from: {} to: {}", hostName, newHost); + return newHost; + } else { + return hostName; + } + } + /** * Generate a unique ID - that is friendly for a URL or file system * @@ -186,7 +206,6 @@ public class IdGenerator { * @param id2 * @return 0 if equal else a positive if id1 is > id2 ... */ - public static int compare(String id1, String id2) { int result = -1; String seed1 = IdGenerator.getSeedFromId(id1); http://git-wip-us.apache.org/repos/asf/activemq/blob/1c392d12/activemq-client/src/test/java/org/apache/activemq/util/IdGeneratorTest.java ---------------------------------------------------------------------- diff --git a/activemq-client/src/test/java/org/apache/activemq/util/IdGeneratorTest.java b/activemq-client/src/test/java/org/apache/activemq/util/IdGeneratorTest.java new file mode 100644 index 0000000..e9e6564 --- /dev/null +++ b/activemq-client/src/test/java/org/apache/activemq/util/IdGeneratorTest.java @@ -0,0 +1,28 @@ +/** + * 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.util; + +import junit.framework.TestCase; + +public class IdGeneratorTest extends TestCase { + + public void testSanitizeHostName() throws Exception { + assertEquals("somehost.lan", IdGenerator.sanitizeHostName("somehost.lan")); + // include a UTF-8 char in the text \u0E08 is a Thai elephant + assertEquals("otherhost.lan", IdGenerator.sanitizeHostName("other\u0E08host.lan")); + } +}
