Package org.faceless.publisher.type
Class URL2
java.lang.Object
org.faceless.publisher.type.URL2
- All Implemented Interfaces:
Serializable
A replacement for the
URI
class that always try to parse the URI
and not throw a URISyntaxException, ever. It will correctly resolve
relative paths for "jar" URLs, and function based on the spec at
https://tools.ietf.org/html/rfc3986 and
Two URLs are considered equal if they have the same string values.
This explicitly excludes link-parameters from the comparison- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionURL2
(String scheme, String userInfo, String host, int port, String path, String query, String fragment) Create a URL from the specified parameters All parameters are supplied unescaped.Create an opaque URL from the specified parameters All parameters are supplied unescaped.Create a new URL2 that is a clone of the supplied URICreate a new URL2 that is a clone of the supplied URL -
Method Summary
Modifier and TypeMethodDescriptionstatic URL2
createFragmentURL
(String fragment) Create a URL consisiting of just the specified fragment, eg "#fragment"Return a String that could be used as a "filename" based on this URL, without any path information.boolean
Returns the decoded authorityReturns the decoded fragment in this URL, or null if there is no fragment.Return a Map of the "fragment parameters" for this URL.getHost()
Returns the host in this URL, or null if there is no host.Get a value previously set by#setParameter
getPath()
Returns the decoded path of this URL, or null if there is no path.int
getPort()
Returns the port in this URL, or -1 if there is no port.getQuery()
Returns the decoded query in this URL, or null if there is no query.Return a Map of the query parameters for this URL, or an empty Map if there are noneReturns the raw scheme-specific part of this URL, before it has been escape-decodedReturns the scheme of this URL.Returns the scheme-specific part of this URL.Returns the decoded userinfo in this URL, or null if there is no userinfo.int
hashCode()
boolean
Tells whether or not this URL is absolute.boolean
Return true if the URL scheme should be subject to CORSboolean
Return true if this URL is a fragment onlyboolean
Return whether the URL is a network scheme that refers to a non-local network host, ie a host that would be publicly routable.boolean
isOpaque()
Tells whether or not this URL is opaque.boolean
isSameDocument
(URL2 uri) Return true if this URL is, or is a fragment of the base URL.boolean
isSameSchemeHostPort
(URL2 url) Return true if this URL has the same scheme, host and port as the specified URL.boolean
Return whether the URL scheme is a file scheme - not local and not network.boolean
Return whether the URL scheme is "local", which is defined in https://fetch.spec.whatwg.org/#local-schem as "about", "blob" or "data".boolean
Return whether the URL scheme is a network scheme (currently http, https, ftp) and therefore subject to CORSboolean
Return whether the URL scheme is secured by TLS.static void
Return a new URL which is the normalized equivalent of this URLstatic URL2
Create a URL from the specified String, or return null if it is so invalid it's beyond salvagerelativize
(URL2 uri, boolean safe) Relativize the supplied URL aginst the current one.Constructs a new URL by parsing the given string and then resolving it against this URL.Resolves the given URL against this URL.toPath()
Convert this URL to a Path, or null if it doesn't correspond to an installedFileSystemProvider
Return the same value astoString()
, but with any password value in the userinfo string replaced with asteriskstoString()
toURI()
Convert this URL2 to aURI
, or return null if it is not possible.toURL()
Convert this URL2 to aURL
.withFragment
(String fragment) Set the fragment on this URLwithFragmentParameters
(Map<String, List<String>> params) Set the Fragment to be the supplied set of fragment parametersSet the query on this URLwithRequestPolicySecureMode
(boolean secureMode) withScheme
(String scheme) withUserInfo
(String userInfo) Set the userInfo on this URL
-
Constructor Details
-
URL2
Create a new URL2 that is a clone of the supplied URL- Parameters:
url
- the url
-
URL2
Create a new URL2 that is a clone of the supplied URI- Parameters:
uri
- the uri
-
URL2
public URL2(String scheme, String userInfo, String host, int port, String path, String query, String fragment) Create a URL from the specified parameters All parameters are supplied unescaped.- Parameters:
scheme
- the scheme, or nulluserInfo
- the user-info for authentcation, or nullhost
- the host, or nullport
- the port, or -1 to leave this unspecifiedpath
- the path, or nullquery
- the query string excluding the question-mark, or nullfragment
- the fragment excluding the hash, or null
-
URL2
Create an opaque URL from the specified parameters All parameters are supplied unescaped.- Parameters:
scheme
- the scheme, or nullssp
- the scheme-specific part, or nullquery
- the query string excluding the question0mark, or nullfragment
- the fragment excluding the hash, or null
-
-
Method Details
-
createFragmentURL
Create a URL consisiting of just the specified fragment, eg "#fragment"- Parameters:
fragment
- the fragment
-
parse
Create a URL from the specified String, or return null if it is so invalid it's beyond salvage -
withRequestPolicySecureMode
-
isSameSchemeHostPort
Return true if this URL has the same scheme, host and port as the specified URL. The scheme, hosts and port must be the same (or null; relative URLs have the same origin); If both URLs were origin URLs, this would be the "same-origin" test.- Parameters:
url
- the URL to compare to- Returns:
- true if the two URLs have the same scheme, host and port, false otherwise
-
toPath
Convert this URL to a Path, or null if it doesn't correspond to an installedFileSystemProvider
-
deriveFilename
Return a String that could be used as a "filename" based on this URL, without any path information. The returned value is guaranteed to not include slash characters, but nothing more. The algorithm for deriving the name is a undefined.- Parameters:
url
- the url
-
toURI
Convert this URL2 to aURI
, or return null if it is not possible. -
toURL
Convert this URL2 to aURL
.- Throws:
IllegalArgumentException
- If this URL is not absoluteMalformedURLException
- If a protocol handler for the URL could not be found, or if some other error occurred while constructing the URL
-
getScheme
Returns the scheme of this URL. -
getAuthority
Returns the decoded authority -
getUserInfo
Returns the decoded userinfo in this URL, or null if there is no userinfo. -
getHost
Returns the host in this URL, or null if there is no host. -
getPort
public int getPort()Returns the port in this URL, or -1 if there is no port. -
getPath
Returns the decoded path of this URL, or null if there is no path. -
getSchemeSpecificPart
Returns the scheme-specific part of this URL. -
getRawSchemeSpecificPart
Returns the raw scheme-specific part of this URL, before it has been escape-decoded -
getQuery
Returns the decoded query in this URL, or null if there is no query. The returned string does not include the leading query character. -
getQueryParameters
Return a Map of the query parameters for this URL, or an empty Map if there are none -
getFragmentParameters
Return a Map of the "fragment parameters" for this URL. These are identical to Query parameters but defined on the Fragment - for example, "path.svg#xwwh=0,0,20,50&t=40,50" -
withFragmentParameters
Set the Fragment to be the supplied set of fragment parameters -
getFragment
Returns the decoded fragment in this URL, or null if there is no fragment. The returned string does not include the leading hash character. -
isAbsolute
public boolean isAbsolute()Tells whether or not this URL is absolute. -
isFragmentOnly
public boolean isFragmentOnly()Return true if this URL is a fragment only -
isOpaque
public boolean isOpaque()Tells whether or not this URL is opaque. -
isSameDocument
Return true if this URL is, or is a fragment of the base URL. The base URL may be null, in which case the URL must be a fragment only- Parameters:
uri
- if not null, the URL which this URL is being tested against
-
resolve
Constructs a new URL by parsing the given string and then resolving it against this URL. The value "#" is a quick way of removing the fragment from the supplied URL. -
resolve
Resolves the given URL against this URL. Special exceptions to the normal way: - relative URLs in the "jar" scheme are resolved to an item within the same Jar -
normalize
Return a new URL which is the normalized equivalent of this URL -
relativize
Relativize the supplied URL aginst the current one.
Where we have
http://a.com/b/c
and relativizehttp://a.com/a/d
, it's unclear if "c" is a folder (in which case the path is "../d" or "c" is a file (in which case the path is "d"). This is the safe flag:- safe - don't risk it. The case above gives "/a/d". This matches
URI
, and always gives correct results, but the path will begin with a slash. - short - if the path ends in a slash it's a folder, otherwise its a file. The result will be shorter, but possibly incorrect.
If this is is a file URL and the file exists, we can resolve this automatically, but otherwise the "safe" flag will be used to determine which strategy to use.
Opaque URLs are never relativized. If the destination is already relative, it's returned.
- Parameters:
uri
- the URL to relativize against this URLsafe
- whether to use the "safe" strategy- Returns:
- the relative URL, or "uri" if it can't be determined
- safe - don't risk it. The case above gives "/a/d". This matches
-
withScheme
-
withFragment
Set the fragment on this URL- Returns:
- this
-
withQuery
Set the query on this URL- Returns:
- this
-
withUserInfo
Set the userInfo on this URL- Returns:
- this
-
getLinkParameters
Get a value previously set by#setParameter
-
toPublicString
Return the same value astoString()
, but with any password value in the userinfo string replaced with asterisks -
toString
-
isSchemeSecure
public boolean isSchemeSecure()Return whether the URL scheme is secured by TLS. Returns true for "https", false otherwise. Used byEgressFilter
to make decisions. -
isSchemeLocal
public boolean isSchemeLocal()Return whether the URL scheme is "local", which is defined in https://fetch.spec.whatwg.org/#local-schem as "about", "blob" or "data". Used byEgressFilter
to make decisions. -
isSchemeFileBased
public boolean isSchemeFileBased()Return whether the URL scheme is a file scheme - not local and not network. -
isSchemeNetwork
public boolean isSchemeNetwork()Return whether the URL scheme is a network scheme (currently http, https, ftp) and therefore subject to CORS -
isCORSApplicable
public boolean isCORSApplicable()Return true if the URL scheme should be subject to CORS -
isHostPublicRoutable
public boolean isHostPublicRoutable()Return whether the URL is a network scheme that refers to a non-local network host, ie a host that would be publicly routable. Non-network URLs return false. -
hashCode
public int hashCode() -
equals
-
main
- Throws:
Exception
-