casper
casper

Reputation: 472

How to access file in resource in Camel process?

I'm developing camel application which runs on FUSE server and I'm trying to read xsd file from resources folder in my project as show in below. But problem is I could not able to read the exact path or the file content of the file in resources.

enter image description here

I try to read "Employee.xsd" file inside process as below code but it did not success.

File file = new File(classLoader.getResource("Employee.xsd").getFile());
String fileContent=FileUtils.readFileToString(file);// Using Commons-IO

If gives java.io.FileNotFoundException: File '\Employee.xsd' does not exist exception. Does any one able to resolve this kind of issue.

Upvotes: 4

Views: 3732

Answers (2)

snerd
snerd

Reputation: 1297

for the next person who is starting out with camel and runs into this

if you're operating from within the camel context it appears as though you can't access classpath resources in the usual way (so ignore the first part of Claus' answer). instead what you need to do is get a hold of the CamelContext as Claus suggests and use that. here is what that looks like from within a Processor's process method:

public void process(Exchange exchange) throws IOException {

    String getMockResponsePathString = exchange.getContext()
                                               .getClassResolver()
                                               .loadResourceAsURL({$MOCK_RESOURCE_NAME})  
                                               .getPath();

    Path mockResponsePath = Paths.get(mockResponsePathString);
    byte[] mockResponseBytes = Files.readAllBytes(mockResponsePath);
    String mockResponseString = new String(mockResponseBytes);

    exchange.getIn().setBody(mockResponseString);
}

Upvotes: 1

Claus Ibsen
Claus Ibsen

Reputation: 55750

You need to load the file from the classpath.

You can search stackoverflow or internet how to load a resource / file from the classpath.

If you have access to CamelContext you can use itsClassResolver to load resources as well.

You can see more about it at the javadoc: http://static.javadoc.io/org.apache.camel/camel-core/2.18.1/org/apache/camel/spi/ClassResolver.html

Upvotes: 1

Related Questions