com.twitter.common.net
Class UrlHelper

java.lang.Object
  extended by com.twitter.common.net.UrlHelper

public class UrlHelper
extends Object


Constructor Summary
UrlHelper()
           
 
Method Summary
static String addProtocol(String url)
          Checks whether a URL specifies its protocol, prepending http if it does not.
static String getDomain(String url)
          Gets the domain from url.
static String getDomainChecked(String url)
          Gets the domain from uri, and throws an exception if it's not a valid uri.
static List<String> getDomainLevels(String host)
          Gets the domain levels for a host.
static String getPath(String url)
          Gets the path from url.
static String stripUrlParameters(String url)
          Strips URL parameters from a url.
static String stripUrlParameters(URL url)
          Convenience method that calls #stripUrlParameters(String) for a URL.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UrlHelper

public UrlHelper()
Method Detail

getDomain

public static String getDomain(String url)
Gets the domain from url.

Parameters:
url - A url.
Returns:
The domain portion of the URL, or null if the url is invalid.

getDomainChecked

public static String getDomainChecked(String url)
                               throws URISyntaxException
Gets the domain from uri, and throws an exception if it's not a valid uri.

Parameters:
url - A url.
Returns:
The domain portion of the given url, or null if the host is undefined.
Throws:
URISyntaxException - if url is not a valid URI

getPath

public static String getPath(String url)
Gets the path from url.

Parameters:
url - A url.
Returns:
The path portion of the URL, or null if the url is invalid.

stripUrlParameters

public static String stripUrlParameters(String url)
Strips URL parameters from a url. This will remove anything after and including a question mark in the URL.

Parameters:
url - The URL to strip parameters from.
Returns:
The original URL with parameters stripped, which will be the original URL if no parameters were found.

stripUrlParameters

public static String stripUrlParameters(URL url)
Convenience method that calls #stripUrlParameters(String) for a URL.

Parameters:
url - The URL to strip parameters from.
Returns:
The original URL with parameters stripped, which will be the original URL if no parameters were found.

addProtocol

public static String addProtocol(String url)
Checks whether a URL specifies its protocol, prepending http if it does not.

Parameters:
url - The URL to fix.
Returns:
The URL with the http protocol specified if no protocol was already specified.

getDomainLevels

public static List<String> getDomainLevels(String host)
Gets the domain levels for a host. For example, sub1.sub2.domain.co.uk would return [sub1.sub2.domain.co.uk, sub2.domain.co.uk, domain.co.uk, co.uk, uk].

Parameters:
host - The host to peel subdomains off from.
Returns:
The domain levels in this host.