Skip to content

Commit

Permalink
Better handling of MicroProfile Rest Client Proxies (eclipse-ee4j#4918)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Pielage <pandrex247@hotmail.com>
  • Loading branch information
Pandrex247 authored Dec 2, 2021
1 parent 3716939 commit aea2d4b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
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"));
}
}

0 comments on commit aea2d4b

Please sign in to comment.