Package org.apache.camel.support
Class ResourceHelper
java.lang.Object
org.apache.camel.support.ResourceHelper
Helper class for loading resources on the classpath or file system.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic StringappendParameters(String uri, Map<String, Object> parameters) Appends the parameters to the given urifindInFileSystem(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.ResourceCreate aResourcefrom bytes.static org.apache.camel.spi.ResourcefromString(String location, String content) Create aResourcefrom aString.static StringGets the scheme from the URI (e.g. file:, classpath: or http:)static booleanDetermines whether the URI has a scheme (e.g. file:, classpath: or http:)static booleanisClasspathUri(String uri) Is the given uri a classpath uri?static booleanIs the given uri a http uri?static org.apache.camel.spi.ResourceresolveMandatoryResource(org.apache.camel.CamelContext camelContext, String uri) Resolves a mandatory resource.static InputStreamresolveMandatoryResourceAsInputStream(org.apache.camel.CamelContext camelContext, String uri) Resolves the mandatory resource.static URLresolveMandatoryResourceAsUrl(org.apache.camel.CamelContext camelContext, String uri) Resolves the mandatory resource.static org.apache.camel.spi.ResourceresolveResource(org.apache.camel.CamelContext camelContext, String uri) Resolves a resource.static InputStreamresolveResourceAsInputStream(org.apache.camel.CamelContext camelContext, String uri) Resolves the resource.static URLresolveResourceAsUrl(org.apache.camel.CamelContext camelContext, String uri) Resolves the resource.
-
Field Details
-
RESOURCE
- See Also:
-
-
Method Details
-
hasScheme
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
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 Registrybean:nameOfBean.methodName or bean:nameOfBean::methodName - to lookup a bean in the Registryand call the method:uri - to lookup the resource using a custom URLStreamHandlerregistered 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
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
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 URISyntaxExceptionAppends 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
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
Create aResourcefrom bytes.- Parameters:
location- a virtual locationcontent- the resource content- Returns:
- a resource wrapping the given byte array
-
fromString
Create aResourcefrom aString. The implementation delegates tofromBytes(String, byte[])by encoding the string as bytes withString.getBytes(Charset)andStandardCharsets.UTF_8as charset.- Parameters:
location- a virtual locationcontent- the resource content- Returns:
- a resource wrapping the given
String
-