CAMEL-6960: Sanitize hostanme in uuid generator.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/1350b4de Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/1350b4de Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/1350b4de Branch: refs/heads/camel-2.12.x Commit: 1350b4defd26910f8aa31866409574e68d182536 Parents: bfdabca Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Nov 13 16:41:26 2013 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Nov 13 16:41:49 2013 +0100 ---------------------------------------------------------------------- .../camel/impl/ActiveMQUuidGenerator.java | 23 ++++++++++++++++++++ .../camel/impl/ActiveMQUuidGeneratorTest.java | 6 +++++ 2 files changed, 29 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/1350b4de/camel-core/src/main/java/org/apache/camel/impl/ActiveMQUuidGenerator.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/impl/ActiveMQUuidGenerator.java b/camel-core/src/main/java/org/apache/camel/impl/ActiveMQUuidGenerator.java index 94de310..2020714 100644 --- a/camel-core/src/main/java/org/apache/camel/impl/ActiveMQUuidGenerator.java +++ b/camel-core/src/main/java/org/apache/camel/impl/ActiveMQUuidGenerator.java @@ -97,6 +97,7 @@ public class ActiveMQUuidGenerator implements UuidGenerator { if (hostName == null) { hostName = "localhost"; } + hostName = sanitizeHostName(hostName); if (ObjectHelper.isEmpty(stub)) { stub = "-1-" + System.currentTimeMillis() + "-"; @@ -127,6 +128,28 @@ public class ActiveMQUuidGenerator implements UuidGenerator { return hostName; } + 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; + } + } + public String generateUuid() { StringBuilder sb = new StringBuilder(length); sb.append(seed); http://git-wip-us.apache.org/repos/asf/camel/blob/1350b4de/camel-core/src/test/java/org/apache/camel/impl/ActiveMQUuidGeneratorTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/impl/ActiveMQUuidGeneratorTest.java b/camel-core/src/test/java/org/apache/camel/impl/ActiveMQUuidGeneratorTest.java index 85fe31e..2bf6e5c 100644 --- a/camel-core/src/test/java/org/apache/camel/impl/ActiveMQUuidGeneratorTest.java +++ b/camel-core/src/test/java/org/apache/camel/impl/ActiveMQUuidGeneratorTest.java @@ -48,4 +48,10 @@ public class ActiveMQUuidGeneratorTest extends TestCase { LOG.info("Took " + TimeUtils.printDuration(watch.stop())); } + public void testSanitizeHostName() throws Exception { + assertEquals("somehost.lan", ActiveMQUuidGenerator.sanitizeHostName("somehost.lan")); + // include a UTF-8 char in the text \u0E08 is a Thai elephant + assertEquals("otherhost.lan", ActiveMQUuidGenerator.sanitizeHostName("other\u0E08host.lan")); + } + }