diff --git a/README.md b/README.md index c93eba39..dd18f20f 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,10 @@ Next, point MCS to that trust store like so mcs -Djavax.net.ssl.trustStore=/path/to/keystore search something ``` +### Usage Behind a Proxy + +If you are running behind a proxy, MCS will respect the `HTTP_PROXY` and `HTTPS_PROXY` environment variables. + ## Configuring MCS Some configuration for MCS is passed through system properties. You can do this every time you invoke MCS by adding `-Dxxx=yyy`. @@ -104,4 +108,4 @@ If you have a new idea, feel free to bring it up using the [discussions](https:/ * [Andres Almiray](https://andresalmiray.com/) did a great job helping me set up [JReleaser](https://jreleaser.org/) to make releasing **mcs** a real breeze! * [Martin Goldhahn](https://github.com/maddingo) shared the idea of searching on class name, and provided some initial API and design ideas for this great feature. * [Willem van Lent](https://github.com/Ocirina) contributed a fix for a parameter whose name didn't match it's behavior. -* [Hanno Embregts](https://hanno.codes/) contributed a fix that makes it more clear why the results are truncated. \ No newline at end of file +* [Hanno Embregts](https://hanno.codes/) contributed a fix that makes it more clear why the results are truncated. diff --git a/pom.xml b/pom.xml index d41473c7..e1fe78dd 100644 --- a/pom.xml +++ b/pom.xml @@ -116,6 +116,7 @@ + --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED stryker-dashboard @@ -177,6 +178,13 @@ + + + + --add-opens java.base/java.util=ALL-UNNAMED + --add-opens java.base/java.lang=ALL-UNNAMED + + diff --git a/src/main/java/it/mulders/mcs/App.java b/src/main/java/it/mulders/mcs/App.java index e835d944..b47e390c 100644 --- a/src/main/java/it/mulders/mcs/App.java +++ b/src/main/java/it/mulders/mcs/App.java @@ -1,5 +1,8 @@ package it.mulders.mcs; +import java.net.URI; +import java.net.URISyntaxException; + import it.mulders.mcs.cli.Cli; import it.mulders.mcs.cli.CommandClassFactory; import it.mulders.mcs.cli.SystemPropertyLoader; @@ -18,6 +21,7 @@ static int doMain(final String... originalArgs) { static int doMain(final Cli cli, final SystemPropertyLoader systemPropertyLoader, final String... originalArgs) { System.setProperties(systemPropertyLoader.getProperties()); + setUpProxy(); var program = new CommandLine(cli, new CommandClassFactory(cli)) .setExecutionExceptionHandler(new McsExecutionExceptionHandler()); @@ -28,6 +32,28 @@ static int doMain(final Cli cli, final SystemPropertyLoader systemPropertyLoader return program.execute(args); } + private static void setUpProxy() { + var httpProxy = System.getenv( "HTTP_PROXY" ); + var httpsProxy = System.getenv( "HTTPS_PROXY" ); + + try { + if ( httpProxy != null && !httpProxy.isEmpty() ) { + final URI uri = new URI( httpProxy ); + + System.setProperty( "http.proxyHost", uri.getHost() ); + System.setProperty( "http.proxyPort", Integer.toString( uri.getPort() ) ); + } + + if ( httpsProxy != null && !httpsProxy.isEmpty() ) { + final URI uri = new URI( httpsProxy ); + System.setProperty( "https.proxyHost", uri.getHost() ); + System.setProperty( "https.proxyPort", Integer.toString( uri.getPort() ) ); + } + } catch ( URISyntaxException e ) { + System.err.println("Error while setting up proxy from environment: HTTP_PROXY=[%s], HTTPS_PROXY=[%s]".formatted( httpProxy, httpsProxy ) ); + } + } + static boolean isInvocationWithoutSearchCommand(CommandLine program, String... args) { try { program.parseArgs(args); diff --git a/src/test/java/it/mulders/mcs/AppIT.java b/src/test/java/it/mulders/mcs/AppIT.java index 30159cdd..c5b63846 100644 --- a/src/test/java/it/mulders/mcs/AppIT.java +++ b/src/test/java/it/mulders/mcs/AppIT.java @@ -3,16 +3,20 @@ import it.mulders.mcs.cli.Cli; import it.mulders.mcs.cli.SystemPropertyLoader; import org.assertj.core.api.WithAssertions; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.Test; +import java.util.List; import java.util.Properties; import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOut; +import static com.github.stefanbirkner.systemlambda.SystemLambda.withEnvironmentVariable; +import static java.util.Arrays.asList; @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) -class AppIT implements WithAssertions { +class AppIT implements WithAssertions { private final Cli command = new Cli() { @Override public SearchCommand createSearchCommand() { @@ -32,6 +36,15 @@ public Integer call() { }; } }; + + @BeforeEach + void clearProxyProperties() { + System.clearProperty("http.proxyHost"); + System.clearProperty("http.proxyPort"); + System.clearProperty("https.proxyHost"); + System.clearProperty("https.proxyPort"); + } + @Test void should_show_version() throws Exception { var output = tapSystemOut(() -> App.doMain("-V")); @@ -68,4 +81,36 @@ public Properties getProperties() { assertThat(System.getProperty("example")).isEqualTo("value"); } -} \ No newline at end of file + + @Test + void should_not_set_proxy_system_properties_when_no_env_variable_is_present() throws Exception { + List values = withEnvironmentVariable("HTTP_PROXY", null) + .and("HTTPS_PROXY", null) + .execute(() -> { + App.doMain(command, new SystemPropertyLoader(), "info.picocli:picocli"); + + return asList(System.getProperty("http.proxyHost"), + System.getProperty("http.proxyPort"), + System.getProperty("https.proxyHost"), + System.getProperty("https.proxyPort")); + }); + + assertThat(values).isEqualTo(asList(null, null, null, null)); + } + + @Test + void should_set_proxy_system_properties_when_env_variables_are_present() throws Exception { + List values = withEnvironmentVariable("HTTP_PROXY", "http://http.proxy.example.com:8080") + .and("HTTPS_PROXY", "http://https.proxy.example.com:8484") + .execute(() -> { + App.doMain(command, new SystemPropertyLoader(), "info.picocli:picocli"); + + return asList(System.getProperty("http.proxyHost"), + System.getProperty("http.proxyPort"), + System.getProperty("https.proxyHost"), + System.getProperty("https.proxyPort")); + }); + + assertThat(values).isEqualTo(asList("http.proxy.example.com", "8080", "https.proxy.example.com", "8484")); + } +}