Package com.mastfrog.url
Class URL
java.lang.Object
com.mastfrog.url.URL
- All Implemented Interfaces:
URLComponent,Serializable,Comparable<URL>,org.netbeans.validation.api.Validating
public final class URL
extends Object
implements URLComponent, org.netbeans.validation.api.Validating, Comparable<URL>
Represents an internet URL. Unlike
java.net.URL, invalid
URLs can be constructed (see isValid() and getInvalidComponent() for
validation).
Also, unlike java.net.URL, the equals() and hashCode()
methods of this class will not attempt to make network connections.
This class does attempt basic normalization of URLs - a URL
representing http://FOO.com/bar.html? is equal to a URL
representing http://foo.com/bar.html.
To create instances, use either URL.parse()
or URLBuilder.- Author:
- Tim Boudreau
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionGet all components of this URL, drilling into nested components and not including their parents where necessary (as inHost,PathandParameters).voidAppend this URL to a StringBuilder, in normalized form.static URLBuilderbuilder()static URLBuilderstatic URLBuilderintGet the components of this URL.booleanDetermine if the passed object is a URL and is an exact match for this URL.static URLGet a URL for a file on disk in file protocol.static URLfromJavaUrl(URL url) Convert a java.net.URL to URL.Get the anchor, if any, of this URL, as inhttp://foo.com/index.html#anchorgetBaseURL(boolean retainPort) Get this URL in its typical browser-based form, omitting any query strings and username/password.Get a human-readable, localized name for this part of the URL.getHost()Get the host portion, if any, of this URL, as inhttp://foo.com/index.htmlGet an aggregate of the host and port.Get the first URLComponent which is not valid, if isValid() == false.Get the parameters of this query, if any, as inhttp://foo.com/stuff/index.html?foo=bar;bar=baz&baz=boo#anchorGet a URL to the parent path of this URL, stripping out any parameters or anchor.Get the user-name portion of this URL, as inhttp://tim:password@foo.com/stuff/index.htmlgetPath()Get the path, if any, of this URL, as inhttp://foo.com:332/stuff/index.html?foo=bar;bar=baz;#anchorgetPort()Get the port, if any, of this URL.org.netbeans.validation.api.ProblemsGet the protocol, if any, of this URL, as inhttp://foo.com/stuff/index.html.static CharsetGet the user-name portion of this URL, as inhttp://tim:password@foo.com/stuff/index.htmlinthashCode()booleanReturns true if this protocol of this URL is a well-known protocol.booleanDetermine if this URL is inferred to refer to a file, not a folder.booleanisSameDomain(String domain) Determine if the domain is the same as the passed internet domain.booleanisSecure()Returns true if the protocol is non-null, is a known protocol, and the protocol is known to use SSL.booleanisValid()Determine if this URL is a legal URL according to the relevant RFCs.static URLParse a URL from a string.booleansimpleEquals(URL other) Determine if this URL points to the same file on the same host, ignoring username/password, anchor and parameters.Create a version of this URL minus its anchor.Create a version of this URL minus its parameters.Creates a version of this URL minus its username, password, query and anchor.toString()Get this URL in normalized form as a string.toURI()withParameter(String name, String value) withProtocol(Protocol protocol)
-
Method Details
-
withProtocol
-
builder
-
builder
-
builder
-
parse
Parse a URL from a string. Note that this method will not throw any exception if the URL is invalid. Call isValid() on the result if you are parsing a URL you did not construct yourself. getInvalidComponent().getName() will indicate exactly what part is invalid. It is not guaranteed that all parts of the passed-in string will be preserved in the returned URL if it is invalid.- Parameters:
url- A URL string- Returns:
- A URL
-
getBaseURL
Get this URL in its typical browser-based form, omitting any query strings and username/password. I.e., if you have a URL forhttp://username:password@foo.com/something?bar=baz#x, you get backhttp://foo.com/something.- Returns:
-
getAnchor
Get the anchor, if any, of this URL, as inhttp://foo.com/index.html#anchor- Returns:
- An anchor or null
-
getHost
Get the host portion, if any, of this URL, as inhttp://foo.com/index.html- Returns:
- A host or null
-
getPath
Get the path, if any, of this URL, as inhttp://foo.com:332/stuff/index.html?foo=bar;bar=baz;#anchor- Returns:
- An anchor or null
-
getPort
Get the port, if any, of this URL. The port returned will either be the explicit port in this URL, as inhttp://foo.com:332/stuff/index.htmlor the default port from the protocol, if any.- Returns:
- A port or null
-
getProtocol
Get the protocol, if any, of this URL, as inhttp://foo.com/stuff/index.html. If the protocol is null, the URL is invalid.- Returns:
- A protocol, or null.
-
getParameters
Get the parameters of this query, if any, as inhttp://foo.com/stuff/index.html?foo=bar;bar=baz&baz=boo#anchor- Returns:
- The parameters or null if empty or unspecified
-
getUserName
Get the user-name portion of this URL, as inhttp://tim:password@foo.com/stuff/index.html- Returns:
- The user name if specified, otherwise null
-
getPassword
Get the user-name portion of this URL, as inhttp://tim:password@foo.com/stuff/index.html- Returns:
- The user name if specified, otherwise null
-
isKnownProtocol
public boolean isKnownProtocol()Returns true if this protocol of this URL is a well-known protocol.- Returns:
- Whether or not the protocol is a supported one, such as
http, https, ftp, file
-
isProbableFileReference
public boolean isProbableFileReference()Determine if this URL is inferred to refer to a file, not a folder. If false, the path portion will include a trailing / character.- Returns:
- whether or not this is a file reference
-
isSameDomain
Determine if the domain is the same as the passed internet domain. For example, "foo.com" and "www.foo.com" have the same domain.- Parameters:
domain-- Returns:
- True if the domain matches
-
getHostAndPort
Get an aggregate of the host and port.- Returns:
- A host and port
-
appendTo
Append this URL to a StringBuilder, in normalized form.- Specified by:
appendToin interfaceURLComponent- Parameters:
sb- A StringBuilder to append to
-
getPathAndQuery
-
isSecure
public boolean isSecure()Returns true if the protocol is non-null, is a known protocol, and the protocol is known to use SSL.- Returns:
- True if this is a secure URL.
-
components
Get the components of this URL.- Returns:
- The components
-
allComponents
Get all components of this URL, drilling into nested components and not including their parents where necessary (as inHost,PathandParameters).- Returns:
- An array of all low-level components of this URL.
-
getInvalidComponent
Get the first URLComponent which is not valid, if isValid() == false.- Returns:
- The invalid component, or null
-
isValid
public boolean isValid()Determine if this URL is a legal URL according to the relevant RFCs. Does not determine if this URL is reachable via a network, only that its syntax is correct.- Specified by:
isValidin interfaceURLComponent- Specified by:
isValidin interfaceorg.netbeans.validation.api.Validating- Returns:
- true if this URL is probably valid.
-
getComponentName
Description copied from interface:URLComponentGet a human-readable, localized name for this part of the URL. Useful if an error message needs to be shown.- Specified by:
getComponentNamein interfaceURLComponent- Returns:
- The component name
-
toString
Get this URL in normalized form as a string. -
toUnescapedForm
-
stripAnchor
Create a version of this URL minus its anchor.- Returns:
- A version of this URL minus everything following the last # character.
-
stripQuery
Create a version of this URL minus its parameters.- Returns:
- A version of this URL minus everything following the last ? character.
-
toSimpleURL
Creates a version of this URL minus its username, password, query and anchor.- Returns:
- A URL with the same protocol, host, port and path as this one.
-
toURI
- Throws:
URISyntaxException
-
withParameter
-
simpleEquals
Determine if this URL points to the same file on the same host, ignoring username/password, anchor and parameters.- Parameters:
other- Another URL- Returns:
- true if the other URL points to the same file via the same protocol
-
getParentURL
Get a URL to the parent path of this URL, stripping out any parameters or anchor.- Returns:
- A parent URL to this one, if the path is deeper than one element. Returns a path to the host with no path if the path is exactly one element deep.
-
fromFile
Get a URL for a file on disk in file protocol.- Parameters:
file- A file- Returns:
- A URL
-
fromJavaUrl
Convert a java.net.URL to URL.- Parameters:
url- A Java URL- Returns:
- A URL
-
equals
Determine if the passed object is a URL and is an exact match for this URL. Note that sometimessimpleEquals(URL)may be preferable, as it will match URLs which point to the same document but may specify a different user, password, parameters or anchor. -
hashCode
public int hashCode() -
getURLEncoding
-
toJavaURL
- Throws:
MalformedURLException
-
getProblems
public org.netbeans.validation.api.Problems getProblems()- Specified by:
getProblemsin interfaceorg.netbeans.validation.api.Validating
-
compareTo
- Specified by:
compareToin interfaceComparable<URL>
-