1
2
3
4
5
6
7
8
9
10
11
12
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
43
44
45
46
47 public class DNS implements Extension, IEventListener {
48 private static final Logger LOG = LoggerFactory.getLogger(DNS.class);
49
50
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 }