Skip to content

Commit

Permalink
[MRESOLVER-382] implement optional bind address
Browse files Browse the repository at this point in the history
  • Loading branch information
bmarwell committed Jul 5, 2023
1 parent d01631f commit e4c0869
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -98,6 +101,7 @@
*/
final class HttpTransporter extends AbstractTransporter {

static final String BIND_ADDRESS = "aether.connector.bind.address";
static final String SUPPORT_WEBDAV = "aether.connector.http.supportWebDav";

static final String PREEMPTIVE_PUT_AUTH = "aether.connector.http.preemptivePutAuth";
Expand Down Expand Up @@ -203,6 +207,10 @@ final class HttpTransporter extends AbstractTransporter {
this.preemptivePutAuth = // defaults to true: Wagon does same
ConfigUtils.getBoolean(
session, true, PREEMPTIVE_PUT_AUTH + "." + repository.getId(), PREEMPTIVE_PUT_AUTH);
String bindAddress = ConfigUtils.getString(session, null, BIND_ADDRESS);
InetAddress bindInetAddress = Optional.ofNullable(bindAddress)
.map(this::resolveAddressOrNull)
.orElse(null);
this.supportWebDav = // defaults to false: who needs it will enable it
ConfigUtils.getBoolean(session, false, SUPPORT_WEBDAV + "." + repository.getId(), SUPPORT_WEBDAV);
String credentialEncoding = ConfigUtils.getString(
Expand Down Expand Up @@ -251,6 +259,7 @@ final class HttpTransporter extends AbstractTransporter {
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(connectTimeout)
.setConnectionRequestTimeout(connectTimeout)
.setLocalAddress(bindInetAddress)
.setSocketTimeout(requestTimeout)
.build();

Expand Down Expand Up @@ -295,6 +304,15 @@ final class HttpTransporter extends AbstractTransporter {
this.client = builder.build();
}

private InetAddress resolveAddressOrNull(String hostOrIp) {
try {
return InetAddress.getByName(hostOrIp);
} catch (UnknownHostException uhe) {
LOGGER.warn("Given bind host (" + hostOrIp + ") cannot be resolved. Reverting to default route.");
return null;
}
}

private static HttpHost toHost(Proxy proxy) {
HttpHost host = null;
if (proxy != null) {
Expand Down

0 comments on commit e4c0869

Please sign in to comment.