Class ResourceHelper

java.lang.Object
org.apache.camel.support.ResourceHelper

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

    • 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(CamelContext, 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
    • resolveResourceAsInputStream

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

      If possible recommended to use resolveMandatoryResourceAsUrl(CamelContext, String)

      Parameters:
      camelContext - the camel context
      uri - URI of the resource
      Returns:
      the resource as an InputStream. Remember to close this stream after usage. Or null if not found.
      Throws:
      IOException - is thrown if error loading the resource
    • resolveMandatoryResourceAsUrl

      public static URL resolveMandatoryResourceAsUrl(org.apache.camel.CamelContext camelContext, String uri) throws FileNotFoundException, MalformedURLException
      Resolves the mandatory resource.
      Parameters:
      camelContext - the camel context
      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.CamelContext camelContext, String uri) throws MalformedURLException
      Resolves the resource.
      Parameters:
      camelContext - the camel context
      uri - uri of the resource
      Returns:
      the resource as an URL. Or null if not found.
      Throws:
      MalformedURLException - if the URI is malformed
    • resolveMandatoryResource

      public static org.apache.camel.spi.Resource resolveMandatoryResource(org.apache.camel.CamelContext camelContext, String uri) throws FileNotFoundException
      Resolves a mandatory resource.
      Parameters:
      camelContext - the camel context
      uri - the uri of the resource
      Returns:
      the Resource
      Throws:
      FileNotFoundException - if the resource could not be found
    • resolveResource

      public static org.apache.camel.spi.Resource resolveResource(org.apache.camel.CamelContext camelContext, String uri)
      Resolves a resource.
      Parameters:
      camelContext - the camel context
      uri - the uri of the resource
      Returns:
      the Resource. Or null if not found
    • 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 (skips hidden files, or files from hidden folders).
      Parameters:
      root - the starting file
      pattern - the Ant pattern
      Returns:
      set of files matching the given pattern
      Throws:
      Exception - is thrown if IO error
    • fromBytes

      public static org.apache.camel.spi.Resource fromBytes(String location, byte[] content)
      Create a Resource from bytes.
      Parameters:
      location - a virtual location
      content - the resource content
      Returns:
      a resource wrapping the given byte array
    • fromString

      public static org.apache.camel.spi.Resource fromString(String location, String content)
      Create a Resource from a String.

      The implementation delegates to fromBytes(String, byte[]) by encoding the string as bytes with String.getBytes(Charset) and StandardCharsets.UTF_8 as charset.
      Parameters:
      location - a virtual location
      content - the resource content
      Returns:
      a resource wrapping the given String