Example usage for org.apache.http.conn.ssl SSLSocketFactory SSLSocketFactory

List of usage examples for org.apache.http.conn.ssl SSLSocketFactory SSLSocketFactory

Introduction

In this page you can find the example usage for org.apache.http.conn.ssl SSLSocketFactory SSLSocketFactory.

Prototype

public SSLSocketFactory(final javax.net.ssl.SSLSocketFactory socketfactory, final String[] supportedProtocols,
        final String[] supportedCipherSuites, final X509HostnameVerifier hostnameVerifier) 

Source Link

Usage

From source file:com.vmware.bdd.plugin.ironfan.impl.RolePackageMapping.java

@SuppressWarnings("deprecation")
private String readDistroManifest() throws Exception {
    File manifestFile = new File(DISTRO_MANIFEST_FILE_PATH);
    if (manifestFile.exists()) {
        // The manifest file is on the local server.
        // No need to reload the file if it's not modified.
        if (lastModified != manifestFile.lastModified()) {
            lastModified = manifestFile.lastModified();
            logger.info("last modified date of manifest file changed. Reloading manifest.");
        } else {//from   w  ww.  j a v a2s.c  o m
            return null;
        }
    }

    BufferedReader in = null;
    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {
        SSLContext sslContext = SSLContexts.custom().useTLS().build();

        sslContext.init(null, new TrustManager[] { new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] x509Certificates, String s)
                    throws CertificateException {
                return;
            }

            @Override
            public void checkServerTrusted(X509Certificate[] x509Certificates, String s)
                    throws CertificateException {
                return;
            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        } }, null);

        TlsClientConfiguration tlsConfiguration = new TlsClientConfiguration();
        SSLSocketFactory socketFactory = new SSLSocketFactory(sslContext, tlsConfiguration.getSslProtocols(),
                tlsConfiguration.getCipherSuites(), SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        Scheme sch = new Scheme("https", 443, socketFactory);
        httpclient.getConnectionManager().getSchemeRegistry().register(sch);
        HttpGet httpget = new HttpGet(new URI(distrosManifestUrl));
        if (eTag != null) {
            httpget.addHeader("If-None-Match", eTag);
        }

        logger.info("executing request: " + httpget.getRequestLine());
        HttpResponse response = httpclient.execute(httpget);

        if (!manifestFile.exists()) {
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
                return null;
            } else {
                logger.debug("ETag of manifest file changed. Reloading manifest.");
                eTag = response.getFirstHeader("ETag").getValue();
                ;
            }
        }
        HttpEntity entity = response.getEntity();

        in = new BufferedReader(new InputStreamReader(entity.getContent()));

        StringBuffer sb = new StringBuffer();
        String line;
        while ((line = in.readLine()) != null) {
            sb.append(line);
        }
        EntityUtils.consume(entity);
        return sb.toString();
    } finally {
        httpclient.getConnectionManager().shutdown();
        if (in != null) {
            in.close();
        }
    }
}