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

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

Introduction

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

Prototype

X509HostnameVerifier ALLOW_ALL_HOSTNAME_VERIFIER

To view the source code for org.apache.http.conn.ssl SSLSocketFactory ALLOW_ALL_HOSTNAME_VERIFIER.

Click Source Link

Usage

From source file:info.semanticsoftware.semassist.android.intents.ServiceIntent.java

public String execute() {
    Log.d(Constants.TAG, "factory execute for " + pipelineName + " on server " + candidServerURL + " params "
            + RTParams + " input " + inputString);
    if (candidServerURL.indexOf("https") < 0) {
        Log.d(Constants.TAG, "non secure post to " + candidServerURL);
        RequestRepresentation request = new RequestRepresentation(SemAssistApp.getInstance(), pipelineName,
                RTParams, inputString);/*from w  ww.  j  a va2  s .c o  m*/
        Representation representation = new StringRepresentation(request.getXML(), MediaType.APPLICATION_XML);
        Representation response = new ClientResource(candidServerURL).post(representation);
        String responseString = "";
        try {
            StringWriter writer = new StringWriter();
            response.write(writer);
            responseString = writer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d(Constants.TAG, "$$$ " + responseString);
        return responseString;
    } else {
        try {
            HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
            DefaultHttpClient client = new DefaultHttpClient();

            SchemeRegistry registry = new SchemeRegistry();
            final KeyStore ks = KeyStore.getInstance("BKS");
            // NOTE: the keystore must have been generated with BKS 146 and not later
            final InputStream in = SemAssistApp.getInstance().getContext().getResources()
                    .openRawResource(R.raw.clientkeystorenew);
            try {
                ks.load(in, SemAssistApp.getInstance().getContext().getString(R.string.keystorePassword)
                        .toCharArray());
            } finally {
                in.close();
            }

            SSLSocketFactory socketFactory = new CustomSSLSocketFactory(ks);
            socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
            registry.register(new Scheme("https", socketFactory, 443));
            SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
            DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());

            // Set verifier
            HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
            RequestRepresentation request = new RequestRepresentation(SemAssistApp.getInstance(), pipelineName,
                    RTParams, inputString);
            Representation representation = new StringRepresentation(request.getXML(),
                    MediaType.APPLICATION_XML);

            HttpPost post = new HttpPost(candidServerURL);
            post.setEntity(new StringEntity(representation.getText()));

            HttpResponse response = httpClient.execute(post);
            HttpEntity entity = response.getEntity();
            InputStream inputstream = entity.getContent();
            InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
            BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

            String string = null;
            String responseString = "";
            while ((string = bufferedreader.readLine()) != null) {
                responseString += string;
            }
            return responseString;
        } catch (Exception e) {
            e.printStackTrace();
        }
    } //else
    return null;
}

From source file:org.jclouds.http.apachehc.config.ApacheHCHttpCommandExecutorServiceModule.java

@Singleton
@Provides/*ww  w.  ja  va 2  s.c om*/
final X509HostnameVerifier newHostnameVerifier(HttpUtils utils) {
    return utils.relaxHostname() ? SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER
            : SSLSocketFactory.STRICT_HOSTNAME_VERIFIER;
}

From source file:com.vmware.vchs.publicapi.samples.HttpUtils.java

/**
 * This method returns an HttpClient instance wrapped to trust all HTTPS certificates.
 * //from   w w  w .j  a  v  a 2s.c o  m
 * @return HttpClient a new instance of HttpClient
 */
static HttpClient createHttpClient() {
    HttpClient base = new DefaultHttpClient();

    try {
        SSLContext ctx = SSLContext.getInstance("TLS");

        // WARNING: This creates a TrustManager that trusts all certificates and should not be used in production code.
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }

            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }
        } };

        ctx.init(null, trustAllCerts, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = base.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 443, ssf));

        return new DefaultHttpClient(ccm, base.getParams());
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:com.blackboard.LearnServer.java

private AbstractHttpClient getTrustAllSSLHttpClient() {
    try {/*  w  ww. j ava 2 s.c om*/
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new TrustAllSSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        System.out.println("WARNING: Could not create Trust All SSL client, using default" + e.getMessage());
        return new DefaultHttpClient();
    }
}

