Package org.apache.camel.support
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 Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
appendParameters(String uri, Map<String,Object> parameters)
Appends the parameters to the given uristatic Set<Path>
findInFileSystem(Path root, String pattern)
Find resources from the file system using Ant-style path patterns.static String
getScheme(String uri)
Gets the scheme from the URI (e.g.static boolean
hasScheme(String uri)
Determines whether the URI has a scheme (e.g.static boolean
isClasspathUri(String uri)
Is the given uri a classpath uri?static boolean
isHttpUri(String uri)
Is the given uri a http uri?static InputStream
resolveMandatoryResourceAsInputStream(org.apache.camel.CamelContext camelContext, String uri)
Resolves the mandatory resource.static URL
resolveMandatoryResourceAsUrl(org.apache.camel.spi.ClassResolver classResolver, String uri)
Resolves the mandatory resource.static InputStream
resolveResourceAsInputStream(org.apache.camel.spi.ClassResolver classResolver, String uri)
Resolves the resource.static URL
resolveResourceAsUrl(org.apache.camel.spi.ClassResolver classResolver, String uri)
Resolves the resource.
-
-
-
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 fromfile: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) resolveMandatoryResourceAsUrl(org.apache.camel.spi.ClassResolver, String)
- Parameters:
camelContext
- the Camel Contexturi
- 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 asInputStream
-
resolveResourceAsInputStream
public static InputStream resolveResourceAsInputStream(org.apache.camel.spi.ClassResolver classResolver, String uri) throws IOException
Resolves the resource. If possible recommended to useresolveMandatoryResourceAsUrl(org.apache.camel.spi.ClassResolver, String)
- Parameters:
classResolver
- the class resolver to load the resource from the classpathuri
- 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.spi.ClassResolver classResolver, String uri) throws FileNotFoundException, MalformedURLException
Resolves the mandatory resource.- Parameters:
classResolver
- the class resolver to load the resource from the classpathuri
- uri of the resource- Returns:
- the resource as an
URL
. - Throws:
FileNotFoundException
- is thrown if the resource file could not be foundMalformedURLException
- 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 classpathuri
- 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 uriparameters
- 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 filepattern
- the Ant pattern- Returns:
- a list of files matching the given pattern
- Throws:
Exception
-
-