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 of 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 of String of Stringsstatic StringreplaceDomainsIn(String commaDelimitedUrls, String replacementDomain)Searches thecommaDelimitedUrlsfor its domains, and if found, replaces all entries withreplacmentDomain.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 StringtoQueryString(Map<String,String> parameters)Converts a Map of String of Strings 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 of 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 withreplacmentDomain. 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 of String of Strings- Parameters:
queryString- the query string to create the map from- Returns:
- a map of the query params
- See Also:
for inverse
-
toQueryString
public static String toQueryString(Map<String,String> parameters)
Converts a Map of String of Strings 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.- Parameters:
parameters- the map of the parameters to create the query string from- Returns:
- a concatenated query string
- See Also:
for inverse
-
-