Class URL

  • All Implemented Interfaces:
    java.io.Serializable

    public class URL
    extends java.lang.Object
    implements java.io.Serializable
    A parsed URL. Immutable.
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      URL​(java.lang.String scheme, java.lang.String schemeData, java.lang.String username, java.lang.String password, Host host, int port, java.lang.Iterable<java.lang.String> pathSegments, java.lang.String query, java.lang.String fragment, boolean isHierarchical)  
      URL​(java.lang.String scheme, java.lang.String schemeData, java.lang.String username, java.lang.String password, Host host, int port, java.lang.String path, java.lang.String query, java.lang.String fragment, boolean isHierarchical)  
    • Field Detail

      • scheme

        private final java.lang.String scheme
      • schemeData

        private final java.lang.String schemeData
      • username

        private final java.lang.String username
      • password

        private final java.lang.String password
      • host

        private final Host host
      • port

        private final int port
      • path

        private final java.lang.String path
      • query

        private final java.lang.String query
      • fragment

        private final java.lang.String fragment
      • isHierarchical

        private final boolean isHierarchical
    • Constructor Detail

      • URL

        URL​(java.lang.String scheme,
            java.lang.String schemeData,
            java.lang.String username,
            java.lang.String password,
            Host host,
            int port,
            java.lang.Iterable<java.lang.String> pathSegments,
            java.lang.String query,
            java.lang.String fragment,
            boolean isHierarchical)
      • URL

        URL​(java.lang.String scheme,
            java.lang.String schemeData,
            java.lang.String username,
            java.lang.String password,
            Host host,
            int port,
            java.lang.String path,
            java.lang.String query,
            java.lang.String fragment,
            boolean isHierarchical)
    • Method Detail

      • scheme

        public java.lang.String scheme()
      • schemeData

        public java.lang.String schemeData()
      • username

        public java.lang.String username()
      • password

        public java.lang.String password()
      • userInfo

        public java.lang.String userInfo()
        Gets user info component (i.e. user:pass). This will return an empty string if neither user or password are set.
        Returns:
      • host

        public Host host()
      • authority

        public java.lang.String authority()
      • port

        public int port()
      • defaultPort

        private static int defaultPort​(java.lang.String scheme)
      • defaultPort

        public int defaultPort()
      • path

        public java.lang.String path()
      • pathSegments

        public java.util.List<java.lang.String> pathSegments()
      • query

        public java.lang.String query()
      • fragment

        public java.lang.String fragment()
      • file

        public java.lang.String file()
      • isHierarchical

        public boolean isHierarchical()
      • isOpaque

        public boolean isOpaque()
      • pathSegmentsToString

        private static java.lang.String pathSegmentsToString​(java.lang.Iterable<java.lang.String> segments)
      • pathStringToSegments

        private static java.util.List<java.lang.String> pathStringToSegments​(java.lang.String path)
      • resolve

        public URL resolve​(java.lang.String input)
                    throws GalimatiasParseException
        Resolves a relative reference to an absolute URL. This is just a convenience method equivalent to:
         
          URL base = URL.parse("http://base.com");
          String relativeReference = "/foo/bar";
          URL absoluteURL = base.resolve(relativeReference);
         
         
        Parameters:
        input - Relative reference.
        Returns:
        Resolved absolute URL.
        Throws:
        GalimatiasParseException
      • relativize

        public java.lang.String relativize​(URL url)
        Returns a relative URL reference for the given URL. Behaves as @{link java.net.URI#relativize(URL)}.
        Parameters:
        url - Absolute URL.
        Returns:
        Relative reference.
      • buildHierarchical

        public static URL buildHierarchical​(java.lang.String scheme,
                                            java.lang.String host)
                                     throws GalimatiasParseException
        Gets a URL object from a relative scheme and a host.
        Parameters:
        scheme -
        host -
        Returns:
        Throws:
        GalimatiasParseException
      • toJavaURI

        public java.net.URI toJavaURI()
                               throws java.net.URISyntaxException
        Converts to URI. Conversion to URI will throw URISyntaxException if the URL contains unescaped unsafe characters as defined in RFC 2396. In order to prevent this, force RFC 2396 compliance when parsing the URL. For example: NOTE 1: This will not make distinction between no user and password and just empty user and no password.
                  
                      URL.parse("http://example.com").toJavaURI().toString() -> "http://example.com"
                      URL.parse("http://@example.com").toJavaURI().toString() -> "http://example.com"
                  
              
        TODO: Check if this exception can actually be thrown
        Returns:
        Throws:
        java.net.URISyntaxException
      • toJavaURL

        public java.net.URL toJavaURL()
                               throws java.net.MalformedURLException
        Converts to URL. This method is guaranteed to not throw an exception for URL protocols http, https, ftp, file and jar. It might or might not throw MalformedURLException for other URL protocols.
        Returns:
        Throws:
        java.net.MalformedURLException
      • fromJavaURI

        public static URL fromJavaURI​(java.net.URI uri)
        Construct a URL from a URI.
        Parameters:
        uri -
        Returns:
      • fromJavaURL

        public static URL fromJavaURL​(java.net.URL url)
        Construct a URL from a URL.
        Parameters:
        url -
        Returns:
      • toString

        public java.lang.String toString()
        Serializes the URL. Note that the "exclude fragment flag" (as in WHATWG standard) is not implemented.
        Overrides:
        toString in class java.lang.Object
      • toHumanString

        public java.lang.String toHumanString()
        Serializes the URL to a human-readable representation. That is, percent-decoded and with IDN domains in its Unicode representation.
        Returns:
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object