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 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
@@ -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
56 changes: 56 additions & 0 deletions tests/integration/externalproperties/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

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

-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.glassfish.jersey.tests.integration</groupId>
<artifactId>project</artifactId>
<version>2.33-SNAPSHOT</version>
</parent>

<artifactId>externalproperties</artifactId>
<packaging>jar</packaging>
<name>jersey-tests-externalproperties</name>

<description>Jersey tests external properties</description>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-jetty</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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.externalproperties;

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.Before;
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 HttpProxyTest 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);
}

@Before
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.setHandler(new ProxyHandler(false));
try {
server.start();
} catch (Exception e) {

}
}

@Test
public void testProxy() {
System.setProperty(ExternalProperties.HTTP_NON_PROXY_HOSTS, "");

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

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

@Test
public void testNonProxy() {
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);
}

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

}

1 change: 1 addition & 0 deletions tests/integration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<module>ejb-multimodule</module>
<module>ejb-multimodule-reload</module>
<module>ejb-test-webapp</module>
<module>externalproperties</module>
<module>j-376</module>
<module>j-441</module>
<module>j-59</module>
Expand Down