org.apache.nutch.util

## Class URLUtil

- java.lang.Object
- - org.apache.nutch.util.URLUtil

-     public class URLUtil
      extends java.lang.Object

  Utility class for URL analysis

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `URLUtil()`

  - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static java.lang.String` | `chooseRepr(java.lang.String src, java.lang.String dst, boolean temp)` Given two urls, a src and a destination of a redirect, it returns the representative url.
    `static java.lang.String` | `getDomainName(java.lang.String url)` Returns the domain name of the url.
    `static java.lang.String` | `getDomainName(java.net.URL url)` Returns the domain name of the url.
    `static DomainSuffix` | `getDomainSuffix(java.lang.String url)` Returns the `DomainSuffix` corresponding to the last public part of the hostname
    `static DomainSuffix` | `getDomainSuffix(java.net.URL url)` Returns the `DomainSuffix` corresponding to the last public part of the hostname
    `static java.lang.String` | `getHost(java.lang.String url)` Returns the lowercased hostname for the url or null if the url is not well formed.
    `static java.lang.String[]` | `getHostBatches(java.lang.String url)` Partitions of the hostname of the url by "."
    `static java.lang.String[]` | `getHostBatches(java.net.URL url)` Partitions of the hostname of the url by "."
    `static java.lang.String` | `getPage(java.lang.String url)` Returns the page for the url.
    `static boolean` | `isSameDomainName(java.lang.String url1, java.lang.String url2)` Returns whether the given urls have the same domain name.
    `static boolean` | `isSameDomainName(java.net.URL url1, java.net.URL url2)` Returns whether the given urls have the same domain name.
    `static void` | `main(java.lang.String[] args)` For testing
    `static java.net.URL` | `resolveURL(java.net.URL base, java.lang.String target)` Resolve relative URL-s and fix a java.net.URL error in handling of URLs with pure query targets.
    `static java.lang.String` | `toASCII(java.lang.String url)`
    `static java.lang.String` | `toUNICODE(java.lang.String url)`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### URLUtil

          public URLUtil()

  - ### Method Detail

    - #### resolveURL

          public static java.net.URL resolveURL(java.net.URL base,
                                                java.lang.String target)
                                         throws java.net.MalformedURLException

      Resolve relative URL-s and fix a java.net.URL error in handling of URLs with pure query targets.
      Parameters:
        `base` - base url
        `target` - target url (may be relative)
      Returns:
        resolved absolute url.
      Throws:
        `java.net.MalformedURLException`

    - #### getDomainName

          public static java.lang.String getDomainName(java.net.URL url)

      Returns the domain name of the url. The domain name of a url is the substring of the url's hostname, w/o subdomain names. As an example
      `getDomainName(conf, new URL(http://lucene.apache.org/))`
      will return
      `apache.org`

    - #### getDomainName

          public static java.lang.String getDomainName(java.lang.String url)
                                                throws java.net.MalformedURLException

      Returns the domain name of the url. The domain name of a url is the substring of the url's hostname, w/o subdomain names. As an example
      `getDomainName(conf, new http://lucene.apache.org/)`
      will return
      `apache.org`
      Throws:
        `java.net.MalformedURLException`

    - #### isSameDomainName

          public static boolean isSameDomainName(java.net.URL url1,
                                                 java.net.URL url2)

      Returns whether the given urls have the same domain name. As an example,
      `isSameDomain(new URL("http://lucene.apache.org") , new URL("http://people.apache.org/"))
      will return true.`
      Returns:
        true if the domain names are equal

    - #### isSameDomainName

          public static boolean isSameDomainName(java.lang.String url1,
                                                 java.lang.String url2)
                                          throws java.net.MalformedURLException

      Returns whether the given urls have the same domain name. As an example,
      `isSameDomain("http://lucene.apache.org" ,"http://people.apache.org/")
      will return true.`
      Returns:
        true if the domain names are equal
      Throws:
        `java.net.MalformedURLException`

    - #### getDomainSuffix

          public static DomainSuffix getDomainSuffix(java.net.URL url)

      Returns the `DomainSuffix` corresponding to the last public part of the hostname

    - #### getDomainSuffix

          public static DomainSuffix getDomainSuffix(java.lang.String url)
                                              throws java.net.MalformedURLException

      Returns the `DomainSuffix` corresponding to the last public part of the hostname
      Throws:
        `java.net.MalformedURLException`

    - #### getHostBatches

          public static java.lang.String[] getHostBatches(java.net.URL url)

      Partitions of the hostname of the url by "."

    - #### getHostBatches

          public static java.lang.String[] getHostBatches(java.lang.String url)
                                                   throws java.net.MalformedURLException

      Partitions of the hostname of the url by "."
      Throws:
        `java.net.MalformedURLException`

    - #### chooseRepr

          public static java.lang.String chooseRepr(java.lang.String src,
                                                    java.lang.String dst,
                                                    boolean temp)

      Given two urls, a src and a destination of a redirect, it returns the representative url.

      This method implements an extended version of the algorithm used by the Yahoo! Slurp crawler described here:
      How does the Yahoo! webcrawler handle redirects?

      Choose target url if either url is malformed.
      If different domains the keep the destination whether or not the redirect is temp or perm
        a.com -> b.com*
      If the redirect is permanent and the source is root, keep the source.
        *a.com -> a.com?y=1 || *a.com -> a.com/xyz/index.html
      If the redirect is permanent and the source is not root and the destination is root, keep the destination
        a.com/xyz/index.html -> a.com*
      If the redirect is permanent and neither the source nor the destination is root, then keep the destination
        a.com/xyz/index.html -> a.com/abc/page.html*
      If the redirect is temporary and source is root and destination is not root, then keep the source
        *a.com -> a.com/xyz/index.html
      If the redirect is temporary and source is not root and destination is root, then keep the destination
        a.com/xyz/index.html -> a.com*
      If the redirect is temporary and neither the source or the destination is root, then keep the shortest url. First check for the shortest host, and if both are equal then check by path. Path is first by length then by the number of / path separators.
        a.com/xyz/index.html -> a.com/abc/page.html*
        *www.a.com/xyz/index.html -> www.news.a.com/xyz/index.html
      If the redirect is temporary and both the source and the destination are root, then keep the shortest sub-domain
        *www.a.com -> www.news.a.com
      While not in this logic there is a further piece of representative url logic that occurs during indexing and after scoring. During creation of the basic fields before indexing, if a url has a representative url stored we check both the url and its representative url (which should never be the same) against their linkrank scores and the highest scoring one is kept as the url and the lower scoring one is held as the orig url inside of the index.
      Parameters:
        `src` - The source url.
        `dst` - The destination url.
        `temp` - Is the redirect a temporary redirect.
      Returns:
        String The representative url.

    - #### getHost

          public static java.lang.String getHost(java.lang.String url)

      Returns the lowercased hostname for the url or null if the url is not well formed.
      Parameters:
        `url` - The url to check.
      Returns:
        String The hostname for the url.

    - #### getPage

          public static java.lang.String getPage(java.lang.String url)

      Returns the page for the url. The page consists of the protocol, host, and path, but does not include the query string. The host is lowercased but the path is not.
      Parameters:
        `url` - The url to check.
      Returns:
        String The page for the url.

    - #### toASCII

          public static java.lang.String toASCII(java.lang.String url)

    - #### toUNICODE

          public static java.lang.String toUNICODE(java.lang.String url)

    - #### main

          public static void main(java.lang.String[] args)

      For testing

Copyright © 2019 The Apache Software Foundation