Better handling of MicroProfile Rest Client Proxies (#4918)

Signed-off-by: Andrew Pielage <pandrex247@hotmail.com>
diff --git a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/RestClientBuilderImpl.java b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/RestClientBuilderImpl.java
index f157e7b..3e3d129 100644
--- a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/RestClientBuilderImpl.java
+++ b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/RestClientBuilderImpl.java
@@ -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;
@@ -445,10 +447,42 @@
         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) {
diff --git a/ext/microprofile/mp-rest-client/src/test/java/org/glassfish/jersey/microprofile/restclient/RestClientBuilderImplTest.java b/ext/microprofile/mp-rest-client/src/test/java/org/glassfish/jersey/microprofile/restclient/RestClientBuilderImplTest.java
new file mode 100644
index 0000000..685e53a
--- /dev/null
+++ b/ext/microprofile/mp-rest-client/src/test/java/org/glassfish/jersey/microprofile/restclient/RestClientBuilderImplTest.java
@@ -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"));
+    }
+}