From source file:nl.esciencecenter.octopus.webservice.JobLauncherService.java

/**
 * Enable insecure SSL in http client like self signed certificates.
 *
 * @param httpClient/*  w w w .j a  v a2 s. c o m*/
 * @throws NoSuchAlgorithmException
 * @throws KeyManagementException
 * @throws KeyStoreException
 * @throws UnrecoverableKeyException
 */
public void useInsecureSSL(HttpClient httpClient)
        throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
    SSLSocketFactory socketFactory;
    socketFactory = new SSLSocketFactory(new TrustStrategy() {

        public boolean isTrusted(final X509Certificate[] chain, String authType) throws CertificateException {
            // Oh, I am easy...
            return true;
        }

    }, org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

    SchemeRegistry registry = httpClient.getConnectionManager().getSchemeRegistry();
    registry.register(new Scheme("https", 443, socketFactory));
}

From source file:com.ntsync.android.sync.client.MyHttpClient.java

private SocketFactory getSSLSocketFactory() {
    InputStream in = null;//  w  ww.  j  av a 2  s  .co  m
    SocketFactory socketFack = null;
    try {
        KeyStore trusted = KeyStore.getInstance("BKS");
        in = context.getResources().openRawResource(R.raw.mykeystore);
        trusted.load(in, "pwd23key".toCharArray());
        SSLSocketFactory sslSocketFack = new SSLSocketFactory(trusted);
        socketFack = sslSocketFack;
        if (Constants.USE_RELEASE_CONFIG) {
            sslSocketFack.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
        } else {
            Log.w(TAG, "Disable SSL Hostname verification");
            sslSocketFack.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        }
    } catch (GeneralSecurityException e) {
        Log.e(TAG, "Loading truststore failed.", e);
    } catch (IOException e) {
        Log.e(TAG, "Loading truststore failed.", e);
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            Log.e(TAG, "closing filescocket failed.", e);
        }
    }
    if (socketFack == null) {
        Log.w(TAG, "Fallback to custom ssl socket factory.");
        socketFack = new MySSLSocketFactory();
    }
    return socketFack;
}

From source file:org.openiot.gsn.http.rest.RestRemoteWrapper.java

public boolean initialize() {
    try {//from   w w w .  j av  a  2s  . c  o  m
        initParams = new RemoteWrapperParamParser(getActiveAddressBean(), false);
        httpclient = new DefaultHttpClient(getHttpClientParams(initParams.getTimeout()));
        // Init the http client
        if (initParams.isSSLRequired()) {
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            trustStore.load(new FileInputStream(new File("conf/servertestkeystore")),
                    Main.getContainerConfig().getSSLKeyStorePassword().toCharArray());
            SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
            socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            int sslPort = Main.getContainerConfig().getSSLPort() > 0 ? Main.getContainerConfig().getSSLPort()
                    : ContainerConfig.DEFAULT_SSL_PORT;
            Scheme sch = new Scheme("https", socketFactory, sslPort);
            httpclient.getConnectionManager().getSchemeRegistry().register(sch);
        }
        Scheme plainsch = new Scheme("http", PlainSocketFactory.getSocketFactory(),
                Main.getContainerConfig().getContainerPort());
        httpclient.getConnectionManager().getSchemeRegistry().register(plainsch);
        //
        lastReceivedTimestamp = initParams.getStartTime();
        structure = connectToRemote();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        return false;
    }
    return true;
}

From source file:org.opcfoundation.ua.application.Server.java

public static Server createServerApplication() {
    Application application = new Application();
    Server server = new Server(application);
    application.getOpctcpSettings().setCertificateValidator(CertificateValidator.ALLOW_ALL);
    application.getHttpsSettings().setCertificateValidator(CertificateValidator.ALLOW_ALL);
    application.getHttpsSettings().setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    return server;
}

From source file:org.ebayopensource.fidouafclient.curl.Curl.java

private static HttpClient createHttpsClient() {
    HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
    SchemeRegistry registry = new SchemeRegistry();
    SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
    socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
    registry.register(new Scheme("https", socketFactory, 443));
    HttpClient client = new DefaultHttpClient();
    SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
    DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());
    return httpClient;
}