1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.transport.jdk;
20
21 import java.net.InetAddress;
22 import java.net.UnknownHostException;
23 import java.net.http.HttpClient;
24
25 import org.eclipse.aether.ConfigurationProperties;
26 import org.eclipse.aether.RepositorySystemSession;
27 import org.eclipse.aether.repository.RemoteRepository;
28 import org.eclipse.aether.util.ConfigUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32
33
34
35
36
37 final class JdkHttpTransporterCustomizer {
38 private static final Logger LOGGER = LoggerFactory.getLogger(JdkHttpTransporterCustomizer.class);
39
40 private JdkHttpTransporterCustomizer() {}
41
42 static void customizeBuilder(
43 RepositorySystemSession session, RemoteRepository repository, HttpClient.Builder builder) {
44 InetAddress localAddress = getHttpLocalAddress(session, repository);
45 if (localAddress != null) {
46 builder.localAddress(localAddress);
47 }
48 }
49
50 static void customizeHttpClient(RepositorySystemSession session, RemoteRepository repository, HttpClient client) {
51 if (!session.addOnSessionEndedHandler(client::close)) {
52 LOGGER.warn("Using Resolver 2 feature without Resolver 2 session handling, you may leak resources.");
53 }
54 }
55
56
57
58
59 private static InetAddress getHttpLocalAddress(RepositorySystemSession session, RemoteRepository repository) {
60 String bindAddress = ConfigUtils.getString(
61 session,
62 null,
63 ConfigurationProperties.HTTP_LOCAL_ADDRESS + "." + repository.getId(),
64 ConfigurationProperties.HTTP_LOCAL_ADDRESS);
65 if (bindAddress == null) {
66 return null;
67 }
68 try {
69 return InetAddress.getByName(bindAddress);
70 } catch (UnknownHostException uhe) {
71 throw new IllegalArgumentException(
72 "Given bind address (" + bindAddress + ") cannot be resolved for remote repository " + repository,
73 uhe);
74 }
75 }
76 }