Class KiwiUrls
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classA simple value class to hold the various parts of the URL. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic StringcreateHttpsUrl(String hostname, int port) Create a well-formed HTTPS URL (String) from the given hostname and port.static StringcreateHttpsUrl(String hostname, int port, String path) Create a well-formed HTTPS URL (String) from the given hostname, port, and path.static URLcreateHttpsUrlObject(String hostname, int port) Create a well-formed HTTPS URL from the given hostname and port.static URLcreateHttpsUrlObject(String hostname, int port, String path) Create a well-formed HTTPS URL from the given hostname, port, and path.static StringcreateHttpUrl(String hostname, int port) Create a well-formed HTTP URL (String) from the given hostname and port.static StringcreateHttpUrl(String hostname, int port, String path) Create a well-formed HTTP URL (String) from the given hostname, port, and path.static URLcreateHttpUrlObject(String hostname, int port) Create a well-formed HTTP URL from the given hostname and port.static URLcreateHttpUrlObject(String hostname, int port, String path) Create a well-formed HTTP URL from the given hostname, port, and path.static StringCreate a well-formed URL string from the givenschemeHostPortand zero or more path components.static StringCreate a well-formed URL (String) from the given protocol, hostname, and port.static StringCreate a well-formed URL (String) from the given protocol, hostname, port, and path.static URLcreateUrlObject(String urlSpec) Wrapper aroundURL's constructor which throws a checkedMalformedURLException.static URLcreateUrlObject(String schemeHostPort, String... pathParts) Create a well-formed URL from the givenschemeHostPortand zero or more path components.static URLcreateUrlObject(String protocol, String hostname, int port) Create a well-formed URL from the given protocol, hostname, and port.static URLcreateUrlObject(String protocol, String hostname, int port, String path) Create a well-formed URL from the given protocol, hostname, port, and path.static KiwiUrls.ComponentsextractAllFrom(String url) Extract all the relevant sections from the givenuri.Extracts the canonical server name from a given URL.Extracts the server's domain name from a given URL.extractPathFrom(String url) Extracts the path from a given URL.static OptionalIntextractPortFrom(String url) Extracts the port from a given URL.extractSchemeFrom(String url) Extracts the scheme from a given URL.Extracts the simple server name from a given URL.static StringprependLeadingSlash(String path) Trimspathand, if a leading slash is not present, adds it.queryStringToMap(String queryString) Converts a query string (composed of key/value pairs separated by&characters) into a Map whose keys are the parameter names and values are the parameter values.queryStringToMultimap(String queryString) Converts a query string into a GuavaMultimapwhose keys are the parameter names and values are lists containing one or more values.queryStringToMultivaluedMap(String queryString) static StringreplaceDomainsIn(String commaDelimitedUrls, String replacementDomain) Searches thecommaDelimitedUrlsfor its domains, and if found, replaces all entries withreplacementDomain.static StringstripLeadingSlash(String path) Trimspathand, if a leading slash is present, removes it.static StringstripTrailingSlash(String url) Trimsurland, if present, strips the trailing slashstripTrailingSlashes(List<String> urls) Trims each URL inurlsand strips any trailing slashesstatic <V> StringtoEncodedQueryString(Map<String, V> parameters) Converts a Map containing String keys andVvalues into a URL-encoded query string.static <V> StringtoEncodedQueryString(Map<String, V> parameters, String charsetName) Converts a Map containing String keys andVvalues into a URL-encoded query string.static <V> StringtoEncodedQueryString(Map<String, V> parameters, Charset charset) Converts a Map containing String keys andVvalues into a URL-encoded query string.static <V> StringtoQueryString(Map<String, V> parameters) Converts a Map containing String keys and V values into one (potentially long) string of key/value parameters.static URLTries to convert the givenuriinto aURL, throwing an unchecked exception if the conversion fails.
-
Field Details
-
UNKNOWN_PORT
public static final int UNKNOWN_PORT- See Also:
-
FTP_PROTOCOL
- See Also:
-
HTTP_PROTOCOL
- See Also:
-
HTTPS_PROTOCOL
- See Also:
-
SFTP_PROTOCOL
- See Also:
-
SCHEME_GROUP
- See Also:
-
CANONICAL_GROUP
- See Also:
-
SUB_DOMAIN_GROUP
- See Also:
-
DOMAIN_GROUP
- See Also:
-
PORT_GROUP
- See Also:
-
PATH_GROUP
- See Also:
-
COMMA_SPLITTER
public static final com.google.common.base.Splitter COMMA_SPLITTER
-
-
Method Details
-
createUrl
Create a well-formed URL (String) from the given protocol, hostname, and port.- Parameters:
protocol- the protocolhostname- the host nameport- the port- Returns:
- the URL as a
Stringobject
-
createUrlObject
Create a well-formed URL from the given protocol, hostname, and port.- Parameters:
protocol- the protocolhostname- the host nameport- the port- Returns:
- the URL as a
URLobject
-
createUrl
Create a well-formed URL (String) from the given protocol, hostname, port, and path.- Parameters:
protocol- the protocolhostname- the host nameport- the portpath- the path- Returns:
- the URL as a
Stringobject
-
createUrlObject
Create a well-formed URL from the given protocol, hostname, port, and path.- Parameters:
protocol- the protocolhostname- the host nameport- the portpath- the path- Returns:
- the URL as a
URLobject
-
createUrlObject
Wrapper aroundURL's constructor which throws a checkedMalformedURLException. This instead assumes the givenurlSpecis valid and throwsUncheckedMalformedURLExceptionin case it is actually not valid.- Parameters:
urlSpec- the String to parse as a URL- Returns:
- a new
URLinstance - Throws:
UncheckedMalformedURLException- that wraps aMalformedURLExceptionif any error occurs
-
createUrl
Create a well-formed URL string from the givenschemeHostPortand zero or more path components.- Parameters:
schemeHostPort- a string containing the scheme, host, and port parts, e.g.,http://acme.com:8080pathParts- zero or more path parts to append- Returns:
- the constructed URL as a
String
-
createUrlObject
Create a well-formed URL from the givenschemeHostPortand zero or more path components.- Parameters:
schemeHostPort- a string containing the scheme, host, and port parts, e.g.,http://acme.com:8080pathParts- zero or more path parts to append- Returns:
- the constructed URL as a
URL
-
toURL
Tries to convert the givenuriinto aURL, throwing an unchecked exception if the conversion fails. The thrown unchecked exception wraps the original checkedMalformedURLException.- Parameters:
uri- the URI to convert- Returns:
- a
URLinstance - Throws:
UncheckedMalformedURLException- if conversion fromURItoURLfails- See Also:
-
prependLeadingSlash
Trimspathand, if a leading slash is not present, adds it.- Parameters:
path- a path- Returns:
- a new String with a leading slash
-
stripLeadingSlash
Trimspathand, if a leading slash is present, removes it.- Parameters:
path- a path- Returns:
- a new String without a leading slash
-
createHttpUrl
Create a well-formed HTTP URL (String) from the given hostname and port.- Parameters:
hostname- the host nameport- the port- Returns:
- a URL as a
String
-
createHttpUrlObject
Create a well-formed HTTP URL from the given hostname and port.- Parameters:
hostname- the host nameport- the port- Returns:
- a URL as a
URL
-
createHttpUrl
Create a well-formed HTTP URL (String) from the given hostname, port, and path.- Parameters:
hostname- the host nameport- the portpath- the path- Returns:
- a URL as a
String
-
createHttpUrlObject
Create a well-formed HTTP URL from the given hostname, port, and path.- Parameters:
hostname- the host nameport- the portpath- the path- Returns:
- a URL as a
URL
-
createHttpsUrl
Create a well-formed HTTPS URL (String) from the given hostname and port.- Parameters:
hostname- the host nameport- the port- Returns:
- a URL as a
String
-
createHttpsUrlObject
Create a well-formed HTTPS URL from the given hostname and port.- Parameters:
hostname- the host nameport- the port- Returns:
- a URL as a
URL
-
createHttpsUrl
Create a well-formed HTTPS URL (String) from the given hostname, port, and path.- Parameters:
hostname- the host nameport- the portpath- the path- Returns:
- a URL as a
String
-
createHttpsUrlObject
Create a well-formed HTTPS URL from the given hostname, port, and path.- Parameters:
hostname- the host nameport- the portpath- the path- Returns:
- a URL as a
URL
-
extractAllFrom
Extract all the relevant sections from the givenuri.As an example, if given
https://news.bbc.co.uk:8080/a-news-articlethis would return the following:- scheme = "https"
- subDomainName = "news"
- domainName = "bbc.co.uk"
- canonicalName = "news.bbc.co.uk"
- port = 8080
- path = "a-news-article"
- Parameters:
url- the URL to analyze- Returns:
- the
KiwiUrls.Componentsfound or an "empty"KiwiUrls.Componentsobject if the URL was invalid - Throws:
IllegalArgumentException- if the port in the URL is not a number- Implementation Note:
- This method does not check if the URL is valid or not.
-
stripTrailingSlash
Trimsurland, if present, strips the trailing slash- Parameters:
url- the URL- Returns:
- the URL minus any trailing slash
-
stripTrailingSlashes
Trims each URL inurlsand strips any trailing slashes- Parameters:
urls- a list of URLs- Returns:
- a list of URLs matching the input URLs minus any trailing slash
-
extractCanonicalNameFrom
Extracts the canonical server name from a given URL.As an example, if given
https://news.bbc.co.uk:8080/a-news-article, this method would return "news.bbc.co.uk"- Parameters:
url- the URL to evaluate- Returns:
- an
Optionalcontaining the canonical server name orOptional.empty()if it could not be found. - Implementation Note:
- This method does not check if the URL is valid or not. Also, if you will need to extract more than
one section of the URL, you should instead use
extractAllFrom(String).
-
extractDomainNameFrom
Extracts the server's domain name from a given URL.As an example, if given
https://news.bbc.co.uk:8080/a-news-article, this method would return "bbc.co.uk"- Parameters:
url- the URL to evaluate- Returns:
- an
Optionalcontaining the server's domain name orOptional.empty()if it could not be found. - Implementation Note:
- This method does not check if the URL is valid or not. Also, if you will need to extract more than
one section of the URL, you should instead use
extractAllFrom(String).
-
extractPathFrom
Extracts the path from a given URL.As an example, if given
https://news.bbc.co.uk:8080/a-news-article, this method would return "a-news-article"- Parameters:
url- the URL to evaluate- Returns:
- an
Optionalcontaining the path orOptional.empty()if it could not be found. - Implementation Note:
- This method does not check if the URL is valid or not. Also, if you will need to extract more than
one section of the URL, you should instead use
extractAllFrom(String).
-
extractPortFrom
Extracts the port from a given URL.As an example, if given
https://news.bbc.co.uk:8080/a-news-article, this method would return "8080" (represented by an int).- Parameters:
url- the URL to evaluate- Returns:
- an
Optionalcontaining the port orOptional.empty()if it could not be found. - Throws:
IllegalArgumentException- if the port is not a number- Implementation Note:
- This method does not check if the URL is valid or not. Also, if you will need to extract more than
one section of the URL, you should instead use
extractAllFrom(String).
-
extractSchemeFrom
Extracts the scheme from a given URL.As an example, if given
https://news.bbc.co.uk:8080/a-news-article, this method would return "https"- Parameters:
url- the URL to evaluate- Returns:
- an
Optionalcontaining the canonical server name orOptional.empty()if it could not be found. - Implementation Note:
- This method does not check if the URL is valid or not. Also, if you will need to extract more than
one section of the URL, you should instead use
extractAllFrom(String).
-
extractSubDomainNameFrom
Extracts the simple server name from a given URL.As an example, if given
https://news.bbc.co.uk:8080/a-news-article, this method would return "news"- Parameters:
url- the URL to evaluate- Returns:
- an
Optionalcontaining the simple server name orOptional.empty()if it could not be found. - Implementation Note:
- This method does not check if the URL is valid or not. Also, if you will need to extract more than
one section of the URL, you should instead use
extractAllFrom(String).
-
replaceDomainsIn
Searches thecommaDelimitedUrlsfor its domains, and if found, replaces all entries withreplacementDomain. ThecommaDelimitedUrlscan be a standalone URL.- Parameters:
commaDelimitedUrls- the comma delimited URLs to searchreplacementDomain- the domain to replace if found- Returns:
- the updated comma-delimited URLs if a domain is found, otherwise
commaDelimitedUrlsunchanged - Implementation Note:
- This method assumes that the domains are the same for all URLs in the
commaDelimitedUrls; it only checks the first URL to obtain the domain.
-
queryStringToMap
Converts a query string (composed of key/value pairs separated by&characters) into a Map whose keys are the parameter names and values are the parameter values.Note specifically that this method does not decode the query string parameters. It splits on literal & characters to obtain the key/value pairs and then on the literal
=to get the name and value of each pair. If a parameter doesn't contain any value, its value is set to an empty string. For example, in the query string?sorted&sortProp=, bothsortedandsortPropwill have an empty string as their value in the returned map.Also note that if a parameter has multiple values, only the first one is returned, e.g., the value of "topping" in the query string
topping=pepperoni&topping=banana+pepper&topping=sausagewill always be "pepperoni".- Parameters:
queryString- the query string to create the map from- Returns:
- a map of the query params
- See Also:
-
queryStringToMultimap
public static com.google.common.collect.Multimap<String,String> queryStringToMultimap(String queryString) Converts a query string into a GuavaMultimapwhose keys are the parameter names and values are lists containing one or more values. Use this method (orqueryStringToMultivaluedMap(String)) when parameters can have multiple values.Like
queryStringToMap(String)this method does not decode the query parameters, and it properly handles parameters with no value.If a parameter has multiple values, they are grouped under the parameter name in the Multimap. For example, given the query string
topping=pepperoni&topping=banana+pepper&topping=sausage, callingmultimap.get("topping")will return aCollectionof strings containing "pepperoni", "banana+pepper", and "sausage".- Parameters:
queryString- the query string to create the Multimap from- Returns:
- a Multimap of the query parameters
- See Also:
-
queryStringToMultivaluedMap
Converts a query string into aMapwhose keys are the parameter names and values areListcontaining one or more values. Use this (orqueryStringToMultimap(String)) when parameters can have multiple values.Like
queryStringToMap(String)this method does not decode the query parameters, and it properly handles parameters with no value.If a parameter has multiple values, they are grouped under the parameter name. For example, given the query string
topping=onion&topping=mushroom&topping=extra+cheese&topping=fresh+basil, callingmap.get("topping")will return aListof strings containing "onion", "mushroom", "extra+cheese", and "fresh+basil".- Parameters:
queryString- the query string to create the Multimap from- Returns:
- a Map of the query parameters
- See Also:
-
toQueryString
Converts a Map containing String keys and V values into one (potentially long) string of key/value parameters. Each key/value parameter is separated by an '=' character, and each parameter pair is separated by an&character.For example,
page=0&pageSize=25&sort1=last&sortDir1=desc&sort2=first&sortDir2=asc.Note specifically that this method does not URL encode the parameters.
- Type Parameters:
V- the type of values in theparametersmap- Parameters:
parameters- the map of the parameters to create the query string from- Returns:
- a concatenated query string
- See Also:
-
toEncodedQueryString
Converts a Map containing String keys andVvalues into a URL-encoded query string. Encodes the query string usingStandardCharsets.UTF_8as the character set.- Type Parameters:
V- the type of values in theparametersmap- Parameters:
parameters- the map of the parameters to create the query string from- Returns:
- a URL-encoded query string
- See Also:
-
toEncodedQueryString
Converts a Map containing String keys andVvalues into a URL-encoded query string. Encodes the query string using the given charsetName, which must be a valid charsetName name. TheCharset.forName(String)method is used to convert the given charsetName name to aCharset; see that class for the exceptions it throws when given illegal arguments.- Type Parameters:
V- the type of values in theparametersmap- Parameters:
parameters- the map of the parameters to create the query string fromcharsetName- the name of theCharset(must be valid viaCharset.forName(String))- Returns:
- a URL-encoded query string
- See Also:
-
toEncodedQueryString
Converts a Map containing String keys andVvalues into a URL-encoded query string. Encodes the query string using the givenCharset.- Type Parameters:
V- the type of values in theparametersmap- Parameters:
parameters- the map of the parameters to create the query string fromcharset- theCharsetto use when encoding the parameters- Returns:
- a URL-encoded query string
- See Also:
-