Skip to content

Commit 85edd5d

Browse files
committed
reduce jetty dependencies
1 parent 3e51063 commit 85edd5d

7 files changed

Lines changed: 51 additions & 62 deletions

src/test/java/org/htmlunit/HttpWebConnectionInsecureSSLTest.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,10 @@
1414
*/
1515
package org.htmlunit;
1616

17-
import static org.eclipse.jetty.http.HttpVersion.HTTP_1_1;
18-
1917
import java.net.URL;
2018

2119
import javax.net.ssl.SSLHandshakeException;
2220

23-
import org.eclipse.jetty.server.SslConnectionFactory;
24-
import org.eclipse.jetty.util.ssl.SslContextFactory.Server;
2521
import org.htmlunit.util.WebConnectionWrapper;
2622
import org.junit.jupiter.api.Assertions;
2723
import org.junit.jupiter.api.Test;
@@ -70,14 +66,7 @@ public void insecureSSL_withWrapper() throws Exception {
7066
}
7167

7268
@Override
73-
public SslConnectionFactory getSslConnectionFactory() {
74-
final URL url = HttpWebConnectionInsecureSSLWithClientCertificateTest.class
75-
.getClassLoader().getResource("insecureSSL.pfx");
76-
77-
final Server contextFactory = new Server();
78-
contextFactory.setKeyStorePath(url.toExternalForm());
79-
contextFactory.setKeyStorePassword("nopassword");
80-
contextFactory.setEndpointIdentificationAlgorithm(null);
81-
return new SslConnectionFactory(contextFactory, HTTP_1_1.toString());
69+
public SSLVariant getSSLVariant() {
70+
return SSLVariant.INSECURE;
8271
}
8372
}

src/test/java/org/htmlunit/HttpWebConnectionInsecureSSLWithClientCertificateTest.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,12 @@
1414
*/
1515
package org.htmlunit;
1616

17-
import static org.eclipse.jetty.http.HttpVersion.HTTP_1_1;
18-
1917
import java.io.ByteArrayInputStream;
2018
import java.io.InputStream;
2119
import java.net.URL;
2220

2321
import javax.net.ssl.SSLHandshakeException;
2422

25-
import org.eclipse.jetty.server.SslConnectionFactory;
26-
import org.eclipse.jetty.util.ssl.SslContextFactory.Server;
2723
import org.junit.jupiter.api.Assertions;
2824
import org.junit.jupiter.api.Test;
2925

@@ -99,14 +95,7 @@ public void insecureSSL_clientCertificatesInputStream() throws Exception {
9995
}
10096

10197
@Override
102-
public SslConnectionFactory getSslConnectionFactory() {
103-
final URL url = HttpWebConnectionInsecureSSLWithClientCertificateTest.class
104-
.getClassLoader().getResource("insecureSSL.pfx");
105-
106-
final Server contextFactory = new Server();
107-
contextFactory.setKeyStorePath(url.toExternalForm());
108-
contextFactory.setKeyStorePassword("nopassword");
109-
contextFactory.setEndpointIdentificationAlgorithm(null);
110-
return new SslConnectionFactory(contextFactory, HTTP_1_1.toString());
98+
public SSLVariant getSSLVariant() {
99+
return SSLVariant.INSECURE;
111100
}
112101
}

src/test/java/org/htmlunit/HttpWebConnectionTruststoreTest.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,10 @@
1414
*/
1515
package org.htmlunit;
1616

17-
import static org.eclipse.jetty.http.HttpVersion.HTTP_1_1;
18-
1917
import java.net.URL;
2018

2119
import javax.net.ssl.SSLHandshakeException;
2220

23-
import org.eclipse.jetty.server.SslConnectionFactory;
24-
import org.eclipse.jetty.util.ssl.SslContextFactory.Server;
2521
import org.junit.jupiter.api.Assertions;
2622
import org.junit.jupiter.api.Test;
2723

@@ -60,15 +56,7 @@ public void selfSignedCertNotInTruststore() throws Exception {
6056
}
6157

