com.gargoylesoftware.htmlunit.WebClientOptions Java Examples

The following examples show how to use com.gargoylesoftware.htmlunit.WebClientOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HtmlUnitSSLConnectionSocketFactory.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Factory method that builds a new SSLConnectionSocketFactory.
 * @param options the current WebClientOptions
 * @return the SSLConnectionSocketFactory
 */
public static SSLConnectionSocketFactory buildSSLSocketFactory(final WebClientOptions options) {
    try {
        final String[] sslClientProtocols = options.getSSLClientProtocols();
        final String[] sslClientCipherSuites = options.getSSLClientCipherSuites();

        final boolean useInsecureSSL = options.isUseInsecureSSL();

        if (!useInsecureSSL) {
            final KeyStore keyStore = options.getSSLClientCertificateStore();
            final KeyStore trustStore = options.getSSLTrustStore();

            return new HtmlUnitSSLConnectionSocketFactory(keyStore,
                    keyStore == null ? null : options.getSSLClientCertificatePassword(),
                    trustStore, useInsecureSSL,
                    sslClientProtocols, sslClientCipherSuites);
        }

        // we need insecure SSL + SOCKS awareness
        String protocol = options.getSSLInsecureProtocol();
        if (protocol == null) {
            protocol = "SSL";
        }
        final SSLContext sslContext = SSLContext.getInstance(protocol);
        sslContext.init(getKeyManagers(options), new X509ExtendedTrustManager[] {new InsecureTrustManager()}, null);

        return new HtmlUnitSSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE,
                                        useInsecureSSL, sslClientProtocols, sslClientCipherSuites);
    }
    catch (final GeneralSecurityException e) {
        throw new RuntimeException(e);
    }
}
 
Example #2
Source File: HtmlUnitSSLConnectionSocketFactory.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private static KeyManager[] getKeyManagers(final WebClientOptions options) {
    if (options.getSSLClientCertificateStore() == null) {
        return null;
    }
    try {
        final KeyStore keyStore = options.getSSLClientCertificateStore();
        final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(
                KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore, options.getSSLClientCertificatePassword());
        return keyManagerFactory.getKeyManagers();
    }
    catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #3
Source File: HtmlUnitSSLConnectionSocketFactory.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Factory method that builds a new SSLConnectionSocketFactory.
 * @param options the current WebClientOptions
 * @return the SSLConnectionSocketFactory
 */
public static SSLConnectionSocketFactory buildSSLSocketFactory(final WebClientOptions options) {
    try {
        final String[] sslClientProtocols = options.getSSLClientProtocols();
        final String[] sslClientCipherSuites = options.getSSLClientCipherSuites();

        final boolean useInsecureSSL = options.isUseInsecureSSL();

        if (!useInsecureSSL) {
            final KeyStore keyStore = options.getSSLClientCertificateStore();
            final KeyStore trustStore = options.getSSLTrustStore();

            return new HtmlUnitSSLConnectionSocketFactory(keyStore,
                    keyStore == null ? null : options.getSSLClientCertificatePassword(),
                    trustStore, useInsecureSSL,
                    sslClientProtocols, sslClientCipherSuites);
        }

        // we need insecure SSL + SOCKS awareness
        String protocol = options.getSSLInsecureProtocol();
        if (protocol == null) {
            protocol = "SSL";
        }
        final SSLContext sslContext = SSLContext.getInstance(protocol);
        sslContext.init(getKeyManagers(options), new TrustManager[]{new InsecureTrustManager2()}, null);

        final SSLConnectionSocketFactory factory = new HtmlUnitSSLConnectionSocketFactory(sslContext,
            NoopHostnameVerifier.INSTANCE,
            useInsecureSSL, sslClientProtocols, sslClientCipherSuites);
        return factory;
    }
    catch (final GeneralSecurityException e) {
        throw new RuntimeException(e);
    }
}
 
Example #4
Source File: HtmlUnitSSLConnectionSocketFactory.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private static KeyManager[] getKeyManagers(final WebClientOptions options) {
    if (options.getSSLClientCertificateStore() == null) {
        return null;
    }
    try {
        final KeyStore keyStore = options.getSSLClientCertificateStore();
        final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(
                KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore, options.getSSLClientCertificatePassword());
        return keyManagerFactory.getKeyManagers();
    }
    catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #5
Source File: HtmlUnitFetcher.java    From sparkler with Apache License 2.0 5 votes vote down vote up
@Override
public void init(JobContext context, String pluginId) throws SparklerException {
    super.init(context, pluginId);
    //TODO: get timeouts from configurations
    driver = new WebClient(BrowserVersion.BEST_SUPPORTED);
    driver.setJavaScriptTimeout(DEFAULT_JS_TIMEOUT);
    WebClientOptions options = driver.getOptions();
    options.setCssEnabled(false);
    options.setAppletEnabled(false);
    options.setDownloadImages(false);
    options.setJavaScriptEnabled(true);
    options.setTimeout(DEFAULT_TIMEOUT);
    options.setUseInsecureSSL(true);
    options.setPopupBlockerEnabled(true);
    options.setDoNotTrackEnabled(true);
    options.setGeolocationEnabled(false);
    options.setHistorySizeLimit(2);
    options.setPrintContentOnFailingStatusCode(false);
    options.setThrowExceptionOnScriptError(false);
    options.setThrowExceptionOnFailingStatusCode(false);
    if (this.httpHeaders != null && !this.httpHeaders.isEmpty()) {
        LOG.info("Found {} headers", this.httpHeaders.size());
        this.httpHeaders.forEach((name, val) -> driver.addRequestHeader(name, val));
    } else {
        LOG.info("No user headers found");
    }
}