Class ResourceHelper


  • public final class ResourceHelper
    extends Object
    Helper class for loading resources on the classpath or file system.
    • Method Detail

      • hasScheme

        public static boolean hasScheme​(String uri)
        Determines whether the URI has a scheme (e.g. file:, classpath: or http:)
        Parameters:
        uri - the URI
        Returns:
        true if the URI starts with a scheme
      • getScheme

        public static String getScheme​(String uri)
        Gets the scheme from the URI (e.g. file:, classpath: or http:)
        Parameters:
        uri - the uri
        Returns:
        the scheme, or null if no scheme
      • resolveMandatoryResourceAsInputStream

        public static InputStream resolveMandatoryResourceAsInputStream​(org.apache.camel.CamelContext camelContext,
                                                                        String uri)
                                                                 throws IOException
        Resolves the mandatory resource.

        The resource uri can refer to the following systems to be loaded from

          file:nameOfFile - to refer to the file system classpath:nameOfFile - to refer to the classpath (default) http:uri - to load the resource using HTTP ref:nameOfBean - to lookup the resource in the Registry bean:nameOfBean.methodName or bean:nameOfBean::methodName - to lookup a bean in the Registry and call the method :uri - to lookup the resource using a custom URLStreamHandler registered for the , on how to register it @see java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
        If no prefix has been given, then the resource is loaded from the classpath

        If possible recommended to use resolveMandatoryResourceAsUrl(org.apache.camel.spi.ClassResolver, String)

        Parameters:
        camelContext - the Camel Context
        uri - URI of the resource
        Returns:
        the resource as an InputStream. Remember to close this stream after usage.
        Throws:
        IOException - is thrown if the resource file could not be found or loaded as InputStream
      • resolveMandatoryResourceAsUrl

        public static URL resolveMandatoryResourceAsUrl​(org.apache.camel.spi.ClassResolver classResolver,
                                                        String uri)
                                                 throws FileNotFoundException,
                                                        MalformedURLException
        Resolves the mandatory resource.
        Parameters:
        classResolver - the class resolver to load the resource from the classpath
        uri - uri of the resource
        Returns:
        the resource as an URL.
        Throws:
        FileNotFoundException - is thrown if the resource file could not be found
        MalformedURLException - if the URI is malformed
      • resolveResourceAsUrl

        public static URL resolveResourceAsUrl​(org.apache.camel.spi.ClassResolver classResolver,
                                               String uri)
                                        throws MalformedURLException
        Resolves the resource.
        Parameters:
        classResolver - the class resolver to load the resource from the classpath
        uri - uri of the resource
        Returns:
        the resource as an URL. Or null if not found.
        Throws:
        MalformedURLException - if the URI is malformed
      • isClasspathUri

        public static boolean isClasspathUri​(String uri)
        Is the given uri a classpath uri?
        Parameters:
        uri - the uri
        Returns:
        true if the uri starts with classpath: or has no scheme and therefore would otherwise be loaded from classpath by default.
      • isHttpUri

        public static boolean isHttpUri​(String uri)
        Is the given uri a http uri?
        Parameters:
        uri - the uri
        Returns:
        true if the uri starts with http: or https:
      • appendParameters

        public static String appendParameters​(String uri,
                                              Map<String,​Object> parameters)
                                       throws URISyntaxException
        Appends the parameters to the given uri
        Parameters:
        uri - the uri
        parameters - the additional parameters (will clear the map)
        Returns:
        a new uri with the additional parameters appended
        Throws:
        URISyntaxException - is thrown if the uri is invalid
      • findInFileSystem

        public static Set<Path> findInFileSystem​(Path root,
                                                 String pattern)
                                          throws Exception
        Find resources from the file system using Ant-style path patterns.
        Parameters:
        root - the starting file
        pattern - the Ant pattern
        Returns:
        a list of files matching the given pattern
        Throws:
        Exception