import org.apache.commons.lang3.StringUtils; //導入方法依賴的package包/類
/**
* @return IDN normalized hostname
*/
public String convert(final String hostname) {
if(!PreferencesFactory.get().getBoolean("connection.hostname.idn")) {
return StringUtils.strip(hostname);
}
if(StringUtils.isNotEmpty(hostname)) {
try {
// Convenience function that implements the IDNToASCII operation as defined in
// the IDNA RFC. This operation is done on complete domain names, e.g: "www.example.com".
// It is important to note that this operation can fail. If it fails, then the input
// domain name cannot be used as an Internationalized Domain Name and the application
// should have methods defined to deal with the failure.
// IDNA.DEFAULT Use default options, i.e., do not process unassigned code points
// and do not use STD3 ASCII rules If unassigned code points are found
// the operation fails with ParseException
final String idn = IDN.toASCII(StringUtils.strip(hostname));
if(log.isDebugEnabled()) {
if(!StringUtils.equals(StringUtils.strip(hostname), idn)) {
log.debug(String.format("IDN hostname for %s is %s", hostname, idn));
}
}
if(StringUtils.isNotEmpty(idn)) {
return idn;
}
}
catch(IllegalArgumentException e) {
log.warn(String.format("Failed to convert hostname %s to IDNA", hostname), e);
}
}
return StringUtils.strip(hostname);
}