Example usage for java.net IDN USE_STD3_ASCII_RULES

List of usage examples for java.net IDN USE_STD3_ASCII_RULES

Introduction

In this page you can find the example usage for java.net IDN USE_STD3_ASCII_RULES.

Prototype

int USE_STD3_ASCII_RULES

To view the source code for java.net IDN USE_STD3_ASCII_RULES.

Click Source Link

Document

Flag to turn on the check against STD-3 ASCII rules

Usage

From source file:org.jivesoftware.util.StringUtils.java

/**
 * Returns a valid domain name, possibly as an ACE-encoded IDN 
 * (per <a href="http://www.ietf.org/rfc/rfc3490.txt">RFC 3490</a>).
 * /*from   w w w  . j  a  v a2s  .c om*/
 * @param domain Proposed domain name
 * @return The validated domain name, possibly ACE-encoded
 * @throws IllegalArgumentException The given domain name is not valid
 */
public static String validateDomainName(String domain) {
    if (domain == null || domain.trim().length() == 0) {
        throw new IllegalArgumentException("Domain name cannot be null or empty");
    }
    String result = IDN.toASCII(domain);
    if (result.equals(domain)) {
        // no conversion; validate again via USE_STD3_ASCII_RULES
        IDN.toASCII(domain, IDN.USE_STD3_ASCII_RULES);
    } else {
        Log.info(MessageFormat.format("Converted domain name: from '{0}' to '{1}'", domain, result));
    }
    return result;
}

From source file:pl.nask.hsn2.normalizers.URLNormalizerUtils.java

public static String dnsToIDN(StringBuilder str, int startIndx, int endIndx) throws URLHostParseException {
    if (str.length() == 0) {
        throw new URLHostParseException("Cannot process empty string");
    }/*from  w w  w  .ja v  a 2s.  c o  m*/
    if (endIndx > str.length()) {
        endIndx = str.length();
    }
    int dd = str.indexOf("..", startIndx);
    if (dd >= 0 && dd < endIndx) {
        throw new URLHostParseException(
                "Sequence '..' is forbidden in DNS: '" + str.substring(startIndx, endIndx) + "'");
    }

    String host;
    try {
        host = IDN.toASCII(str.substring(startIndx, endIndx), IDN.USE_STD3_ASCII_RULES & IDN.ALLOW_UNASSIGNED);
        if (host.startsWith(".")) {
            throw new URLHostParseException("DNS name cannot start with .");
        }
    } catch (IllegalArgumentException e) {
        throw new URLHostParseException("Error converting DNS:" + str.toString(), e);
    }

    if (host.length() == str.length() && !EncodingType.DNS_ALLOWED.allowed(host)) {
        throw new URLHostParseException("Error converting DNS: " + str.toString());
    }

    if (host.length() == 0) {
        return host;
    }
    host = host.toLowerCase();

    str.replace(startIndx, endIndx, host);
    return host;
}