6258
@Override
63-
public SslConnectionFactory getSslConnectionFactory() {
64-
final URL url = HttpWebConnectionInsecureSSLWithClientCertificateTest.class
65-
.getClassLoader().getResource("self-signed-cert.keystore");
66-
67-
final Server contextFactory = new Server();
68-
contextFactory.setKeyStoreType("jks");
69-
contextFactory.setKeyStorePath(url.toExternalForm());
70-
contextFactory.setKeyStorePassword("nopassword");
71-
contextFactory.setEndpointIdentificationAlgorithm(null);
72-
return new SslConnectionFactory(contextFactory, HTTP_1_1.toString());
59+
public SSLVariant getSSLVariant() {
60+
return SSLVariant.SELF_SIGNED;
7361
}
7462
}

src/test/java/org/htmlunit/SocksProxyTest.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,12 @@
1414
*/
1515
package org.htmlunit;
1616

17-
import static org.eclipse.jetty.http.HttpVersion.HTTP_1_1;
18-
1917
import java.io.IOException;
2018
import java.net.MalformedURLException;
2119
import java.net.Socket;
2220
import java.net.SocketException;
2321
import java.net.URL;
2422

25-
import org.eclipse.jetty.server.SslConnectionFactory;
26-
import org.eclipse.jetty.util.ssl.SslContextFactory.Server;
2723
import org.htmlunit.html.HtmlPage;
2824
import org.junit.jupiter.api.Assertions;
2925
import org.junit.jupiter.api.Assumptions;
@@ -115,14 +111,7 @@ private WebClient getWebClientWithSocksProxy() {
115111
}
116112

117113
@Override
118-
public SslConnectionFactory getSslConnectionFactory() {
119-
final URL url = HttpWebConnectionInsecureSSLWithClientCertificateTest.class
120-
.getClassLoader().getResource("insecureSSL.pfx");
121-
122-
final Server contextFactory = new Server();
123-
contextFactory.setKeyStorePath(url.toExternalForm());
124-
contextFactory.setKeyStorePassword("nopassword");
125-
contextFactory.setEndpointIdentificationAlgorithm(null);
126-
return new SslConnectionFactory(contextFactory, HTTP_1_1.toString());
114+
public SSLVariant getSSLVariant() {
115+
return SSLVariant.INSECURE;
127116
}
128117
}

src/test/java/org/htmlunit/WebServerTestCase.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import javax.servlet.Servlet;
2727

2828
import org.eclipse.jetty.server.Server;
29-
import org.eclipse.jetty.server.SslConnectionFactory;
3029
import org.htmlunit.WebDriverTestCase.MockWebConnectionServlet;
3130
import org.htmlunit.html.HtmlPage;
3231
import org.htmlunit.util.JettyServerUtils;
@@ -45,6 +44,12 @@
4544
*/
4645
public abstract class WebServerTestCase extends WebTestCase {
4746

47+
public enum SSLVariant {
48+
NONE,
49+
INSECURE,
50+
SELF_SIGNED
51+
}
52+
4853
/** Timeout used when waiting for successful bind. */
4954
public static final int BIND_TIMEOUT = 1000;
5055

@@ -66,7 +71,7 @@ protected void startWebServer(final String resourceBase) throws Exception {
6671
throw new IllegalStateException("startWebServer() can not be called twice");
6772
}
6873

69-
server_ = JettyServerUtils.startWebServer(PORT, resourceBase, null, null, isBasicAuthentication(), getSslConnectionFactory());
74+
server_ = JettyServerUtils.startWebServer(PORT, resourceBase, null, null, isBasicAuthentication(), getSSLVariant());
7075
}
7176

7277
/**
@@ -245,7 +250,7 @@ protected void startWebServer(final MockWebConnection mockConnection) throws Exc
245250
final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
246251
servlets.put("/*", MockWebConnectionServlet.class);
247252

248-
STATIC_SERVER_ = JettyServerUtils.startWebServer(PORT, "./", servlets, null, isBasicAuthentication(), getSslConnectionFactory());
253+
STATIC_SERVER_ = JettyServerUtils.startWebServer(PORT, "./", servlets, null, isBasicAuthentication(), getSSLVariant());
249254
}
250255
MockWebConnectionServlet.setMockconnection(mockConnection);
251256
}
@@ -301,10 +306,10 @@ protected boolean isBasicAuthentication() {
301306
}
302307

303308
/**
304-
* @return SslConnectionFactory for https
309+
* @return the {@link SSLVariant} to be used
305310
*/
306-
protected SslConnectionFactory getSslConnectionFactory() {
307-
return null;
311+
public SSLVariant getSSLVariant() {
312+
return SSLVariant.NONE;
308313
}
309314

310315
/**

src/test/java/org/htmlunit/archunit/ArchitectureTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,6 @@ public void check(final JavaMethod method, final ConditionEvents events) {
658658
public static final ArchRule jettyPackageRule = noClasses()
659659
.should().dependOnClassesThat().resideInAnyPackage("org.eclipse.jetty..");
660660

661-
662661
/**
663662
* Some methods should not be used.
664663
*/

src/test/java/org/htmlunit/util/JettyServerUtils.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
*/
1515
package org.htmlunit.util;
1616

17+
import static org.eclipse.jetty.http.HttpVersion.HTTP_1_1;
18+
1719
import java.io.IOException;
1820
import java.net.BindException;
21+
import java.net.URL;
1922
import java.nio.charset.Charset;
2023
import java.util.EnumSet;
2124
import java.util.Map;
@@ -45,7 +48,9 @@
4548
import org.eclipse.jetty.servlet.ServletHolder;
4649
import org.eclipse.jetty.util.security.Constraint;
4750
import org.eclipse.jetty.util.thread.QueuedThreadPool;
51+
import org.htmlunit.HttpWebConnectionInsecureSSLWithClientCertificateTest;
4852
import org.htmlunit.WebServerTestCase;
53+
import org.htmlunit.WebServerTestCase.SSLVariant;
4954
import org.htmlunit.WebTestCase;
5055

5156
/**
@@ -59,7 +64,7 @@ public static Server startWebServer(final int port, final String resourceBase,
5964
final Map<String, Class<? extends Servlet>> servlets,
6065
final Charset serverCharset,
6166
final boolean isBasicAuthentication,
62-
final SslConnectionFactory sslConnectionFactory) throws Exception {
67+
final WebServerTestCase.SSLVariant sslVariant) throws Exception {
6368
final Server server = buildServer(port);
6469

6570
final ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
@@ -113,7 +118,31 @@ public static Server startWebServer(final int port, final String resourceBase,
113118

114119
server.setHandler(context);
115120

116-
if (sslConnectionFactory != null) {
121+
if (sslVariant != SSLVariant.NONE) {
122+
org.eclipse.jetty.util.ssl.SslContextFactory.Server contextFactory = null;
123+
124+
switch (sslVariant) {
125+
case INSECURE: {
126+
final URL url = HttpWebConnectionInsecureSSLWithClientCertificateTest.class
127+
.getClassLoader().getResource("insecureSSL.pfx");
128+
129+
contextFactory = new org.eclipse.jetty.util.ssl.SslContextFactory.Server();
130+
contextFactory.setKeyStorePath(url.toExternalForm());
131+
contextFactory.setKeyStorePassword("nopassword");
132+
contextFactory.setEndpointIdentificationAlgorithm(null);
133+
}
134+
case SELF_SIGNED: {
135+
final URL url = HttpWebConnectionInsecureSSLWithClientCertificateTest.class
136+
.getClassLoader().getResource("self-signed-cert.keystore");
137+
138+
contextFactory = new org.eclipse.jetty.util.ssl.SslContextFactory.Server();
139+
contextFactory.setKeyStoreType("jks");
140+
contextFactory.setKeyStorePath(url.toExternalForm());
141+
contextFactory.setKeyStorePassword("nopassword");
142+
contextFactory.setEndpointIdentificationAlgorithm(null);
143+
}
144+
}
145+
117146
final HttpConfiguration sslConfiguration = new HttpConfiguration();
118147
final SecureRequestCustomizer secureRequestCustomizer = new SecureRequestCustomizer();
119148

@@ -125,6 +154,7 @@ public static Server startWebServer(final int port, final String resourceBase,
125154

126155
final HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory(sslConfiguration);
127156

157+
final SslConnectionFactory sslConnectionFactory = new SslConnectionFactory(contextFactory, HTTP_1_1.toString());
128158
final ServerConnector connector = new ServerConnector(server, sslConnectionFactory, httpConnectionFactory);
129159
connector.setPort(WebTestCase.PORT2);
130160
server.addConnector(connector);

0 commit comments

Comments
 (0)