AMQ-2505: Sanitize hostname in Id generator to only include ascii chars as otherwise this can cause problems in openwire protocol.

This commit is contained in:
Claus Ibsen 2013-11-13 16:37:49 +01:00
parent e1a363f96e
commit 6ed8f43d8d
2 changed files with 54 additions and 7 deletions

View File

@ -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);

View File

@ -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"));
}
}