Repository: netty/netty
File: handler/src/main/java/io/netty/handler/ssl/EnhancingX509ExtendedTrustManager.java (Java)

Code before:
                            // Though the hostname matcher won't be looking at them, having them there can help
                            // debugging cases where hostname verification was enabled when it shouldn't be.
                            sb.append("URI");
                        } else {
                            continue;
                        }
                        sb.append(':').append((String) altNames.get(1)).append(SEPARATOR);
                        hasNames = true;
                    }
                    if (hasNames) {
                        // Strip of the last separator
                        sb.setLength(sb.length() - SEPARATOR.length());
                    }
                }
                sb.append("], CN=").append(getCommonName(cert)).append('.');
            }
            throw new CertificateException(sb.toString(), e);
        }
        throw e;
    }

    private static String getSNIHostName(SSLSession session) {
        if (!(session instanceof ExtendedSSLSession)) {
            return null;
        }
        List<SNIServerName> names = ((ExtendedSSLSession)session).getRequestedServerNames();
        for (SNIServerName sni : names) {
            if (sni instanceof SNIHostName) {
                SNIHostName hostName = (SNIHostName) sni;
                return hostName.getAsciiName();
            }
        }
        return null;
    }

    private static String getCommonName(X509Certificate cert) {
        try {
            // 1. Get the X500Principal (better than getSubjectDN which is implementation dependent and deprecated)
            X500Principal principal = cert.getSubjectX500Principal();
            // 2. Parse the DN using LdapName
            LdapName ldapName = new LdapName(principal.getName());
            // 3. Iterate over the Relative Distinguished Names (RDNs) to find CN
            for (Rdn rdn : ldapName.getRdns()) {
                if (rdn.getType().equalsIgnoreCase("CN")) {
                    return rdn.getValue().toString();
                }
            }
        } catch (Exception ignore) {
            // ignore
        }
        return "null";

Review comment (suggestion):
```suggestion
        List<SNIServerName> names = ((ExtendedSSLSession) session).getRequestedServerNames();
```

For the checkstyle complaint.

Code after:
                            // Though the hostname matcher won't be looking at them, having them there can help
                            // debugging cases where hostname verification was enabled when it shouldn't be.
                            sb.append("URI");
                        } else {
                            continue;
                        }
                        sb.append(':').append((String) altNames.get(1)).append(SEPARATOR);
                        hasNames = true;
                    }
                    if (hasNames) {
                        // Strip of the last separator
                        sb.setLength(sb.length() - SEPARATOR.length());
                    }
                }
                sb.append("], CN=").append(getCommonName(cert)).append('.');
            }
            throw new CertificateException(sb.toString(), e);
        }
        throw e;
    }

    private static String getSNIHostName(SSLSession session) {
        if (!(session instanceof ExtendedSSLSession)) {
            return null;
        }
        List<SNIServerName> names = ((ExtendedSSLSession) session).getRequestedServerNames();
        for (SNIServerName sni : names) {
            if (sni instanceof SNIHostName) {
                SNIHostName hostName = (SNIHostName) sni;
                return hostName.getAsciiName();
            }
        }
        return null;
    }

    private static String getCommonName(X509Certificate cert) {
        try {
            // 1. Get the X500Principal (better than getSubjectDN which is implementation dependent and deprecated)
            X500Principal principal = cert.getSubjectX500Principal();
            // 2. Parse the DN using LdapName
            LdapName ldapName = new LdapName(principal.getName());
            // 3. Iterate over the Relative Distinguished Names (RDNs) to find CN
            for (Rdn rdn : ldapName.getRdns()) {
                if (rdn.getType().equalsIgnoreCase("CN")) {
                    return rdn.getValue().toString();
                }
            }
        } catch (Exception ignore) {
            // ignore
        }
        return "null";