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

Add http.maxRedirects to External Properties and tests #4671

Open
wants to merge 4 commits 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,12 @@ public final class ExternalProperties {
*/
public static final String HTTP_NON_PROXY_HOSTS = "http.nonProxyHosts";

/**
* This integer value determines the maximum number, for a given request,
* of HTTP redirects that will be automatically followed by the protocol handler.
*/
public static final String HTTP_MAX_REDIRECTS = "http.maxRedirects";

/**
* Prevent instantiation.
*/
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/externalproperties/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<http.maxRedirects>1</http.maxRedirects>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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.ExternalProperties;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Assert;
import org.junit.Test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;
import java.net.URI;

public class HttpMaxRedirectsTest extends JerseyTest {

@Path("resource")
public static class RedirectResource {

@GET
public Response getRedirect1() {
return Response
.seeOther(URI.create("resource/redirect1"))
.build();
}

@GET
@Path("/redirect1")
public Response getRedirect2() {
return Response
.seeOther(URI.create("resource/redirect2"))
.build();
}

@GET
@Path("/redirect2")
public Response getRedirect3() {
return Response
.seeOther(URI.create("resource/finalredirect"))
.build();
}

@GET
@Path("/finalredirect")
public String finalDestination() {
return "You have been redirected 3 times !";
}

}

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

@Test
public void testFailRedirect() {
try {
target("resource").request().get();
Assert.fail("Should have thrown ProcessingException"
+ " because of too many redirects.");
} catch (ProcessingException pe) {
Assert.assertEquals("java.net.ProtocolException: Server redirected too many times (1)",
pe.getMessage());
}

}

}
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 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