Example usage for io.netty.resolver HostsFileParser parse

List of usage examples for io.netty.resolver HostsFileParser parse

Introduction

In this page you can find the example usage for io.netty.resolver HostsFileParser parse.

Prototype

public static HostsFileEntries parse(Reader reader) throws IOException 

Source Link

Document

Parse a reader of hosts file format.

Usage

From source file:io.vertx.core.impl.resolver.DnsResolverProvider.java

License:Open Source License

public DnsResolverProvider(VertxImpl vertx, AddressResolverOptions options) {
    List<String> dnsServers = options.getServers();
    List<InetSocketAddress> serverList = new ArrayList<>();
    if (dnsServers != null && dnsServers.size() > 0) {
        for (String dnsServer : dnsServers) {
            int sep = dnsServer.indexOf(':');
            String ipAddress;/*  ww w.j  a v  a  2 s .  co  m*/
            int port;
            if (sep != -1) {
                ipAddress = dnsServer.substring(0, sep);
                port = Integer.parseInt(dnsServer.substring(sep + 1));
            } else {
                ipAddress = dnsServer;
                port = 53;
            }
            try {
                serverList.add(new InetSocketAddress(
                        InetAddress.getByAddress(NetUtil.createByteArrayFromIpAddressString(ipAddress)), port));
            } catch (UnknownHostException e) {
                throw new VertxException(e);
            }
        }
    } else {
        DnsServerAddressStream stream = DnsServerAddresses.defaultAddresses().stream();
        Set<InetSocketAddress> all = new HashSet<>();
        while (true) {
            InetSocketAddress address = stream.next();
            if (all.contains(address)) {
                break;
            }
            serverList.add(address);
            all.add(address);
        }
    }
    DnsServerAddresses nameServerAddresses = options.isRotateServers()
            ? DnsServerAddresses.rotational(serverList)
            : DnsServerAddresses.sequential(serverList);

    Map<String, InetAddress> entries;
    if (options.getHostsPath() != null) {
        File file = vertx.resolveFile(options.getHostsPath()).getAbsoluteFile();
        try {
            if (!file.exists() || !file.isFile()) {
                throw new IOException();
            }
            entries = HostsFileParser.parse(file);
        } catch (IOException e) {
            throw new VertxException("Cannot read hosts file " + file.getAbsolutePath());
        }
    } else if (options.getHostsValue() != null) {
        try {
            entries = HostsFileParser.parse(new StringReader(options.getHostsValue().toString()));
        } catch (IOException e) {
            throw new VertxException("Cannot read hosts config ", e);
        }
    } else {
        entries = HostsFileParser.parseSilently();
    }

    this.vertx = vertx;
    this.resolverGroup = new AddressResolverGroup<InetSocketAddress>() {

        @Override
        protected io.netty.resolver.AddressResolver<InetSocketAddress> newResolver(EventExecutor executor)
                throws Exception {

            DnsAddressResolverGroup group = new DnsAddressResolverGroup(NioDatagramChannel.class,
                    nameServerAddresses) {
                @Override
                protected NameResolver<InetAddress> newNameResolver(EventLoop eventLoop,
                        ChannelFactory<? extends DatagramChannel> channelFactory,
                        DnsServerAddresses nameServerAddresses) throws Exception {
                    DnsNameResolverBuilder builder = new DnsNameResolverBuilder((EventLoop) executor);
                    builder.hostsFileEntriesResolver(inetHost -> {
                        InetAddress addr = entries.get(inetHost);
                        if (addr == null) {
                            addr = entries.get(inetHost.toLowerCase(Locale.ENGLISH));
                        }
                        return addr;
                    });
                    builder.channelType(NioDatagramChannel.class);
                    builder.nameServerAddresses(nameServerAddresses);
                    builder.optResourceEnabled(options.isOptResourceEnabled());
                    builder.ttl(options.getCacheMinTimeToLive(), options.getCacheMaxTimeToLive());
                    builder.negativeTtl(options.getCacheNegativeTimeToLive());
                    builder.queryTimeoutMillis(options.getQueryTimeout());
                    builder.maxQueriesPerResolve(options.getMaxQueries());
                    builder.recursionDesired(options.getRdFlag());
                    if (options.getSearchDomains() != null) {
                        builder.searchDomains(options.getSearchDomains());
                        int ndots = options.getNdots();
                        if (ndots == -1) {
                            ndots = AddressResolver.DEFAULT_NDOTS_RESOLV_OPTION;
                        }
                        builder.ndots(ndots);
                    }
                    return builder.build();
                }
            };

            io.netty.resolver.AddressResolver<InetSocketAddress> resolver = group.getResolver(executor);
            resolvers.add(new ResolverRegistration(resolver, (EventLoop) executor));

            return resolver;
        }
    };
}