Example usage for com.google.common.net InternetDomainName from

List of usage examples for com.google.common.net InternetDomainName from

Introduction

In this page you can find the example usage for com.google.common.net InternetDomainName from.

Prototype

public static InternetDomainName from(String domain) 

Source Link

Document

Returns an instance of InternetDomainName after lenient validation.

Usage

From source file:google.registry.flows.domain.DomainInfoFlow.java

private ImmutableList<ResponseExtension> getDomainResponseExtensions(DomainResource domain, DateTime now)
        throws EppException {
    ImmutableList.Builder<ResponseExtension> extensions = new ImmutableList.Builder<>();
    addSecDnsExtensionIfPresent(extensions, domain.getDsData());
    ImmutableSet<GracePeriodStatus> gracePeriodStatuses = domain.getGracePeriodStatuses();
    if (!gracePeriodStatuses.isEmpty()) {
        extensions.add(RgpInfoExtension.create(gracePeriodStatuses));
    }/* ww w. j a  v a 2 s  . co  m*/
    FeeInfoCommandExtensionV06 feeInfo = eppInput.getSingleExtension(FeeInfoCommandExtensionV06.class);
    if (feeInfo != null) { // Fee check was requested.
        FeeInfoResponseExtensionV06.Builder builder = new FeeInfoResponseExtensionV06.Builder();
        handleFeeRequest(feeInfo, builder, InternetDomainName.from(targetId), null, now, pricingLogic);
        extensions.add(builder.build());
    }
    return extensions.build();
}

From source file:google.registry.flows.domain.DomainFlowUtils.java

/**
 * Returns parsed version of {@code name} if domain name label follows our naming rules and is
 * under one of the given allowed TLDs.//from  w ww  .j a v a  2s.  c om
 *
 * <p><b>Note:</b> This method does not perform language validation with IDN tables.
 *
 * @see #validateDomainNameWithIdnTables(InternetDomainName)
 */
static InternetDomainName validateDomainName(String name) throws EppException {
    if (!ALLOWED_CHARS.matchesAllOf(name)) {
        throw new BadDomainNameCharacterException();
    }
    List<String> parts = Splitter.on('.').splitToList(name);
    if (parts.size() <= 1) {
        throw new BadDomainNamePartsCountException();
    }
    if (any(parts, equalTo(""))) {
        throw new EmptyDomainNamePartException();
    }
    validateFirstLabel(parts.get(0));
    InternetDomainName domainName = InternetDomainName.from(name);
    Optional<InternetDomainName> tldParsed = findTldForName(domainName);
    if (!tldParsed.isPresent()) {
        throw new TldDoesNotExistException(domainName.parent().toString());
    }
    if (domainName.parts().size() != tldParsed.get().parts().size() + 1) {
        throw new BadDomainNamePartsCountException();
    }
    return domainName;
}

From source file:com.crosstreelabs.cognitio.conductor.Conductor.java

protected CatalogueEntry createChild(final CatalogueEntry of, final String link) {
    CatalogueEntry child = new CatalogueEntry();

    URL url;//from  www.j  a  va 2 s.  c o m
    try {
        url = URL.parse(link);
    } catch (GalimatiasParseException ex) {
        return null;
    }

    child.location = link;
    child.host = new Host();
    child.host.host = InternetDomainName.from(url.host().toString()).topPrivateDomain().toString();
    child.path = url.path();
    child.initialParent = of.id;
    child.initialDepth = of.initialDepth + 1;
    child.firstSeen = new DateTime();
    return child;
}

From source file:google.registry.dns.writer.clouddns.CloudDnsWriter.java

/**
 * Publish A/AAAA records to Cloud DNS./*from  w w w  .  j a  v  a  2s  .  c  om*/
 *
 * <p>Cloud DNS has no API for glue -- A/AAAA records are automatically matched to their
 * corresponding NS records to serve glue.
 */
@Override
public void publishHost(String hostName) {
    // Get the superordinate domain name of the host.
    InternetDomainName host = InternetDomainName.from(hostName);
    Optional<InternetDomainName> tld = Registries.findTldForName(host);

    // Host not managed by our registry, no need to update DNS.
    if (!tld.isPresent()) {
        logger.severefmt("publishHost called for invalid host %s", hostName);
        return;
    }

    // Extract the superordinate domain name. The TLD and host may have several dots so this
    // must calculate a sublist.
    ImmutableList<String> hostParts = host.parts();
    ImmutableList<String> tldParts = tld.get().parts();
    ImmutableList<String> domainParts = hostParts.subList(hostParts.size() - tldParts.size() - 1,
            hostParts.size());
    String domain = Joiner.on(".").join(domainParts);

    // Refresh the superordinate domain, since we shouldn't be publishing glue records if we are not
    // authoritative for the superordinate domain.
    publishDomain(domain);
}

From source file:edu.wisc.ssec.mcidasv.data.PolarOrbitTrackDataSource.java

