Java Code Examples for org.xbill.DNS.Lookup#setCache()

The following examples show how to use org.xbill.DNS.Lookup#setCache() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GoogleResolverCheck.java    From entrada with GNU General Public License v3.0 7 votes vote down vote up
@Override
protected List<String> fetch() {

  try {
    Resolver resolver = new SimpleResolver();
    // dns resolvers may take a long time to return a response.
    resolver.setTimeout(timeout);
    Lookup l =
        new Lookup(StringUtils.endsWith(hostname, ".") ? hostname : hostname + ".", Type.TXT);
    // always make sure the cache is empty
    l.setCache(new Cache());
    l.setResolver(resolver);
    Record[] records = l.run();
    if (records != null && records.length > 0) {
      return parse(records[0]);
    }
  } catch (Exception e) {
    log.error("Problem while adding Google resolvers, continue without", e);
  }

  log.error("No Google resolver addresses found");
  return Collections.emptyList();
}
 
Example 2
Source File: Helperfunctions.java    From open-rmbt with Apache License 2.0 6 votes vote down vote up
public static String reverseDNSLookup(final InetAddress adr)
{
    try
    {
        final Name name = ReverseMap.fromAddress(adr);
        
        final Lookup lookup = new Lookup(name, Type.PTR);
        lookup.setResolver(new SimpleResolver());
        lookup.setCache(null);
        final Record[] records = lookup.run();
        if (lookup.getResult() == Lookup.SUCCESSFUL)
            for (final Record record : records)
                if (record instanceof PTRRecord)
                {
                    final PTRRecord ptr = (PTRRecord) record;
                    return ptr.getTarget().toString();
                }
    }
    catch (final Exception e)
    {
    }
    return null;
}
 
Example 3
Source File: DNSJavaService.java    From james-project with Apache License 2.0 5 votes vote down vote up
/**
 * Looks up DNS records of the specified type for the specified name.
 * <p/>
 * This method is a public wrapper for the private implementation method
 *
 * @param namestr  the name of the host to be looked up
 * @param type     the type of record desired
 * @param typeDesc the description of the record type, for debugging purpose
 */
protected Record[] lookup(String namestr, int type, String typeDesc) throws TemporaryResolutionException {
    // Name name = null;
    try {
        // name = Name.fromString(namestr, Name.root);
        Lookup l = new Lookup(namestr, type);

        l.setCache(cache);
        l.setResolver(resolver);
        l.setCredibility(dnsCredibility);
        l.setSearchPath(searchPaths);
        Record[] r = l.run();

        try {
            if (l.getResult() == Lookup.TRY_AGAIN) {
                throw new TemporaryResolutionException("DNSService is temporary not reachable");
            } else {
                return r;
            }
        } catch (IllegalStateException ise) {
            // This is okay, because it mimics the original behaviour
            // TODO find out if it's a bug in DNSJava
            LOGGER.warn("Error determining result ", ise);
            throw new TemporaryResolutionException("DNSService is temporary not reachable");
        }

        // return rawDNSLookup(name, false, type, typeDesc);
    } catch (TextParseException tpe) {
        // TODO: Figure out how to handle this correctly.
        LOGGER.error("Couldn't parse name {}", namestr, tpe);
        return null;
    }
}
 
Example 4
Source File: Helperfunctions.java    From open-rmbt with Apache License 2.0 5 votes vote down vote up
public static Long getASN(final InetAddress adr)
{
    try
    {
        final Name postfix;
        if (adr instanceof Inet6Address)
            postfix = Name.fromConstantString("origin6.asn.cymru.com");
        else
            postfix = Name.fromConstantString("origin.asn.cymru.com");
        
        final Name name = getReverseIPName(adr, postfix);
        System.out.println("lookup: " + name);
        
        final Lookup lookup = new Lookup(name, Type.TXT);
        lookup.setResolver(new SimpleResolver());
        lookup.setCache(null);
        final Record[] records = lookup.run();
        if (lookup.getResult() == Lookup.SUCCESSFUL)
            for (final Record record : records)
                if (record instanceof TXTRecord)
                {
                    final TXTRecord txt = (TXTRecord) record;
                    @SuppressWarnings("unchecked")
                    final List<String> strings = txt.getStrings();
                    if (strings != null && !strings.isEmpty())
                    {
                        final String result = strings.get(0);
                        final String[] parts = result.split(" ?\\| ?");
                        if (parts != null && parts.length >= 1)
                            return new Long(parts[0]);
                    }
                }
    }
    catch (final Exception e)
    {
    }
    return null;
}
 
Example 5
Source File: Helperfunctions.java    From open-rmbt with Apache License 2.0 5 votes vote down vote up
public static String getASName(final long asn)
{
    try
    {
        final Name postfix = Name.fromConstantString("asn.cymru.com.");
        final Name name = new Name(String.format("AS%d", asn), postfix);
        System.out.println("lookup: " + name);
        
        final Lookup lookup = new Lookup(name, Type.TXT);
        lookup.setResolver(new SimpleResolver());
        lookup.setCache(null);
        final Record[] records = lookup.run();
        if (lookup.getResult() == Lookup.SUCCESSFUL)
            for (final Record record : records)
                if (record instanceof TXTRecord)
                {
                    final TXTRecord txt = (TXTRecord) record;
                    @SuppressWarnings("unchecked")
                    final List<String> strings = txt.getStrings();
                    if (strings != null && !strings.isEmpty())
                    {
                        System.out.println(strings);
                        
                        final String result = strings.get(0);
                        final String[] parts = result.split(" ?\\| ?");
                        if (parts != null && parts.length >= 1)
                            return parts[4];
                    }
                }
    }
    catch (final Exception e)
    {
    }
    return null;
}
 
Example 6
Source File: Helperfunctions.java    From open-rmbt with Apache License 2.0 5 votes vote down vote up
public static String getAScountry(final long asn)
{
    try
    {
        final Name postfix = Name.fromConstantString("asn.cymru.com.");
        final Name name = new Name(String.format("AS%d", asn), postfix);
        System.out.println("lookup: " + name);
        
        final Lookup lookup = new Lookup(name, Type.TXT);
        lookup.setResolver(new SimpleResolver());
        lookup.setCache(null);
        final Record[] records = lookup.run();
        if (lookup.getResult() == Lookup.SUCCESSFUL)
            for (final Record record : records)
                if (record instanceof TXTRecord)
                {
                    final TXTRecord txt = (TXTRecord) record;
                    @SuppressWarnings("unchecked")
                    final List<String> strings = txt.getStrings();
                    if (strings != null && !strings.isEmpty())
                    {
                        final String result = strings.get(0);
                        final String[] parts = result.split(" ?\\| ?");
                        if (parts != null && parts.length >= 1)
                            return parts[1];
                    }
                }
    }
    catch (final Exception e)
    {
    }
    return null;
}