Class URL2

java.lang.Object
org.faceless.publisher.type.URL2
All Implemented Interfaces:
Serializable

public class URL2 extends Object implements 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 Details

    • URL2

      public URL2(URL url)
      Create a new URL2 that is a clone of the supplied URL
      Parameters:
      url - the url
    • URL2

      public URL2(URI uri)
      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 null
      userInfo - the user-info for authentcation, or null
      host - the host, or null
      port - the port, or -1 to leave this unspecified
      path - the path, or null
      query - the query string excluding the question-mark, or null
      fragment - the fragment excluding the hash, or null
    • URL2

      public URL2(String scheme, String ssp, String query, String fragment)
      Create an opaque URL from the specified parameters All parameters are supplied unescaped.
      Parameters:
      scheme - the scheme, or null
      ssp - the scheme-specific part, or null
      query - the query string excluding the question0mark, or null
      fragment - the fragment excluding the hash, or null
  • Method Details

    • createFragmentURL

      public static URL2 createFragmentURL(String fragment)
      Create a URL consisiting of just the specified fragment, eg "#fragment"
      Parameters:
      fragment - the fragment
    • parse

      public static URL2 parse(String s)
      Create a URL from the specified String, or return null if it is so invalid it's beyond salvage
    • withRequestPolicySecureMode

      public URL2 withRequestPolicySecureMode(boolean secureMode)
    • isSameSchemeHostPort

      public boolean isSameSchemeHostPort(URL2 url)
      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

      public Path toPath()
      Convert this URL to a Path, or null if it doesn't correspond to an installed FileSystemProvider
    • deriveFilename

      public String 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

      public URI toURI()
      Convert this URL2 to a URI, or return null if it is not possible.
    • toURL

      public URL toURL() throws MalformedURLException
      Convert this URL2 to a URL.
      Throws:
      IllegalArgumentException - If this URL is not absolute
      MalformedURLException - If a protocol handler for the URL could not be found, or if some other error occurred while constructing the URL
    • getScheme

      public String getScheme()
      Returns the scheme of this URL.
    • getAuthority

      public String getAuthority()
      Returns the decoded authority
    • getUserInfo

      public String getUserInfo()
      Returns the decoded userinfo in this URL, or null if there is no userinfo.
    • getHost

      public String 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

      public String getPath()
      Returns the decoded path of this URL, or null if there is no path.
    • getSchemeSpecificPart

      public String getSchemeSpecificPart()
      Returns the scheme-specific part of this URL.
    • getRawSchemeSpecificPart

      public String getRawSchemeSpecificPart()
      Returns the raw scheme-specific part of this URL, before it has been escape-decoded
    • getQuery

      public String 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

      public Map<String,List<String>> getQueryParameters()
      Return a Map of the query parameters for this URL, or an empty Map if there are none
    • getFragmentParameters

      public Map<String,List<String>> 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

      public URL2 withFragmentParameters(Map<String,List<String>> params)
      Set the Fragment to be the supplied set of fragment parameters
    • getFragment

      public String 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

      public boolean isSameDocument(URL2 uri)
      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

      public URL2 resolve(String s)
      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

      public URL2 resolve(URL2 uri)
      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

      public URL2 normalize()
      Return a new URL which is the normalized equivalent of this URL
    • relativize

      public URL2 relativize(URL2 uri, boolean safe)

      Relativize the supplied URL aginst the current one.

      Where we have http://a.com/b/c and relativize http://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 URL
      safe - whether to use the "safe" strategy
      Returns:
      the relative URL, or "uri" if it can't be determined
    • withScheme

      public URL2 withScheme(String scheme)
    • withFragment

      public URL2 withFragment(String fragment)
      Set the fragment on this URL
      Returns:
      this
    • withQuery

      public URL2 withQuery(String query)
      Set the query on this URL
      Returns:
      this
    • withUserInfo

      public URL2 withUserInfo(String userInfo)
      Set the userInfo on this URL
      Returns:
      this
    • getLinkParameters

      public Map<String,List<? extends Term>> getLinkParameters()
      Get a value previously set by #setParameter
    • toPublicString

      public String toPublicString()
      Return the same value as toString(), but with any password value in the userinfo string replaced with asterisks
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • isSchemeSecure

      public boolean isSchemeSecure()
      Return whether the URL scheme is secured by TLS. Returns true for "https", false otherwise. Used by EgressFilter 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 by EgressFilter 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()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • main

      public static void main(String[] args) throws Exception
      Throws:
      Exception