View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   *
14   */
15  package org.esigate.extension.http;
16  
17  import java.net.InetAddress;
18  import java.net.UnknownHostException;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.List;
22  import java.util.Properties;
23  
24  import org.apache.http.HttpHost;
25  import org.apache.http.conn.DnsResolver;
26  import org.apache.http.impl.client.HttpClientBuilder;
27  import org.esigate.Driver;
28  import org.esigate.Parameters;
29  import org.esigate.events.Event;
30  import org.esigate.events.EventDefinition;
31  import org.esigate.events.EventManager;
32  import org.esigate.events.IEventListener;
33  import org.esigate.events.impl.HttpClientBuilderEvent;
34  import org.esigate.extension.Extension;
35  import org.esigate.util.Parameter;
36  import org.esigate.util.ParameterCollection;
37  import org.esigate.util.UriUtils;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  
41  /**
42   * DNS extension that allow to associates IP addresses to the given host in a DNS overrider. The IP addresses are
43   * assumed to be already resolved.
44   * 
45   * @author Alexis Thaveau on 26/01/16.
46   */
47  public class DNS implements Extension, IEventListener {
48      private static final Logger LOG = LoggerFactory.getLogger(DNS.class);
49  
50      // Core parameters
51      public static final Parameter<Collection<String>> REMOTE_IP = new ParameterCollection("remoteIP");
52  
53      private DnsResolver dnsResolver;
54  
55      public DnsResolver getDnsResolver() {
56          return dnsResolver;
57      }
58  
59      @Override
60      public void init(Driver driver, Properties properties) {
61          Collection<String> ips = REMOTE_IP.getValue(properties);
62  
63          if (ips.isEmpty()) {
64              LOG.error("Missing configuration properties for driver {}. Property {}", driver.getConfiguration()
65                      .getInstanceName(), REMOTE_IP.getName());
66          } else {
67              String[] remoteURLS = Parameters.REMOTE_URL_BASE.getValue(properties);
68              List<InetAddress> inetAddresses = new ArrayList<>();
69              CustomizableDNSResolver customizableDNSResolver = new CustomizableDNSResolver();
70              dnsResolver = customizableDNSResolver;
71              for (String ip : ips) {
72                  try {
73                      inetAddresses.add(InetAddress.getByName(ip));
74                  } catch (UnknownHostException e) {
75                      LOG.error("Unable to resolve InetAddress [{}]", ip, e);
76                  }
77              }
78  
79              for (String remoteURL : remoteURLS) {
80  
81                  HttpHost host = UriUtils.extractHost(remoteURL);
82                  customizableDNSResolver.add(host.getHostName(), inetAddresses.toArray(new InetAddress[] {}));
83              }
84  
85              driver.getEventManager().register(EventManager.EVENT_HTTP_BUILDER_INITIALIZATION, this);
86          }
87      }
88  
89      @Override
90      public boolean event(EventDefinition id, Event event) {
91          HttpClientBuilderEvent httpClientBuilderEvent = (HttpClientBuilderEvent) event;
92          HttpClientBuilder httpClientBuilder = httpClientBuilderEvent.getHttpClientBuilder();
93          httpClientBuilder.setDnsResolver(dnsResolver);
94          return false;
95      }
96  }