如何使用Apache Camel转储使用HTTP组件发送的HTTP正文和标头

时间:2013-11-14 11:45:05

标签: java groovy apache-camel

如何使用此路由转储使用Apache Camel HTTP组件发送的HTTP正文和标头:

   from('direct:abc').
   setHeader(Exchange.HTTP_URI, constant("${config.gnutch.solr.coreUrl}/select")).
   setHeader(Exchange.HTTP_QUERY, constant("q=${q}&wt=xml")).
   setHeader(Exchange.CONTENT_TYPE, constant('application/xml')).
   setHeader(Exchange.HTTP_METHOD, constant('GET')).
   setBody(constant(null)).
   to("http://null")

这是groovy中的Camel DSL代码。这可能吗?

3 个答案:

答案 0 :(得分:8)

你有没有试过像

这样的东西
from("direct:abc")
 .to("http://domain.com/")
 .to("log:DEBUG?showBody=true&showHeaders=true")

此外,HTTP Component Documentation表示您可以从交换中提取HttpServletRequest,例如

HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);

然后你可以做,

from("direct:abc").to("http://domain.com").process(new Processor() {
    public void process(Exchange exchange) throws Exception {
        HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);
        // Log request parameters
    }
});

答案 1 :(得分:5)

这将有所帮助,请在日志消息中使用:

$ {头}

$ {in.headers}

这将打印任何传入的标头。

结帐处:http://camel.apache.org/simple.html

示例:

答案 2 :(得分:4)

尝试使用HttpClient记录器之一记录标题和内容。

Logging Practices(本例中为3.x版)中描述了。

我正在使用名称为

的记录器
  • httpclient.wire
  • org.apache.commons.httpclient.HttpConnection

这给我的输出如下:

o.a.c.httpclient.HttpConnection - Open connection to 0.0.0.0:12454
httpclient.wire.header - >> "POST /some/path HTTP/1.1[\r][\n]"
httpclient.wire.header - >> "breadcrumbId: ID-localhost-55077[\r][\n]"
httpclient.wire.header - >> "path: http://0.0.0.0:65432/some/other/path[\r][\n]"
httpclient.wire.header - >> "User-Agent: Jakarta Commons-HttpClient/3.1[\r][\n]"
httpclient.wire.header - >> "Host: 0.0.0.0:12454[\r][\n]"
httpclient.wire.header - >> "Content-Length: 117[\r][\n]"
httpclient.wire.header - >> "[\r][\n]"
httpclient.wire.content - >> "{"a":"afeaafe","b":{"c":"53413"},"d":{"e":"vsegefawawewr"}}"
httpclient.wire.header - << "HTTP/1.1 200 OK[\r][\n]"
httpclient.wire.header - << "HTTP/1.1 200 OK[\r][\n]"
httpclient.wire.header - << "Date: Fri, 08 Apr 2016 07:24:24 GMT[\r][\n]"
httpclient.wire.header - << "Content-Type: application/octet-stream[\r][\n]"
httpclient.wire.header - << "Date: Fri, 08 Apr 2016 07:24:24 GMT[\r][\n]"
httpclient.wire.header - << "Content-Length: 7[\r][\n]"
httpclient.wire.header - << "Server: Jetty(9.2.10.v20150310)[\r][\n]"
httpclient.wire.header - << "[\r][\n]"
httpclient.wire.content - << "Success"
o.a.c.httpclient.HttpConnection - Releasing connection back to connection manager.