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

Unable to create a Proxy of ProxyType.SYSTEM #6446

Closed
fssouza opened this issue Mar 21, 2023 · 0 comments · Fixed by #6526
Closed

Unable to create a Proxy of ProxyType.SYSTEM #6446

fssouza opened this issue Mar 21, 2023 · 0 comments · Fixed by #6526
Assignees
Labels
3.x Issues for 3.x version branch bug Something isn't working P2 webclient
Milestone

Comments

@fssouza
Copy link

fssouza commented Mar 21, 2023

Environment Details

  • Helidon Version: 2.5.0
  • Helidon MP
  • JDK version: 17.0.3
  • OS: MacOS
  • Docker version (if applicable): N/A

Problem Description

Unable to create a Proxy of ProxyType.SYSTEM. This is how I was able to workaround but it is not ideal since I'm not using the SystemSelector.

    static Optional<Proxy> createProxyFromSystemProperties() {
        String httpsProxy = System.getProperty("https.proxyHost");
        String host = httpsProxy == null ? System.getProperty("http.proxyHost") : httpsProxy;
        String noProxy = System.getProperty("http.nonProxyHosts");
        String proxyPort = System.getProperty("http.proxyPort");
        if (host != null) {
            Proxy.Builder proxyBuilder = Proxy.builder()
                    .type(Proxy.ProxyType.HTTP)
                    .host(host);

            if (noProxy != null) {
                Arrays.stream(noProxy.split(Pattern.quote("|")))
                        // Helidon NoProxy only works if the * is not provided
                        .map(s -> s.startsWith("*.") ? s.substring(1) : s)
                        .forEach(proxyBuilder::addNoProxy);
            }

            if (proxyPort != null) {
                proxyBuilder.port(Integer.parseInt(proxyPort));
            }
            return Optional.ofNullable(proxyBuilder.build());
        }
        return Optional.empty();
    }

Steps to reproduce

I've created the following test classe in project generated by the helidon-quickstart-mp archetype to highlight the issue:

package io.helidon.examples.quickstart.mp;

import io.helidon.webclient.Proxy;

import java.lang.reflect.Field;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
public class SystemProxyTest {
    @Test
    void staticMethodCreationTest() throws NoSuchFieldException, IllegalAccessException {
        // The javadoc says:
        // Create from environment and system properties.
        // Returns: a proxy instance configured based on this system settings
        Proxy proxy = Proxy.create();
        Field field = proxy.getClass().getDeclaredField("type");
        field.setAccessible(true);

        assertEquals(Proxy.ProxyType.SYSTEM, field.get(proxy));
    }

    @Test
    void builderCreationTest() throws NoSuchFieldException, IllegalAccessException {
        // The javadoc says:
        // Configure proxy from environment variables and system properties.
        // Params:
        // useIt – use system selector
        Proxy proxy = Proxy.builder()
                .useSystemSelector(true)
                .build();
        Field field = proxy.getClass().getDeclaredField("type");
        field.setAccessible(true);

        assertEquals(Proxy.ProxyType.SYSTEM, field.get(proxy));
    }
}

I've added the official javadoc in that code snippet to highlight that the behavior is different than expected.

The culprit seems to be this condition (which still exists in Helidon 3.0):

if ((null == host) || (host.isEmpty() && (null == systemSelector))) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.x Issues for 3.x version branch bug Something isn't working P2 webclient
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants