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

Create PropertiesClass for external properties (http.proxyHost, http.proxyPort, http.nonProxyHosts) #4611

Merged
merged 7 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2020 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;

import org.glassfish.jersey.internal.util.PropertiesClass;

@PropertiesClass
public final class ExternalProperties {

/**
* Property used to specify the hostname, or address, of the proxy server.
*/
public static final String HTTP_PROXY_HOST = "http.proxyHost";

/**
* Property used to specify the port number of the proxy server.
*/
public static final String HTTP_PROXY_PORT = "http.proxyPort";

/**
* Property used to indicates the hosts that should be accessed
* without going through the proxy.
*/
public static final String HTTP_NON_PROXY_HOSTS = "http.nonProxyHosts";

/**
* Prevent instantiation.
*/
private ExternalProperties() {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class SystemPropertiesConfigurationModel implements ExternalConfigurationModel<V

private static final Logger log = Logger.getLogger(SystemPropertiesConfigurationModel.class.getName());
static final List<String> PROPERTY_CLASSES = Arrays.asList(
"org.glassfish.jersey.ExternalProperties",
"org.glassfish.jersey.server.ServerProperties",
"org.glassfish.jersey.client.ClientProperties",
"org.glassfish.jersey.servlet.ServletProperties",
Expand Down
7 changes: 6 additions & 1 deletion tests/e2e-core-common/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) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2017, 2020 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 @@ -43,6 +43,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
tvallin marked this conversation as resolved.
Show resolved Hide resolved
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-jetty</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright (c) 2020 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.e2e.common;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
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.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;

public class TestExternalProperties extends JerseyTest {

private static final String PROXY_HOST = "localhost";
private static final String PROXY_PORT = "9997";
private static boolean proxyHit = false;

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

@GET
public String getOK() {
return "OK";
}

}

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

@Test
public void testProxy() {
startFakeProxy();

System.setProperty(ExternalProperties.HTTP_PROXY_HOST, PROXY_HOST);
System.setProperty(ExternalProperties.HTTP_PROXY_PORT, PROXY_PORT);
System.setProperty(ExternalProperties.HTTP_NON_PROXY_HOSTS, "");

Response response = target("resource").request().get();

Assert.assertEquals(407, response.getStatus());
}

@Test
public void testNonProxy() {
startFakeProxy();

System.setProperty(ExternalProperties.HTTP_PROXY_HOST, PROXY_HOST);
System.setProperty(ExternalProperties.HTTP_PROXY_PORT, PROXY_PORT);
System.setProperty(ExternalProperties.HTTP_NON_PROXY_HOSTS, "localhost");

Response response = target("resource").request().get();

Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("OK", response.readEntity(String.class));
Assert.assertFalse(proxyHit);
}

private void startFakeProxy() {
Server server = new Server(9997);
tvallin marked this conversation as resolved.
Show resolved Hide resolved
server.setHandler(new ProxyHandler(false));
try {
server.start();
} catch (Exception e) {

}
}

class ProxyHandler extends AbstractHandler {
@Override
public void handle(String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response) {
proxyHit = true;
response.setStatus(407);
baseRequest.setHandled(true);
}

ProxyHandler(boolean pProxyHit) {
super();
proxyHit = pProxyHit;
}
}

}