Skip to content

Commit

Permalink
added new set and get for ssl context
Browse files Browse the repository at this point in the history
  • Loading branch information
alexvrv committed Sep 3, 2024
1 parent 7114b9a commit 704fba6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
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 Down Expand Up @@ -65,6 +67,7 @@ public class WebClientOptions implements Serializable {

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 @@ public String getSSLInsecureProtocol() {
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,17 @@ 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);

Check failure on line 112 in src/main/java/org/htmlunit/httpclient/HtmlUnitSSLConnectionSocketFactory.java

View workflow job for this annotation

GitHub Actions / CheckStyle

[checkstyle] reported by reviewdog 🐶 Line is longer than 120 characters (found 123). Raw Output: /home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/httpclient/HtmlUnitSSLConnectionSocketFactory.java:112:0: error: Line is longer than 120 characters (found 123). (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)
}
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

0 comments on commit 704fba6

Please sign in to comment.