简体   繁体   English

Apache Camel:URI Escaping 在 HTTP(和其他生产者)

[英]Apache Camel: URI Escaping in HTTP (and other Producers)

In an Apache Camel route, I want to take several headers and compose them into an HTTP query string in a safe way.在 Apache 骆驼路线中,我想以安全的方式获取多个标头并将它们组合成 HTTP 查询字符串。 The only examples I've found either use constant(), which isn't useful for building dynamic query strings, or they use simple() which doesn't offer URL escaping.我发现的唯一示例要么使用对构建动态查询字符串无用的 constant(),要么使用不提供 URL escaping 的 simple()。

For example, take the following snippet right from HTTP component's documentation:例如,从 HTTP 组件的文档中获取以下代码片段:

   from("direct:start")
      .setHeader(Exchange.HTTP_QUERY, constant("order=123&detail=short"))
      .to("http://oldhost");

This is 90% of the way there, but what if you don't always want order id 123?这已经完成了 90%,但是如果您不总是想要订单 id 123 怎么办? We'd like to be able to substitute a header value here.我们希望能够在此处替换 header 值。 So, the next logical version of this is to switch to simple:因此,下一个合乎逻辑的版本是切换到简单:

   from("direct:start")
    .setHeader(Exchange.HTTP_QUERY, simple("order=${header.orderId}&detail=short"))
    .to("http://oldhost");

But this has the major issue of not being URL encoded.但这有一个主要问题是没有被 URL 编码。 This means that a space (or any reserved character) in header.orderId results in an exception thrown by the HTTP component for an invalid query string.这意味着 header.orderId 中的空格(或任何保留字符)会导致 HTTP 组件针对无效查询字符串引发异常。

So the only way that's left is to use JavaScript, which is very verbose for something like this, or to write a custom processor.所以剩下的唯一方法是使用 JavaScript,这对于这样的事情非常冗长,或者编写自定义处理器。 It seems like this should be something that's built-in, so I'm asking here to see if I'm missing an obvious/normal way to do what I'm looking for here?看起来这应该是内置的东西,所以我在这里问我是否错过了一种明显/正常的方式来做我在这里寻找的事情?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM