|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the |
| 10 | + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | + * version 2 with the GNU Classpath Exception, which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + */ |
| 16 | + |
| 17 | +package org.glassfish.jersey.client; |
| 18 | + |
| 19 | +import org.glassfish.jersey.client.internal.HttpUrlConnector; |
| 20 | +import org.glassfish.jersey.client.spi.Connector; |
| 21 | +import org.glassfish.jersey.internal.MapPropertiesDelegate; |
| 22 | +import org.glassfish.jersey.internal.PropertiesDelegate; |
| 23 | +import org.hamcrest.MatcherAssert; |
| 24 | +import org.hamcrest.Matchers; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +import javax.net.ssl.HttpsURLConnection; |
| 28 | +import javax.net.ssl.SSLContext; |
| 29 | +import javax.net.ssl.SSLSocketFactory; |
| 30 | +import javax.ws.rs.client.Client; |
| 31 | +import javax.ws.rs.client.ClientBuilder; |
| 32 | +import javax.ws.rs.core.Response; |
| 33 | +import java.io.IOException; |
| 34 | +import java.net.HttpURLConnection; |
| 35 | +import java.net.URISyntaxException; |
| 36 | +import java.net.URL; |
| 37 | +import java.net.URLConnection; |
| 38 | +import java.util.concurrent.atomic.AtomicReference; |
| 39 | +import java.util.function.Supplier; |
| 40 | + |
| 41 | +public class SSLSocketFactoryTest { |
| 42 | + static final AtomicReference<SSLSocketFactory> factoryHolder = new AtomicReference<>(); |
| 43 | + static SSLSocketFactory defaultSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory(); |
| 44 | + |
| 45 | + // @Test |
| 46 | + // Alternative test |
| 47 | + // Check KeepAliveCache#get(URL url, Object obj) |
| 48 | + public void testSingleConnection() throws InterruptedException, IOException { |
| 49 | + Client client = ClientBuilder.newClient(); |
| 50 | + |
| 51 | + for (int i = 0; i < 3; i++) { |
| 52 | + try (Response response = client.target("https://www.spiegel.de") |
| 53 | + .request() |
| 54 | + .get()) { |
| 55 | + |
| 56 | + response.readEntity(String.class); |
| 57 | + System.out.println(String.format("response = %s", response)); |
| 58 | + Thread.sleep(1000); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + System.in.read(); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testSslContextFactoryOnClientIsSameForConsecutiveRequests() throws IOException, URISyntaxException { |
| 67 | + int firstRequestFactory, secondRequestFactory = 0; |
| 68 | + Client client = ClientBuilder.newClient(); |
| 69 | + HttpUrlConnectorProvider.ConnectionFactory connectionFactory = (url) -> (HttpURLConnection) url.openConnection(); |
| 70 | + SSLSocketFactoryConnector connector = (SSLSocketFactoryConnector) new SSlSocketFactoryUrlConnectorProvider() |
| 71 | + .createHttpUrlConnector(client, connectionFactory, 4096, true, false); |
| 72 | + URL url = new URL("https://somewhere.whereever:8080"); |
| 73 | + URLConnection urlConnection = url.openConnection(); |
| 74 | + |
| 75 | + // First Request |
| 76 | + connector.setSslContextFactory(client, new ClientRequest(url.toURI(), |
| 77 | + (ClientConfig) client.getConfiguration(), new MapPropertiesDelegate())); |
| 78 | + connector.secureConnection((JerseyClient) client, (HttpURLConnection) urlConnection); |
| 79 | + firstRequestFactory = factoryHolder.get().hashCode(); |
| 80 | + |
| 81 | + // reset to the default socketFactory |
| 82 | + ((HttpsURLConnection) urlConnection).setSSLSocketFactory(defaultSocketFactory); |
| 83 | + |
| 84 | + // Second Request |
| 85 | + connector.setSslContextFactory(client, new ClientRequest(url.toURI(), |
| 86 | + (ClientConfig) client.getConfiguration(), new MapPropertiesDelegate())); |
| 87 | + connector.secureConnection((JerseyClient) client, (HttpURLConnection) urlConnection); |
| 88 | + secondRequestFactory = factoryHolder.get().hashCode(); |
| 89 | + |
| 90 | + MatcherAssert.assertThat(firstRequestFactory, Matchers.equalTo(secondRequestFactory)); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testSslContextFactoryOnRequestIsSameForConsecutiveRequests() throws IOException, URISyntaxException { |
| 95 | + int firstRequestFactory, secondRequestFactory = 0; |
| 96 | + Client client = ClientBuilder.newClient(); |
| 97 | + SSLContext sslContext = new SslContextClientBuilder().build(); |
| 98 | + HttpUrlConnectorProvider.ConnectionFactory connectionFactory = (url) -> (HttpURLConnection) url.openConnection(); |
| 99 | + SSLSocketFactoryConnector connector = (SSLSocketFactoryConnector) new SSlSocketFactoryUrlConnectorProvider() |
| 100 | + .createHttpUrlConnector(client, connectionFactory, 4096, true, false); |
| 101 | + URL url = new URL("https://somewhere.whereever:8080"); |
| 102 | + URLConnection urlConnection = url.openConnection(); |
| 103 | + PropertiesDelegate propertiesDelegate = new MapPropertiesDelegate(); |
| 104 | + propertiesDelegate.setProperty(ClientProperties.SSL_CONTEXT_SUPPLIER, (Supplier<SSLContext>) () -> sslContext); |
| 105 | + |
| 106 | + // First Request |
| 107 | + connector.setSslContextFactory(client, new ClientRequest(url.toURI(), |
| 108 | + (ClientConfig) client.getConfiguration(), propertiesDelegate)); |
| 109 | + connector.secureConnection((JerseyClient) client, (HttpURLConnection) urlConnection); |
| 110 | + firstRequestFactory = factoryHolder.get().hashCode(); |
| 111 | + |
| 112 | + // reset to the default socketFactory |
| 113 | + ((HttpsURLConnection) urlConnection).setSSLSocketFactory(defaultSocketFactory); |
| 114 | + |
| 115 | + // Second Request |
| 116 | + connector.setSslContextFactory(client, new ClientRequest(url.toURI(), |
| 117 | + (ClientConfig) client.getConfiguration(), propertiesDelegate)); |
| 118 | + connector.secureConnection((JerseyClient) client, (HttpURLConnection) urlConnection); |
| 119 | + secondRequestFactory = factoryHolder.get().hashCode(); |
| 120 | + |
| 121 | + MatcherAssert.assertThat(firstRequestFactory, Matchers.equalTo(secondRequestFactory)); |
| 122 | + } |
| 123 | + |
| 124 | + private static class SSLSocketFactoryConnector extends HttpUrlConnector { |
| 125 | + public SSLSocketFactoryConnector(Client client, HttpUrlConnectorProvider.ConnectionFactory connectionFactory, |
| 126 | + int chunkSize, boolean fixLengthStreaming, boolean setMethodWorkaround) { |
| 127 | + super(client, connectionFactory, chunkSize, fixLengthStreaming, setMethodWorkaround); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + protected void secureConnection(JerseyClient client, HttpURLConnection uc) { |
| 132 | + super.secureConnection(client, uc); |
| 133 | + if (HttpsURLConnection.class.isInstance(uc)) { |
| 134 | + SSLSocketFactory factory = ((HttpsURLConnection) uc).getSSLSocketFactory(); |
| 135 | + factoryHolder.set(factory); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + protected void setSslContextFactory(Client client, ClientRequest request) { |
| 141 | + super.setSslContextFactory(client, request); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private static class SSlSocketFactoryUrlConnectorProvider extends HttpUrlConnectorProvider { |
| 146 | + @Override |
| 147 | + protected Connector createHttpUrlConnector(Client client, ConnectionFactory connectionFactory, int chunkSize, |
| 148 | + boolean fixLengthStreaming, boolean setMethodWorkaround) { |
| 149 | + return new SSLSocketFactoryConnector( |
| 150 | + client, |
| 151 | + connectionFactory, |
| 152 | + chunkSize, |
| 153 | + fixLengthStreaming, |
| 154 | + setMethodWorkaround); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments