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

Better handling of MicroProfile Rest Client Proxies #4918

Merged
merged 2 commits into from
Dec 2, 2021
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
Expand Up @@ -36,6 +36,8 @@
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.Priority;
import javax.net.ssl.HostnameVerifier;
Expand Down Expand Up @@ -445,10 +447,42 @@ public RestClientBuilder proxyAddress(String proxyHost, int proxyPort) {
if (proxyPort <= 0 || proxyPort > 65535) {
throw new IllegalArgumentException("Invalid proxy port");
}
property(ClientProperties.PROXY_URI, proxyHost + ":" + proxyPort);

// If proxyString is something like "localhost:8765" we need to add a scheme since the connectors expect one
String proxyString = createProxyString(proxyHost, proxyPort);

property(ClientProperties.PROXY_URI, proxyString);
return this;
}

static String createProxyString(String proxyHost, int proxyPort) {
boolean prependScheme = false;
String proxyString = proxyHost + ":" + proxyPort;

if (proxyString.split(":").length == 2) {
// Check if first character is a number to account for if proxyHost is given as an IP rather than a name
// URI.create("127.0.0.1:8765") will lead to an IllegalArgumentException
if (proxyString.matches("\\d.*")) {
prependScheme = true;
} else {
// "localhost:8765" will set the scheme as "localhost" and the host as "null"
URI proxyURI = URI.create(proxyString);
if (proxyURI.getHost() == null && proxyURI.getScheme().equals(proxyHost)) {
prependScheme = true;
}
}
}

if (prependScheme) {
proxyString = "http://" + proxyString;
Logger.getLogger(RestClientBuilderImpl.class.getName()).log(Level.FINE,
"No scheme provided with proxyHost: " + proxyHost + ". Defaulting to HTTP, proxy address = "
+ proxyString);
}

return proxyString;
}

@Override
public RestClientBuilder queryParamStyle(QueryParamStyle queryParamStyle) {
if (queryParamStyle != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2021 Payara Foundation 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.microprofile.restclient;

import org.junit.Assert;
import org.junit.Test;

import static org.glassfish.jersey.microprofile.restclient.RestClientBuilderImpl.createProxyString;

public class RestClientBuilderImplTest {

@Test
public void createProxyStringTest() {
Assert.assertTrue(createProxyString("localhost", 8765).equals("http://localhost:8765"));
Assert.assertTrue(createProxyString("http://localhost", 8765).equals("http://localhost:8765"));
Assert.assertTrue(createProxyString("127.0.0.1", 8765).equals("http://127.0.0.1:8765"));
Assert.assertTrue(createProxyString("http://192.168.1.1", 8765).equals("http://192.168.1.1:8765"));
}
}