Class KiwiUrls
- java.lang.Object
-
- org.kiwiproject.net.KiwiUrls
-
public class KiwiUrls extends Object
Static utilities for creating URLs
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classKiwiUrls.ComponentsA simple value class to hold the various parts of the URL.
-
Field Summary
Fields Modifier and Type Field Description static StringCANONICAL_GROUPstatic com.google.common.base.SplitterCOMMA_SPLITTERstatic StringDOMAIN_GROUPstatic StringFTP_PROTOCOLstatic StringHTTP_PROTOCOLstatic StringHTTPS_PROTOCOLstatic StringPATH_GROUPstatic StringPORT_GROUPstatic StringSCHEME_GROUPstatic StringSFTP_PROTOCOLstatic StringSUB_DOMAIN_GROUPstatic intUNKNOWN_PORT
-
Constructor Summary
Constructors Constructor Description KiwiUrls()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static 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 StringcreateUrl(String schemeHostPort, String... pathComponents)Create a well-formed URL string from the givenschemeHostPortand zero or more path components.static StringcreateUrl(String protocol, String hostname, int port)Create a well-formed URL (String) from the given protocol, hostname, and port.static StringcreateUrl(String protocol, String hostname, int port, String path)Create a well-formed URL (String) from the given protocol, hostname, port and path.static URLcreateUrlObject(String urlSpec)Wrapper around URL's constructor which throws a checkedMalformedURLException.static URLcreateUrlObject(String schemeHostPort, String... pathComponents)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.static Optional<String>extractCanonicalNameFrom(String url)Extracts the canonical server name from a given URL.static Optional<String>extractDomainNameFrom(String url)Extracts the server's domain name from a given URL.static Optional<String>extractPathFrom(String url)Extracts the path from a given URL.static OptionalIntextractPortFrom(String url)Extracts the port from a given URL.static Optional<String>extractSchemeFrom(String url)Extracts the scheme from a given URL.static Optional<String>extractSubDomainNameFrom(String url)Extracts the simple server name from a given URL.static StringprependLeadingSlash(String path)Trimspathand, if a leading slash is not present, adds it.static Map<String,String>queryStringToMap(String queryString)Converts a query string (comprised of key/value pairs separated by '&' characters) into a Map whose keys are the parameter names and values are the parameter values.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.static Map<String,List<String>>queryStringToMultivaluedMap(String queryString)static StringreplaceDomainsIn(String commaDelimitedUrls, String replacementDomain)Searches thecommaDelimitedUrlsfor its domains, and if found, replaces all entries withreplacementDomain.static StringstripTrailingSlash(String url)Trimsurland, if present, strips the trailing slashstatic List<String>stripTrailingSlashes(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 (each key/value parameter is separated by an '=' character), with each parameter pair separated by an '&' character.static URLtoURL(URI uri)Tries to convert the givenuriinto aURL, throwing an unchecked exception if the conversion fails.
-
-
-
Field Detail
-
UNKNOWN_PORT
public static final int UNKNOWN_PORT
- See Also:
- Constant Field Values
-
FTP_PROTOCOL
public static final String FTP_PROTOCOL
- See Also:
- Constant Field Values
-
HTTP_PROTOCOL
public static final String HTTP_PROTOCOL
- See Also:
- Constant Field Values
-
HTTPS_PROTOCOL
public static final String HTTPS_PROTOCOL
- See Also:
- Constant Field Values
-
SFTP_PROTOCOL
public static final String SFTP_PROTOCOL
- See Also:
- Constant Field Values
-
SCHEME_GROUP
public static final String SCHEME_GROUP
- See Also:
- Constant Field Values
-
CANONICAL_GROUP
public static final String CANONICAL_GROUP
- See Also:
- Constant Field Values
-
SUB_DOMAIN_GROUP
public static final String SUB_DOMAIN_GROUP
- See Also:
- Constant Field Values
-
DOMAIN_GROUP
public static final String DOMAIN_GROUP
- See Also:
- Constant Field Values
-
PORT_GROUP
public static final String PORT_GROUP
- See Also:
- Constant Field Values
-
PATH_GROUP
public static final String PATH_GROUP
- See Also:
- Constant Field Values
-
COMMA_SPLITTER
public static final com.google.common.base.Splitter COMMA_SPLITTER
-
-
Method Detail
-
createUrl
public static String createUrl(String protocol, String hostname, int port)
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
public static URL createUrlObject(String protocol, String hostname, int port)
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
public static String createUrl(String protocol, String hostname, int port, String path)
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
public static URL createUrlObject(String protocol, String hostname, int port, String path)
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
public static URL createUrlObject(String urlSpec)
Wrapper around URL'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
public static String createUrl(String schemeHostPort, String... pathComponents)
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:8080pathComponents- zero or more path components to append- Returns:
- the constructed URL as a
String
-
createUrlObject
public static URL createUrlObject(String schemeHostPort, String... pathComponents)
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:8080pathComponents- zero or more path components to append- Returns:
- the constructed URL as a
URL
-
toURL
public static URL toURL(URI uri)
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:
URI.toURL()
-
prependLeadingSlash
public static String prependLeadingSlash(String path)
Trimspathand, if a leading slash is not present, adds it.- Parameters:
path- a path- Returns:
- a new String with a leading slash
-
createHttpUrl
public static String createHttpUrl(String hostname, int port)
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
public static URL createHttpUrlObject(String hostname, int port)
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
public static String createHttpUrl(String hostname, int port, String path)
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
public static URL createHttpUrlObject(String hostname, int port, String path)
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
public static String createHttpsUrl(String hostname, int port)
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
public static URL createHttpsUrlObject(String hostname, int port)
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
public static String createHttpsUrl(String hostname, int port, String path)
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
public static URL createHttpsUrlObject(String hostname, int port, String path)
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
public static KiwiUrls.Components extractAllFrom(String url)
Extract all the relevant sections from the givenuri.As an example, if given "https://news.bbc.co.uk:8080/a-news-article" this 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 emptyKiwiUrls.Componentsobject if the URL was invalid - Implementation Note:
- This method does not check if the URL is valid or not.
-
stripTrailingSlash
public static String stripTrailingSlash(String url)
Trimsurland, if present, strips the trailing slash- Parameters:
url- the URL- Returns:
- the URL minus any trailing slash
-
stripTrailingSlashes
public static List<String> stripTrailingSlashes(List<String> urls)
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
public static Optional<String> extractCanonicalNameFrom(String url)
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
public static Optional<String> extractDomainNameFrom(String url)
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
public static Optional<String> extractPathFrom(String url)
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
public static OptionalInt extractPortFrom(String url)
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. - 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
public static Optional<String> extractSchemeFrom(String url)
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
public static Optional<String> extractSubDomainNameFrom(String url)
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
public static String replaceDomainsIn(String commaDelimitedUrls, String replacementDomain)
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
public static Map<String,String> queryStringToMap(String queryString)
Converts a query string (comprised 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 values 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:
toQueryString(Map) for the inverse operation
-
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(String)
-
queryStringToMultivaluedMap
public static Map<String,List<String>> queryStringToMultivaluedMap(String queryString)
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:
queryStringToMultimap(String)
-
toQueryString
public static <V> String toQueryString(Map<String,V> parameters)
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), with each parameter pair separated by an '&' character.Note specifically that this method does not URL encode the parameters.
- Type Parameters:
V- the type of values in the parameters map- Parameters:
parameters- the map of the parameters to create the query string from- Returns:
- a concatenated query string
- See Also:
queryStringToMap(String) for the inverse operation
-
toEncodedQueryString
public static <V> String toEncodedQueryString(Map<String,V> parameters)
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 the parameters map- Parameters:
parameters- the map of the parameters to create the query string from- Returns:
- a URL-encoded query string
- See Also:
URLEncoder.encode(String, Charset)
-
toEncodedQueryString
public static <V> String toEncodedQueryString(Map<String,V> parameters, String charsetName)
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 the parameters map- 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:
Charset.forName(String),URLEncoder.encode(String, Charset)
-
toEncodedQueryString
public static <V> String toEncodedQueryString(Map<String,V> parameters, Charset charset)
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 the parameters map- 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:
URLEncoder.encode(String, Charset)
-
-