Skip to content

Commit 97926d3

Browse files
authored
Create PropertiesClass for external properties (http.proxyHost, http.proxyPort, http.nonProxyHosts) (#4611)
* Create properties class for external properties Signed-off-by: tvallin <[email protected]>
1 parent 4a70e66 commit 97926d3

File tree

5 files changed

+212
-0
lines changed

5 files changed

+212
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2020 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;
18+
19+
import org.glassfish.jersey.internal.util.PropertiesClass;
20+
21+
@PropertiesClass
22+
public final class ExternalProperties {
23+
24+
/**
25+
* Property used to specify the hostname, or address, of the proxy server.
26+
*/
27+
public static final String HTTP_PROXY_HOST = "http.proxyHost";
28+
29+
/**
30+
* Property used to specify the port number of the proxy server.
31+
*/
32+
public static final String HTTP_PROXY_PORT = "http.proxyPort";
33+
34+
/**
35+
* Property used to indicates the hosts that should be accessed
36+
* without going through the proxy.
37+
*/
38+
public static final String HTTP_NON_PROXY_HOSTS = "http.nonProxyHosts";
39+
40+
/**
41+
* Prevent instantiation.
42+
*/
43+
private ExternalProperties() {
44+
}
45+
46+
}

core-common/src/main/java/org/glassfish/jersey/internal/config/SystemPropertiesConfigurationModel.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class SystemPropertiesConfigurationModel implements ExternalConfigurationModel<V
4444

4545
private static final Logger log = Logger.getLogger(SystemPropertiesConfigurationModel.class.getName());
4646
static final List<String> PROPERTY_CLASSES = Arrays.asList(
47+
"org.glassfish.jersey.ExternalProperties",
4748
"org.glassfish.jersey.server.ServerProperties",
4849
"org.glassfish.jersey.client.ClientProperties",
4950
"org.glassfish.jersey.servlet.ServletProperties",
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
5+
6+
This program and the accompanying materials are made available under the
7+
terms of the Eclipse Public License v. 2.0, which is available at
8+
http://www.eclipse.org/legal/epl-2.0.
9+
10+
This Source Code may also be made available under the following Secondary
11+
Licenses when the conditions for such availability set forth in the
12+
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
13+
version 2 with the GNU Classpath Exception, which is available at
14+
https://www.gnu.org/software/classpath/license.html.
15+
16+
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
17+
18+
-->
19+
20+
<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">
21+
<modelVersion>4.0.0</modelVersion>
22+
<parent>
23+
<groupId>org.glassfish.jersey.tests.integration</groupId>
24+
<artifactId>project</artifactId>
25+
<version>2.33-SNAPSHOT</version>
26+
</parent>
27+
28+
<artifactId>externalproperties</artifactId>
29+
<packaging>jar</packaging>
30+
<name>jersey-tests-externalproperties</name>
31+
32+
<description>Jersey tests external properties</description>
33+
34+
<dependencies>
35+
<dependency>
36+
<groupId>junit</groupId>
37+
<artifactId>junit</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
42+
<artifactId>jersey-test-framework-provider-jetty</artifactId>
43+
<scope>test</scope>
44+
</dependency>
45+
</dependencies>
46+
47+
<build>
48+
<plugins>
49+
<plugin>
50+
<groupId>org.apache.maven.plugins</groupId>
51+
<artifactId>maven-surefire-plugin</artifactId>
52+
</plugin>
53+
</plugins>
54+
</build>
55+
56+
</project>
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright (c) 2020 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.tests.externalproperties;
18+
19+
import org.eclipse.jetty.server.Request;
20+
import org.eclipse.jetty.server.Server;
21+
import org.eclipse.jetty.server.handler.AbstractHandler;
22+
import org.glassfish.jersey.ExternalProperties;
23+
import org.glassfish.jersey.server.ResourceConfig;
24+
import org.glassfish.jersey.test.JerseyTest;
25+
import org.junit.Assert;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
29+
import javax.servlet.http.HttpServletRequest;
30+
import javax.servlet.http.HttpServletResponse;
31+
import javax.ws.rs.GET;
32+
import javax.ws.rs.Path;
33+
import javax.ws.rs.core.Application;
34+
import javax.ws.rs.core.Response;
35+
36+
public class HttpProxyTest extends JerseyTest {
37+
38+
private static final String PROXY_HOST = "localhost";
39+
private static final String PROXY_PORT = "9997";
40+
private static boolean proxyHit = false;
41+
42+
@Path("resource")
43+
public static class ProxyTestResource {
44+
45+
@GET
46+
public String getOK() {
47+
return "OK";
48+
}
49+
50+
}
51+
52+
@Override
53+
protected Application configure() {
54+
return new ResourceConfig(ProxyTestResource.class);
55+
}
56+
57+
@Before
58+
public void startFakeProxy() {
59+
System.setProperty(ExternalProperties.HTTP_PROXY_HOST, PROXY_HOST);
60+
System.setProperty(ExternalProperties.HTTP_PROXY_PORT, PROXY_PORT);
61+
Server server = new Server(Integer.parseInt(PROXY_PORT));
62+
server.setHandler(new ProxyHandler(false));
63+
try {
64+
server.start();
65+
} catch (Exception e) {
66+
67+
}
68+
}
69+
70+
@Test
71+
public void testProxy() {
72+
System.setProperty(ExternalProperties.HTTP_NON_PROXY_HOSTS, "");
73+
74+
Response response = target("resource").request().get();
75+
76+
Assert.assertEquals(407, response.getStatus());
77+
}
78+
79+
@Test
80+
public void testNonProxy() {
81+
System.setProperty(ExternalProperties.HTTP_NON_PROXY_HOSTS, "localhost");
82+
83+
Response response = target("resource").request().get();
84+
85+
Assert.assertEquals(200, response.getStatus());
86+
Assert.assertEquals("OK", response.readEntity(String.class));
87+
Assert.assertFalse(proxyHit);
88+
}
89+
90+
class ProxyHandler extends AbstractHandler {
91+
@Override
92+
public void handle(String target,
93+
Request baseRequest,
94+
HttpServletRequest request,
95+
HttpServletResponse response) {
96+
proxyHit = true;
97+
response.setStatus(407);
98+
baseRequest.setHandled(true);
99+
}
100+
101+
ProxyHandler(boolean pProxyHit) {
102+
super();
103+
proxyHit = pProxyHit;
104+
}
105+
}
106+
107+
}
108+

tests/integration/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<module>ejb-multimodule</module>
4141
<module>ejb-multimodule-reload</module>
4242
<module>ejb-test-webapp</module>
43+
<module>externalproperties</module>
4344
<module>j-376</module>
4445
<module>j-441</module>
4546
<module>j-59</module>

0 commit comments

Comments
 (0)