Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added new set and get for ssl context #865

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/main/java/org/htmlunit/WebClientOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

import org.apache.commons.io.FileUtils;

import javax.net.ssl.SSLContext;

/**
* Represents options of a {@link WebClient}.
*
Expand All @@ -35,7 +37,7 @@
* @author Madis Pärn
* @author Ronald Brill
*/
public class WebClientOptions implements Serializable {

Check failure on line 40 in src/main/java/org/htmlunit/WebClientOptions.java

View workflow job for this annotation

GitHub Actions / PMD

[PMD] reported by reviewdog 🐶 Too many fields Raw Output: {"locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/WebClientOptions.java"},"region":{"endColumn":2,"endLine":954,"startColumn":55,"startLine":40}}}],"message":{"text":"Too many fields"},"ruleId":"TooManyFields","ruleIndex":0}

/** 1920. */
private static final int DEFAULT_SCRREN_WIDTH = 1920;
Expand Down Expand Up @@ -65,6 +67,7 @@

private boolean useInsecureSSL_; // default is secure SSL
private String sslInsecureProtocol_;
private SSLContext sslContext_;

Check failure on line 70 in src/main/java/org/htmlunit/WebClientOptions.java

View workflow job for this annotation

GitHub Actions / PMD

[PMD] reported by reviewdog 🐶 The field 'sslContext_' of serializable class 'org.htmlunit.WebClientOptions' is of non-serializable type 'javax.net.ssl.SSLContext'. Raw Output: {"locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/WebClientOptions.java"},"region":{"endColumn":35,"endLine":70,"startColumn":24,"startLine":70}}}],"message":{"text":"The field 'sslContext_' of serializable class 'org.htmlunit.WebClientOptions' is of non-serializable type 'javax.net.ssl.SSLContext'."},"ruleId":"NonSerializableClass","ruleIndex":47}

private boolean fileProtocolForXMLHttpRequestsAllowed_;

Expand Down Expand Up @@ -515,6 +518,23 @@
return sslInsecureProtocol_;
}

/**
* Sets the SSL Context, used only when {@link #setUseInsecureSSL(boolean)} is set to {@code true}.
* @param sslContext the SSL Context for insecure SSL connections,
* {@code null} to use for default value
*/
public void setSSLContext(final SSLContext sslContext) {
sslContext_ = sslContext;
}

/**
* Gets the SSL Context, to be used only when {@link #setUseInsecureSSL(boolean)} is set to {@code true}.
* @return the SSL Context for insecure SSL connections
*/
public SSLContext getSSLContext() {
return sslContext_;
}

/**
* Sets the SSL server certificate trust store. All server certificates will be validated against
* this trust store.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,18 @@ public static SSLConnectionSocketFactory buildSSLSocketFactory(final WebClientOp
sslClientProtocols, sslClientCipherSuites);
}

// we need insecure SSL + SOCKS awareness
String protocol = options.getSSLInsecureProtocol();
if (protocol == null) {
protocol = "SSL";
SSLContext sslContext = options.getSSLContext();
if (sslContext == null) {
// we need insecure SSL + SOCKS awareness
String protocol = options.getSSLInsecureProtocol();
if (protocol == null) {
protocol = "SSL";
}

sslContext = SSLContext.getInstance(protocol);
sslContext.init(getKeyManagers(options),
new X509ExtendedTrustManager[]{new InsecureTrustManager()}, null);
}
final SSLContext sslContext = SSLContext.getInstance(protocol);
sslContext.init(getKeyManagers(options), new X509ExtendedTrustManager[] {new InsecureTrustManager()}, null);

return new HtmlUnitSSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE,
useInsecureSSL, sslClientProtocols, sslClientCipherSuites);
Expand Down