Example usage for org.apache.http.impl.auth NegotiateSchemeFactory NegotiateSchemeFactory

List of usage examples for org.apache.http.impl.auth NegotiateSchemeFactory NegotiateSchemeFactory

Introduction

In this page you can find the example usage for org.apache.http.impl.auth NegotiateSchemeFactory NegotiateSchemeFactory.

Prototype

public NegotiateSchemeFactory() 

Source Link

Usage

From source file:com.servoy.extensions.plugins.http.HttpClient.java

public HttpClient(IClientPluginAccess plugin) {
    client = new DefaultHttpClient();
    client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, Boolean.TRUE);
    client.getAuthSchemes().register(AuthPolicy.NTLM, new NTLMSchemeFactory());
    client.getAuthSchemes().register(AuthPolicy.SPNEGO, new NegotiateSchemeFactory());
    this.plugin = plugin;

    try {// w ww  .  j a va  2s  .c  o m
        final AllowedCertTrustStrategy allowedCertTrustStrategy = new AllowedCertTrustStrategy();
        SSLSocketFactory sf = new SSLSocketFactory(allowedCertTrustStrategy) {
            @Override
            public Socket connectSocket(Socket socket, InetSocketAddress remoteAddress,
                    InetSocketAddress localAddress, HttpParams params)
                    throws IOException, UnknownHostException, ConnectTimeoutException {
                if (socket instanceof SSLSocket) {
                    try {
                        Method s = socket.getClass().getMethod("setHost", String.class);
                        s.invoke(socket, remoteAddress.getHostName());
                    } catch (NoSuchMethodException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (InvocationTargetException ex) {
                    } catch (IllegalArgumentException ex) {
                    } catch (SecurityException ex) {
                    }
                }
                try {
                    return super.connectSocket(socket, remoteAddress, localAddress, params);
                } catch (SSLPeerUnverifiedException ex) {
                    X509Certificate[] lastCertificates = allowedCertTrustStrategy.getAndClearLastCertificates();
                    if (lastCertificates != null) {
                        // allow for next time
                        if (HttpClient.this.plugin.getApplicationType() == IClientPluginAccess.CLIENT
                                || HttpClient.this.plugin.getApplicationType() == IClientPluginAccess.RUNTIME) {
                            // show dialog
                            CertificateDialog dialog = new CertificateDialog(
                                    ((ISmartRuntimeWindow) HttpClient.this.plugin.getCurrentRuntimeWindow())
                                            .getWindow(),
                                    remoteAddress, lastCertificates);
                            if (dialog.shouldAccept()) {
                                allowedCertTrustStrategy.add(lastCertificates);
                                // try it again now with the new chain.
                                return super.connectSocket(socket, remoteAddress, localAddress, params);
                            }
                        } else {
                            Debug.error("Couldn't connect to " + remoteAddress
                                    + ", please make sure that the ssl certificates of that site are added to the java keystore."
                                    + "Download the keystore in the browser and update the java cacerts file in jre/lib/security: "
                                    + "keytool -import -file downloaded.crt -keystore cacerts");
                        }
                    }
                    throw ex;
                } finally {
                    // always just clear the last request.
                    allowedCertTrustStrategy.getAndClearLastCertificates();
                }

            }
        };
        Scheme https = new Scheme("https", 443, sf); //$NON-NLS-1$
        client.getConnectionManager().getSchemeRegistry().register(https);
    } catch (Exception e) {
        Debug.error("Can't register a https scheme", e); //$NON-NLS-1$
    }
}

From source file:org.apache.http.impl.client.AbstractStatisticsGatheringHttpClient.java

protected AuthSchemeRegistry createAuthSchemeRegistry() {
    AuthSchemeRegistry registry = new AuthSchemeRegistry();
    registry.register(AuthPolicy.BASIC, new BasicSchemeFactory());
    registry.register(AuthPolicy.DIGEST, new DigestSchemeFactory());
    registry.register(AuthPolicy.NTLM, new NTLMSchemeFactory());
    registry.register(AuthPolicy.SPNEGO, new NegotiateSchemeFactory());
    return registry;
}