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  
30  /**
31   * JDK Transport customizer.
32   *
33   * @since TBD
34   */
35  final class JdkHttpTransporterCustomizer {
36      private JdkHttpTransporterCustomizer() {}
37  
38      static void customizeBuilder(
39              RepositorySystemSession session, RemoteRepository repository, HttpClient.Builder builder) {
40          InetAddress localAddress = getHttpLocalAddress(session, repository);
41          if (localAddress != null) {
42              builder.localAddress(localAddress);
43          }
44      }
45  
46      static void customizeHttpClient(RepositorySystemSession session, RemoteRepository repository, HttpClient client) {}
47  
48      /**
49       * Returns non-null {@link InetAddress} if set in configuration, {@code null} otherwise.
50       */
51      private static InetAddress getHttpLocalAddress(RepositorySystemSession session, RemoteRepository repository) {
52          String bindAddress = ConfigUtils.getString(
53                  session,
54                  null,
55                  ConfigurationProperties.HTTP_LOCAL_ADDRESS + "." + repository.getId(),
56                  ConfigurationProperties.HTTP_LOCAL_ADDRESS);
57          if (bindAddress == null) {
58              return null;
59          }
60          try {
61              return InetAddress.getByName(bindAddress);
62          } catch (UnknownHostException uhe) {
63              throw new IllegalArgumentException(
64                      "Given bind address (" + bindAddress + ") cannot be resolved for remote repository " + repository,
65                      uhe);
66          }
67      }
68  }