Skip to content

Commit aea2d4b

Browse files
authored
Better handling of MicroProfile Rest Client Proxies (#4918)
Signed-off-by: Andrew Pielage <[email protected]>
1 parent 3716939 commit aea2d4b

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/RestClientBuilderImpl.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import java.util.concurrent.Executors;
3737
import java.util.concurrent.TimeUnit;
3838
import java.util.function.Supplier;
39+
import java.util.logging.Level;
40+
import java.util.logging.Logger;
3941

4042
import javax.annotation.Priority;
4143
import javax.net.ssl.HostnameVerifier;
@@ -445,10 +447,42 @@ public RestClientBuilder proxyAddress(String proxyHost, int proxyPort) {
445447
if (proxyPort <= 0 || proxyPort > 65535) {
446448
throw new IllegalArgumentException("Invalid proxy port");
447449
}
448-
property(ClientProperties.PROXY_URI, proxyHost + ":" + proxyPort);
450+
451+
// If proxyString is something like "localhost:8765" we need to add a scheme since the connectors expect one
452+
String proxyString = createProxyString(proxyHost, proxyPort);
453+
454+
property(ClientProperties.PROXY_URI, proxyString);
449455
return this;
450456
}
451457

458+
static String createProxyString(String proxyHost, int proxyPort) {
459+
boolean prependScheme = false;
460+
String proxyString = proxyHost + ":" + proxyPort;
461+
462+
if (proxyString.split(":").length == 2) {
463+
// Check if first character is a number to account for if proxyHost is given as an IP rather than a name
464+
// URI.create("127.0.0.1:8765") will lead to an IllegalArgumentException
465+
if (proxyString.matches("\\d.*")) {
466+
prependScheme = true;
467+
} else {
468+
// "localhost:8765" will set the scheme as "localhost" and the host as "null"
469+
URI proxyURI = URI.create(proxyString);
470+
if (proxyURI.getHost() == null && proxyURI.getScheme().equals(proxyHost)) {
471+
prependScheme = true;
472+
}
473+
}
474+
}
475+
476+
if (prependScheme) {
477+
proxyString = "http://" + proxyString;
478+
Logger.getLogger(RestClientBuilderImpl.class.getName()).log(Level.FINE,
479+
"No scheme provided with proxyHost: " + proxyHost + ". Defaulting to HTTP, proxy address = "
480+
+ proxyString);
481+
}
482+
483+
return proxyString;
484+
}
485+
452486
@Override
453487
public RestClientBuilder queryParamStyle(QueryParamStyle queryParamStyle) {
454488
if (queryParamStyle != null) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2021 Payara Foundation 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.microprofile.restclient;
18+
19+
import org.junit.Assert;
20+
import org.junit.Test;
21+
22+
import static org.glassfish.jersey.microprofile.restclient.RestClientBuilderImpl.createProxyString;
23+
24+
public class RestClientBuilderImplTest {
25+
26+
@Test
27+
public void createProxyStringTest() {
28+
Assert.assertTrue(createProxyString("localhost", 8765).equals("http://localhost:8765"));
29+
Assert.assertTrue(createProxyString("http://localhost", 8765).equals("http://localhost:8765"));
30+
Assert.assertTrue(createProxyString("127.0.0.1", 8765).equals("http://127.0.0.1:8765"));
31+
Assert.assertTrue(createProxyString("http://192.168.1.1", 8765).equals("http://192.168.1.1:8765"));
32+
}
33+
}

0 commit comments

Comments
 (0)