Apache Camel enrich message with file content on request

后端 未结 1 1058
日久生厌
日久生厌 2021-02-09 14:21

I\'m implementing RESTful service (using CXFRS component) which should return files for some requests. Each file is fetched by its id and extension, i.e. restfulservice.co

相关标签:
1条回答
  • 2021-02-09 14:32

    Simple way often is the best way. I refuse to deal with Apache Camel file component in this case and implemented following processor:

    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's working like a charm

    0 讨论(0)
提交回复
热议问题