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

IPv4 system property tests with connectors #4738

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -37,6 +37,15 @@ public final class ExternalProperties {
*/
public static final String HTTP_NON_PROXY_HOSTS = "http.nonProxyHosts";

/**
* If IPv6 is available on the operating system the underlying native socket will be,
* by default, an IPv6 socket which lets applications connect to, and accept connections from,
* both IPv4 and IPv6 hosts.
* However, in the case an application would rather use IPv4 only sockets, then this property can be set to true.
* The implication is that it will not be possible for the application to communicate with IPv6 only hosts.
*/
public static final String JAVA_NET_PREFERIPV4STACK = "java.net.preferIPv4Stack";

/**
* Prevent instantiation.
*/
Expand Down
37 changes: 36 additions & 1 deletion tests/integration/externalproperties/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2020, 2021 Oracle and/or its affiliates. All rights reserved.

This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -42,13 +42,48 @@
<artifactId>jersey-test-framework-provider-jetty</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.connectors</groupId>
<artifactId>jersey-apache-connector</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.connectors</groupId>
<artifactId>jersey-grizzly-connector</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.connectors</groupId>
<artifactId>jersey-jdk-connector</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.connectors</groupId>
<artifactId>jersey-jetty-connector</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.connectors</groupId>
<artifactId>jersey-netty-connector</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<java.net.preferIPv4Stack>true</java.net.preferIPv4Stack>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -22,6 +22,7 @@
import org.glassfish.jersey.ExternalProperties;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -38,6 +39,7 @@ public class HttpProxyTest extends JerseyTest {
private static final String PROXY_HOST = "localhost";
private static final String PROXY_PORT = "9997";
private static boolean proxyHit = false;
private static Server server;

@Path("resource")
public static class ProxyTestResource {
Expand All @@ -58,7 +60,7 @@ protected Application configure() {
public void startFakeProxy() {
System.setProperty(ExternalProperties.HTTP_PROXY_HOST, PROXY_HOST);
System.setProperty(ExternalProperties.HTTP_PROXY_PORT, PROXY_PORT);
Server server = new Server(Integer.parseInt(PROXY_PORT));
server = new Server(Integer.parseInt(PROXY_PORT));
server.setHandler(new ProxyHandler(false));
try {
server.start();
Expand All @@ -67,6 +69,11 @@ public void startFakeProxy() {
}
}

@After
public void stopFakeProxy() throws Exception {
server.stop();
}

@Test
public void testProxy() {
System.setProperty(ExternalProperties.HTTP_NON_PROXY_HOSTS, "");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.tests.externalproperties;

import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.HttpUrlConnectorProvider;
import org.glassfish.jersey.client.spi.ConnectorProvider;
import org.glassfish.jersey.grizzly.connector.GrizzlyConnectorProvider;
import org.glassfish.jersey.jdk.connector.JdkConnectorProvider;
import org.glassfish.jersey.jetty.connector.JettyConnectorProvider;
import org.glassfish.jersey.netty.connector.NettyConnectorProvider;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import javax.ws.rs.ProcessingException;
import javax.ws.rs.core.Application;
import java.util.Arrays;
import java.util.List;

@RunWith(Parameterized.class)
public class IPv4ConnectorTest extends JerseyTest {

@Parameterized.Parameters(name = "{index}: {0}")
public static List<Object[]> connectors() {
return Arrays.asList(new Object[][]{
{HttpUrlConnectorProvider.class},
{GrizzlyConnectorProvider.class},
{JettyConnectorProvider.class},
{ApacheConnectorProvider.class},
{GrizzlyConnectorProvider.class},
{NettyConnectorProvider.class},
{JdkConnectorProvider.class},
});
}

private final ConnectorProvider connectorProvider;

public IPv4ConnectorTest(Class<? extends ConnectorProvider> connectorProviderClass)
throws IllegalAccessException, InstantiationException {
this.connectorProvider = connectorProviderClass.newInstance();
}

@Override
protected Application configure() {
return new ResourceConfig();
}

@Override
protected void configureClient(ClientConfig config) {
config.connectorProvider(connectorProvider);
}

@Test
public void testIPv4Address() {
try {
target().request().get();
} catch (ProcessingException pe) {
if (connectorProvider instanceof JettyConnectorProvider) {
Assert.assertEquals("java.net.SocketException: Could not connect to /0:0:0:0:0:0:0:1:9997",
pe.getCause().getMessage());
} else {
Assert.assertTrue(pe.getCause().getMessage().contains("Protocol family unavailable"));
}
}
}
}