Apache Camel enriches the message with the contents of the file on request

I am implementing a RESTful service (using the CXFRS component), which should return files for some requests. Each file is extracted by its identifier and extension, i.e. restfulservice.com/path/file/1/pdf . Each added file never changes. Files should not be moved or deleted after extraction, and usually they should be available at the same time. Here is part of my Camel context:

 from("direct:fetchFile") .process(fetchFileProcessor) // set file.id & file.extension .bean(fileService, "fetchFile(${header.file.id}, ${header.file.extension})") // set body to filename .setHeader("CamelFileName", simple("${body}")) .choice() .when(header("file.extension").isEqualTo("xml")) .pollEnrich("file:///{{application.fileStorage.basePath}}/xml?noop=true", 500) .when(header("file.extension").isEqualTo("pdf")) .pollEnrich("file:///{{application.fileStorage.basePath}}/pdf?noop=true", 500) .end() .convertBodyTo(File.class) .bean(responseProvider, "getResponse(${body}, 200)"); 

The problem with this configuration is that the response has a non-empty body only for the second request (why?), Without timeout maintenance it enters the eternal cycle of the second request with a debug message

 DEBUG oaccfFileConsumer - Took 0.000 seconds to poll <base path>\xml 

Apace Camel Version - 2.10.4

Any help would be appreciated

UPD1 :
On the Content Enricher> page there is a warning that "pollEnrich does not have access to data from the current Exchange." But nothing changes if I add fileName=${body} to the URL file

UPD2 :
It appears that pollEnrich does not support the dynamic fileName specified in the URL ( link ). Current route:

 from("direct:fetchFile") .process(fetchFileProcessor) // set file.id & file.extension .bean(fileService, "fetchFile(${header.file.id}, ${header.file.extension})") // set body to filename .choice() .when(header("file.extension").isEqualTo("xml")) .pollEnrich("file:///{{application.fileStorage.basePath}}/xml?fileName=${body}&noop=true", 500) .setHeader("asset.type", simple(MediaType.APPLICATION_XML)) .when(header("file.extension").isEqualTo("pdf")) .pollEnrich("file:///{{application.fileStorage.basePath}}/pdf?fileName=${body}&noop=true", 500) .setHeader("asset.type", simple("application/pdf")) .end() .convertBodyTo(File.class) .process(multipartProcessor) // add file ass attachment to multipart body and set it as body .bean(responseProvider, "getResponse(${body}, 200)"); 

UPD3
I am trying to implement a custom processor to use PollingConsumer with dynamic file names:

 @Override public void process(Exchange exchange) throws Exception { Long timeout = exchange.getIn().getHeader("file.timeout", Long.class); if (enrichUri == null) { throw new FileNotFoundException("'file.url' header not set"); } CamelContext context = exchange.getContext(); Endpoint endpoint = context.getEndpoint(enrichUri); PollingConsumer consumer = endpoint.createPollingConsumer(); consumer.start(); Exchange consumedExchange; try { if (timeout == null || timeout < 0) { consumedExchange = consumer.receive(); } else if (timeout == 0) { consumedExchange = consumer.receiveNoWait(); } else { consumedExchange = consumer.receive(timeout); } } catch (Exception e) { throw new AssetNotFoundException(e); } finally { consumer.stop(); } exchange.getIn().setBody(consumedExchange.getIn().getBody()); } 

Now it returns the contents of the file on the first response, but with each subsequent request, I got an eternal loop over the log messages:

 DEBUG oaccfFileConsumer - Took 0.000 seconds to poll <base path>\xml 

UPD4
I implemented a dynamic route that is added before processing and deleted after it. This method is described in this in the Apache Camel forum. The route uses the above processor to use the file. The result is the same

+6
source share
1 answer

The easy way is often the best way. In this case, I refuse to work with the Apache Camel file component and the following processor is implemented:

 public class FileLoadingProcessor implements Processor { @Override public void process(Exchange exchange) throws Exception { String filename = exchange.getIn().getBody(String.class); // message body contains filename String filePath = exchange.getIn().getHeader("fileprocessor.filepath", String.class); if (filePath == null || filename == null) { // throw some custom exception } URI uri = new URI(filePath.concat(filename)); File file = new File(uri); if (!file.exists()) { throw new FileNotFoundException(String.format("File %s not found on %s", filename, filePath)); } exchange.getIn().setBody(file); } 

Now it works like a charm

+9
source

All Articles