View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.http.impl;
28  
29  import java.net.InetAddress;
30  import java.net.UnknownHostException;
31  import java.util.Arrays;
32  import java.util.Map;
33  import java.util.concurrent.ConcurrentHashMap;
34  
35  import org.apache.hc.client5.http.DnsResolver;
36  import org.apache.hc.core5.annotation.Contract;
37  import org.apache.hc.core5.annotation.ThreadingBehavior;
38  import org.apache.hc.core5.util.Args;
39  import org.slf4j.Logger;
40  import org.slf4j.LoggerFactory;
41  
42  /**
43   * In-memory {@link DnsResolver} implementation.
44   *
45   * @since 4.2
46   */
47  @Contract(threading = ThreadingBehavior.STATELESS)
48  public class InMemoryDnsResolver implements DnsResolver {
49  
50      /** Logger associated to this class. */
51      private static final Logger LOG = LoggerFactory.getLogger(InMemoryDnsResolver.class);
52  
53      /**
54       * In-memory collection that will hold the associations between a host name
55       * and an array of InetAddress instances.
56       */
57      private final Map<String, InetAddress[]> dnsMap;
58  
59      /**
60       * Builds a DNS resolver that will resolve the host names against a
61       * collection held in-memory.
62       */
63      public InMemoryDnsResolver() {
64          dnsMap = new ConcurrentHashMap<>();
65      }
66  
67      /**
68       * Associates the given array of IP addresses to the given host in this DNS overrider.
69       * The IP addresses are assumed to be already resolved.
70       *
71       * @param host
72       *            The host name to be associated with the given IP.
73       * @param ips
74       *            array of IP addresses to be resolved by this DNS overrider to the given
75       *            host name.
76       */
77      public void add(final String host, final InetAddress... ips) {
78          Args.notNull(host, "Host name");
79          Args.notNull(ips, "Array of IP addresses");
80          dnsMap.put(host, ips);
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public InetAddress[] resolve(final String host) throws UnknownHostException {
88          final InetAddress[] resolvedAddresses = dnsMap.get(host);
89          if (LOG.isInfoEnabled()) {
90              LOG.info("Resolving {} to {}", host, Arrays.deepToString(resolvedAddresses));
91          }
92          if(resolvedAddresses == null){
93              throw new UnknownHostException(host + " cannot be resolved");
94          }
95          return resolvedAddresses;
96      }
97  
98      @Override
99      public String resolveCanonicalHostname(final String host) throws UnknownHostException {
100         final InetAddress[] resolvedAddresses = resolve(host);
101         if (resolvedAddresses.length > 0) {
102             return resolvedAddresses[0].getCanonicalHostName();
103         }
104         return host;
105     }
106 }