/**
 * Create a nice looking name for this instance.
 * /*from  w w w  . j a va2  s .  co m*/
 * <p>Given a URL like
 * {@code http://celestrak.com/NORAD/elements/weather.txt}, this method
 * will return {@code celestrak: /NORAD/elements/weather.txt}.</p>
 * 
 * <p>If the hostname from {@code urlStr} could not be sufficiently reduced,
 * this method will simply use the entire hostname. A URL like
 * {@code http://adde.ssec.wisc.edu/weather.txt} will return
 * {@code adde.ssec.wisc.edu: weather.txt}.</p>
 * 
 * <p>If there was a problem parsing {@code urlStr}, the method will try
 * to return the filename. A URL like 
 * {@code http://celestrak.com/NORAD/elements/weather.txt} would return 
 * {@code weather.txt}.</p>
 * 
 * <p>If all of the above fails, {@code urlStr} will be returned.</p>
 * 
 * @param urlStr URL of the TLE information. Cannot be {@code null}.
 * 
 * @return Either the name as described above, or {@code null} if there was
 *         a problem.
 */
public static String makeNameForRemoteSource(String urlStr) {
    Objects.requireNonNull(urlStr, "Cannot use a null URL string");
    String result;
    try {
        URL url = new URL(urlStr);
        String host = url.getHost();
        String path = url.getPath();

        // thank you, guava!
        InternetDomainName domain = InternetDomainName.from(host);

        // suffix will be something like 'com' or 'co.uk', so suffixStart
        // needs to start one character earlier to remove the trailing '.'
        String suffix = domain.publicSuffix().toString();
        int suffixStart = host.indexOf(suffix) - 1;
        String trimmed = host.substring(0, suffixStart);

        // Trying this with 'http://adde.ssec.wisc.edu/weather.txt' will
        // result in trimmed being 'adde.ssec.wisc', and I imagine there
        // are more edge cases. With that in mind, we just use the hostname
        // if it looks like trimmed doesn't look nice
        if (trimmed.indexOf('.') > -1) {
            result = host + ": " + path;
        } else {
            result = trimmed + ": " + path;
        }
    } catch (IllegalArgumentException e) {
        // InternetDomainName.from() call likely failed; simply return
        // original URL string as specified by the javadoc!
        result = urlStr;
        logger.warn("Problem with URL '" + urlStr + '\'', e);
    } catch (MalformedURLException e) {
        logger.error("Bad URL", e);
        int lastSlash = urlStr.lastIndexOf('/');
        if (lastSlash > -1) {
            // need the "+1" to get rid of the slash
            result = urlStr.substring(lastSlash + 1);
        } else {
            result = urlStr;
        }
    }
    return result;
}

From source file:com.qwazr.crawler.web.manager.WebCrawlThread.java

private boolean matchesInitialDomain(URI uri) {
    String host = uri.getHost();//w w  w . j  a v  a2 s .  co  m
    if (StringUtils.isEmpty(host))
        return false;
    if (!InternetDomainName.isValid(host))
        return false;
    return internetDomainName.equals(InternetDomainName.from(host));
}

From source file:google.registry.flows.domain.DomainRestoreRequestFlow.java

private void verifyRestoreAllowed(Update command, DomainResource existingDomain,
        FeeUpdateCommandExtension feeUpdate, FeesAndCredits feesAndCredits, DateTime now) throws EppException {
    verifyOptionalAuthInfo(authInfo, existingDomain);
    if (!isSuperuser) {
        verifyResourceOwnership(clientId, existingDomain);
        verifyNotReserved(InternetDomainName.from(targetId), false);
        verifyPremiumNameIsNotBlocked(targetId, now, clientId);
    }//from  ww w .  j a  v  a  2s  . c  o  m
    // No other changes can be specified on a restore request.
    if (!command.noChangesPresent()) {
        throw new RestoreCommandIncludesChangesException();
    }
    // Domain must be within the redemptionPeriod to be eligible for restore.
    if (!existingDomain.getGracePeriodStatuses().contains(GracePeriodStatus.REDEMPTION)) {
        throw new DomainNotEligibleForRestoreException();
    }
    checkAllowedAccessToTld(clientId, existingDomain.getTld());
    validateFeeChallenge(targetId, existingDomain.getTld(), now, feeUpdate, feesAndCredits);
}

From source file:google.registry.dns.writer.dnsupdate.DnsUpdateWriter.java

private String findTldFromName(String name) {
    return Registries.findTldForNameOrThrow(InternetDomainName.from(name)).toString();
}

From source file:com.addthis.hydra.data.filter.bundle.BundleFilterURL.java

@Override
public boolean filter(Bundle bundle) {
    String pv = ValueUtil.asNativeString(field.getValue(bundle));
    if (!asFile) {
        if (pv == null || pv.length() < 7) {
            return invalidExit;
        }//  w  w w.ja v a 2  s.co  m
        String lpv = pv.trim().toLowerCase();
        if (!(lpv.startsWith("http"))) {
            if (fixProto) {
                if (clean && lpv.indexOf("%2f") >= 0) {
                    pv = LessBytes.urldecode(pv);
                }
                pv = "http://".concat(pv);
            } else {
                return invalidExit;
            }
        }
        if (clean && (lpv.startsWith("http%") || lpv.startsWith("https%"))) {
            pv = LessBytes.urldecode(pv);
        }
    }
    // up to two 'decoding' passes on the url to try and find a valid one
    for (int i = 0; i < 2; i++) {
        if (pv == null) {
            return invalidExit;
        }
        try {
            URL urec = asFile ? new URL("file://".concat(pv)) : new URL(pv);
            String urlhost = urec.getHost();
            String returnhost = null;
            if (resolveIP) {
                synchronized (iphost) {
                    returnhost = iphost.get(urlhost).toLowerCase();
                    if (returnhost == null) {
                        returnhost = resolveDottedIP(urlhost);
                        iphost.put(urlhost, returnhost);
                        if (iphost.size() > maxhostcache) {
                            iphost.removeEldest();
                        }
                    }
                }
            } else {
                returnhost = urlhost.toLowerCase();
            }
            // store cleaned up (url decoded) version back to packet
            if (clean) {
                if (urec != null && urec.getPath().isEmpty()) {
                    // if the path element is null, append the slash
                    pv = pv.concat("/");
                }
                field.setValue(bundle, ValueFactory.create(pv));
            }
            if (setHost != null) {
                if (toBaseDomain) {
                    returnhost = NetUtil.getBaseDomain(returnhost);
                } else if (toTopPrivateDomain) {
                    if (returnhost != null && InternetDomainName.isValid(returnhost)) {
                        InternetDomainName domain = InternetDomainName.from(returnhost);
                        if (domain.hasPublicSuffix() && domain.isUnderPublicSuffix()) {
                            InternetDomainName topPrivateDomain = domain.topPrivateDomain();
                            returnhost = topPrivateDomain.toString();
                        }
                    }
                }
                setHost.setValue(bundle, ValueFactory.create(returnhost));
            }
            if (setPath != null) {
                setPath.setValue(bundle, ValueFactory.create(urec.getPath()));
            }
            if (setParams != null) {
                setParams.setValue(bundle, ValueFactory.create(urec.getQuery()));
            }
            if (setAnchor != null) {
                setAnchor.setValue(bundle, ValueFactory.create(urec.getRef()));
            }
            if (setHostNormal != null) {
                Matcher m = hostNormalPattern.matcher(returnhost);
                if (m.find()) {
                    returnhost = m.group(1);
                }
                setHostNormal.setValue(bundle, ValueFactory.create(returnhost));
            }
            if (setTopPrivateDomain != null) {
                String topDomain = returnhost;
                if (InternetDomainName.isValid(returnhost)) {
                    InternetDomainName domainName = InternetDomainName.from(returnhost);
                    if (domainName.isTopPrivateDomain() || domainName.isUnderPublicSuffix()) {
                        topDomain = DOT_JOINER.join(domainName.topPrivateDomain().parts());
                    }
                }
                setTopPrivateDomain.setValue(bundle, ValueFactory.create(topDomain));
            }
        } catch (MalformedURLException e) {
            if (pv.indexOf("%3") > 0 && pv.indexOf("%2") > 0) {
                pv = LessBytes.urldecode(pv);
            } else {
                if (debugMalformed) {
                    System.err.println("malformed(" + i + ") " + pv);
                }
                return invalidExit;
            }
        }
    }
    return true;
}

From source file:com.crosstreelabs.cognitio.Main.java

private void loadSeeds() throws IOException {
    Collection<File> seedFiles = new ArrayList<>();
    if (mc.conductorSeedFiles != null && !mc.conductorSeedFiles.isEmpty()) {
        seedFiles = mc.conductorSeedFiles;
    } else {//from  w  ww.jav a 2  s  .  com
        seedFiles.add(new File(mc.getHomeDirectory(), "seeds.txt"));
    }
    if (seedFiles.isEmpty()) {
        return;
    }

    final HostService hostService = injector.get(HostService.class);
    final CatalogueService catalogueService = injector.get(CatalogueService.class);

    for (File seedFile : seedFiles) {
        if (!seedFile.exists()) {
            LOGGER.warn("Seed file {} does not exist", seedFile);
            continue;
        }

        Iterator<String> it = FileUtils.lineIterator(seedFile);
        while (it.hasNext()) {
            String line = it.next();
            if (line == null || line.trim().isEmpty() || line.trim().startsWith("#")) {
                continue;
            }

            line = line.trim();

            io.mola.galimatias.URL url;
            try {
                url = io.mola.galimatias.URL.parse(line);
            } catch (GalimatiasParseException ex) {
                continue;
            }
            String tld = InternetDomainName.from(url.host().toString()).topPrivateDomain().toString();

            Host host = hostService.findByDomain(tld);
            if (host == null) {
                host = new Host();
                host.host = tld;
            }

            CatalogueEntry seed = new CatalogueEntry();
            seed.location = line;
            seed.initialDepth = 0;
            seed.status = Status.PENDING;
            seed.host = host;
            if (catalogueService.findForLocation(seed.location) == null) {
                LOGGER.info("Found seed: {}", line);
                catalogueService.save(seed);
            }
        }
    }
}