Updated Branches:
  refs/heads/camel-2.12.x bfdabca32 -> 1350b4def
  refs/heads/master 30ead6b82 -> 1f5ca2af9


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/1f5ca2af
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/1f5ca2af
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/1f5ca2af

Branch: refs/heads/master
Commit: 1f5ca2af97016c7100b7e8ca917b6bd015198353
Parents: 30ead6b
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:26 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/1f5ca2af/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/1f5ca2af/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"));
+    }
+
 }

Reply via email to