Camel:将文件保存到依赖于响应的文件夹

时间:2014-06-02 12:51:20

标签: java apache-camel

我有这样的DSL:

            from("file:data/inbox?noop=true")
                .marshal()
                .string("UTF-8")
                .setHeader(Exchange.CONTENT_TYPE,constant("application/json"))
            .to("http://www.a-service.com")
                .choice()
                    .when(new Predicate() {
                        @Override
                        public boolean matches(Exchange exchange) {
                            Message in = exchange.getIn();
                            String msg = in.getBody(String.class);

                            System.out.println(" Response: " + msg);

                            if(msg.contains("\"status\":\"OK\"")){
                                return true;
                            }else{
                                return false;
                            }
                        }
                    })
                        // OK!!!
                        .to("file:data/outbox_success")
                    .otherwise()
                        // NOT OK !!!
                        .to("file:data/outbox_fail");

我希望如果http响应具有" status":" OK",文件将转到" data / outbox_success"。否则,他们将转到" data / outbox_fail"。

但这不是我的预期:是的,文件已被复制到" outbox_XXX" 但文件中没有内容。

我猜这是因为" In"消息已更改为http响应。

那么,如何根据' http'的响应将文件复制到文件夹? ?

1 个答案:

答案 0 :(得分:1)

尝试将标题存储在标题中供以后使用:

 from("file:data/inbox?noop=true")
            .marshal()
            .string("UTF-8")
            .setHeader(Exchange.CONTENT_TYPE,constant("application/json"))
        .setHeader("fileBody", simple(${body}))
        .to("http://www.a-service.0com")
        .setHeader("webResponse", simple(${body})) //store the response from the http call
        .setBody(simple(${header.fileBody})) //reset body to the original file body
            .choice()
                .when(new Predicate() {
                    @Override
                    public boolean matches(Exchange exchange) {
                        Message in = exchange.getIn();
                        String msg = in.getHeader("webResponse");

                        System.out.println(" Response: " + msg);

                        if(msg.contains("\"status\":\"OK\"")){
                            return true;
                        }else{
                            return false;
                        }
                    }
                })

                    // OK!!!
                    .to("file:data/outbox_success")
                .otherwise()
                    // NOT OK !!!
                    .to("file:data/outbox_fail");