Apache的骆驼充实消息,要求文件内容 [英] Apache Camel enrich message with file content on request

查看:211
本文介绍了Apache的骆驼充实消息,要求文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现RESTful服务(使用CXFRS组件),它应该返回文件的一些请求。每个文件是由它的id和延伸,即 restfulservice.com/path/file/1/pdf 获取。每个文件一旦被添加永远不会改变。文件不应该被移动或取后删除,一般应该是可访问的并发。这里是我的骆驼上下文的一部分:

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.com/path/file/1/pdf. Each file once added never changes. Files should not be moved or deleted after fetching and generally they should be accessible concurrently. 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 response has non-empty body only for second(why?) request, without timeout set service enters on eternal loop on second request with debug message

DEBUG o.a.c.c.f.FileConsumer - Took 0.000 seconds to poll <base path>\xml

宇瞻骆驼的版本是2.10.4

Apace Camel version is 2.10.4

任何帮助将AP preciated

Any help would be appreciated

UPD1 :结果
有警告内容富集器页面上,说'pollEnrich不会从当前的Exchange访问任何数据。但是,如果我添加没什么变化文件名= $ {身体} 到文件网址

UPD1:
There is warning on Content Enricher page, saying 'pollEnrich does not access any data from the current Exchange'. But nothing changes if I add fileName=${body} to file URL

UPD2 :结果
好像pollEnrich不支持动态文件名在URL中指定(的链接)。在当前时刻路线:

UPD2:
It seems like pollEnrich do not support dynamic fileName specified in URL (link). Route at current moment:

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 结果
我试图实现自定义的处理器使用PollingConsumer动态文件名:

UPD3
I'm trying to to implement 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 file contents on first response, but on each succeeding request I got eternal loop of above log messages:

DEBUG o.a.c.c.f.FileConsumer - Took 0.000 seconds to poll <base path>\xml

UPD4 结果
我实现了这是处理前加入后它删除动态路由。这种方法在<一个描述href=\"http://camel.465427.n5.nabble.com/How-to-grab-a-only-one-file-with-camel-tp4930104p5508497.html\">this在发布Apache的骆驼论坛。路由使用上述处理器消耗的文件。的结果是相同的

UPD4
I've implemented dynamic route which is added before processing and removed after it. This method is described in this post in Apache Camel forum. Route uses above processor to consume file. The result is the same

推荐答案

简单的方法往往是最好的方式。我拒绝处理在这种情况下Apache的骆驼文件组件,并实现以下处理器:

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

这篇关于Apache的骆驼充实消息,要求文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