private static URLConnection getURLConnection()

in src/main/java/org/apache/maven/report/projectinfo/ProjectInfoReportUtils.java [269:355]


    private static URLConnection getURLConnection(URL url, MavenProject project, Settings settings) throws IOException {
        URLConnection conn = url.openConnection();
        conn.setConnectTimeout(TIMEOUT);
        conn.setReadTimeout(TIMEOUT);

        // conn authorization
        // @formatter:off
        if (settings.getServers() != null
                && !settings.getServers().isEmpty()
                && project != null
                && project.getDistributionManagement() != null
                && (project.getDistributionManagement().getRepository() != null
                        || project.getDistributionManagement().getSnapshotRepository() != null)
                && (StringUtils.isNotEmpty(project.getDistributionManagement()
                                .getRepository()
                                .getUrl())
                        || StringUtils.isNotEmpty(project.getDistributionManagement()
                                .getSnapshotRepository()
                                .getUrl())))
        // @formatter:on
        {
            Server server = null;
            if (url.toString()
                    .contains(
                            project.getDistributionManagement().getRepository().getUrl())) {
                server = settings.getServer(
                        project.getDistributionManagement().getRepository().getId());
            }
            if (server == null
                    && url.toString()
                            .contains(project.getDistributionManagement()
                                    .getSnapshotRepository()
                                    .getUrl())) {
                server = settings.getServer(project.getDistributionManagement()
                        .getSnapshotRepository()
                        .getId());
            }

            if (server != null
                    && StringUtils.isNotEmpty(server.getUsername())
                    && StringUtils.isNotEmpty(server.getPassword())) {
                String up =
                        server.getUsername().trim() + ":" + server.getPassword().trim();
                String upEncoded = new String(Base64.encodeBase64Chunked(up.getBytes())).trim();

                conn.setRequestProperty("Authorization", "Basic " + upEncoded);
            }
        }

        if (conn instanceof HttpsURLConnection) {
            HostnameVerifier hostnameverifier = new HostnameVerifier() {
                /** {@inheritDoc} */
                public boolean verify(String urlHostName, SSLSession session) {
                    return true;
                }
            };
            ((HttpsURLConnection) conn).setHostnameVerifier(hostnameverifier);

            TrustManager[] trustAllCerts = new TrustManager[] {
                new X509TrustManager() {
                    /** {@inheritDoc} */
                    public void checkClientTrusted(final X509Certificate[] chain, final String authType) {}

                    /** {@inheritDoc} */
                    public void checkServerTrusted(final X509Certificate[] chain, final String authType) {}

                    /** {@inheritDoc} */
                    public X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }
                }
            };

            try {
                SSLContext sslContext = SSLContext.getInstance("SSL");
                sslContext.init(null, trustAllCerts, new SecureRandom());

                SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

                ((HttpsURLConnection) conn).setSSLSocketFactory(sslSocketFactory);
            } catch (NoSuchAlgorithmException | KeyManagementException e1) {
                // ignore
            }
        }

        return conn;
    }