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(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
    • URL2

      public URL2(URL2 uri)
      Create a new URL2 that is a clone of the supplied value
    • URL2

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

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

    • 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
    • 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 component of this URL.
    • 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"
    • setFragmentParameters

      public URL2 setFragmentParameters(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.
    • 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
    • setFragment

      public URL2 setFragment(String fragment)
      Set the fragment on this URL
      Returns:
      this
    • setQuery

      public URL2 setQuery(String query)
      Set the query on this URL
      Returns:
      this
    • setUserInfo

      public URL2 setUserInfo(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
    • toOriginString

      public String toOriginString()
      Return the "origin" version of this URL; just the scheme, host and port. If this URL is not a network-resolving URL, return null
    • 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
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

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