Class UriUtils


  • public class UriUtils
    extends Object
    Uitilities to modify and deal with URIs
    Author:
    markr
    • Constructor Summary

      Constructors 
      Constructor Description
      UriUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static URI asDirectory​(URI uri)
      Make sure it is a directory by adding a trailing forward slash if not present
      static URI asRelativeUri​(URI uri)
      Method that attempts to strip everything from the URI to make it a relative URI.
      static URI asRelativeUriFromJar​(URI uri)
      Extract relative URI based on provided URI, knowing that the uri points to a location within a jar.
      static URI asRelativeUriFromRoot​(URI uri)
      Specifically meant for URIs that have been made relative, e.g.
      static URI asRelativeUriFromUserDir​(URI uri)
      Extract relative URI based on provided URI and the user.dir property set for the application.
      static URI asRelativeUriFromWorkingDir​(URI uri)
      Extract relative URI based on provided URI and the current working directory of the application.
      static URI getJvmUserDirUri()
      Find user dir URI of JVM, which is the current working dir unless overridden upon instantiation of the Jvm
      static URI getWorkingDirUri()
      Collect working directory as URI
      static boolean hasPath​(URI uri)
      Check if path is present
      static boolean hasScheme​(URI uri)
      Check if scheme is present
      static boolean isInJar​(URI uri)
      Check if URI is contained within a JAR file
      static boolean isRegularFile​(URI uri)
      Check if URI is a regular file in the default file system, e.g., a local file not in a zip or jar
      static URI jarUriOfJarEntry​(URI uri)
      collect only the jar URI part by stripping all beyond the jar, i.e., remove the part that points to a location within the jar If this is not a jar URI, we return the original URI
      static URI removeInitialScheme​(URI uri)
      create URI without its initial scheme (if any), if no scheme, return original uri
      static URI uriOfJarUrl​(URL jarFileURL)
      Create a URI knowing that the passed in URL points to a jar file, i.e., we transform something from file:<path>/xyz.jar to jar:file:<path>/xyz.jar!/.
    • Constructor Detail

      • UriUtils

        public UriUtils()
    • Method Detail

      • getWorkingDirUri

        public static URI getWorkingDirUri()
        Collect working directory as URI
        Returns:
        URI of working directory
      • getJvmUserDirUri

        public static URI getJvmUserDirUri()
                                    throws URISyntaxException
        Find user dir URI of JVM, which is the current working dir unless overridden upon instantiation of the Jvm
        Returns:
        URI of JVM user dir property, null if this could not be found
        Throws:
        URISyntaxException - thrown if error
      • isInJar

        public static boolean isInJar​(URI uri)
        Check if URI is contained within a JAR file
        Parameters:
        uri - to check
        Returns:
        true when within jar, false otherwise
      • isRegularFile

        public static boolean isRegularFile​(URI uri)
        Check if URI is a regular file in the default file system, e.g., a local file not in a zip or jar
        Parameters:
        uri - to check
        Returns:
        true when within jar, false otherwise
      • asDirectory

        public static URI asDirectory​(URI uri)
        Make sure it is a directory by adding a trailing forward slash if not present
        Parameters:
        uri - to extract from
        Returns:
        URI as directory
      • jarUriOfJarEntry

        public static URI jarUriOfJarEntry​(URI uri)
        collect only the jar URI part by stripping all beyond the jar, i.e., remove the part that points to a location within the jar If this is not a jar URI, we return the original URI
        Parameters:
        uri - to extract jar component from
        Returns:
        URI with only jar component
      • asRelativeUriFromRoot

        public static URI asRelativeUriFromRoot​(URI uri)
        Specifically meant for URIs that have been made relative, e.g. no longer have a scheme (or are of type file), and therefore represent or provide a path. We only collect the path component, if absent, we remove the scheme and try again. If still not possible we return original uri. When path is found, we ensure that a "/" is included in the beginning to signal the uri location is to be found from the root of whatever is the root of this application
        Parameters:
        uri - to extract from
        Returns:
        URI as relative from root.
      • removeInitialScheme

        public static URI removeInitialScheme​(URI uri)
        create URI without its initial scheme (if any), if no scheme, return original uri
        Parameters:
        uri - to check
        Returns:
        true when path is present false otherwise
      • hasScheme

        public static boolean hasScheme​(URI uri)
        Check if scheme is present
        Parameters:
        uri - to check
        Returns:
        true when scheme is present false otherwise
      • hasPath

        public static boolean hasPath​(URI uri)
        Check if path is present
        Parameters:
        uri - to check
        Returns:
        true when path is present false otherwise
      • uriOfJarUrl

        public static URI uriOfJarUrl​(URL jarFileURL)
        Create a URI knowing that the passed in URL points to a jar file, i.e., we transform something from file:<path>/xyz.jar to jar:file:<path>/xyz.jar!/.
        Parameters:
        jarFileURL - to create URI for
        Returns:
        created URI
      • asRelativeUriFromWorkingDir

        public static URI asRelativeUriFromWorkingDir​(URI uri)
        Extract relative URI based on provided URI and the current working directory of the application. If there is no overlap copy of original URI is returned
        Parameters:
        uri - to relativise
        Returns:
        created URI
      • asRelativeUriFromUserDir

        public static URI asRelativeUriFromUserDir​(URI uri)
                                            throws URISyntaxException
        Extract relative URI based on provided URI and the user.dir property set for the application. If there is no overlap or user dir could not be obtained, the original URI is returned
        Parameters:
        uri - to relativise
        Returns:
        created URI
        Throws:
        URISyntaxException - thrown if error
      • asRelativeUriFromJar

        public static URI asRelativeUriFromJar​(URI uri)
        Extract relative URI based on provided URI, knowing that the uri points to a location within a jar. We relativise against the jar location stripping all but the internal location relative to the jar. If the uri is not pointing to a jar location, the original uri is returned
        Parameters:
        uri - to relativise
        Returns:
        created URI
      • asRelativeUri

        public static URI asRelativeUri​(URI uri)
                                 throws URISyntaxException
        Method that attempts to strip everything from the URI to make it a relative URI. This is useful if the URI points to a resource that should be available within the application. Given that resources are to be streamed from a String, apply this method and convert to String afterward to let Java search for the resource. Often we cannot provide the (absolute) URI because it does not work in case it, for example, points to non file like objects, or files contained within other file systems, such as jars. When providing the correct relative URI, Java can work around this whereas otherwise it cannot.

        We attempt three ways to make the URI relative: (i) relative against the current working directory, (ii) relative against the user.dir, (iii) relative against the URI jar file (if applicable). If none succeed, the original URI is returned

        Parameters:
        uri - to convert
        Returns:
        relativised URI, original uri if unable to create a relatives version
        Throws:
        URISyntaxException - thrown if error