Skip to content

Make ProxySelector and Authenticator optional in ProxyConfig #745

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

Merged
merged 1 commit into from
Apr 10, 2025
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 @@ -23,7 +23,6 @@
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

import org.eclipse.esmf.aspectmodel.resolver.exceptions.ModelResolutionException;
Expand Down Expand Up @@ -70,8 +69,8 @@ public HttpResponse<byte[]> downloadFileAsResponse( final URL fileUrl, final Map
.version( HttpClient.Version.HTTP_1_1 )
.followRedirects( HttpClient.Redirect.ALWAYS )
.connectTimeout( Duration.ofSeconds( 10 ) );
Optional.ofNullable( proxyConfig.proxy() ).ifPresent( clientBuilder::proxy );
Optional.ofNullable( proxyConfig.authenticator() ).ifPresent( clientBuilder::authenticator );
proxyConfig.proxy().ifPresent( clientBuilder::proxy );
proxyConfig.authenticator().ifPresent( clientBuilder::authenticator );
final HttpClient client = clientBuilder.build();
final String[] headersArray = headers.entrySet().stream()
.flatMap( entry -> Stream.of( entry.getKey(), entry.getValue() ) )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,31 @@
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.soabase.recordbuilder.core.RecordBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Configuration of proxy settings with optional {@link ProxySelector} and {@link Authenticator}
*
* @param proxy the proxy selector
* @param authenticator the authenticator
*/
@RecordBuilder
public record ProxyConfig(
ProxySelector proxy,
Authenticator authenticator
Optional<ProxySelector> proxy,
Optional<Authenticator> authenticator
) {
private static final Logger LOG = LoggerFactory.getLogger( ProxyConfig.class );

public static final ProxyConfig NO_PROXY = new ProxyConfig( null, null );
public static final ProxyConfig NO_PROXY = new ProxyConfig( Optional.empty(), Optional.empty() );

public static ProxyConfig from( final String host, final int port ) {
return new ProxyConfig( ProxySelector.of( new InetSocketAddress( host, port ) ), null );
return new ProxyConfig( Optional.of( ProxySelector.of( new InetSocketAddress( host, port ) ) ), Optional.empty() );
}

public static ProxyConfig detectProxySettings() {
Expand All @@ -54,7 +61,8 @@ public static ProxyConfig detectProxySettings() {
final String host = System.getProperty( "http.proxyHost" );
final String port = System.getProperty( "http.proxyPort" );
if ( host != null && port != null ) {
return new ProxyConfig( ProxySelector.of( new InetSocketAddress( host, Integer.parseInt( port ) ) ), null );
return new ProxyConfig( Optional.of( ProxySelector.of( new InetSocketAddress( host, Integer.parseInt( port ) ) ) ),
Optional.empty() );
}
return NO_PROXY;
}
Expand Down
Loading