Example usage for org.apache.hadoop.security.ssl SSLFactory init

List of usage examples for org.apache.hadoop.security.ssl SSLFactory init

Introduction

In this page you can find the example usage for org.apache.hadoop.security.ssl SSLFactory init.

Prototype

public void init() throws GeneralSecurityException, IOException 

Source Link

Document

Initializes the factory.

Usage

From source file:org.apache.atlas.security.SecureClientUtils.java

License:Apache License

private static ConnectionConfigurator newSslConnConfigurator(final int timeout, Configuration conf)
        throws IOException, GeneralSecurityException {
    final SSLFactory factory;
    final SSLSocketFactory sf;
    final HostnameVerifier hv;

    factory = new SSLFactory(SSLFactory.Mode.CLIENT, conf);
    factory.init();
    sf = factory.createSSLSocketFactory();
    hv = factory.getHostnameVerifier();/* w  w w.  j  a va  2  s. c o m*/

    return new ConnectionConfigurator() {
        @Override
        public HttpURLConnection configure(HttpURLConnection conn) throws IOException {
            if (conn instanceof HttpsURLConnection) {
                HttpsURLConnection c = (HttpsURLConnection) conn;
                c.setSSLSocketFactory(sf);
                c.setHostnameVerifier(hv);
            }
            setTimeouts(conn, timeout);
            return conn;
        }
    };
}

From source file:org.apache.slider.core.registry.retrieve.AMWebClient.java

License:Apache License

private static URLConnectionClientHandler getUrlConnectionClientHandler() {
    return new URLConnectionClientHandler(new HttpURLConnectionFactory() {
        @Override// w w  w .  ja  v  a2s  . c  o  m
        public HttpURLConnection getHttpURLConnection(URL url) throws IOException {
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            if (connection.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) {
                // is a redirect - are we changing schemes?
                String redirectLocation = connection.getHeaderField(HttpHeaders.LOCATION);
                String originalScheme = url.getProtocol();
                String redirectScheme = URI.create(redirectLocation).getScheme();
                if (!originalScheme.equals(redirectScheme)) {
                    // need to fake it out by doing redirect ourselves
                    log.info("Protocol change during redirect. Redirecting {} to URL {}", url,
                            redirectLocation);
                    URL redirectURL = new URL(redirectLocation);
                    connection = (HttpURLConnection) redirectURL.openConnection();
                }
            }
            if (connection instanceof HttpsURLConnection) {
                log.debug("Attempting to configure HTTPS connection using client " + "configuration");
                final SSLFactory factory;
                final SSLSocketFactory sf;
                final HostnameVerifier hv;

                try {
                    HttpsURLConnection c = (HttpsURLConnection) connection;
                    factory = new SSLFactory(SSLFactory.Mode.CLIENT, new Configuration());
                    factory.init();
                    sf = factory.createSSLSocketFactory();
                    hv = factory.getHostnameVerifier();
                    c.setSSLSocketFactory(sf);
                    c.setHostnameVerifier(hv);
                } catch (Exception e) {
                    log.info("Unable to configure HTTPS connection from "
                            + "configuration.  Using JDK properties.");
                }

            }
            return connection;
        }
    });
}

From source file:org.apache.slider.core.registry.retrieve.RegistryRetriever.java

License:Apache License

private static URLConnectionClientHandler getUrlConnectionClientHandler() {
    return new URLConnectionClientHandler(new HttpURLConnectionFactory() {
        @Override/* w w w.jav a2  s  .c  o  m*/
        public HttpURLConnection getHttpURLConnection(URL url) throws IOException {
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            if (connection.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) {
                // is a redirect - are we changing schemes?
                String redirectLocation = connection.getHeaderField(HttpHeaders.LOCATION);
                String originalScheme = url.getProtocol();
                String redirectScheme = URI.create(redirectLocation).getScheme();
                if (!originalScheme.equals(redirectScheme)) {
                    // need to fake it out by doing redirect ourselves
                    log.info("Protocol change during redirect. Redirecting {} to URL {}", url,
                            redirectLocation);
                    URL redirectURL = new URL(redirectLocation);
                    connection = (HttpURLConnection) redirectURL.openConnection();
                }
            }
            if (connection instanceof HttpsURLConnection) {
                log.debug("Attempting to configure HTTPS connection using client " + "configuration");
                final SSLFactory factory;
                final SSLSocketFactory sf;
                final HostnameVerifier hv;

                try {
                    HttpsURLConnection c = (HttpsURLConnection) connection;
                    factory = new SSLFactory(SSLFactory.Mode.CLIENT, new Configuration());
                    factory.init();
                    sf = factory.createSSLSocketFactory();
                    hv = factory.getHostnameVerifier();
                    c.setSSLSocketFactory(sf);
                    c.setHostnameVerifier(hv);
                } catch (Exception e) {
                    log.info("Unable to configure HTTPS connection from "
                            + "configuration.  Leveraging JDK properties.");
                }

            }
            return connection;
        }
    });
}

From source file:org.apache.slider.core.restclient.SliderURLConnectionFactory.java

License:Apache License

/**
 * Create a new ConnectionConfigurator for SSL connections
 *//*  ww w  .  j a v  a  2s  .  c om*/
private static ConnectionConfigurator newSslConnConfigurator(final int timeout, Configuration conf)
        throws IOException, GeneralSecurityException {
    final SSLFactory factory;
    final SSLSocketFactory sf;
    final HostnameVerifier hv;

    factory = new SSLFactory(SSLFactory.Mode.CLIENT, conf);
    factory.init();
    sf = factory.createSSLSocketFactory();
    hv = factory.getHostnameVerifier();

    return new ConnectionConfigurator() {
        @Override
        public HttpURLConnection configure(HttpURLConnection conn) throws IOException {
            if (conn instanceof HttpsURLConnection) {
                HttpsURLConnection c = (HttpsURLConnection) conn;
                c.setSSLSocketFactory(sf);
                c.setHostnameVerifier(hv);
            }
            SliderURLConnectionFactory.setupConnection(conn, timeout);
            return conn;
        }
    };
}

From source file:org.apache.tez.dag.api.client.TimelineReaderFactory.java

License:Apache License

private static ConnectionConfigurator getNewSSLConnectionConf(final Configuration conf, final int connTimeout)
        throws IOException {
    final SSLFactory sslFactory;
    final SSLSocketFactory sslSocketFactory;
    final HostnameVerifier hostnameVerifier;

    sslFactory = new SSLFactory(CLIENT, conf);
    try {/* w  w  w  .j a v  a 2  s . c  o m*/
        sslFactory.init();
        sslSocketFactory = sslFactory.createSSLSocketFactory();
    } catch (GeneralSecurityException e) {
        sslFactory.destroy();
        throw new IOException("Failed to initialize ssl factory");
    }
    hostnameVerifier = sslFactory.getHostnameVerifier();

    return new ConnectionConfigurator() {
        @Override
        public HttpURLConnection configure(HttpURLConnection httpURLConnection) throws IOException {
            if (!(httpURLConnection instanceof HttpsURLConnection)) {
                throw new IOException("Expected https connection");
            }
            HttpsURLConnection httpsURLConnection = (HttpsURLConnection) httpURLConnection;
            httpsURLConnection.setSSLSocketFactory(sslSocketFactory);
            httpsURLConnection.setHostnameVerifier(hostnameVerifier);
            setTimeouts(httpsURLConnection, connTimeout);

            return httpsURLConnection;
        }
    };
}