Example usage for java.net IDN toASCII

List of usage examples for java.net IDN toASCII

Introduction

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

Prototype

public static String toASCII(String input, int flag) 

Source Link

Document

Translates a string from Unicode to ASCII Compatible Encoding (ACE), as defined by the ToASCII operation of <a href="http://www.ietf.org/rfc/rfc3490.txt">RFC 3490</a>.

Usage

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");
    }//  ww  w  .j a v  a2  s . 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;
}

From source file:org.apache.metron.common.typosquat.HomoglyphStrategy.java

@Override
public Set<String> generateCandidates(String originalString) {
    Set<String> result = new HashSet<>();
    String domain = originalString;
    if (StringUtils.isEmpty(domain)) {
        return result;
    }//from w  w  w  . jav a2s. c o  m
    if (isAce(domain)) {
        //this is an ace domain.
        domain = IDN.toUnicode(domain);
    }
    for (int ws = 0; ws < domain.length(); ws++) {
        for (int i = 0; i < domain.length() - ws + 1; ++i) {
            String win = domain.substring(i, i + ws);
            for (int j = 0; j < ws; j++) {
                char c = win.charAt(j);
                if (glyphs.containsKey(c)) {
                    for (String g : glyphs.get(c)) {
                        String winNew = win.replaceAll("" + c, g);
                        String d = domain.substring(0, i) + winNew + domain.substring(i + ws);
                        result.add(d);
                        if (!isAce(d)) {
                            try {
                                String dAscii = IDN.toASCII(d, IDN.ALLOW_UNASSIGNED);
                                if (!d.equals(dAscii)) {
                                    result.add(dAscii);
                                }
                            } catch (IllegalArgumentException iae) {
                                LOG.debug("Unable to parse " + d + ": " + iae.getMessage(), iae);
                            }
                        }
                    }
                }
            }
        }
    }
    return result;
}

From source file:com.chiorichan.http.ssl.SniNegotiator.java

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (!handshaken && in.readableBytes() >= 5) {
        String hostname = sniHostNameFromHandshakeInfo(in);
        if (hostname != null)
            hostname = IDN.toASCII(hostname, IDN.ALLOW_UNASSIGNED).toLowerCase(Locale.US);
        this.hostname = hostname;

        selectedContext = SslManager.instance().map(hostname);

        if (handshaken) {
            SSLEngine engine = selectedContext.newEngine(ctx.alloc());

            List<String> supportedCipherSuites = Arrays.asList(engine.getSupportedCipherSuites());

            if (!supportedCipherSuites.containsAll(enabledCipherSuites))
                for (String cipher : enabledCipherSuites)
                    if (!supportedCipherSuites.contains(cipher)) {
                        NetworkManager.getLogger()
                                .severe(String.format(
                                        "The SSL/TLS cipher suite '%s' is not supported by SSL Provider %s",
                                        cipher, SslContext.defaultServerProvider().name()));
                        enabledCipherSuites.remove(cipher);
                    }//from www. java 2 s .c  o  m

            engine.setUseClientMode(false);
            engine.setEnabledCipherSuites(enabledCipherSuites.toArray(new String[0]));

            ctx.pipeline().replace(this, ctx.name(), new SslExceptionHandler(engine));
        }
    }
}