View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * JDK Transport customizer.
34   *
35   * @since TBD
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       * Returns non-null {@link InetAddress} if set in configuration, {@code null} otherwise.
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  }