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 (skips hidden files, or files from hidden folders).static org.apache.camel.spi.Resource
fromBytes(String location, byte[] content)
Create aResource
from bytes.static org.apache.camel.spi.Resource
fromString(String location, String content)
Create aResource
from aString
.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 org.apache.camel.spi.Resource
resolveMandatoryResource(org.apache.camel.CamelContext camelContext, String uri)
Resolves a mandatory resource.static InputStream
resolveMandatoryResourceAsInputStream(org.apache.camel.CamelContext camelContext, String uri)
Resolves the mandatory resource.static URL
resolveMandatoryResourceAsUrl(org.apache.camel.CamelContext camelContext, String uri)
Resolves the mandatory resource.static org.apache.camel.spi.Resource
resolveResource(org.apache.camel.CamelContext camelContext, String uri)
Resolves a resource.static InputStream
resolveResourceAsInputStream(org.apache.camel.CamelContext camelContext, String uri)
Resolves the resource.static URL
resolveResourceAsUrl(org.apache.camel.CamelContext camelContext, 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(CamelContext, 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.CamelContext camelContext, String uri) throws IOException
Resolves the resource. If possible recommended to useresolveMandatoryResourceAsUrl(CamelContext, String)
- Parameters:
camelContext
- the camel contexturi
- 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 contexturi
- 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.CamelContext camelContext, String uri) throws MalformedURLException
Resolves the resource.- Parameters:
camelContext
- the camel contexturi
- 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 contexturi
- 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 contexturi
- 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 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 (skips hidden files, or files from hidden folders).- Parameters:
root
- the starting filepattern
- 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 aResource
from bytes.- Parameters:
location
- a virtual locationcontent
- 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 aResource
from aString
. The implementation delegates tofromBytes(String, byte[])
by encoding the string as bytes withString.getBytes(Charset)
andStandardCharsets.UTF_8
as charset.- Parameters:
location
- a virtual locationcontent
- the resource content- Returns:
- a resource wrapping the given
String
-
-